Support shared user data for scripts (#4710)

The same data is returned for the script and all of its functions,
including those which are created by an eval call.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg 2021-07-20 10:33:23 +02:00 committed by GitHub
parent 9ff25dbc12
commit 713d90b5a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 780 additions and 126 deletions

View File

@ -243,6 +243,7 @@ Option bits for [jerry_parse_options_t](#jerry_parse_options_t).
- JERRY_PARSE_MODULE - Parse source as an ECMAScript module
- JERRY_PARSE_HAS_RESOURCE - `resource_name_p` and `resource_name_length` fields are valid
- JERRY_PARSE_HAS_START - `start_line` and `start_column` fields are valid
- JERRY_PARSE_HAS_USER_VALUE - `user_value` field is valid
*New in version [[NEXT_RELEASE]]*.
@ -310,6 +311,10 @@ Flags for [jerry_exec_snapshot](#jerry_exec_snapshot) functions:
- JERRY_SNAPSHOT_EXEC_COPY_DATA - copy snapshot data into memory (see below)
- JERRY_SNAPSHOT_EXEC_ALLOW_STATIC - allow executing static snapshots
- JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION - load snapshot as function instead of executing it
- JERRY_SNAPSHOT_EXEC_HAS_RESOURCE - resource_name_p and resource_name_length fields are valid
in [jerry_exec_snapshot_option_values_t](#jerry_exec_snapshot_option_values_t)
- JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE - user_value field is valid
in [jerry_exec_snapshot_option_values_t](#jerry_exec_snapshot_option_values_t)
*Changed in version [[NEXT_RELEASE]]*: The `JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION` value is added,
which replaces the `jerry_load_function_snapshot` function.
@ -536,6 +541,8 @@ typedef struct
* if JERRY_PARSE_HAS_RESOURCE is set in options */
uint32_t start_line; /**< start line of the source code if JERRY_PARSE_HAS_START is set in options */
uint32_t start_column; /**< start column of the source code if JERRY_PARSE_HAS_START is set in options */
jerry_value_t user_value; /**< user value assigned to all functions created by this script including eval
* calls executed by the script if JERRY_PARSE_HAS_USER_VALUE is set in options */
} jerry_parse_options_t;
```
@ -547,6 +554,7 @@ typedef struct
- [jerry_parse_function](#jerry_parse_function)
- [jerry_generate_snapshot](#jerry_generate_snapshot)
- [jerry_generate_function_snapshot](#jerry_generate_function_snapshot)
- [jerry_exec_snapshot](#jerry_exec_snapshot)
- [jerry_parse_option_enable_feature_t](#jerry_parse_option_enable_feature_t)
## jerry_property_descriptor_t
@ -1231,6 +1239,33 @@ TypedArray support is not in the engine.
- [jerry_get_typedarray_type](#jerry_get_typedarray_type)
## jerry_exec_snapshot_option_values_t
**Summary**
Various configuration options for [jerry_exec_snapshot](#jerry_exec_snapshot)
**Prototype**
```c
typedef struct
{
const jerry_char_t *resource_name_p; /**< resource name (usually a file name)
* if JERRY_SNAPSHOT_EXEC_HAS_RESOURCE is set in exec_snapshot_opts */
size_t resource_name_length; /**< length of resource name
* if JERRY_SNAPSHOT_EXEC_HAS_RESOURCE is set in exec_snapshot_opts */
jerry_value_t user_value; /**< user value assigned to all functions created by this script including
* eval calls executed by the script if JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE
* is set in exec_snapshot_opts */
} jerry_exec_snapshot_option_values_t;
```
*New in version [[NEXT_RELEASE]]*.
**See also**
- [jerry_exec_snapshot](#jerry_exec_snapshot)
# General engine functions
## jerry_init
@ -10221,19 +10256,24 @@ jerry_value_t
jerry_exec_snapshot (const uint32_t *snapshot_p,
size_t snapshot_size,
size_t func_index,
uint32_t exec_snapshot_opts);
uint32_t exec_snapshot_opts,
const jerry_exec_snapshot_option_values_t *options_values_p);
```
- `snapshot_p` - pointer to snapshot.
- `snapshot_size` - size of snapshot in bytes.
- `func_index` - index of executed function.
- `exec_snapshot_opts` - any combination of [jerry_exec_snapshot_opts_t](#jerry_exec_snapshot_opts_t) flags.
- `options_values_p` - additional loadig options, can be NULL if not used. The fields are described in
[jerry_exec_snapshot_option_values_t](#jerry_exec_snapshot_option_values_t).
- return value
- result of bytecode, if run was successful.
- thrown error, otherwise (an error is reported if the snapshot execution feature is not enabled).
*Changed in version 2.0*: Added `func_index` and `exec_snapshot_opts` arguments. Removed the `copy_bytecode` last argument.
*Changed in version [[NEXT_RELEASE]]*: Added `options_p` argument.
**Example 1**
[doctest]: # ()
@ -10269,7 +10309,8 @@ main (void)
jerry_value_t res = jerry_exec_snapshot (snapshot_buffer,
snapshot_size,
0,
0);
0,
NULL);
/* 'res' now contains 'string from snapshot' */
jerry_release_value (res);
@ -10317,7 +10358,8 @@ main (void)
jerry_value_t func = jerry_exec_snapshot (snapshot_buffer,
snapshot_size,
0,
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION,
NULL);
/* 'func' can be used now as a function object. */
jerry_value_t this_value = jerry_create_undefined ();
@ -11141,6 +11183,74 @@ main (void)
- [jerry_create_external_function](#jerry_create_external_function)
## jerry_get_user_value
**Summary**
Returns the user value assigned to a script / module / function. This value is
set by the parser when the JERRY_PARSE_HAS_USER_VALUE flag is set in the `options`
member of the [jerry_parse_options_t](#jerry_parse_options_t) structure.
*Notes*:
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
is no longer needed.
**Prototype**
```c
jerry_value_t
jerry_get_user_value (const jerry_value_t value);
```
- `value` - script / module / function value which executes JavaScript
code (native modules / functions do not have user value).
- return
- user value - if available,
- undefined - otherwise
*New in version [[NEXT_RELEASE]]*.
**Example**
```c
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
const jerry_char_t script[] = "function abc() {} abc";
jerry_value user_value = jerry_create_object ();
jerry_parse_options_t parse_options;
parse_options.options = ECMA_PARSE_HAS_USER_VALUE;
parse_options.user_value = user_value;
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, &parse_options);
jerry_release_value (user_value);
/* The jerry_get_user_value returns the object which
* was created by jerry_create_object before. */
jerry_value user_value = jerry_get_user_value (parsed_code);
jerry_release_value (parsed_code);
jerry_release_value (user_value);
jerry_cleanup ();
return 0;
}
```
**See also**
- [jerry_parse](#jerry_parse)
- [jerry_parse_function](#jerry_parse_function)
- [jerry_generate_snapshot](#jerry_generate_snapshot)
- [jerry_generate_function_snapshot](#jerry_generate_function_snapshot)
- [jerry_exec_snapshot](#jerry_exec_snapshot)
# Functions for realm objects
These APIs all depend on build option (`JERRY_BUILTIN_REALMS`).

View File

@ -50,9 +50,6 @@ snapshot_get_global_flags (bool has_regex, /**< regex literal is present */
#if JERRY_ESNEXT
flags |= (has_class ? JERRY_SNAPSHOT_HAS_CLASS_LITERAL : 0);
#endif /* JERRY_ESNEXT */
#if JERRY_BUILTIN_REALMS
flags |= JERRY_SNAPSHOT_HAS_REALM_VALUE;
#endif /* JERRY_BUILTIN_REALMS */
return flags;
} /* snapshot_get_global_flags */
@ -71,9 +68,6 @@ snapshot_check_global_flags (uint32_t global_flags) /**< global flags */
#if JERRY_ESNEXT
global_flags &= (uint32_t) ~JERRY_SNAPSHOT_HAS_CLASS_LITERAL;
#endif /* JERRY_ESNEXT */
#if JERRY_BUILTIN_REALMS
global_flags |= JERRY_SNAPSHOT_HAS_REALM_VALUE;
#endif /* JERRY_BUILTIN_REALMS */
return global_flags == snapshot_get_global_flags (false, false);
} /* snapshot_check_global_flags */
@ -395,9 +389,7 @@ static_snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< c
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
#if JERRY_BUILTIN_REALMS
args_p->realm_value = JMEM_CP_NULL;
#endif /* JERRY_BUILTIN_REALMS */
args_p->script_value = JMEM_CP_NULL;
}
else
{
@ -407,9 +399,7 @@ static_snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< c
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
#if JERRY_BUILTIN_REALMS
args_p->realm_value = JMEM_CP_NULL;
#endif /* JERRY_BUILTIN_REALMS */
args_p->script_value = JMEM_CP_NULL;
}
for (uint32_t i = 0; i < const_literal_end; i++)
@ -569,6 +559,7 @@ static ecma_compiled_code_t *
snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of the
* current primary function */
const uint8_t *literal_base_p, /**< literal start */
cbc_script_t *script_p, /**< script */
bool copy_bytecode) /**< byte code should be copied to memory */
{
ecma_compiled_code_t *bytecode_p = (ecma_compiled_code_t *) base_addr_p;
@ -598,6 +589,14 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
uint32_t const_literal_end;
uint32_t literal_end;
if (JERRY_UNLIKELY (script_p->refs_and_type >= CBC_SCRIPT_REF_MAX))
{
/* This is probably never happens in practice. */
jerry_fatal (ERR_REF_COUNT_LIMIT);
}
script_p->refs_and_type += CBC_SCRIPT_REF_ONE;
if (bytecode_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
uint8_t *byte_p = (uint8_t *) bytecode_p;
@ -608,9 +607,7 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
header_size = sizeof (cbc_uint16_arguments_t);
#if JERRY_BUILTIN_REALMS
ECMA_SET_INTERNAL_VALUE_POINTER (args_p->realm_value, ecma_builtin_get_global ());
#endif /* JERRY_BUILTIN_REALMS */
ECMA_SET_INTERNAL_VALUE_POINTER (args_p->script_value, script_p);
}
else
{
@ -622,9 +619,7 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
header_size = sizeof (cbc_uint8_arguments_t);
#if JERRY_BUILTIN_REALMS
ECMA_SET_INTERNAL_VALUE_POINTER (args_p->realm_value, ecma_builtin_get_global ());
#endif /* JERRY_BUILTIN_REALMS */
ECMA_SET_INTERNAL_VALUE_POINTER (args_p->script_value, script_p);
}
if (copy_bytecode
@ -729,6 +724,7 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
ecma_compiled_code_t *literal_bytecode_p;
literal_bytecode_p = snapshot_load_compiled_code (base_addr_p + literal_offset,
literal_base_p,
script_p,
copy_bytecode);
ECMA_SET_INTERNAL_VALUE_POINTER (literal_start_p[i],
@ -930,14 +926,18 @@ jerry_value_t
jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
size_t snapshot_size, /**< size of snapshot */
size_t func_index, /**< index of primary function */
uint32_t exec_snapshot_opts) /**< jerry_exec_snapshot_opts_t option bits */
uint32_t exec_snapshot_opts, /**< jerry_exec_snapshot_opts_t option bits */
const jerry_exec_snapshot_option_values_t *option_values_p) /**< additional option values,
* can be NULL if not used */
{
#if JERRY_SNAPSHOT_EXEC
JERRY_ASSERT (snapshot_p != NULL);
uint32_t allowed_opts = (JERRY_SNAPSHOT_EXEC_COPY_DATA
| JERRY_SNAPSHOT_EXEC_ALLOW_STATIC
| JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);
| JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION
| JERRY_SNAPSHOT_EXEC_HAS_RESOURCE
| JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE);
if ((exec_snapshot_opts & ~(allowed_opts)) != 0)
{
@ -998,16 +998,58 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
}
else
{
ecma_value_t user_value = ECMA_VALUE_EMPTY;
if ((exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE)
&& option_values_p != NULL)
{
user_value = option_values_p->user_value;
}
uint32_t script_size = (user_value != ECMA_VALUE_EMPTY ? sizeof (cbc_script_user_t)
: sizeof (cbc_script_t));
cbc_script_t *script_p = jmem_heap_alloc_block (script_size);
CBC_SCRIPT_SET_TYPE (script_p, user_value, CBC_SCRIPT_REF_ONE);
#if JERRY_BUILTIN_REALMS
script_p->realm_p = (ecma_object_t *) JERRY_CONTEXT (global_object_p);
#endif /* JERRY_BUILTIN_REALMS */
#if JERRY_RESOURCE_NAME
ecma_value_t resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
if ((exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_HAS_RESOURCE)
&& option_values_p != NULL
&& option_values_p->resource_name_length > 0)
{
resource_name = ecma_find_or_create_literal_string (option_values_p->resource_name_p,
(lit_utf8_size_t) option_values_p->resource_name_length);
}
script_p->resource_name = resource_name;
#endif /* JERRY_RESOURCE_NAME */
const uint8_t *literal_base_p = snapshot_data_p + header_p->lit_table_offset;
bytecode_p = snapshot_load_compiled_code ((const uint8_t *) bytecode_p,
literal_base_p,
script_p,
(exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_COPY_DATA) != 0);
if (bytecode_p == NULL)
{
JERRY_ASSERT (script_p->refs_and_type >= CBC_SCRIPT_REF_ONE);
jmem_heap_free_block (script_p, script_size);
return ecma_raise_type_error (invalid_format_error_p);
}
script_p->refs_and_type -= CBC_SCRIPT_REF_ONE;
if (user_value != ECMA_VALUE_EMPTY)
{
((cbc_script_user_t *) script_p)->user_value = ecma_copy_value_if_not_object (user_value);
}
}
#if JERRY_PARSER_DUMP_BYTE_CODE
@ -1063,6 +1105,7 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
JERRY_UNUSED (snapshot_size);
JERRY_UNUSED (func_index);
JERRY_UNUSED (exec_snapshot_opts);
JERRY_UNUSED (option_values_p);
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Snapshot execution is not supported");
#endif /* JERRY_SNAPSHOT_EXEC */

View File

@ -45,8 +45,7 @@ typedef enum
{
/* 8 bits are reserved for dynamic features */
JERRY_SNAPSHOT_HAS_REGEX_LITERAL = (1u << 0), /**< byte code has regex literal */
JERRY_SNAPSHOT_HAS_REALM_VALUE = (1u << 1), /**< byte code has realm value */
JERRY_SNAPSHOT_HAS_CLASS_LITERAL = (1u << 2), /**< byte code has class literal */
JERRY_SNAPSHOT_HAS_CLASS_LITERAL = (1u << 1), /**< byte code has class literal */
/* 24 bits are reserved for compile time features */
JERRY_SNAPSHOT_FOUR_BYTE_CPOINTER = (1u << 8) /**< deprecated, an unused placeholder now */
} jerry_snapshot_global_flags_t;

View File

@ -393,7 +393,8 @@ jerry_parse (const jerry_char_t *source_p, /**< script source */
uint32_t allowed_parse_options = (JERRY_PARSE_STRICT_MODE
| JERRY_PARSE_MODULE
| JERRY_PARSE_HAS_RESOURCE
| JERRY_PARSE_HAS_START);
| JERRY_PARSE_HAS_START
| JERRY_PARSE_HAS_USER_VALUE);
if (options_p != NULL && (options_p->options & ~allowed_parse_options) != 0)
{
@ -493,7 +494,8 @@ jerry_parse_function (const jerry_char_t *arg_list_p, /**< script source */
uint32_t allowed_parse_options = (JERRY_PARSE_STRICT_MODE
| JERRY_PARSE_HAS_RESOURCE
| JERRY_PARSE_HAS_START);
| JERRY_PARSE_HAS_START
| JERRY_PARSE_HAS_USER_VALUE);
if (options_p != NULL && (options_p->options & ~allowed_parse_options) != 0)
{
@ -5381,6 +5383,36 @@ jerry_get_resource_name (const jerry_value_t value) /**< jerry api value */
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
} /* jerry_get_resource_name */
/**
* Returns the user value assigned to a script / module / function.
*
* Note:
* This value is usually set by the parser when
* the JERRY_PARSE_HAS_USER_VALUE flag is passed.
*
* @return user value
*/
jerry_value_t
jerry_get_user_value (const jerry_value_t value) /**< jerry api value */
{
const ecma_compiled_code_t *bytecode_p = ecma_bytecode_get_from_value (value);
if (bytecode_p == NULL)
{
return ECMA_VALUE_UNDEFINED;
}
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_p)->script_value;
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
if (CBC_SCRIPT_GET_TYPE (script_p) == CBC_SCRIPT_GENERIC)
{
return ECMA_VALUE_UNDEFINED;
}
return ecma_copy_value (((cbc_script_user_t *) script_p)->user_value);
} /* jerry_get_user_value */
/**
* Replaces the currently active realm with another realm.
*

View File

@ -1068,9 +1068,7 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t,
ext_func_p->u.function.scope_cp));
#if JERRY_ESNEXT || JERRY_BUILTIN_REALMS
const ecma_compiled_code_t *byte_code_p = ecma_op_function_get_compiled_code (ext_func_p);
#endif /* JERRY_ESNEXT || JERRY_BUILTIN_REALMS */
#if JERRY_ESNEXT
if (CBC_FUNCTION_IS_ARROW (byte_code_p->status_flags))
@ -1089,29 +1087,27 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
}
#endif /* JERRY_ESNEXT */
#if JERRY_BUILTIN_REALMS
#if JERRY_SNAPSHOT_EXEC
if (ext_func_p->u.function.bytecode_cp == JMEM_CP_NULL)
if (JERRY_UNLIKELY (byte_code_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
{
/* Static snapshot functions have a global realm */
break;
}
#endif /* JERRY_SNAPSHOT_EXEC */
ecma_object_t *realm_p;
ecma_value_t script_value = ((cbc_uint8_arguments_t *) byte_code_p)->script_value;
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
if (byte_code_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
if (CBC_SCRIPT_GET_TYPE (script_p) == CBC_SCRIPT_USER_OBJECT)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) byte_code_p;
realm_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, args_p->realm_value);
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) byte_code_p;
realm_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, args_p->realm_value);
cbc_script_user_t *script_user_p = (cbc_script_user_t *) script_p;
JERRY_ASSERT (ecma_is_value_object (script_user_p->user_value));
ecma_gc_set_object_visited (ecma_get_object_from_value (script_user_p->user_value));
}
ecma_gc_set_object_visited (realm_p);
#if JERRY_BUILTIN_REALMS
ecma_gc_set_object_visited (script_p->realm_p);
#endif /* JERRY_BUILTIN_REALMS */
break;
}

View File

@ -15,6 +15,7 @@
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
@ -1514,6 +1515,32 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */
}
}
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_p)->script_value;
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
script_p->refs_and_type -= CBC_SCRIPT_REF_ONE;
if (script_p->refs_and_type < CBC_SCRIPT_REF_ONE)
{
size_t script_size = sizeof (cbc_script_t);
uint32_t type = CBC_SCRIPT_GET_TYPE (script_p);
if (type != CBC_SCRIPT_GENERIC)
{
script_size = sizeof (cbc_script_user_t);
if (type == CBC_SCRIPT_USER_VALUE)
{
cbc_script_user_t *script_user_p = (cbc_script_user_t *) script_p;
JERRY_ASSERT (!ecma_is_value_object (script_user_p->user_value));
ecma_free_value (script_user_p->user_value);
}
}
jmem_heap_free_block (script_p, script_size);
}
#if JERRY_ESNEXT
if (bytecode_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS)
{
@ -1584,6 +1611,72 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */
((size_t) bytecode_p->size) << JMEM_ALIGNMENT_LOG);
} /* ecma_bytecode_deref */
/**
* Gets the byte code asigned to a script / module / function
*
* @return byte code - if available, NULL - otherwise
*/
const ecma_compiled_code_t *
ecma_bytecode_get_from_value (ecma_value_t value) /**< compiled code */
{
if (!ecma_is_value_object (value))
{
return NULL;
}
ecma_object_t *object_p = ecma_get_object_from_value (value);
while (true)
{
switch (ecma_get_object_type (object_p))
{
case ECMA_OBJECT_TYPE_CLASS:
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
if (ext_object_p->u.cls.type == ECMA_OBJECT_CLASS_SCRIPT)
{
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t,
ext_object_p->u.cls.u3.value);
}
#if JERRY_MODULE_SYSTEM
if (ext_object_p->u.cls.type == ECMA_OBJECT_CLASS_MODULE)
{
ecma_module_t *module_p = (ecma_module_t *) object_p;
if (!(module_p->header.u.cls.u2.module_flags & ECMA_MODULE_IS_NATIVE))
{
return module_p->u.compiled_code_p;
}
}
#endif /* JERRY_MODULE_SYSTEM */
return NULL;
}
case ECMA_OBJECT_TYPE_FUNCTION:
{
if (!ecma_get_object_is_builtin (object_p))
{
return ecma_op_function_get_compiled_code ((ecma_extended_object_t *) object_p);
}
return NULL;
}
case ECMA_OBJECT_TYPE_BOUND_FUNCTION:
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
object_p = ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t,
ext_object_p->u.bound_function.target_function);
break;
}
default:
{
return NULL;
}
}
}
} /* ecma_bytecode_get_from_value */
/**
* Resolve the position of the arguments list start of the compiled code
*
@ -1711,16 +1804,15 @@ ecma_value_t
ecma_get_resource_name (const ecma_compiled_code_t *bytecode_p) /**< compiled code */
{
#if JERRY_RESOURCE_NAME
if (bytecode_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
#if JERRY_SNAPSHOT_EXEC
if (JERRY_UNLIKELY (bytecode_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_p;
ecma_value_t *lit_pool_p = (ecma_value_t *) ((uint8_t *) bytecode_p + sizeof (cbc_uint16_arguments_t));
return lit_pool_p[args_p->const_literal_end - args_p->register_end - 1];
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
}
#endif /* JERRY_SNAPSHOT_EXEC */
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_p;
ecma_value_t *lit_pool_p = (ecma_value_t *) ((uint8_t *) bytecode_p + sizeof (cbc_uint8_arguments_t));
return lit_pool_p[args_p->const_literal_end - args_p->register_end - 1];
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_p)->script_value;
return ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value)->resource_name;
#else /* !JERRY_RESOURCE_NAME */
JERRY_UNUSED (bytecode_p);
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);

