Support parsing of scripts / functions stored in string values (#4728)

Function arguments must be passed as string values.
Snapshots are generated from compiled code rather than source code.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-08-11 17:37:12 +02:00
committed by GitHub
parent b7dead7b05
commit 3ed93cfb51
24 changed files with 601 additions and 696 deletions
+5 -2
View File
@@ -567,6 +567,10 @@ typedef struct
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 */
const uint8_t *source_start_p; /**< source start */
lit_utf8_size_t source_size; /**< source size */
const uint8_t *arguments_start_p; /**< function argument list start */
lit_utf8_size_t arguments_size; /**< function argument list size */
ecma_value_t script_value; /**< current script as value */
ecma_value_t user_value; /**< current user value */
@@ -853,8 +857,7 @@ bool scanner_literal_is_created (parser_context_t *context_p, uint16_t literal_i
bool scanner_literal_exists (parser_context_t *context_p, uint16_t literal_index);
#endif /* JERRY_ESNEXT */
void scanner_scan_all (parser_context_t *context_p, const uint8_t *arg_list_p, const uint8_t *arg_list_end_p,
const uint8_t *source_p, const uint8_t *source_end_p);
void scanner_scan_all (parser_context_t *context_p);
/**
* @}
+104 -58
View File
@@ -1761,10 +1761,7 @@ JERRY_STATIC_ASSERT (PARSER_SCANNING_SUCCESSFUL == PARSER_HAS_LATE_LIT_INIT,
* @return compiled code
*/
static ecma_compiled_code_t *
parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
size_t arg_list_size, /**< size of function argument list */
const uint8_t *source_p, /**< valid UTF-8 source code */
size_t source_size, /**< size of the source code */
parser_parse_source (void *source_p, /**< source code */
uint32_t parse_opts, /**< ecma_parse_opts_t option bits */
const jerry_parse_options_t *options_p) /**< additional configuration options */
{
@@ -1775,6 +1772,16 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
context.status_flags = parse_opts & PARSER_STRICT_MODE_MASK;
context.global_status_flags = parse_opts;
#if JERRY_ESNEXT
context.status_flags |= PARSER_RESTORE_STATUS_FLAGS (parse_opts);
context.tagged_template_literal_cp = JMEM_CP_NULL;
#endif /* JERRY_ESNEXT */
context.stack_depth = 0;
context.stack_limit = 0;
context.options_p = options_p;
context.arguments_start_p = NULL;
context.arguments_size = 0;
#if JERRY_MODULE_SYSTEM
if (context.global_status_flags & ECMA_PARSE_MODULE)
{
@@ -1784,8 +1791,23 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
context.module_names_p = NULL;
#endif /* JERRY_MODULE_SYSTEM */
if (arg_list_p != NULL)
ecma_value_t argument_list = ECMA_VALUE_EMPTY;
if (context.options_p != NULL
&& (context.options_p->options & JERRY_PARSE_HAS_ARGUMENT_LIST))
{
argument_list = context.options_p->argument_list;
}
else if (context.global_status_flags & ECMA_PARSE_HAS_ARGUMENT_LIST_VALUE)
{
JERRY_ASSERT (context.global_status_flags & ECMA_PARSE_HAS_SOURCE_VALUE);
argument_list = ((ecma_value_t *) source_p)[1];
}
if (argument_list != ECMA_VALUE_EMPTY)
{
JERRY_ASSERT (ecma_is_value_string (argument_list));
context.status_flags |= PARSER_IS_FUNCTION;
#if JERRY_ESNEXT
if (parse_opts & ECMA_PARSE_GENERATOR_FUNCTION)
@@ -1797,20 +1819,49 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
context.status_flags |= PARSER_IS_ASYNC_FUNCTION;
}
#endif /* JERRY_ESNEXT */
ecma_string_t *string_p = ecma_get_string_from_value (argument_list);
uint8_t flags = ECMA_STRING_FLAG_EMPTY;
context.arguments_start_p = ecma_string_get_chars (string_p, &context.arguments_size, NULL, NULL, &flags);
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
{
context.global_status_flags |= ECMA_PARSE_INTERNAL_FREE_ARG_LIST;
}
}
#if JERRY_ESNEXT
context.status_flags |= PARSER_RESTORE_STATUS_FLAGS (parse_opts);
context.tagged_template_literal_cp = JMEM_CP_NULL;
#endif /* JERRY_ESNEXT */
if (!(context.global_status_flags & ECMA_PARSE_HAS_SOURCE_VALUE))
{
context.source_start_p = ((parser_source_char_t *) source_p)->source_p;
context.source_size = (lit_utf8_size_t) ((parser_source_char_t *) source_p)->source_size;
}
else
{
ecma_value_t source = ((ecma_value_t *) source_p)[0];
context.stack_depth = 0;
context.stack_limit = 0;
context.options_p = options_p;
context.last_context_p = NULL;
context.last_statement.current_p = NULL;
context.token.flags = 0;
lexer_init_line_info (&context);
JERRY_ASSERT (ecma_is_value_string (source));
ecma_string_t *string_p = ecma_get_string_from_value (source);
uint8_t flags = ECMA_STRING_FLAG_EMPTY;
context.source_start_p = ecma_string_get_chars (string_p, &context.source_size, NULL, NULL, &flags);
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
{
context.global_status_flags |= ECMA_PARSE_INTERNAL_FREE_SOURCE;
}
}
#if JERRY_DEBUGGER
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
{
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE,
JERRY_DEBUGGER_NO_SUBTYPE,
context.source_start_p,
context.source_size);
}
#endif /* JERRY_DEBUGGER */
context.user_value = ECMA_VALUE_EMPTY;
@@ -1862,9 +1913,10 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
ecma_value_t resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
if (context.options_p != NULL
&& (context.options_p->options & JERRY_PARSE_HAS_RESOURCE)
&& ecma_is_value_string (context.options_p->resource_name))
&& (context.options_p->options & JERRY_PARSE_HAS_RESOURCE))
{
JERRY_ASSERT (ecma_is_value_string (context.options_p->resource_name));
ecma_ref_ecma_string (ecma_get_string_from_value (context.options_p->resource_name));
resource_name = context.options_p->resource_name;
}
@@ -1878,6 +1930,11 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
ECMA_SET_INTERNAL_VALUE_POINTER (context.script_value, context.script_p);
context.last_context_p = NULL;
context.last_statement.current_p = NULL;
context.token.flags = 0;
lexer_init_line_info (&context);
scanner_info_t scanner_info_end;
scanner_info_end.next_p = NULL;
scanner_info_end.source_p = NULL;
@@ -1929,16 +1986,12 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
if (context.is_show_opcodes)
{
JERRY_DEBUG_MSG ("\n--- %s parsing start ---\n\n",
(arg_list_p == NULL) ? "Script"
: "Function");
(context.arguments_start_p == NULL) ? "Script"
: "Function");
}
#endif /* JERRY_PARSER_DUMP_BYTE_CODE */
scanner_scan_all (&context,
arg_list_p,
arg_list_p + arg_list_size,
source_p,
source_p + source_size);
scanner_scan_all (&context);
if (JERRY_UNLIKELY (context.error != PARSER_ERR_NO_ERROR))
{
@@ -1950,15 +2003,15 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
return NULL;
}
if (arg_list_p == NULL)
if (context.arguments_start_p == NULL)
{
context.source_p = source_p;
context.source_end_p = source_p + source_size;
context.source_p = context.source_start_p;
context.source_end_p = context.source_start_p + context.source_size;
}
else
{
context.source_p = arg_list_p;
context.source_end_p = arg_list_p + arg_list_size;
context.source_p = context.arguments_start_p;
context.source_end_p = context.arguments_start_p + context.arguments_size;
}
context.u.allocated_buffer_p = NULL;
@@ -1989,15 +2042,15 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
* lexer_next_token() must be immediately called. */
lexer_next_token (&context);
if (arg_list_p != NULL)
if (context.arguments_start_p != NULL)
{
parser_parse_function_arguments (&context, LEXER_EOS);
JERRY_ASSERT (context.next_scanner_info_p->type == SCANNER_TYPE_END_ARGUMENTS);
scanner_release_next (&context, sizeof (scanner_info_t));
context.source_p = source_p;
context.source_end_p = source_p + source_size;
context.source_p = context.source_start_p;
context.source_end_p = context.source_start_p + context.source_size;
lexer_init_line_info (&context);
lexer_next_token (&context);
@@ -2016,7 +2069,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
#endif /* JERRY_MODULE_SYSTEM */
else
{
JERRY_ASSERT (context.next_scanner_info_p->source_p == source_p
JERRY_ASSERT (context.next_scanner_info_p->source_p == context.source_start_p
&& context.next_scanner_info_p->type == SCANNER_TYPE_FUNCTION);
#if JERRY_ESNEXT
@@ -2059,7 +2112,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
&& context.stack.first_p->next_p == NULL
&& context.stack.last_p == NULL);
JERRY_ASSERT (arg_list_p != NULL || !(context.status_flags & PARSER_ARGUMENTS_NEEDED));
JERRY_ASSERT (context.arguments_start_p != NULL || !(context.status_flags & PARSER_ARGUMENTS_NEEDED));
context.script_p->refs_and_type -= CBC_SCRIPT_REF_ONE;
@@ -2072,8 +2125,8 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
if (context.is_show_opcodes)
{
JERRY_DEBUG_MSG ("\n%s parsing successfully completed. Total byte code size: %d bytes\n",
(arg_list_p == NULL) ? "Script"
: "Function",
(context.arguments_start_p == NULL) ? "Script"
: "Function",
(int) context.total_byte_code_size);
}
#endif /* JERRY_PARSER_DUMP_BYTE_CODE */
@@ -2122,13 +2175,23 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
if (context.is_show_opcodes)
{
JERRY_DEBUG_MSG ("\n--- %s parsing end ---\n\n",
(arg_list_p == NULL) ? "Script"
: "Function");
(context.arguments_start_p == NULL) ? "Script"
: "Function");
}
#endif /* JERRY_PARSER_DUMP_BYTE_CODE */
parser_stack_free (&context);
if (context.global_status_flags & ECMA_PARSE_INTERNAL_FREE_SOURCE)
{
jmem_heap_free_block ((void *) context.source_start_p, context.source_size);
}
if (context.global_status_flags & ECMA_PARSE_INTERNAL_FREE_ARG_LIST)
{
jmem_heap_free_block ((void *) context.arguments_start_p, context.arguments_size);
}
if (compiled_code_p != NULL)
{
return compiled_code_p;
@@ -2930,29 +2993,12 @@ parser_raise_error (parser_context_t *context_p, /**< context */
* NULL - otherwise
*/
ecma_compiled_code_t *
parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
size_t arg_list_size, /**< size of function argument list */
const uint8_t *source_p, /**< source code */
size_t source_size, /**< size of the source code */
parser_parse_script (void *source_p, /**< source code */
uint32_t parse_opts, /**< ecma_parse_opts_t option bits */
const jerry_parse_options_t *options_p) /**< additional configuration options */
{
#if JERRY_PARSER
#if JERRY_DEBUGGER
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
{
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE,
JERRY_DEBUGGER_NO_SUBTYPE,
source_p,
source_size);
}
#endif /* JERRY_DEBUGGER */
ecma_compiled_code_t *bytecode_p = parser_parse_source (arg_list_p,
arg_list_size,
source_p,
source_size,
ecma_compiled_code_t *bytecode_p = parser_parse_source (source_p,
parse_opts,
options_p);
+10 -3
View File
@@ -192,11 +192,18 @@ typedef enum
*/
typedef uint32_t parser_line_counter_t;
/**
* Source code as character data.
*/
typedef struct
{
const uint8_t *source_p; /**< valid UTF-8 source code */
size_t source_size; /**< size of the source code */
} parser_source_char_t;
/* Note: source must be a valid UTF-8 string */
ecma_compiled_code_t *
parser_parse_script (const uint8_t *arg_list_p, size_t arg_list_size,
const uint8_t *source_p, size_t source_size,
uint32_t parse_opts, const jerry_parse_options_t *options_p);
parser_parse_script (void *source_p, uint32_t parse_opts, const jerry_parse_options_t *options_p);
#if JERRY_ERROR_MESSAGES
const char *parser_error_to_string (parser_error_t);
+12 -15
View File
@@ -2463,11 +2463,7 @@ scanner_scan_statement_end (parser_context_t *context_p, /**< context */
* Scan the whole source code.
*/
void JERRY_ATTR_NOINLINE
scanner_scan_all (parser_context_t *context_p, /**< context */
const uint8_t *arg_list_p, /**< function argument list */
const uint8_t *arg_list_end_p, /**< end of argument list */
const uint8_t *source_p, /**< valid UTF-8 source code */
const uint8_t *source_end_p) /**< end of source code */
scanner_scan_all (parser_context_t *context_p) /**< context */
{
scanner_context_t scanner_context;
@@ -2507,10 +2503,10 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
PARSER_TRY (context_p->try_buffer)
{
if (arg_list_p == NULL)
if (context_p->arguments_start_p == NULL)
{
context_p->source_p = source_p;
context_p->source_end_p = source_end_p;
context_p->source_p = context_p->source_start_p;
context_p->source_end_p = context_p->source_start_p + context_p->source_size;
uint16_t status_flags = (SCANNER_LITERAL_POOL_FUNCTION
| SCANNER_LITERAL_POOL_NO_ARGUMENTS
@@ -2522,7 +2518,7 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
}
scanner_literal_pool_t *literal_pool_p = scanner_push_literal_pool (context_p, &scanner_context, status_flags);
literal_pool_p->source_p = source_p;
literal_pool_p->source_p = context_p->source_start_p;
parser_stack_push_uint8 (context_p, SCAN_STACK_SCRIPT);
@@ -2531,8 +2527,8 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
}
else
{
context_p->source_p = arg_list_p;
context_p->source_end_p = arg_list_end_p;
context_p->source_p = context_p->arguments_start_p;
context_p->source_end_p = context_p->arguments_start_p + context_p->arguments_size;
uint16_t status_flags = SCANNER_LITERAL_POOL_FUNCTION;
@@ -3115,8 +3111,8 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
scanner_context.end_arguments_p = scanner_info_p;
context_p->next_scanner_info_p = scanner_info_p;
context_p->source_p = source_p;
context_p->source_end_p = source_end_p;
context_p->source_p = context_p->source_start_p;
context_p->source_end_p = context_p->source_start_p + context_p->source_size;
lexer_init_line_info (context_p);
#if JERRY_ESNEXT
@@ -3525,7 +3521,8 @@ scan_completed:
if (context_p->is_show_opcodes)
{
scanner_info_t *info_p = context_p->next_scanner_info_p;
const uint8_t *source_start_p = (arg_list_p == NULL) ? source_p : arg_list_p;
const uint8_t *source_start_p = (context_p->arguments_start_p == NULL ? context_p->source_start_p
: context_p->arguments_start_p);
while (info_p->type != SCANNER_TYPE_END)
{
@@ -3537,7 +3534,7 @@ scan_completed:
case SCANNER_TYPE_END_ARGUMENTS:
{
JERRY_DEBUG_MSG (" END_ARGUMENTS\n");
source_start_p = source_p;
source_start_p = context_p->source_start_p;
break;
}
case SCANNER_TYPE_FUNCTION: