Rework module linking (#4632)

The module linking process from jerry_parse is moved out into
a new jerry_module_link function, and jerry_parse is limited to
create unlinked modules.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-03-30 15:40:09 +02:00
committed by GitHub
parent 874a6a49d5
commit 6c484f3529
22 changed files with 1574 additions and 789 deletions
+3 -5
View File
@@ -551,7 +551,7 @@ typedef struct
parser_stack_iterator_t last_statement; /**< last statement position */
#if JERRY_MODULE_SYSTEM
ecma_module_node_t *module_current_node_p; /**< import / export node that is being processed */
ecma_module_names_t *module_names_p; /**< import / export names that is being processed */
lexer_literal_t *module_identifier_lit_p; /**< the literal for the identifier of the current element */
#endif /* JERRY_MODULE_SYSTEM */
@@ -858,16 +858,14 @@ void parser_free_jumps (parser_stack_iterator_t iterator);
*/
extern const lexer_lit_location_t lexer_default_literal;
void parser_module_finalize_export_node (parser_context_t *context_p);
void parser_module_finalize_import_node (parser_context_t *context_p);
void parser_module_check_request_place (parser_context_t *context_p);
void parser_module_context_init (parser_context_t *context_p);
void parser_module_handle_module_specifier (parser_context_t *context_p);
void parser_module_append_names (parser_context_t *context_p, ecma_module_names_t **module_names_p);
void parser_module_handle_module_specifier (parser_context_t *context_p, ecma_module_node_t **node_list_p);
void parser_module_handle_requests (parser_context_t *context_p);
void parser_module_parse_export_clause (parser_context_t *context_p);
void parser_module_parse_import_clause (parser_context_t *context_p);
void parser_module_set_default (parser_context_t *context_p);
ecma_module_node_t *parser_module_create_module_node (parser_context_t *context_p);
bool parser_module_check_duplicate_import (parser_context_t *context_p, ecma_string_t *local_name_p);
bool parser_module_check_duplicate_export (parser_context_t *context_p, ecma_string_t *export_name_p);
void parser_module_append_export_name (parser_context_t *context_p);
+137 -165
View File
@@ -43,7 +43,8 @@ bool
parser_module_check_duplicate_import (parser_context_t *context_p, /**< parser context */
ecma_string_t *local_name_p) /**< newly imported name */
{
ecma_module_names_t *module_names_p = context_p->module_current_node_p->module_names_p;
ecma_module_names_t *module_names_p = context_p->module_names_p;
while (module_names_p != NULL)
{
if (ecma_compare_ecma_strings (module_names_p->local_name_p, local_name_p))
@@ -55,6 +56,7 @@ parser_module_check_duplicate_import (parser_context_t *context_p, /**< parser c
}
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;
@@ -113,7 +115,8 @@ parser_module_check_duplicate_export (parser_context_t *context_p, /**< parser c
ecma_string_t *export_name_p) /**< exported identifier */
{
/* We have to check in the currently constructed node, as well as all of the already added nodes. */
ecma_module_names_t *current_names_p = context_p->module_current_node_p->module_names_p;
ecma_module_names_t *current_names_p = context_p->module_names_p;
while (current_names_p != NULL)
{
if (ecma_compare_ecma_strings (current_names_p->imex_name_p, export_name_p))
@@ -123,27 +126,23 @@ 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_current_p)->local_exports_p;
if (export_node_p != NULL)
ecma_module_names_t *name_p = JERRY_CONTEXT (module_current_p)->local_exports_p;
while (name_p != NULL)
{
JERRY_ASSERT (export_node_p->next_p == NULL);
ecma_module_names_t *name_p = export_node_p->module_names_p;
while (name_p != NULL)
if (ecma_compare_ecma_strings (name_p->imex_name_p, export_name_p))
{
if (ecma_compare_ecma_strings (name_p->imex_name_p, export_name_p))
{
return true;
}
name_p = name_p->next_p;
return true;
}
name_p = name_p->next_p;
}
export_node_p = JERRY_CONTEXT (module_current_p)->indirect_exports_p;
ecma_module_node_t *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;
name_p = export_node_p->module_names_p;
while (name_p != NULL)
{
@@ -162,107 +161,6 @@ parser_module_check_duplicate_export (parser_context_t *context_p, /**< parser c
return false;
} /* parser_module_check_duplicate_export */
/**
* Add export node to parser context.
*/
void
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;
ecma_module_node_t **export_list_p;
/* Check which list we should add it to. */
if (module_node_p->module_request_p)
{
/* If the export node has a module request, that means it's either an indirect export, or a star export. */
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_current_p)->star_exports_p);
}
else
{
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_current_p)->local_exports_p);
}
/* Check if we have a node with the same module request, append to it if we do. */
ecma_module_node_t *stored_exports_p = *export_list_p;
while (stored_exports_p != NULL)
{
if (stored_exports_p->module_request_p == module_node_p->module_request_p)
{
ecma_module_names_t *module_names_p = module_node_p->module_names_p;
if (module_names_p != NULL)
{
while (module_names_p->next_p != NULL)
{
module_names_p = module_names_p->next_p;
}
module_names_p->next_p = stored_exports_p->module_names_p;
stored_exports_p->module_names_p = module_node_p->module_names_p;
module_node_p->module_names_p = NULL;
}
jmem_heap_free_block (module_node_p, sizeof (ecma_module_node_t));
return;
}
stored_exports_p = stored_exports_p->next_p;
}
module_node_p->next_p = *export_list_p;
*export_list_p = module_node_p;
} /* parser_module_finalize_export_node */
/**
* Add import node to parser context.
*/
void
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_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_p != NULL)
{
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;
if (module_names_p != NULL)
{
while (module_names_p->next_p != NULL)
{
module_names_p = module_names_p->next_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;
}
jmem_heap_free_block (module_node_p, sizeof (ecma_module_node_t));
return;
}
stored_imports_p = stored_imports_p->next_p;
}
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.
*/
@@ -271,37 +169,21 @@ parser_module_add_names_to_node (parser_context_t *context_p, /**< parser contex
ecma_string_t *imex_name_p, /**< import/export name */
ecma_string_t *local_name_p) /**< local name */
{
ecma_module_names_t *new_names_p = (ecma_module_names_t *) parser_malloc (context_p,
sizeof (ecma_module_names_t));
memset (new_names_p, 0, sizeof (ecma_module_names_t));
ecma_module_names_t *new_name_p = (ecma_module_names_t *) parser_malloc (context_p,
sizeof (ecma_module_names_t));
ecma_module_node_t *module_node_p = context_p->module_current_node_p;
new_names_p->next_p = module_node_p->module_names_p;
module_node_p->module_names_p = new_names_p;
new_name_p->next_p = context_p->module_names_p;
context_p->module_names_p = new_name_p;
JERRY_ASSERT (imex_name_p != NULL);
ecma_ref_ecma_string (imex_name_p);
new_names_p->imex_name_p = imex_name_p;
new_name_p->imex_name_p = imex_name_p;
JERRY_ASSERT (local_name_p != NULL);
ecma_ref_ecma_string (local_name_p);
new_names_p->local_name_p = local_name_p;
new_name_p->local_name_p = local_name_p;
} /* parser_module_add_names_to_node */
/**
* Create a permanent import/export node from a template node.
* @return - the copy of the template if the second parameter is not NULL.
* - otherwise: an empty node.
*/
ecma_module_node_t *
parser_module_create_module_node (parser_context_t *context_p) /**< parser context */
{
ecma_module_node_t *node_p = (ecma_module_node_t *) parser_malloc (context_p, sizeof (ecma_module_node_t));
memset (node_p, 0, sizeof (ecma_module_node_t));
return node_p;
} /* parser_module_create_module_node */
/**
* Parse an ExportClause.
*/
@@ -529,13 +411,41 @@ parser_module_check_request_place (parser_context_t *context_p) /**< parser cont
}
} /* parser_module_check_request_place */
/**
* Append names to the names list.
*/
void
parser_module_append_names (parser_context_t *context_p, /**< parser context */
ecma_module_names_t **module_names_p) /**< target names */
{
ecma_module_names_t *last_name_p = context_p->module_names_p;
if (last_name_p == NULL)
{
return;
}
if (*module_names_p != NULL)
{
while (last_name_p->next_p != NULL)
{
last_name_p = last_name_p->next_p;
}
last_name_p->next_p = *module_names_p;
}
*module_names_p = context_p->module_names_p;
context_p->module_names_p = NULL;
} /* parser_module_append_names */
/**
* Handle module specifier at the end of the import / export statement.
*/
void
parser_module_handle_module_specifier (parser_context_t *context_p) /**< parser context */
parser_module_handle_module_specifier (parser_context_t *context_p, /**< parser context */
ecma_module_node_t **node_list_p) /**< target node list */
{
ecma_module_node_t *module_node_p = context_p->module_current_node_p;
if (context_p->token.type != LEXER_LITERAL
|| context_p->token.lit_location.type != LEXER_STRING_LITERAL
|| context_p->token.lit_location.length == 0)
@@ -545,32 +455,94 @@ parser_module_handle_module_specifier (parser_context_t *context_p) /**< parser
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
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);
lexer_literal_t *path_p = context_p->lit_object.literal_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_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,
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_module (path_p);
module_found:
module_node_p->module_request_p = module_p;
lexer_next_token (context_p);
/* The lexer_next_token may throw an error, so the path is constructed after its call. */
ecma_string_t *path_string_p = ecma_new_ecma_string_from_utf8 (path_p->u.char_p, path_p->prop.length);
ecma_module_node_t *node_p = JERRY_CONTEXT (module_current_p)->imports_p;
ecma_module_node_t *last_node_p = NULL;
/* Check if we have an import node with the same module request. */
while (node_p != NULL)
{
if (ecma_compare_ecma_strings (ecma_get_string_from_value (node_p->u.path_or_module), path_string_p))
{
ecma_deref_ecma_string (path_string_p);
break;
}
last_node_p = node_p;
node_p = node_p->next_p;
}
if (node_p == NULL)
{
node_p = (ecma_module_node_t *) jmem_heap_alloc_block_null_on_error (sizeof (ecma_module_node_t));
if (node_p == NULL)
{
ecma_deref_ecma_string (path_string_p);
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
}
if (last_node_p == NULL)
{
JERRY_CONTEXT (module_current_p)->imports_p = node_p;
}
else
{
last_node_p->next_p = node_p;
}
node_p->next_p = NULL;
node_p->module_names_p = NULL;
node_p->u.path_or_module = ecma_make_string_value (path_string_p);
}
/* Append to imports. */
if (node_list_p == NULL)
{
parser_module_append_names (context_p, &node_p->module_names_p);
return;
}
ecma_value_t *module_object_p = &node_p->u.path_or_module;
node_p = *node_list_p;
last_node_p = NULL;
while (node_p != NULL)
{
if (node_p->u.module_object_p == module_object_p)
{
parser_module_append_names (context_p, &node_p->module_names_p);
return;
}
last_node_p = node_p;
node_p = node_p->next_p;
}
node_p = (ecma_module_node_t *) parser_malloc (context_p, sizeof (ecma_module_node_t));
if (last_node_p == NULL)
{
*node_list_p = node_p;
}
else
{
last_node_p->next_p = node_p;
}
node_p->next_p = NULL;
node_p->module_names_p = context_p->module_names_p;
node_p->u.module_object_p = module_object_p;
context_p->module_names_p = NULL;
} /* parser_module_handle_module_specifier */
#endif /* JERRY_MODULE_SYSTEM */
+9 -15
View File
@@ -2425,11 +2425,9 @@ static void
parser_parse_import_statement (parser_context_t *context_p) /**< parser context */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_IMPORT);
JERRY_ASSERT (context_p->module_names_p == NULL);
parser_module_check_request_place (context_p);
context_p->module_current_node_p = parser_module_create_module_node (context_p);
lexer_next_token (context_p);
/* Check for a ModuleSpecifier*/
@@ -2482,7 +2480,7 @@ parser_parse_import_statement (parser_context_t *context_p) /**< parser context
if (context_p->token.type == LEXER_MULTIPLY)
{
/* NameSpaceImport*/
/* NameSpaceImport */
lexer_next_token (context_p);
if (!lexer_token_is_identifier (context_p, "as", 2))
{
@@ -2527,10 +2525,7 @@ parser_parse_import_statement (parser_context_t *context_p) /**< parser context
lexer_next_token (context_p);
}
parser_module_handle_module_specifier (context_p);
parser_module_finalize_import_node (context_p);
context_p->module_current_node_p = NULL;
parser_module_handle_module_specifier (context_p, NULL);
} /* parser_parse_import_statement */
/**
@@ -2543,11 +2538,10 @@ static bool
parser_parse_export_statement (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_EXPORT);
JERRY_ASSERT (context_p->module_names_p == NULL);
parser_module_check_request_place (context_p);
context_p->module_current_node_p = parser_module_create_module_node (context_p);
bool consume_last_statement = false;
lexer_next_token (context_p);
@@ -2628,8 +2622,8 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */
}
lexer_next_token (context_p);
parser_module_handle_module_specifier (context_p);
break;
parser_module_handle_module_specifier (context_p, &(JERRY_CONTEXT (module_current_p)->star_exports_p));
return false;
}
case LEXER_KEYW_VAR:
case LEXER_KEYW_LET:
@@ -2660,7 +2654,8 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */
if (lexer_token_is_identifier (context_p, "from", 4))
{
lexer_next_token (context_p);
parser_module_handle_module_specifier (context_p);
parser_module_handle_module_specifier (context_p, &(JERRY_CONTEXT (module_current_p)->indirect_exports_p));
return false;
}
break;
}
@@ -2672,8 +2667,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */
}
context_p->status_flags &= (uint32_t) ~(PARSER_MODULE_DEFAULT_CLASS_OR_FUNC | PARSER_MODULE_STORE_IDENT);
parser_module_finalize_export_node (context_p);
context_p->module_current_node_p = NULL;
parser_module_append_names (context_p, &(JERRY_CONTEXT (module_current_p)->local_exports_p));
return consume_last_statement;
} /* parser_parse_export_statement */
+3 -3
View File
@@ -1810,7 +1810,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
context.status_flags |= PARSER_IS_STRICT;
}
context.module_current_node_p = NULL;
context.module_names_p = NULL;
#endif /* JERRY_MODULE_SYSTEM */
if (arg_list_p != NULL)
@@ -2051,9 +2051,9 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
scanner_cleanup (&context);
#if JERRY_MODULE_SYSTEM
if (context.module_current_node_p != NULL)
if (context.module_names_p != NULL)
{
ecma_module_release_module_nodes (context.module_current_node_p);
ecma_module_release_module_names (context.module_names_p);
}
#endif