View File

@ -532,6 +532,7 @@ void ecma_raise_error_from_error_reference (ecma_value_t value);
void ecma_bytecode_ref (ecma_compiled_code_t *bytecode_p);
void ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p);
const ecma_compiled_code_t *ecma_bytecode_get_from_value (ecma_value_t value);
ecma_value_t *ecma_compiled_code_resolve_arguments_start (const ecma_compiled_code_t *bytecode_header_p);
#if JERRY_ESNEXT
ecma_value_t *ecma_compiled_code_resolve_function_name (const ecma_compiled_code_t *bytecode_header_p);

View File

@ -759,29 +759,17 @@ ecma_op_function_get_compiled_code (ecma_extended_object_t *function_p) /**< fun
extern inline ecma_global_object_t * JERRY_ATTR_ALWAYS_INLINE
ecma_op_function_get_realm (const ecma_compiled_code_t *bytecode_header_p) /**< byte code header */
{
ecma_value_t realm_value;
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;
realm_value = args_p->realm_value;
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;
realm_value = args_p->realm_value;
}
#if JERRY_SNAPSHOT_EXEC
if (JERRY_LIKELY (realm_value != JMEM_CP_NULL))
if (JERRY_UNLIKELY (bytecode_header_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
{
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t, realm_value);
return (ecma_global_object_t *) ecma_builtin_get_global ();
}
return (ecma_global_object_t *) ecma_builtin_get_global ();
#else /* !JERRY_SNAPSHOT_EXEC */
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t, realm_value);
#endif /* JERRY_SNAPSHOT_EXEC */
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_header_p)->script_value;
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
return (ecma_global_object_t *) script_p->realm_p;
} /* ecma_op_function_get_realm */
/**

View File

@ -360,6 +360,7 @@ bool jerry_backtrace_is_strict (jerry_backtrace_frame_t *frame_p);
*/
void jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb, void *user_p, uint32_t frequency);
jerry_value_t jerry_get_resource_name (const jerry_value_t value);
jerry_value_t jerry_get_user_value (const jerry_value_t value);
/**
* Array buffer components.

View File

@ -41,15 +41,33 @@ typedef enum
} jerry_generate_snapshot_opts_t;
/**
* Flags for jerry_exec_snapshot_at and jerry_load_function_snapshot_at.
* Flags for jerry_exec_snapshot.
*/
typedef enum
{
JERRY_SNAPSHOT_EXEC_COPY_DATA = (1u << 0), /**< copy snashot data */
JERRY_SNAPSHOT_EXEC_ALLOW_STATIC = (1u << 1), /**< static snapshots allowed */
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION = (1u << 2), /**< load snapshot as function instead of executing it */
JERRY_SNAPSHOT_EXEC_HAS_RESOURCE = (1u << 3), /**< resource_name_p and resource_name_length fields are valid
* in jerry_exec_snapshot_option_values_t */
JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE = (1u << 4), /**< user_value field is valid
* in jerry_exec_snapshot_option_values_t */
} jerry_exec_snapshot_opts_t;
/**
* Various configuration options for jerry_exec_snapshot.
*/
typedef struct
{
const jerry_char_t *resource_name_p; /**< resource name (usually a file name)
* if JERRY_SNAPSHOT_EXEC_HAS_RESOURCE is set in exec_snapshot_opts */
size_t resource_name_length; /**< length of resource name
* if JERRY_SNAPSHOT_EXEC_HAS_RESOURCE is set in exec_snapshot_opts */
jerry_value_t user_value; /**< user value assigned to all functions created by this script including
* eval calls executed by the script if JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE
* is set in exec_snapshot_opts */
} jerry_exec_snapshot_option_values_t;
/**
* Snapshot functions.
*/
@ -63,7 +81,8 @@ jerry_value_t jerry_generate_function_snapshot (const jerry_char_t *source_p, si
uint32_t *buffer_p, size_t buffer_size);
jerry_value_t jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_size,
size_t func_index, uint32_t exec_snapshot_opts);
size_t func_index, uint32_t exec_snapshot_opts,
const jerry_exec_snapshot_option_values_t *options_values_p);
size_t jerry_merge_snapshots (const uint32_t **inp_buffers_p, size_t *inp_buffer_sizes_p, size_t number_of_snapshots,
uint32_t *out_buffer_p, size_t out_buffer_size, const char **error_p);

