Support dynamic import calls (#4652)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -869,6 +869,25 @@ jerry_module_get_namespace (const jerry_value_t module_val) /**< module */
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
} /* jerry_module_get_namespace */
|
||||
|
||||
/**
|
||||
* Sets the callback which is called when dynamic imports are resolved
|
||||
*/
|
||||
void
|
||||
jerry_module_set_import_callback (jerry_module_import_callback_t callback_p, /**< callback which handles
|
||||
* dynamic import calls */
|
||||
void *user_p) /**< user pointer passed to the callback */
|
||||
{
|
||||
jerry_assert_api_available ();
|
||||
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
JERRY_CONTEXT (module_import_callback_p) = callback_p;
|
||||
JERRY_CONTEXT (module_import_callback_user_p) = user_p;
|
||||
#else /* !JERRY_MODULE_SYSTEM */
|
||||
JERRY_UNUSED (callback_p);
|
||||
JERRY_UNUSED (user_p);
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
} /* jerry_module_set_import_callback */
|
||||
|
||||
/**
|
||||
* Creates a native module with a list of exports. The initial state of the module is linked.
|
||||
*
|
||||
|
||||
@@ -1371,6 +1371,82 @@ error:
|
||||
return ECMA_VALUE_ERROR;
|
||||
} /* ecma_module_link */
|
||||
|
||||
/**
|
||||
* Compute the result of 'import()' calls
|
||||
*
|
||||
* @return promise object representing the result of the operation
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_module_import (ecma_value_t specifier, /**< module specifier */
|
||||
ecma_value_t user_value) /**< user value assigned to the script */
|
||||
{
|
||||
ecma_string_t *specifier_p = ecma_op_to_string (specifier);
|
||||
|
||||
if (JERRY_UNLIKELY (specifier_p == NULL))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (JERRY_CONTEXT (module_import_callback_p) == NULL)
|
||||
{
|
||||
ecma_deref_ecma_string (specifier_p);
|
||||
goto error_module_instantiate;
|
||||
}
|
||||
|
||||
jerry_value_t result;
|
||||
result = JERRY_CONTEXT (module_import_callback_p) (ecma_make_string_value (specifier_p),
|
||||
user_value,
|
||||
JERRY_CONTEXT (module_import_callback_user_p));
|
||||
ecma_deref_ecma_string (specifier_p);
|
||||
|
||||
if (JERRY_UNLIKELY (ecma_is_value_error_reference (result)))
|
||||
{
|
||||
ecma_raise_error_from_error_reference (result);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (ecma_is_value_object (result)
|
||||
&& ecma_is_promise (ecma_get_object_from_value (result)))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
ecma_module_t *module_p = ecma_module_get_resolved_module (result);
|
||||
|
||||
if (module_p == NULL)
|
||||
{
|
||||
ecma_free_value (result);
|
||||
goto error_module_instantiate;
|
||||
}
|
||||
|
||||
if (module_p->header.u.cls.u1.module_state != JERRY_MODULE_STATE_EVALUATED)
|
||||
{
|
||||
ecma_deref_object (&module_p->header.object);
|
||||
goto error_module_instantiate;
|
||||
}
|
||||
|
||||
result = ecma_op_create_promise_object (ECMA_VALUE_EMPTY, ECMA_VALUE_UNDEFINED, NULL);
|
||||
ecma_fulfill_promise (result, ecma_make_object_value (module_p->namespace_object_p));
|
||||
ecma_deref_object (&module_p->header.object);
|
||||
return result;
|
||||
|
||||
error_module_instantiate:
|
||||
ecma_raise_range_error (ECMA_ERR_MSG ("Module cannot be instantiated"));
|
||||
|
||||
error:
|
||||
if (jcontext_has_pending_abort ())
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
ecma_value_t exception = jcontext_take_exception ();
|
||||
|
||||
ecma_value_t promise = ecma_op_create_promise_object (ECMA_VALUE_EMPTY, ECMA_VALUE_UNDEFINED, NULL);
|
||||
ecma_reject_promise (promise, exception);
|
||||
ecma_free_value (exception);
|
||||
return promise;
|
||||
} /* ecma_module_import */
|
||||
|
||||
/**
|
||||
* Cleans up and releases a module structure including all referenced modules.
|
||||
*/
|
||||
|
||||
@@ -120,6 +120,7 @@ ecma_value_t ecma_module_link (ecma_module_t *module_p,
|
||||
jerry_module_resolve_callback_t callback_p,
|
||||
void *user_p);
|
||||
ecma_value_t ecma_module_evaluate (ecma_module_t *module_p);
|
||||
ecma_value_t ecma_module_import (ecma_value_t specifier, ecma_value_t user_value);
|
||||
|
||||
ecma_module_t *ecma_module_create (void);
|
||||
void ecma_module_cleanup_context (void);
|
||||
|
||||
@@ -279,6 +279,7 @@ void jerry_module_set_state_changed_callback (jerry_module_state_changed_callbac
|
||||
size_t jerry_module_get_number_of_requests (const jerry_value_t module_val);
|
||||
jerry_value_t jerry_module_get_request (const jerry_value_t module_val, size_t request_index);
|
||||
jerry_value_t jerry_module_get_namespace (const jerry_value_t module_val);
|
||||
void jerry_module_set_import_callback (jerry_module_import_callback_t callback_p, void *user_p);
|
||||
|
||||
jerry_value_t jerry_native_module_create (jerry_native_module_evaluate_callback_t callback,
|
||||
const jerry_value_t * const exports_p, size_t number_of_exports);
|
||||
|
||||
@@ -30,7 +30,7 @@ extern "C"
|
||||
/**
|
||||
* Jerry snapshot format version.
|
||||
*/
|
||||
#define JERRY_SNAPSHOT_VERSION (66u)
|
||||
#define JERRY_SNAPSHOT_VERSION (67u)
|
||||
|
||||
/**
|
||||
* Flags for jerry_generate_snapshot and jerry_generate_function_snapshot.
|
||||
|
||||
@@ -555,6 +555,13 @@ typedef jerry_value_t (*jerry_module_resolve_callback_t) (const jerry_value_t sp
|
||||
const jerry_value_t referrer,
|
||||
void *user_p);
|
||||
|
||||
/**
|
||||
* Callback which is called when an import is resolved dynamically to get the referenced module.
|
||||
*/
|
||||
typedef jerry_value_t (*jerry_module_import_callback_t) (const jerry_value_t specifier,
|
||||
const jerry_value_t user_value,
|
||||
void *user_p);
|
||||
|
||||
/**
|
||||
* Callback which is called after the module enters into linked, evaluated or error state.
|
||||
*/
|
||||
|
||||
@@ -152,6 +152,8 @@ struct jerry_context_t
|
||||
jerry_module_state_changed_callback_t module_state_changed_callback_p; /**< callback which is called after the
|
||||
* state of a module is changed */
|
||||
void *module_state_changed_callback_user_p; /**< user pointer for module_state_changed_callback_p */
|
||||
jerry_module_import_callback_t module_import_callback_p; /**< callback for dynamic module import */
|
||||
void *module_import_callback_user_p; /**< user pointer for module_import_callback_p */
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
|
||||
vm_frame_ctx_t *vm_top_context_p; /**< top (current) interpreter context */
|
||||
|
||||
@@ -31,7 +31,7 @@ JERRY_STATIC_ASSERT (offsetof (cbc_uint8_arguments_t, script_value) == offsetof
|
||||
*/
|
||||
JERRY_STATIC_ASSERT (CBC_END == 238,
|
||||
number_of_cbc_opcodes_changed);
|
||||
JERRY_STATIC_ASSERT (CBC_EXT_END == 146,
|
||||
JERRY_STATIC_ASSERT (CBC_EXT_END == 147,
|
||||
number_of_cbc_ext_opcodes_changed);
|
||||
|
||||
#if JERRY_PARSER || JERRY_PARSER_DUMP_BYTE_CODE
|
||||
|
||||
@@ -598,6 +598,8 @@
|
||||
VM_OC_COPY_FROM_ARG) \
|
||||
CBC_OPCODE (CBC_EXT_PUSH_REST_OBJECT, CBC_NO_FLAG, 1, \
|
||||
VM_OC_PUSH_REST_OBJECT) \
|
||||
CBC_OPCODE (CBC_EXT_MODULE_IMPORT, CBC_NO_FLAG, 0, \
|
||||
VM_OC_MODULE_IMPORT) \
|
||||
CBC_OPCODE (CBC_EXT_STRING_CONCAT, CBC_NO_FLAG, -1, \
|
||||
VM_OC_STRING_CONCAT | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
|
||||
CBC_OPCODE (CBC_EXT_STRING_CONCAT_RIGHT_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
|
||||
|
||||
@@ -1929,6 +1929,16 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
|
||||
break;
|
||||
}
|
||||
#endif /* JERRY_ESNEXT */
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
case LEXER_KEYW_IMPORT:
|
||||
{
|
||||
if (new_was_seen)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_IMPORT_AFTER_NEW);
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
}
|
||||
|
||||
/* Bracketed expressions are primary expressions. At this
|
||||
@@ -2303,6 +2313,28 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
|
||||
&& context_p->token.type != LEXER_COMMA);
|
||||
}
|
||||
#endif /* JERRY_ESNEXT */
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
case LEXER_KEYW_IMPORT:
|
||||
{
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->token.type != LEXER_LEFT_PAREN)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
|
||||
}
|
||||
|
||||
lexer_next_token (context_p);
|
||||
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
|
||||
|
||||
if (context_p->token.type != LEXER_RIGHT_PAREN)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
|
||||
}
|
||||
|
||||
parser_emit_cbc_ext (context_p, CBC_EXT_MODULE_IMPORT);
|
||||
break;
|
||||
}
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
default:
|
||||
{
|
||||
bool is_left_hand_side = (*grouping_level_p == PARSE_EXPR_LEFT_HAND_SIDE);
|
||||
|
||||
@@ -2424,6 +2424,18 @@ 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);
|
||||
|
||||
if (lexer_check_next_character (context_p, LIT_CHAR_LEFT_PAREN))
|
||||
{
|
||||
if (context_p->status_flags & PARSER_IS_FUNCTION)
|
||||
{
|
||||
parser_parse_expression_statement (context_p, PARSE_EXPR);
|
||||
return;
|
||||
}
|
||||
|
||||
parser_parse_block_expression (context_p, PARSE_EXPR);
|
||||
return;
|
||||
}
|
||||
|
||||
parser_module_check_request_place (context_p);
|
||||
lexer_next_token (context_p);
|
||||
|
||||
|
||||
@@ -1372,8 +1372,11 @@ parser_error_to_string (parser_error_t error) /**< error code */
|
||||
{
|
||||
return "Export not defined in module";
|
||||
}
|
||||
case PARSER_ERR_IMPORT_AFTER_NEW:
|
||||
{
|
||||
return "Module import call is not allowed after new";
|
||||
}
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
|
||||
default:
|
||||
{
|
||||
JERRY_ASSERT (error == PARSER_ERR_NO_ERROR);
|
||||
|
||||
@@ -181,6 +181,7 @@ typedef enum
|
||||
PARSER_ERR_DUPLICATED_EXPORT_IDENTIFIER, /**< duplicated export identifier name */
|
||||
PARSER_ERR_DUPLICATED_IMPORT_BINDING, /**< duplicated import binding name */
|
||||
PARSER_ERR_EXPORT_NOT_DEFINED, /**< export is not defined in module */
|
||||
PARSER_ERR_IMPORT_AFTER_NEW, /**< module import call is not allowed after new */
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
|
||||
PARSER_ERR_NON_STRICT_ARG_DEFINITION /**< non-strict argument definition */
|
||||
|
||||
@@ -311,6 +311,20 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
|
||||
return SCAN_KEEP_TOKEN;
|
||||
}
|
||||
#endif /* JERRY_ESNEXT */
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
case LEXER_KEYW_IMPORT:
|
||||
{
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->token.type != LEXER_LEFT_PAREN)
|
||||
{
|
||||
scanner_raise_error (context_p);
|
||||
}
|
||||
|
||||
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
|
||||
return SCAN_KEEP_TOKEN;
|
||||
}
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
case LEXER_RIGHT_PAREN:
|
||||
{
|
||||
if (stack_top == SCAN_STACK_PAREN_EXPRESSION)
|
||||
@@ -1681,13 +1695,20 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
case LEXER_KEYW_IMPORT:
|
||||
{
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->token.type == LEXER_LEFT_PAREN)
|
||||
{
|
||||
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
|
||||
return SCAN_KEEP_TOKEN;
|
||||
}
|
||||
|
||||
if (stack_top != SCAN_STACK_SCRIPT)
|
||||
{
|
||||
scanner_raise_error (context_p);
|
||||
}
|
||||
|
||||
scanner_context_p->mode = SCAN_MODE_STATEMENT_END;
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->token.type == LEXER_LITERAL
|
||||
&& context_p->token.lit_location.type == LEXER_STRING_LITERAL)
|
||||
|
||||
@@ -4554,6 +4554,40 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
|
||||
JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p);
|
||||
continue;
|
||||
}
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
case VM_OC_MODULE_IMPORT:
|
||||
{
|
||||
left_value = *(--stack_top_p);
|
||||
|
||||
ecma_value_t user_value = ECMA_VALUE_UNDEFINED;
|
||||
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_header_p)->script_value;
|
||||
|
||||
#if JERRY_SNAPSHOT_EXEC
|
||||
if (JERRY_UNLIKELY (!(bytecode_header_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION)))
|
||||
{
|
||||
#endif /* JERRY_SNAPSHOT_EXEC */
|
||||
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
|
||||
|
||||
if (CBC_SCRIPT_GET_TYPE (script_p) != CBC_SCRIPT_GENERIC)
|
||||
{
|
||||
user_value = ((cbc_script_user_t *) script_p)->user_value;
|
||||
}
|
||||
#if JERRY_SNAPSHOT_EXEC
|
||||
}
|
||||
#endif /* JERRY_SNAPSHOT_EXEC */
|
||||
|
||||
result = ecma_module_import (left_value, user_value);
|
||||
ecma_free_value (left_value);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
*stack_top_p++ = result;
|
||||
continue;
|
||||
}
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
#if JERRY_DEBUGGER
|
||||
case VM_OC_BREAKPOINT_ENABLED:
|
||||
{
|
||||
|
||||
@@ -300,6 +300,10 @@ typedef enum
|
||||
VM_OC_PUSH_STATIC_FIELD_FUNC, /**< push static field initializer function */
|
||||
VM_OC_ADD_COMPUTED_FIELD, /**< add computed field name */
|
||||
#endif /* JERRY_ESNEXT */
|
||||
#if JERRY_MODULE_SYSTEM
|
||||
VM_OC_MODULE_IMPORT, /**< module dynamic import */
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
|
||||
VM_OC_NONE, /**< a special opcode for unsupported byte codes */
|
||||
} vm_oc_types;
|
||||
|
||||
@@ -384,6 +388,9 @@ typedef enum
|
||||
VM_OC_PUSH_STATIC_FIELD_FUNC = VM_OC_NONE, /**< push static field initializer function */
|
||||
VM_OC_ADD_COMPUTED_FIELD = VM_OC_NONE, /**< add computed field name */
|
||||
#endif /* !JERRY_ESNEXT */
|
||||
#if !JERRY_MODULE_SYSTEM
|
||||
VM_OC_MODULE_IMPORT = VM_OC_NONE, /**< module dynamic import */
|
||||
#endif /* JERRY_MODULE_SYSTEM */
|
||||
|
||||
VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */
|
||||
} vm_oc_unused_types;
|
||||
|
||||
Reference in New Issue
Block a user