Rework module parsing and execution (#4462)

This patch disables automatic detection of module code, and instead
requires the user to explicitly specify whether to parse a source
as a module or as a script.

To achieve this the jerry_parse API function now takes a new option
which signals that the source should be parsed as a module.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai daniel.batyai@h-lab.eu
This commit is contained in:
Dániel Bátyai
2021-01-18 15:33:43 +01:00
committed by GitHub
parent ef8a6a9f39
commit 0fec9135ec
85 changed files with 575 additions and 492 deletions
+27 -79
View File
@@ -55,7 +55,7 @@ parser_module_check_duplicate_import (parser_context_t *context_p, /**< parser c
module_names_p = module_names_p->next_p;
}
ecma_module_node_t *module_node_p = JERRY_CONTEXT (module_top_context_p)->imports_p;
ecma_module_node_t *module_node_p = JERRY_CONTEXT (module_current_p)->imports_p;
while (module_node_p != NULL)
{
module_names_p = module_node_p->module_names_p;
@@ -124,7 +124,7 @@ parser_module_check_duplicate_export (parser_context_t *context_p, /**< parser c
current_names_p = current_names_p->next_p;
}
ecma_module_node_t *export_node_p = JERRY_CONTEXT (module_top_context_p)->local_exports_p;
ecma_module_node_t *export_node_p = JERRY_CONTEXT (module_current_p)->local_exports_p;
if (export_node_p != NULL)
{
JERRY_ASSERT (export_node_p->next_p == NULL);
@@ -141,7 +141,7 @@ parser_module_check_duplicate_export (parser_context_t *context_p, /**< parser c
}
}
export_node_p = JERRY_CONTEXT (module_top_context_p)->indirect_exports_p;
export_node_p = JERRY_CONTEXT (module_current_p)->indirect_exports_p;
while (export_node_p != NULL)
{
ecma_module_names_t *name_p = export_node_p->module_names_p;
@@ -167,7 +167,7 @@ parser_module_check_duplicate_export (parser_context_t *context_p, /**< parser c
* Add export node to parser context.
*/
void
parser_module_add_export_node_to_context (parser_context_t *context_p) /**< parser context */
parser_module_finalize_export_node (parser_context_t *context_p) /**< parser context */
{
ecma_module_node_t *module_node_p = context_p->module_current_node_p;
context_p->module_current_node_p = NULL;
@@ -180,17 +180,17 @@ parser_module_add_export_node_to_context (parser_context_t *context_p) /**< pars
if (!module_node_p->module_names_p)
{
/* If there are no names in the node, then it's a star export. */
export_list_p = &(JERRY_CONTEXT (module_top_context_p)->star_exports_p);
export_list_p = &(JERRY_CONTEXT (module_current_p)->star_exports_p);
}
else
{
export_list_p = &(JERRY_CONTEXT (module_top_context_p)->indirect_exports_p);
export_list_p = &(JERRY_CONTEXT (module_current_p)->indirect_exports_p);
}
}
else
{
/* If there is no module request, then it's a local export. */
export_list_p = &(JERRY_CONTEXT (module_top_context_p)->local_exports_p);
export_list_p = &(JERRY_CONTEXT (module_current_p)->local_exports_p);
}
/* Check if we have a node with the same module request, append to it if we do. */
@@ -213,7 +213,7 @@ parser_module_add_export_node_to_context (parser_context_t *context_p) /**< pars
module_node_p->module_names_p = NULL;
}
ecma_module_release_module_nodes (module_node_p);
jmem_heap_free_block (module_node_p, sizeof (ecma_module_node_t));
return;
}
@@ -222,22 +222,22 @@ parser_module_add_export_node_to_context (parser_context_t *context_p) /**< pars
module_node_p->next_p = *export_list_p;
*export_list_p = module_node_p;
} /* parser_module_add_export_node_to_context */
} /* parser_module_finalize_export_node */
/**
* Add import node to parser context.
*/
void
parser_module_add_import_node_to_context (parser_context_t *context_p) /**< parser context */
parser_module_finalize_import_node (parser_context_t *context_p) /**< parser context */
{
ecma_module_node_t *module_node_p = context_p->module_current_node_p;
context_p->module_current_node_p = NULL;
ecma_module_node_t *stored_imports = JERRY_CONTEXT (module_top_context_p)->imports_p;
ecma_module_node_t *stored_imports_p = JERRY_CONTEXT (module_current_p)->imports_p;
/* Check if we have a node with the same module request, append to it if we do. */
while (stored_imports != NULL)
while (stored_imports_p != NULL)
{
if (stored_imports->module_request_p == module_node_p->module_request_p)
if (stored_imports_p->module_request_p == module_node_p->module_request_p)
{
ecma_module_names_t *module_names_p = module_node_p->module_names_p;
@@ -248,21 +248,21 @@ parser_module_add_import_node_to_context (parser_context_t *context_p) /**< pars
module_names_p = module_names_p->next_p;
}
module_names_p->next_p = stored_imports->module_names_p;
stored_imports->module_names_p = module_node_p->module_names_p;
module_names_p->next_p = stored_imports_p->module_names_p;
stored_imports_p->module_names_p = module_node_p->module_names_p;
module_node_p->module_names_p = NULL;
}
ecma_module_release_module_nodes (module_node_p);
jmem_heap_free_block (module_node_p, sizeof (ecma_module_node_t));
return;
}
stored_imports = stored_imports->next_p;
stored_imports_p = stored_imports_p->next_p;
}
module_node_p->next_p = JERRY_CONTEXT (module_top_context_p)->imports_p;
JERRY_CONTEXT (module_top_context_p)->imports_p = module_node_p;
} /* parser_module_add_import_node_to_context */
module_node_p->next_p = JERRY_CONTEXT (module_current_p)->imports_p;
JERRY_CONTEXT (module_current_p)->imports_p = module_node_p;
} /* parser_module_finalize_import_node */
/**
* Add module names to current module node.
@@ -289,50 +289,6 @@ parser_module_add_names_to_node (parser_context_t *context_p, /**< parser contex
new_names_p->local_name_p = local_name_p;
} /* parser_module_add_names_to_node */
/**
* Create module context if needed.
*/
void
parser_module_context_init (parser_context_t *context_p)
{
if (JERRY_CONTEXT (module_top_context_p) == NULL)
{
ecma_module_context_t *module_context_p;
module_context_p = (ecma_module_context_t *) jmem_heap_alloc_block (sizeof (ecma_module_context_t));
memset (module_context_p, 0, sizeof (ecma_module_context_t));
JERRY_CONTEXT (module_top_context_p) = module_context_p;
ecma_string_t *path_str_p = ecma_get_string_from_value (context_p->resource_name);
lit_utf8_size_t path_str_size;
uint8_t flags = ECMA_STRING_FLAG_EMPTY;
const lit_utf8_byte_t *path_str_chars_p = ecma_string_get_chars (path_str_p,
&path_str_size,
NULL,
NULL,
&flags);
ecma_string_t *path_p = ecma_module_create_normalized_path (path_str_chars_p,
(prop_length_t) path_str_size);
if (path_p == NULL)
{
ecma_ref_ecma_string (path_str_p);
path_p = path_str_p;
}
ecma_module_t *module_p = ecma_module_find_or_create_module (path_p);
module_p->state = ECMA_MODULE_STATE_EVALUATED;
/* The lexical scope of the root module does not exist yet. */
module_p->scope_p = NULL;
module_p->context_p = module_context_p;
module_context_p->module_p = module_p;
}
} /* parser_module_context_init */
/**
* Create a permanent import/export node from a template node.
* @return - the copy of the template if the second parameter is not NULL.
@@ -567,7 +523,8 @@ parser_module_check_request_place (parser_context_t *context_p) /**< parser cont
if (context_p->last_context_p != NULL
|| context_p->stack_top_uint8 != 0
|| (context_p->status_flags & PARSER_IS_FUNCTION)
|| (context_p->global_status_flags & ECMA_PARSE_EVAL))
|| (context_p->global_status_flags & ECMA_PARSE_EVAL)
|| (context_p->global_status_flags & ECMA_PARSE_MODULE) == 0)
{
parser_raise_error (context_p, PARSER_ERR_MODULE_UNEXPECTED);
}
@@ -592,34 +549,25 @@ parser_module_handle_module_specifier (parser_context_t *context_p) /**< parser
ecma_string_t *name_p = ecma_new_ecma_string_from_utf8 (context_p->lit_object.literal_p->u.char_p,
context_p->lit_object.literal_p->prop.length);
ecma_module_t *module_p = ecma_module_find_module (name_p);
ecma_module_t *module_p = ecma_module_find_native_module (name_p);
if (module_p)
{
ecma_deref_ecma_string (name_p);
goto module_found;
}
ecma_value_t native = jerry_port_get_native_module (ecma_make_string_value (name_p));
if (!ecma_is_value_undefined (native))
{
JERRY_ASSERT (ecma_is_value_object (native));
ecma_object_t *module_object_p = ecma_get_object_from_value (native);
module_p = ecma_module_create_native_module (name_p, module_object_p);
goto module_found;
}
ecma_deref_ecma_string (name_p);
ecma_string_t *path_p = ecma_module_create_normalized_path (context_p->lit_object.literal_p->u.char_p,
context_p->lit_object.literal_p->prop.length);
context_p->lit_object.literal_p->prop.length,
JERRY_CONTEXT (module_current_p)->path_p);
if (path_p == NULL)
{
parser_raise_error (context_p, PARSER_ERR_FILE_NOT_FOUND);
}
module_p = ecma_module_find_or_create_module (path_p);
module_p = ecma_module_find_module (path_p);
module_found:
module_node_p->module_request_p = module_p;