View File

@ -168,6 +168,7 @@ typedef enum
JERRY_PARSE_MODULE = (1 << 1), /**< parse source as an ECMAScript module */
JERRY_PARSE_HAS_RESOURCE = (1 << 2), /**< resource_name_p and resource_name_length fields are valid */
JERRY_PARSE_HAS_START = (1 << 3), /**< start_line and start_column fields are valid */
JERRY_PARSE_HAS_USER_VALUE = (1 << 4), /**< user_value field is valid */
} jerry_parse_option_enable_feature_t;
/**
@ -182,6 +183,8 @@ typedef struct
* if JERRY_PARSE_HAS_RESOURCE is set in options */
uint32_t start_line; /**< start line of the source code if JERRY_PARSE_HAS_START is set in options */
uint32_t start_column; /**< start column of the source code if JERRY_PARSE_HAS_START is set in options */
jerry_value_t user_value; /**< user value assigned to all functions created by this script including eval
* calls executed by the script if JERRY_PARSE_HAS_USER_VALUE is set in options */
} jerry_parse_options_t;
/**

View File

@ -15,11 +15,15 @@
#include "js-parser-internal.h"
JERRY_STATIC_ASSERT ((sizeof (cbc_uint8_arguments_t) % sizeof (jmem_cpointer_t)) == 0,
sizeof_cbc_uint8_arguments_t_must_be_divisible_by_sizeof_jmem_cpointer_t);
/* These two checks only checks the compiler, they have no effect on the code. */
JERRY_STATIC_ASSERT (sizeof (cbc_uint8_arguments_t) == 16,
sizeof_cbc_uint8_arguments_t_must_be_16_byte_long);
JERRY_STATIC_ASSERT ((sizeof (cbc_uint16_arguments_t) % sizeof (jmem_cpointer_t)) == 0,
sizeof_cbc_uint16_arguments_t_must_be_divisible_by_sizeof_jmem_cpointer_t);
JERRY_STATIC_ASSERT (sizeof (cbc_uint16_arguments_t) == 24,
sizeof_cbc_uint16_arguments_t_must_be_24_byte_long);
JERRY_STATIC_ASSERT (offsetof (cbc_uint8_arguments_t, script_value) == offsetof (cbc_uint16_arguments_t, script_value),
script_value_in_cbc_uint8_arguments_and_cbc_uint16_arguments_must_be_in_the_same_offset);
/**
* The reason of these two static asserts to notify the developer to increase the JERRY_SNAPSHOT_VERSION

View File

@ -848,13 +848,11 @@ typedef struct
ecma_compiled_code_t header; /**< compiled code header */
uint8_t stack_limit; /**< maximum number of values stored on the stack */
uint8_t argument_end; /**< number of arguments expected by the function */
ecma_value_t script_value; /**< script value */
uint8_t register_end; /**< end position of the register group */
uint8_t ident_end; /**< end position of the identifier group */
uint8_t const_literal_end; /**< end position of the const literal group */
uint8_t literal_end; /**< end position of the literal group */
#if JERRY_BUILTIN_REALMS
ecma_value_t realm_value; /**< realm value */
#endif /* JERRY_BUILTIN_REALMS */
} cbc_uint8_arguments_t;
/**
@ -864,15 +862,13 @@ typedef struct
{
ecma_compiled_code_t header; /**< compiled code header */
uint16_t stack_limit; /**< maximum number of values stored on the stack */
ecma_value_t script_value; /**< script value */
uint16_t argument_end; /**< number of arguments expected by the function */
uint16_t register_end; /**< end position of the register group */
uint16_t ident_end; /**< end position of the identifier group */
uint16_t const_literal_end; /**< end position of the const literal group */
uint16_t literal_end; /**< end position of the literal group */
uint16_t padding; /**< an unused value */
#if JERRY_BUILTIN_REALMS
ecma_value_t realm_value; /**< realm value */
#endif /* JERRY_BUILTIN_REALMS */
} cbc_uint16_arguments_t;
/**
@ -968,6 +964,69 @@ typedef enum
*/
#define CBC_EXTENDED_INFO_GET_LENGTH(extended_info) (extended_info)
/**
* Shared script data.
*/
typedef enum
{
CBC_SCRIPT_GENERIC, /**< script without user specific data */
CBC_SCRIPT_USER_OBJECT, /**< script with a user object */
CBC_SCRIPT_USER_VALUE, /**< script with a non-object user value */
} cbc_script_type;
/**
* Value for increasing or decreasing the script reference counter.
*/
#define CBC_SCRIPT_REF_ONE 0x4
/**
* Get the type of a script.
*/
#define CBC_SCRIPT_GET_TYPE(script_p) ((script_p)->refs_and_type & (CBC_SCRIPT_REF_ONE - 1))
/**
* Maximum value of script reference counter.
*/
#define CBC_SCRIPT_REF_MAX (UINT32_MAX - CBC_SCRIPT_REF_ONE + 1)
/**
* Sets the type of a script using the user_value.
*/
#define CBC_SCRIPT_SET_TYPE(script_p, user_value, ref_count) \
do \
{ \
(script_p)->refs_and_type = ((ref_count) | CBC_SCRIPT_GENERIC); \
if ((user_value) != ECMA_VALUE_EMPTY) \
{ \
(script_p)->refs_and_type = (ecma_is_value_object (user_value) ? ((ref_count) | CBC_SCRIPT_USER_OBJECT) \
: ((ref_count) | CBC_SCRIPT_USER_VALUE)); \
} \
} \
while (false)
/**
* Shared script data.
*/
typedef struct
{
#if JERRY_BUILTIN_REALMS
ecma_object_t *realm_p; /**< realm object */
#endif /* JERRY_BUILTIN_REALMS */
uint32_t refs_and_type; /**< reference counter and type of the function */
#if JERRY_RESOURCE_NAME
ecma_value_t resource_name; /**< resource name */
#endif /* JERRY_RESOURCE_NAME */
} cbc_script_t;
/**
* Script data with user value.
*/
typedef struct
{
cbc_script_t header; /**< script header */
ecma_value_t user_value; /**< user value */
} cbc_script_user_t;
#define CBC_OPCODE(arg1, arg2, arg3, arg4) arg1,
/**

View File

@ -563,9 +563,12 @@ typedef struct
uint32_t global_status_flags; /**< global status flags */
uint16_t stack_depth; /**< current stack depth */
uint16_t stack_limit; /**< maximum stack depth */
const jerry_parse_options_t *options_p; /**< parse options */
const jerry_parse_options_t *options_p; /**< parse options */
parser_saved_context_t *last_context_p; /**< last saved context */
parser_stack_iterator_t last_statement; /**< last statement position */
cbc_script_t *script_p; /**< current script */
ecma_value_t script_value; /**< current script as value */
ecma_value_t user_value; /**< current user value */
#if JERRY_MODULE_SYSTEM
ecma_module_names_t *module_names_p; /**< import / export names that is being processed */
@ -630,10 +633,6 @@ typedef struct
parser_line_counter_t last_breakpoint_line; /**< last line where breakpoint has been inserted */
#endif /* JERRY_DEBUGGER */
#if JERRY_RESOURCE_NAME
ecma_value_t resource_name; /**< resource name */
#endif /* JERRY_RESOURCE_NAME */
#if JERRY_LINE_INFO
parser_line_info_data_t line_info; /**< line info data */
parser_line_counter_t last_line_info_line; /**< last line where line info has been inserted */

