Provide support for native modules with ES6 imports (#3090)

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2019-10-02 12:20:43 +02:00
committed by GitHub
parent 2a89eec98b
commit f1883b9e7d
10 changed files with 415 additions and 201 deletions
+123 -29
View File
@@ -97,12 +97,13 @@ ecma_module_create_normalized_path (const uint8_t *char_p, /**< module specifier
} /* ecma_module_create_normalized_path */
/**
* Checks if we already have a module request in the module list.
* Find a module with a specific identifier
*
* @return pointer to found or newly created module structure
* @return pointer to ecma_module_t, if found
* NULL, otherwise
*/
ecma_module_t *
ecma_module_find_or_create_module (ecma_string_t * const path_p) /**< module path */
ecma_module_find_module (ecma_string_t *const path_p) /**< module identifier */
{
ecma_module_t *current_p = JERRY_CONTEXT (ecma_modules_p);
while (current_p != NULL)
@@ -114,16 +115,59 @@ ecma_module_find_or_create_module (ecma_string_t * const path_p) /**< module pat
current_p = current_p->next_p;
}
current_p = (ecma_module_t *) jmem_heap_alloc_block (sizeof (ecma_module_t));
memset (current_p, 0, sizeof (ecma_module_t));
ecma_ref_ecma_string (path_p);
current_p->path_p = path_p;
current_p->next_p = JERRY_CONTEXT (ecma_modules_p);
JERRY_CONTEXT (ecma_modules_p) = current_p;
return current_p;
} /* ecma_module_find_module */
/**
* Create a new module
*
* @return pointer to created module
*/
static ecma_module_t *
ecma_module_create_module (ecma_string_t *const path_p) /**< module identifier */
{
ecma_module_t *module_p = (ecma_module_t *) jmem_heap_alloc_block (sizeof (ecma_module_t));
memset (module_p, 0, sizeof (ecma_module_t));
module_p->path_p = path_p;
module_p->next_p = JERRY_CONTEXT (ecma_modules_p);
JERRY_CONTEXT (ecma_modules_p) = module_p;
return module_p;
} /* ecma_module_create_module */
/**
* Checks if we already have a module request in the module list.
*
* @return pointer to found or newly created module structure
*/
ecma_module_t *
ecma_module_find_or_create_module (ecma_string_t *const path_p) /**< module path */
{
ecma_module_t *module_p = ecma_module_find_module (path_p);
if (module_p)
{
ecma_deref_ecma_string (path_p);
return module_p;
}
return ecma_module_create_module (path_p);
} /* ecma_module_find_or_create_module */
/**
* Create a new native module
*
* @return pointer to created module
*/
ecma_module_t *
ecma_module_create_native_module (ecma_string_t *const path_p, /**< module identifier */
ecma_object_t *const namespace_p) /**< module namespace */
{
ecma_module_t *module_p = ecma_module_create_module (path_p);
module_p->state = ECMA_MODULE_STATE_NATIVE;
module_p->namespace_object_p = namespace_p;
return module_p;
} /* ecma_module_create_native_module */
/**
* Creates a module context.
*
@@ -274,6 +318,30 @@ ecma_module_resolve_export (ecma_module_t * const module_p, /**< base module */
continue;
}
if (current_module_p->state == ECMA_MODULE_STATE_NATIVE)
{
ecma_object_t *object_p = current_module_p->namespace_object_p;
ecma_value_t prop_value = ecma_op_object_find_own (ecma_make_object_value (object_p),
object_p,
current_export_name_p);
if (ecma_is_value_found (prop_value))
{
found = true;
found_record.module_p = current_module_p;
found_record.name_p = current_export_name_p;
ecma_free_value (prop_value);
}
if (ecma_compare_ecma_string_to_magic_id (current_export_name_p, LIT_MAGIC_STRING_DEFAULT))
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("No default export in native module."));
break;
}
ecma_module_resolve_stack_pop (&stack_p);
continue;
}
if (context_p->local_exports_p != NULL)
{
/* 15.2.1.16.3 / 4 */
@@ -645,25 +713,44 @@ ecma_module_connect_imports (void)
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Ambiguous import request."));
}
result = ecma_module_evaluate (record.module_p);
if (ECMA_IS_VALUE_ERROR (result))
if (record.module_p->state == ECMA_MODULE_STATE_NATIVE)
{
return result;
ecma_object_t *object_p = record.module_p->namespace_object_p;
ecma_value_t prop_value = ecma_op_object_find_own (ecma_make_object_value (object_p),
object_p,
record.name_p);
JERRY_ASSERT (ecma_is_value_found (prop_value));
ecma_op_create_mutable_binding (local_env_p, import_names_p->local_name_p, true /* is_deletable */);
ecma_op_set_mutable_binding (local_env_p,
import_names_p->local_name_p,
prop_value,
false /* is_strict */);
ecma_free_value (prop_value);
}
else
{
result = ecma_module_evaluate (record.module_p);
ecma_object_t *ref_base_lex_env_p;
ecma_value_t prop_value = ecma_op_get_value_lex_env_base (record.module_p->scope_p,
&ref_base_lex_env_p,
record.name_p);
if (ECMA_IS_VALUE_ERROR (result))
{
return result;
}
ecma_op_create_mutable_binding (local_env_p, import_names_p->local_name_p, true /* is_deletable */);
ecma_op_set_mutable_binding (local_env_p,
import_names_p->local_name_p,
prop_value,
false /* is_strict */);
ecma_object_t *ref_base_lex_env_p;
ecma_value_t prop_value = ecma_op_get_value_lex_env_base (record.module_p->scope_p,
&ref_base_lex_env_p,
record.name_p);
ecma_free_value (prop_value);
ecma_op_create_mutable_binding (local_env_p, import_names_p->local_name_p, true /* is_deletable */);
ecma_op_set_mutable_binding (local_env_p,
import_names_p->local_name_p,
prop_value,
false /* is_strict */);
ecma_free_value (prop_value);
}
}
import_names_p = import_names_p->next_p;
@@ -873,6 +960,17 @@ static void
ecma_module_release_module (ecma_module_t *module_p) /**< module */
{
ecma_deref_ecma_string (module_p->path_p);
if (module_p->namespace_object_p != NULL)
{
ecma_deref_object (module_p->namespace_object_p);
}
if (module_p->state == ECMA_MODULE_STATE_NATIVE)
{
goto finished;
}
if (module_p->state >= ECMA_MODULE_STATE_PARSING)
{
ecma_module_release_module_context (module_p->context_p);
@@ -889,11 +987,7 @@ ecma_module_release_module (ecma_module_t *module_p) /**< module */
ecma_bytecode_deref (module_p->compiled_code_p);
}
if (module_p->namespace_object_p != NULL)
{
ecma_deref_object (module_p->namespace_object_p);
}
finished:
jmem_heap_free_block (module_p, sizeof (ecma_module_t));
} /* ecma_module_release_module */
+9 -5
View File
@@ -70,6 +70,7 @@ typedef enum
ECMA_MODULE_STATE_PARSED = 2, /**< module has been parsed */
ECMA_MODULE_STATE_EVALUATING = 3, /**< module is currently being evaluated */
ECMA_MODULE_STATE_EVALUATED = 4, /**< module has been evaluated */
ECMA_MODULE_STATE_NATIVE = 5, /**< module is native */
} ecma_module_state_t;
/**
@@ -116,18 +117,21 @@ typedef struct ecma_module_resolve_stack
} ecma_module_resolve_stack_t;
bool ecma_module_resolve_set_insert (ecma_module_resolve_set_t **set_p,
ecma_module_t * const module_p,
ecma_string_t * const export_name_p);
ecma_module_t *const module_p,
ecma_string_t *const export_name_p);
void ecma_module_resolve_set_cleanup (ecma_module_resolve_set_t *set_p);
void ecma_module_resolve_stack_push (ecma_module_resolve_stack_t **stack_p,
ecma_module_t * const module_p,
ecma_string_t * const export_name_p);
ecma_module_t *const module_p,
ecma_string_t *const export_name_p);
void ecma_module_resolve_stack_pop (ecma_module_resolve_stack_t **stack_p);
ecma_string_t *ecma_module_create_normalized_path (const uint8_t *char_p,
prop_length_t size);
ecma_module_t *ecma_module_find_or_create_module (ecma_string_t * const path_p);
ecma_module_t *ecma_module_find_module (ecma_string_t *const path_p);
ecma_module_t *ecma_module_create_native_module (ecma_string_t *const path_p,
ecma_object_t *const namespace_p);
ecma_module_t *ecma_module_find_or_create_module (ecma_string_t *const path_p);
ecma_value_t ecma_module_connect_imports (void);
ecma_value_t ecma_module_parse_modules (void);
+15
View File
@@ -21,6 +21,7 @@
#include <stdio.h>
#include "jerryscript-compiler.h"
#include "jerryscript-core.h"
#ifdef __cplusplus
extern "C"
@@ -236,6 +237,20 @@ size_t jerry_port_normalize_path (const char *in_path_p,
size_t out_buf_size,
char *base_file_p);
/**
* Get the module object of a native module.
*
* Note:
* This port function is called by jerry-core when ES2015_MODULE_SYSTEM
* is enabled.
*
* @param name String value of the module specifier.
*
* @return Undefined, if 'name' is not a native module
* jerry_value_t containing the module object, otherwise
*/
jerry_value_t jerry_port_get_native_module (jerry_value_t name);
/**
* @}
*/
+25 -7
View File
@@ -284,16 +284,12 @@ parser_module_context_init (void)
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);
if (path_p != path_str_p)
{
ecma_deref_ecma_string (path_p);
}
module_p->state = ECMA_MODULE_STATE_EVALUATED;
module_p->scope_p = ecma_get_global_environment ();
ecma_ref_object (module_p->scope_p);
@@ -542,6 +538,28 @@ 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);
ecma_module_t *module_p = ecma_module_find_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);
@@ -550,9 +568,9 @@ parser_module_handle_module_specifier (parser_context_t *context_p) /**< parser
parser_raise_error (context_p, PARSER_ERR_FILE_NOT_FOUND);
}
ecma_module_t *module_p = ecma_module_find_or_create_module (path_p);
ecma_deref_ecma_string (path_p);
module_p = ecma_module_find_or_create_module (path_p);
module_found:
module_node_p->module_request_p = module_p;
lexer_next_token (context_p);
} /* parser_module_handle_module_specifier */