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:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user