View File

@ -62,17 +62,6 @@ parser_compute_indicies (parser_context_t *context_p, /**< context */
uint16_t ident_count = 0;
uint16_t const_literal_count = 0;
#if JERRY_RESOURCE_NAME
/* Resource name will be stored as the last const literal. */
if (JERRY_UNLIKELY (context_p->literal_count >= PARSER_MAXIMUM_NUMBER_OF_LITERALS))
{
parser_raise_error (context_p, PARSER_ERR_LITERAL_LIMIT_REACHED);
}
const_literal_count++;
context_p->literal_count++;
#endif /* JERRY_RESOURCE_NAME */
uint16_t ident_index;
uint16_t const_literal_index;
uint16_t literal_index;
@ -198,11 +187,6 @@ parser_compute_indicies (parser_context_t *context_p, /**< context */
}
}
#if JERRY_RESOURCE_NAME
/* Resource name will be stored as the last const literal. */
const_literal_index++;
#endif /* JERRY_RESOURCE_NAME */
JERRY_ASSERT (ident_index == context_p->register_count + ident_count);
JERRY_ASSERT (const_literal_index == ident_index + const_literal_count);
JERRY_ASSERT (literal_index <= context_p->register_count + context_p->literal_count);
@ -663,6 +647,14 @@ parser_post_processing (parser_context_t *context_p) /**< context */
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
if (JERRY_UNLIKELY (context_p->script_p->refs_and_type >= CBC_SCRIPT_REF_MAX))
{
/* This is probably never happens in practice. */
jerry_fatal (ERR_REF_COUNT_LIMIT);
}
context_p->script_p->refs_and_type += CBC_SCRIPT_REF_ONE;
JERRY_ASSERT (context_p->literal_count <= PARSER_MAXIMUM_NUMBER_OF_LITERALS);
#if JERRY_DEBUGGER
@ -963,14 +955,12 @@ parser_post_processing (parser_context_t *context_p) /**< context */
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) compiled_code_p;
args_p->stack_limit = context_p->stack_limit;
args_p->script_value = context_p->script_value;
args_p->argument_end = context_p->argument_count;
args_p->register_end = context_p->register_count;
args_p->ident_end = ident_end;
args_p->const_literal_end = const_literal_end;
args_p->literal_end = context_p->literal_count;
#if JERRY_BUILTIN_REALMS
ECMA_SET_INTERNAL_VALUE_POINTER (args_p->realm_value, JERRY_CONTEXT (global_object_p));
#endif /* JERRY_BUILTIN_REALMS */
compiled_code_p->status_flags |= CBC_CODE_FLAGS_UINT16_ARGUMENTS;
byte_code_p += sizeof (cbc_uint16_arguments_t);
@ -981,13 +971,11 @@ parser_post_processing (parser_context_t *context_p) /**< context */
args_p->stack_limit = (uint8_t) context_p->stack_limit;
args_p->argument_end = (uint8_t) context_p->argument_count;
args_p->script_value = context_p->script_value;
args_p->register_end = (uint8_t) context_p->register_count;
args_p->ident_end = (uint8_t) ident_end;
args_p->const_literal_end = (uint8_t) const_literal_end;
args_p->literal_end = (uint8_t) context_p->literal_count;
#if JERRY_BUILTIN_REALMS
ECMA_SET_INTERNAL_VALUE_POINTER (args_p->realm_value, JERRY_CONTEXT (global_object_p));
#endif /* JERRY_BUILTIN_REALMS */
byte_code_p += sizeof (cbc_uint8_arguments_t);
}
@ -1088,10 +1076,6 @@ parser_post_processing (parser_context_t *context_p) /**< context */
parser_init_literal_pool (context_p, literal_pool_p);
#if JERRY_RESOURCE_NAME
literal_pool_p[const_literal_end - 1] = context_p->resource_name;
#endif /* JERRY_RESOURCE_NAME */
page_p = context_p->byte_code.first_p;
offset = 0;
real_offset = 0;
@ -1828,14 +1812,55 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
context.token.flags = 0;
lexer_init_line_info (&context);
context.user_value = ECMA_VALUE_EMPTY;
if ((context.global_status_flags & ECMA_PARSE_EVAL)
&& JERRY_CONTEXT (vm_top_context_p) != NULL)
{
const ecma_compiled_code_t *bytecode_header_p = JERRY_CONTEXT (vm_top_context_p)->shared_p->bytecode_header_p;
#if JERRY_SNAPSHOT_EXEC
if (JERRY_LIKELY (!(bytecode_header_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION)))
{
#endif /* JERRY_SNAPSHOT_EXEC */
ecma_value_t parent_script_value = ((cbc_uint8_arguments_t *) bytecode_header_p)->script_value;;
cbc_script_t *parent_script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, parent_script_value);
if (CBC_SCRIPT_GET_TYPE (parent_script_p) != CBC_SCRIPT_GENERIC)
{
context.user_value = ((cbc_script_user_t *) parent_script_p)->user_value;
}
#if JERRY_SNAPSHOT_EXEC
}
#endif /* JERRY_SNAPSHOT_EXEC */
}
else if (context.options_p != NULL
&& (context.options_p->options & JERRY_PARSE_HAS_USER_VALUE))
{
context.user_value = context.options_p->user_value;
}
uint32_t script_size = (context.user_value != ECMA_VALUE_EMPTY ? sizeof (cbc_script_user_t)
: sizeof (cbc_script_t));
context.script_p = jmem_heap_alloc_block_null_on_error (script_size);
if (JERRY_UNLIKELY (context.script_p == NULL))
{
/* It is unlikely that memory can be allocated in an out-of-memory
* situation. However, a simple value can still be thrown. */
jcontext_raise_exception (ECMA_VALUE_NULL);
return NULL;
}
CBC_SCRIPT_SET_TYPE (context.script_p, context.user_value, CBC_SCRIPT_REF_ONE);
#if JERRY_BUILTIN_REALMS
context.script_p->realm_p = (ecma_object_t *) JERRY_CONTEXT (global_object_p);
#endif /* JERRY_BUILTIN_REALMS */
#if JERRY_RESOURCE_NAME
ecma_value_t resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
if (context.global_status_flags & ECMA_PARSE_EVAL)
{
resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL);
}
if (context.options_p != NULL
&& (context.options_p->options & JERRY_PARSE_HAS_RESOURCE)
&& context.options_p->resource_name_length > 0)
@ -1843,9 +1868,15 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
resource_name = ecma_find_or_create_literal_string (context.options_p->resource_name_p,
(lit_utf8_size_t) context.options_p->resource_name_length);
}
else if (context.global_status_flags & ECMA_PARSE_EVAL)
{
resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL);
}
context.resource_name = resource_name;
#endif /* !JERRY_RESOURCE_NAME */
context.script_p->resource_name = resource_name;
#endif /* JERRY_RESOURCE_NAME */
ECMA_SET_INTERNAL_VALUE_POINTER (context.script_value, context.script_p);
scanner_info_t scanner_info_end;
scanner_info_end.next_p = NULL;
@ -2030,6 +2061,13 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
JERRY_ASSERT (arg_list_p != NULL || !(context.status_flags & PARSER_ARGUMENTS_NEEDED));
context.script_p->refs_and_type -= CBC_SCRIPT_REF_ONE;
if (context.user_value != ECMA_VALUE_EMPTY)
{
((cbc_script_user_t *) context.script_p)->user_value = ecma_copy_value_if_not_object (context.user_value);
}
#if JERRY_PARSER_DUMP_BYTE_CODE
if (context.is_show_opcodes)
{
@ -2061,6 +2099,9 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
compiled_code_p = NULL;
parser_free_literals (&context.literal_pool);
parser_cbc_stream_free (&context.byte_code);
JERRY_ASSERT (context.script_p->refs_and_type >= CBC_SCRIPT_REF_ONE);
jmem_heap_free_block (context.script_p, script_size);
}
PARSER_TRY_END
@ -2130,7 +2171,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
ecma_raise_standard_error_with_format (JERRY_ERROR_SYNTAX,
"% [%:%:%]",
err_str_val,
context.resource_name,
resource_name,
line_str_val,
col_str_val);

View File

@ -56,7 +56,7 @@ typedef enum
PARSER_ERR_INVALID_NUMBER, /**< invalid number literal */
PARSER_ERR_MISSING_EXPONENT, /**< missing exponent */
PARSER_ERR_IDENTIFIER_AFTER_NUMBER, /**< identifier start after number */
PARSER_ERR_INVALID_UNDERSCORE_IN_NUMBER, /**< invalid use of underscore in number */
PARSER_ERR_INVALID_UNDERSCORE_IN_NUMBER, /**< invalid use of underscore in number */
#if JERRY_BUILTIN_BIGINT
PARSER_ERR_INVALID_BIGINT, /**< number is not a valid BigInt */
#endif /* JERRY_BUILTIN_BIGINT */

View File

@ -137,7 +137,8 @@ restart:
ret_value = jerry_exec_snapshot ((uint32_t *) source_p,
source_size,
source_file_p->snapshot_index,
JERRY_SNAPSHOT_EXEC_COPY_DATA);
JERRY_SNAPSHOT_EXEC_COPY_DATA,
NULL);
jerry_port_release_source (source_p);
break;

View File

@ -77,6 +77,7 @@ set(SOURCE_UNIT_TEST_MAIN_MODULES
test-regexp.c
test-regression-3588.c
test-resource-name.c
test-script-user-value.c
test-snapshot.c
test-special-proxy.c
test-string-to-number.c

View File

@ -0,0 +1,192 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "jerryscript.h"
#include "test-common.h"
static jerry_value_t user_values[4];
#define USER_VALUES_SIZE (sizeof (user_values) / sizeof (jerry_value_t))
static void
test_parse (const char *source_p, /**< source code */
jerry_parse_options_t *options_p, /**< options passed to jerry_parse */
bool run_code) /**< run the code after parsing */
{
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
options_p->user_value = user_values[i];
jerry_value_t result = jerry_parse ((const jerry_char_t *) source_p,
strlen (source_p),
options_p);
TEST_ASSERT (!jerry_value_is_error (result));
if (run_code)
{
jerry_value_t parse_result = result;
result = jerry_run (result);
jerry_release_value (parse_result);
TEST_ASSERT (!jerry_value_is_error (result));
}
jerry_value_t user_value = jerry_get_user_value (result);
jerry_value_t compare_value = jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL,
user_value,
user_values[i]);
TEST_ASSERT (jerry_value_is_true (compare_value));
jerry_release_value (compare_value);
jerry_release_value (user_value);
jerry_release_value (result);
}
} /* test_parse */
static void
test_parse_function (const char *source_p, /**< source code */
jerry_parse_options_t *options_p, /**< options passed to jerry_parse */
bool run_code) /**< run the code after parsing */
{
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
options_p->user_value = user_values[i];
jerry_value_t result = jerry_parse_function ((const jerry_char_t *) "",
0,
(const jerry_char_t *) source_p,
strlen (source_p),
options_p);
TEST_ASSERT (!jerry_value_is_error (result));
if (run_code)
{
jerry_value_t parse_result = result;
jerry_value_t this_value = jerry_create_undefined ();
result = jerry_call_function (result, this_value, NULL, 0);
jerry_release_value (parse_result);
jerry_release_value (this_value);
TEST_ASSERT (!jerry_value_is_error (result));
}
jerry_value_t user_value = jerry_get_user_value (result);
jerry_value_t compare_value = jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL,
user_value,
user_values[i]);
TEST_ASSERT (jerry_value_is_true (compare_value));
jerry_release_value (compare_value);
jerry_release_value (user_value);
jerry_release_value (result);
}
} /* test_parse_function */
int
main (void)
{
TEST_INIT ();
jerry_init (JERRY_INIT_EMPTY);
user_values[0] = jerry_create_object ();
user_values[1] = jerry_create_null ();
user_values[2] = jerry_create_number (5.5);
user_values[3] = jerry_create_string ((const jerry_char_t *) "AnyString...");
jerry_parse_options_t parse_options;
const char *source_p = TEST_STRING_LITERAL ("");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, false);
test_parse_function (source_p, &parse_options, false);
if (jerry_is_feature_enabled (JERRY_FEATURE_MODULE))
{
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, false);
}
source_p = TEST_STRING_LITERAL ("function f() { }\n"
"f");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() { return function() {} }\n"
"f()");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("return function() {}");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() {}')\n"
"f");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() { return eval(\\'(function () {})\\') }')\n"
"f()");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() {}')\n"
"return f");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("eval('function f() { return eval(\\'(function () {})\\') }')\n"
"return f()");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"f.bind(1)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"f.bind(1).bind(2, 3)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"return f.bind(1)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
source_p = TEST_STRING_LITERAL ("function f() {}\n"
"return f.bind(1).bind(2, 3)");
parse_options.options = JERRY_PARSE_HAS_USER_VALUE;
test_parse_function (source_p, &parse_options, true);
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
jerry_value_t result = jerry_get_user_value (user_values[i]);
TEST_ASSERT (jerry_value_is_undefined (result));
jerry_release_value (result);
}
for (size_t i = 0; i < USER_VALUES_SIZE; i++)
{
jerry_release_value (user_values[i]);
}
jerry_cleanup ();
return 0;
} /* main */

View File

@ -89,7 +89,8 @@ static void test_function_snapshot (void)
jerry_value_t function_obj = jerry_exec_snapshot (function_snapshot_buffer,
function_snapshot_size,
0,
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION);
JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION,
NULL);
TEST_ASSERT (!jerry_value_is_error (function_obj));
TEST_ASSERT (jerry_value_is_function (function_obj));
@ -117,7 +118,7 @@ static void test_function_snapshot (void)
static void arguments_test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint32_t exec_snapshot_flags)
{
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_value_is_number (res));
double raw_value = jerry_get_number_value (res);
@ -176,7 +177,7 @@ static void test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint
sizeof (magic_string_lengths) / sizeof (jerry_length_t),
magic_string_lengths);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags);
jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_value_is_string (res));
@ -190,6 +191,76 @@ static void test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint
jerry_cleanup ();
} /* test_exec_snapshot */
static void test_snapshot_with_user (void)
{
if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE)
&& jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC))
{
static uint32_t snapshot_buffer[SNAPSHOT_BUFFER_SIZE];
const jerry_char_t code_to_snapshot[] = TEST_STRING_LITERAL (
"function f() {}\n"
"f"
);
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t result = jerry_generate_snapshot (code_to_snapshot,
sizeof (code_to_snapshot) - 1,
NULL,
0,
snapshot_buffer,
SNAPSHOT_BUFFER_SIZE);
TEST_ASSERT (!jerry_value_is_error (result)
&& jerry_value_is_number (result));
size_t snapshot_size = (size_t) jerry_get_number_value (result);
jerry_release_value (result);
for (int i = 0; i < 3; i++)
{
jerry_exec_snapshot_option_values_t snapshot_exec_options;
if (i == 0)
{
snapshot_exec_options.user_value = jerry_create_object ();
}
else if (i == 1)
{
snapshot_exec_options.user_value = jerry_create_number (-3.5);
}
else
{
snapshot_exec_options.user_value = jerry_create_string ((const jerry_char_t *) "AnyString...");
}
result = jerry_exec_snapshot (snapshot_buffer,
snapshot_size,
0,
JERRY_SNAPSHOT_EXEC_HAS_USER_VALUE,
&snapshot_exec_options);
TEST_ASSERT (!jerry_value_is_error (result)
&& jerry_value_is_function (result));
jerry_value_t user_value = jerry_get_user_value (result);
jerry_release_value (result);
result = jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL,
user_value,
snapshot_exec_options.user_value);
TEST_ASSERT (jerry_value_is_true (result));
jerry_release_value (result);
jerry_release_value (user_value);
jerry_release_value (snapshot_exec_options.user_value);
}
jerry_cleanup ();
}
} /* test_snapshot_with_user */
int
main (void)
{
@ -227,7 +298,7 @@ main (void)
jerry_release_value (generate_result);
/* Static snapshots are not supported by default. */
jerry_value_t exec_result = jerry_exec_snapshot (snapshot_buffer, snapshot_size, 0, 0);
jerry_value_t exec_result = jerry_exec_snapshot (snapshot_buffer, snapshot_size, 0, 0, NULL);
TEST_ASSERT (jerry_value_is_error (exec_result));
jerry_release_value (exec_result);
@ -308,12 +379,12 @@ main (void)
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 0, 0);
jerry_value_t res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 0, 0, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_get_number_value (res) == 123);
jerry_release_value (res);
res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 1, 0);
res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 1, 0, NULL);
TEST_ASSERT (!jerry_value_is_error (res));
TEST_ASSERT (jerry_get_number_value (res) == 456);
jerry_release_value (res);
@ -393,5 +464,7 @@ main (void)
test_function_arguments_snapshot ();
test_snapshot_with_user ();
return 0;
} /* main */