diff --git a/docs/02.API-REFERENCE.md b/docs/02.API-REFERENCE.md index 835e960b7..ed6278fbc 100644 --- a/docs/02.API-REFERENCE.md +++ b/docs/02.API-REFERENCE.md @@ -952,6 +952,31 @@ typedef void (*jerry_module_state_changed_callback_t) (jerry_module_state_t new_ **See also** - [jerry_module_set_state_changed_callback](#jerry_module_set_state_changed_callback) +## jerry_module_import_meta_callback_t + +**Summary** + +Callback which is called when an import.meta expression of a module is evaluated the first time. +The object returned by import.meta is passed as the `meta_object` argument to the callback, and +the callback can set the initial status of the object (e.g. add properties or set prototype). + +**Prototype** + +```c +typedef void (*jerry_module_import_meta_callback_t) (const jerry_value_t module, + const jerry_value_t meta_object, + void *user_p); +``` + +- `module` - module whose import.meta object is requested. +- `meta_object` - import.meta object created for the module. +- `user_p` - pointer passed to [jerry_module_set_import_meta_callback](#jerry_module_set_import_meta_callback). + +*New in version [[NEXT_RELEASE]]*. + +**See also** +- [jerry_module_set_import_meta_callback](#jerry_module_set_import_meta_callback) + ## jerry_native_module_evaluate_callback_t **Summary** @@ -4996,13 +5021,12 @@ Sets a callback which is called after a module state is changed to linked, evalu **Prototype** ```c -void -jerry_module_set_state_changed_callback (jerry_module_state_changed_callback_t callback, - void *user_p) +void jerry_module_set_state_changed_callback (jerry_module_state_changed_callback_t callback, + void *user_p); ``` - `callback` - callback, which is called after the state change. -- `module_val` - pointer passed to the callback function. +- `user_p` - pointer passed to the callback function. *New in version [[NEXT_RELEASE]]*. @@ -5060,6 +5084,82 @@ main (void) - [jerry_module_state_t](#jerry_module_state_t) - [jerry_module_state_changed_callback_t](#jerry_module_state_changed_callback_t) +## jerry_module_set_import_meta_callback + +**Summary** + +Sets a callback which is called when an import.meta expression of a module is evaluated the first time. + +*Notes*: +- This API depends on a build option (`JERRY_MODULE_SYSTEM`) and can be checked + in runtime with the `JERRY_FEATURE_MODULE` feature enum value, + see: [jerry_is_feature_enabled](#jerry_is_feature_enabled). + +**Prototype** + +```c +void jerry_module_set_import_meta_callback (jerry_module_import_meta_callback_t callback, + void *user_p); +``` + +- `callback` - callback, which is called when an import.meta + expression of a module is evaluated the first time +- `user_p` - pointer passed to the callback function. + +*New in version [[NEXT_RELEASE]]*. + +**Example** + +[doctest]: # (test="compile") + +```c +#include +#include + +static void +module_import_meta_callback (const jerry_value_t module, /**< module */ + const jerry_value_t meta_object, /**< import.meta object */ + void *user_p) /**< user pointer */ +{ + (void) user_p; + + /* Create a property for the meta object */ + jerry_value_t property_name_value = jerry_create_string ((const jerry_char_t *) "prop"); + jerry_value_t property_value = jerry_create_string ((const jerry_char_t *) "prop"); + jerry_value_t result_value = jerry_set_property (meta_object, property_name_value, property_value); + jerry_release_value (result_value); + jerry_release_value (property_value); + jerry_release_value (property_name_value); +} /* module_import_meta_callback */ + +int +main (void) +{ + jerry_init (JERRY_INIT_EMPTY); + + const jerry_char_t script[] = "import.meta"; + + jerry_module_set_import_meta_callback (module_import_meta_callback, NULL); + + jerry_parse_options_t parse_options; + parse_options.options = JERRY_PARSE_MODULE; + + jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); + + jerry_release_value (jerry_module_link (module_value, NULL, NULL)); + jerry_release_value (jerry_module_evaluate (module_value)); + + jerry_release_value (module_value); + + jerry_cleanup (); + return 0; +} +``` + +**See also** + +- [jerry_module_import_meta_callback_t](#jerry_module_import_meta_callback_t) + ## jerry_module_get_number_of_requests **Summary** diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 8937abafd..e3ae1ebf3 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -740,6 +740,24 @@ jerry_module_set_state_changed_callback (jerry_module_state_changed_callback_t c #endif /* JERRY_MODULE_SYSTEM */ } /* jerry_module_set_state_changed_callback */ +/** + * Sets a callback which is called when an import.meta expression of a module is evaluated the first time. + */ +void +jerry_module_set_import_meta_callback (jerry_module_import_meta_callback_t callback, /**< callback */ + void *user_p) /**< pointer passed to the callback */ +{ + jerry_assert_api_available (); + +#if JERRY_MODULE_SYSTEM + JERRY_CONTEXT (module_import_meta_callback_p) = callback; + JERRY_CONTEXT (module_import_meta_callback_user_p) = user_p; +#else /* !JERRY_MODULE_SYSTEM */ + JERRY_UNUSED (callback); + JERRY_UNUSED (user_p); +#endif /* JERRY_MODULE_SYSTEM */ +} /* jerry_module_set_import_meta_callback */ + /** * Returns the number of import/export requests of a module * @@ -5489,7 +5507,7 @@ jerry_get_user_value (const jerry_value_t value) /**< jerry api value */ ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_p)->script_value; 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) + if (!(script_p->refs_and_type & CBC_SCRIPT_HAS_USER_VALUE)) { return ECMA_VALUE_UNDEFINED; } diff --git a/jerry-core/ecma/base/ecma-gc.c b/jerry-core/ecma/base/ecma-gc.c index e6745b4d4..f16a0ce9c 100644 --- a/jerry-core/ecma/base/ecma-gc.c +++ b/jerry-core/ecma/base/ecma-gc.c @@ -428,7 +428,7 @@ ecma_gc_mark_compiled_code (const ecma_compiled_code_t *compiled_code_p) /**< co ecma_value_t script_value = ((cbc_uint8_arguments_t *) compiled_code_p)->script_value; cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value); - if (CBC_SCRIPT_GET_TYPE (script_p) == CBC_SCRIPT_USER_OBJECT) + if (script_p->refs_and_type & CBC_SCRIPT_USER_VALUE_IS_OBJECT) { ecma_value_t user_value = CBC_SCRIPT_GET_USER_VALUE (script_p); @@ -436,6 +436,16 @@ ecma_gc_mark_compiled_code (const ecma_compiled_code_t *compiled_code_p) /**< co ecma_gc_set_object_visited (ecma_get_object_from_value (user_value)); } +#if JERRY_MODULE_SYSTEM + if (script_p->refs_and_type & CBC_SCRIPT_HAS_IMPORT_META) + { + ecma_value_t import_meta = CBC_SCRIPT_GET_IMPORT_META (script_p, script_p->refs_and_type); + + JERRY_ASSERT (ecma_is_value_object (import_meta)); + ecma_gc_set_object_visited (ecma_get_object_from_value (import_meta)); + } +#endif /* JERRY_MODULE_SYSTEM */ + #if JERRY_BUILTIN_REALMS ecma_gc_set_object_visited (script_p->realm_p); #endif /* JERRY_BUILTIN_REALMS */ diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h index aea5422ab..0a7b6187c 100644 --- a/jerry-core/ecma/base/ecma-globals.h +++ b/jerry-core/ecma/base/ecma-globals.h @@ -130,8 +130,11 @@ typedef enum #if JERRY_ESNEXT ECMA_PARSE_INTERNAL_PRE_SCANNING = (1u << 16), /**< the parser is in pre-scanning mode */ #endif /* JERRY_ESNEXT */ +#if JERRY_MODULE_SYSTEM + ECMA_PARSE_INTERNAL_HAS_IMPORT_META = (1u << 17), /**< module has import.meta expression */ +#endif /* JERRY_MODULE_SYSTEM */ #if JERRY_FUNCTION_TO_STRING - ECMA_PARSE_INTERNAL_HAS_4_BYTE_MARKER = (1u << 17), /**< source has 4 byte marker */ + ECMA_PARSE_INTERNAL_HAS_4_BYTE_MARKER = (1u << 18), /**< source has 4 byte marker */ #endif /* JERRY_FUNCTION_TO_STRING */ #ifndef JERRY_NDEBUG /** diff --git a/jerry-core/ecma/base/ecma-helpers.c b/jerry-core/ecma/base/ecma-helpers.c index e6469d20b..4c5dda7d5 100644 --- a/jerry-core/ecma/base/ecma-helpers.c +++ b/jerry-core/ecma/base/ecma-helpers.c @@ -1485,14 +1485,13 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */ if (script_p->refs_and_type < CBC_SCRIPT_REF_ONE) { size_t script_size = sizeof (cbc_script_t); + uint32_t type = script_p->refs_and_type; - uint32_t type = CBC_SCRIPT_GET_TYPE (script_p); - - if (type != CBC_SCRIPT_GENERIC) + if (type & CBC_SCRIPT_HAS_USER_VALUE) { script_size += sizeof (ecma_value_t); - if (type == CBC_SCRIPT_USER_VALUE) + if (!(type & CBC_SCRIPT_USER_VALUE_IS_OBJECT)) { ecma_value_t user_value = CBC_SCRIPT_GET_USER_VALUE (script_p); @@ -1505,10 +1504,19 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */ ecma_deref_ecma_string (ecma_get_string_from_value (script_p->resource_name)); #endif /* JERRY_RESOURCE_NAME */ +#if JERRY_MODULE_SYSTEM + if (type & CBC_SCRIPT_HAS_IMPORT_META) + { + JERRY_ASSERT (ecma_is_value_object (CBC_SCRIPT_GET_IMPORT_META (script_p, type))); + + script_size += sizeof (ecma_value_t); + } +#endif /* JERRY_MODULE_SYSTEM */ + #if JERRY_FUNCTION_TO_STRING ecma_deref_ecma_string (ecma_get_string_from_value (script_p->source_code)); - if (script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS) + if (type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS) { ecma_deref_ecma_string (ecma_get_string_from_value (CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, type))); script_size += sizeof (ecma_value_t); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c index c3ddd8cf4..d1d71ea25 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c @@ -133,7 +133,7 @@ ecma_builtin_function_prototype_object_to_string (ecma_object_t *func_obj_p) /** JERRY_ASSERT (script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS); #endif /* JERRY_SNAPSHOT_EXEC */ - source_code = CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, CBC_SCRIPT_GET_TYPE (script_p)); + source_code = CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, script_p->refs_and_type); } ecma_string_t *result_string_p; @@ -179,7 +179,7 @@ ecma_builtin_function_prototype_object_to_string (ecma_object_t *func_obj_p) /** #endif /* JERRY_ESNEXT */ ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (ecma_get_magic_string (header_id)); - ecma_value_t function_arguments = CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, CBC_SCRIPT_GET_TYPE (script_p)); + ecma_value_t function_arguments = CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, script_p->refs_and_type); ecma_stringbuilder_append (&builder, ecma_get_string_from_value (function_arguments)); ecma_stringbuilder_append_raw (&builder, (const lit_utf8_byte_t *) "\n) {\n", 5); diff --git a/jerry-core/include/jerryscript-core.h b/jerry-core/include/jerryscript-core.h index f45c76a30..39572343a 100644 --- a/jerry-core/include/jerryscript-core.h +++ b/jerry-core/include/jerryscript-core.h @@ -274,6 +274,7 @@ jerry_value_t jerry_module_link (const jerry_value_t module_val, jerry_value_t jerry_module_evaluate (const jerry_value_t module_val); jerry_module_state_t jerry_module_get_state (const jerry_value_t module_val); void jerry_module_set_state_changed_callback (jerry_module_state_changed_callback_t callback, void *user_p); +void jerry_module_set_import_meta_callback (jerry_module_import_meta_callback_t callback, void *user_p); 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); diff --git a/jerry-core/include/jerryscript-snapshot.h b/jerry-core/include/jerryscript-snapshot.h index 0ac226d6d..9ba60c46a 100644 --- a/jerry-core/include/jerryscript-snapshot.h +++ b/jerry-core/include/jerryscript-snapshot.h @@ -30,7 +30,7 @@ extern "C" /** * Jerry snapshot format version. */ -#define JERRY_SNAPSHOT_VERSION (68u) +#define JERRY_SNAPSHOT_VERSION (69u) /** * Flags for jerry_generate_snapshot and jerry_generate_function_snapshot. diff --git a/jerry-core/include/jerryscript-types.h b/jerry-core/include/jerryscript-types.h index 50966d13c..cbdef6dca 100644 --- a/jerry-core/include/jerryscript-types.h +++ b/jerry-core/include/jerryscript-types.h @@ -588,6 +588,12 @@ typedef jerry_value_t (*jerry_module_import_callback_t) (const jerry_value_t spe typedef void (*jerry_module_state_changed_callback_t) (jerry_module_state_t new_state, const jerry_value_t module, const jerry_value_t value, void *user_p); +/** + * Callback which is called when an import.meta expression of a module is evaluated the first time. + */ +typedef void (*jerry_module_import_meta_callback_t) (const jerry_value_t module, + const jerry_value_t meta_object, void *user_p); + /** * Callback which is called by jerry_module_evaluate to evaluate the native module. */ diff --git a/jerry-core/jcontext/jcontext.h b/jerry-core/jcontext/jcontext.h index e2a45ae39..87084e7c6 100644 --- a/jerry-core/jcontext/jcontext.h +++ b/jerry-core/jcontext/jcontext.h @@ -152,6 +152,10 @@ 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_meta_callback_t module_import_meta_callback_p; /**< callback which is called when an + * import.meta expression of a module + * is evaluated the first time */ + void *module_import_meta_callback_user_p; /**< user pointer for module_import_meta_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 */ diff --git a/jerry-core/parser/js/byte-code.c b/jerry-core/parser/js/byte-code.c index ecd943665..682d950ce 100644 --- a/jerry-core/parser/js/byte-code.c +++ b/jerry-core/parser/js/byte-code.c @@ -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 == 147, +JERRY_STATIC_ASSERT (CBC_EXT_END == 148, number_of_cbc_ext_opcodes_changed); #if JERRY_PARSER || JERRY_PARSER_DUMP_BYTE_CODE diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index 1f9f0321a..062e27acc 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -600,6 +600,8 @@ VM_OC_PUSH_REST_OBJECT) \ CBC_OPCODE (CBC_EXT_MODULE_IMPORT, CBC_NO_FLAG, 0, \ VM_OC_MODULE_IMPORT) \ + CBC_OPCODE (CBC_EXT_MODULE_IMPORT_META, CBC_NO_FLAG, 1, \ + VM_OC_MODULE_IMPORT_META) \ 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, \ @@ -998,42 +1000,36 @@ typedef enum */ typedef enum { - CBC_SCRIPT_GENERIC, /**< script without user specific data */ - CBC_SCRIPT_USER_OBJECT, /**< script with a user object */ - CBC_SCRIPT_USER_VALUE, /**< script with a non-object user value */ + CBC_SCRIPT_HAS_USER_VALUE = (1 << 0), /**< script has user value */ + CBC_SCRIPT_USER_VALUE_IS_OBJECT = (1 << 1), /**< user value is object */ + CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS = (1 << 2), /**< script is a function with arguments source code */ + CBC_SCRIPT_HAS_IMPORT_META = (1 << 3), /**< script is a module with import.meta object */ } cbc_script_type; -/** - * Script is a function with arguments source code. - */ -#define CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS 0x4 - /** * Value for increasing or decreasing the script reference counter. */ -#define CBC_SCRIPT_REF_ONE 0x8 +#define CBC_SCRIPT_REF_ONE 0x10 /** * Maximum value of script reference counter. */ #define CBC_SCRIPT_REF_MAX (UINT32_MAX - CBC_SCRIPT_REF_ONE + 1) -/** - * Get the type of a script. - */ -#define CBC_SCRIPT_GET_TYPE(script_p) ((script_p)->refs_and_type & 0x3) - /** * Sets the type of a script using the user_value. */ #define CBC_SCRIPT_SET_TYPE(script_p, user_value, ref_count) \ do \ { \ - (script_p)->refs_and_type = ((ref_count) | CBC_SCRIPT_GENERIC); \ + (script_p)->refs_and_type = (ref_count); \ if ((user_value) != ECMA_VALUE_EMPTY) \ { \ - (script_p)->refs_and_type = (ecma_is_value_object (user_value) ? ((ref_count) | CBC_SCRIPT_USER_OBJECT) \ - : ((ref_count) | CBC_SCRIPT_USER_VALUE)); \ + (script_p)->refs_and_type |= CBC_SCRIPT_HAS_USER_VALUE; \ + if (ecma_is_value_object (user_value)) \ + { \ + (script_p)->refs_and_type |= CBC_SCRIPT_USER_VALUE_IS_OBJECT; \ + } \ } \ } \ while (false) @@ -1073,7 +1069,13 @@ typedef struct * Get function arguments. */ #define CBC_SCRIPT_GET_FUNCTION_ARGUMENTS(script_p, type) \ - (CBC_SCRIPT_GET_OPTIONAL_VALUES (script_p)[(type) != CBC_SCRIPT_GENERIC ? 1 : 0]) + (CBC_SCRIPT_GET_OPTIONAL_VALUES (script_p)[((type) & CBC_SCRIPT_HAS_USER_VALUE) ? 1 : 0]) + +/** + * Get import.meta object. + */ +#define CBC_SCRIPT_GET_IMPORT_META(script_p, type) \ + (CBC_SCRIPT_GET_OPTIONAL_VALUES (script_p)[((type) & CBC_SCRIPT_HAS_USER_VALUE) ? 1 : 0]) #define CBC_OPCODE(arg1, arg2, arg3, arg4) arg1, diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index 98dec512e..c76de868e 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -491,6 +491,9 @@ static const keyword_string_t keywords_with_length_4[] = LEXER_KEYWORD ("else", LEXER_KEYW_ELSE), LEXER_KEYWORD ("enum", LEXER_KEYW_ENUM), LEXER_KEYWORD ("eval", LEXER_KEYW_EVAL), +#if JERRY_MODULE_SYSTEM + LEXER_KEYWORD ("meta", LEXER_KEYW_META), +#endif /* JERRY_MODULE_SYSTEM */ LEXER_KEYWORD ("null", LEXER_LIT_NULL), LEXER_KEYWORD ("this", LEXER_KEYW_THIS), LEXER_KEYWORD ("true", LEXER_LIT_TRUE), diff --git a/jerry-core/parser/js/js-lexer.h b/jerry-core/parser/js/js-lexer.h index 71b7c0394..8f2a5e84b 100644 --- a/jerry-core/parser/js/js-lexer.h +++ b/jerry-core/parser/js/js-lexer.h @@ -192,6 +192,8 @@ typedef enum LEXER_KEYW_IMPORT, /**< import */ LEXER_KEYW_ENUM, /**< enum */ +#define LEXER_FIRST_NON_RESERVED_KEYWORD LEXER_EXPRESSION_START + /* These are virtual tokens. */ LEXER_EXPRESSION_START, /**< expression start */ LEXER_PROPERTY_GETTER, /**< property getter function */ @@ -203,14 +205,13 @@ typedef enum LEXER_INVALID_PATTERN, /**< special value for invalid destructuring pattern */ #endif /* JERRY_ESNEXT */ + /* Keywords which are not keyword tokens. */ #if JERRY_ESNEXT - /* Keywords which are not keyword tokens. */ -#define LEXER_FIRST_NON_RESERVED_KEYWORD LEXER_KEYW_ASYNC LEXER_KEYW_ASYNC, /**< async */ -#else /* !JERRY_ESNEXT */ - /* Keywords which are not keyword tokens. */ -#define LEXER_FIRST_NON_RESERVED_KEYWORD LEXER_KEYW_EVAL #endif /* JERRY_ESNEXT */ +#if JERRY_MODULE_SYSTEM + LEXER_KEYW_META, /**< meta */ +#endif /* JERRY_MODULE_SYSTEM */ /* Keywords which cannot be assigned in strict mode. */ #define LEXER_FIRST_NON_STRICT_ARGUMENTS LEXER_KEYW_EVAL diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index 6e347a8e8..dcb84f92d 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -1930,16 +1930,6 @@ 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 @@ -2330,11 +2320,39 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */ { lexer_next_token (context_p); + if (context_p->token.type == LEXER_DOT) + { + lexer_next_token (context_p); + + if (context_p->token.type != LEXER_LITERAL + || context_p->token.lit_location.type != LEXER_IDENT_LITERAL + || context_p->token.keyword_type != LEXER_KEYW_META + || (context_p->token.lit_location.status_flags & LEXER_LIT_LOCATION_HAS_ESCAPE)) + { + parser_raise_error (context_p, PARSER_ERR_META_EXPECTED); + } + + if (!(context_p->global_status_flags & ECMA_PARSE_MODULE)) + { + parser_raise_error (context_p, PARSER_ERR_IMPORT_META_REQUIRE_MODULE); + } + + JERRY_ASSERT (context_p->global_status_flags & ECMA_PARSE_INTERNAL_HAS_IMPORT_META); + + parser_emit_cbc_ext (context_p, CBC_EXT_MODULE_IMPORT_META); + break; + } + if (context_p->token.type != LEXER_LEFT_PAREN) { parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED); } + if (new_was_seen) + { + parser_raise_error (context_p, PARSER_ERR_IMPORT_AFTER_NEW); + } + lexer_next_token (context_p); parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA); diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index 6ce13c522..4c00fdc3b 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -2427,7 +2427,7 @@ 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 (lexer_check_next_characters (context_p, LIT_CHAR_LEFT_PAREN, LIT_CHAR_DOT)) { if (context_p->status_flags & PARSER_IS_FUNCTION) { diff --git a/jerry-core/parser/js/js-parser-util.c b/jerry-core/parser/js/js-parser-util.c index 3b590f0e9..ff0773313 100644 --- a/jerry-core/parser/js/js-parser-util.c +++ b/jerry-core/parser/js/js-parser-util.c @@ -1401,6 +1401,14 @@ parser_error_to_string (parser_error_t error) /**< error code */ { return "Module import call is not allowed after new"; } + case PARSER_ERR_META_EXPECTED: + { + return "Expected 'meta' keyword"; + } + case PARSER_ERR_IMPORT_META_REQUIRE_MODULE: + { + return "Cannot use 'import.meta' outside a module"; + } #endif /* JERRY_MODULE_SYSTEM */ default: { diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index 25f43e7b2..10eb517fb 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -1849,6 +1849,7 @@ parser_parse_source (void *source_p, /**< source code */ context.stack_depth = 0; context.stack_limit = 0; context.options_p = options_p; + context.script_p = NULL; context.arguments_start_p = NULL; context.arguments_size = 0; #if JERRY_MODULE_SYSTEM @@ -1946,7 +1947,7 @@ parser_parse_source (void *source_p, /**< source code */ ecma_value_t parent_script_value = ((cbc_uint8_arguments_t *) bytecode_header_p)->script_value;; cbc_script_t *parent_script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, parent_script_value); - if (CBC_SCRIPT_GET_TYPE (parent_script_p) != CBC_SCRIPT_GENERIC) + if (parent_script_p->refs_and_type & CBC_SCRIPT_HAS_USER_VALUE) { context.user_value = CBC_SCRIPT_GET_USER_VALUE (parent_script_p); } @@ -1960,36 +1961,6 @@ parser_parse_source (void *source_p, /**< source code */ context.user_value = context.options_p->user_value; } - size_t script_size = sizeof (cbc_script_t); - - if (context.user_value != ECMA_VALUE_EMPTY) - { - script_size += sizeof (ecma_value_t); - } - -#if JERRY_FUNCTION_TO_STRING - if (context.argument_list != ECMA_VALUE_EMPTY) - { - script_size += sizeof (ecma_value_t); - } -#endif /* JERRY_FUNCTION_TO_STRING */ - - context.script_p = jmem_heap_alloc_block_null_on_error (script_size); - - if (JERRY_UNLIKELY (context.script_p == NULL)) - { - /* It is unlikely that memory can be allocated in an out-of-memory - * situation. However, a simple value can still be thrown. */ - jcontext_raise_exception (ECMA_VALUE_NULL); - return NULL; - } - - CBC_SCRIPT_SET_TYPE (context.script_p, context.user_value, CBC_SCRIPT_REF_ONE); - -#if JERRY_BUILTIN_REALMS - context.script_p->realm_p = (ecma_object_t *) JERRY_CONTEXT (global_object_p); -#endif /* JERRY_BUILTIN_REALMS */ - #if JERRY_RESOURCE_NAME ecma_value_t resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); @@ -2005,12 +1976,8 @@ parser_parse_source (void *source_p, /**< source code */ { resource_name = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL); } - - context.script_p->resource_name = resource_name; #endif /* JERRY_RESOURCE_NAME */ - 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; @@ -2085,6 +2052,27 @@ parser_parse_source (void *source_p, /**< source code */ return NULL; } + size_t script_size = sizeof (cbc_script_t); + + if (context.user_value != ECMA_VALUE_EMPTY) + { + script_size += sizeof (ecma_value_t); + } + +#if JERRY_FUNCTION_TO_STRING + if (context.argument_list != ECMA_VALUE_EMPTY) + { + script_size += sizeof (ecma_value_t); + } +#endif /* JERRY_FUNCTION_TO_STRING */ + +#if JERRY_MODULE_SYSTEM + if (context.global_status_flags & ECMA_PARSE_INTERNAL_HAS_IMPORT_META) + { + script_size += sizeof (ecma_value_t); + } +#endif /* JERRY_MODULE_SYSTEM */ + if (context.arguments_start_p == NULL) { context.source_p = context.source_start_p; @@ -2116,6 +2104,20 @@ parser_parse_source (void *source_p, /**< source code */ PARSER_TRY (context.try_buffer) { + context.script_p = parser_malloc (&context, script_size); + + CBC_SCRIPT_SET_TYPE (context.script_p, context.user_value, CBC_SCRIPT_REF_ONE); + +#if JERRY_BUILTIN_REALMS + context.script_p->realm_p = (ecma_object_t *) JERRY_CONTEXT (global_object_p); +#endif /* JERRY_BUILTIN_REALMS */ + +#if JERRY_RESOURCE_NAME + context.script_p->resource_name = resource_name; +#endif /* JERRY_RESOURCE_NAME */ + + ECMA_SET_INTERNAL_VALUE_POINTER (context.script_value, context.script_p); + /* Pushing a dummy value ensures the stack is never empty. * This simplifies the stack management routines. */ parser_stack_push_uint8 (&context, CBC_MAXIMUM_BYTE_VALUE); @@ -2203,6 +2205,17 @@ parser_parse_source (void *source_p, /**< source code */ CBC_SCRIPT_GET_USER_VALUE (context.script_p) = ecma_copy_value_if_not_object (context.user_value); } +#if JERRY_MODULE_SYSTEM + if (context.global_status_flags & ECMA_PARSE_INTERNAL_HAS_IMPORT_META) + { + int idx = (context.user_value != ECMA_VALUE_EMPTY) ? 1 : 0; + ecma_value_t module = ecma_make_object_value ((ecma_object_t *) JERRY_CONTEXT (module_current_p)); + + CBC_SCRIPT_GET_OPTIONAL_VALUES (context.script_p)[idx] = module; + context.script_p->refs_and_type |= CBC_SCRIPT_HAS_IMPORT_META; + } +#endif /* JERRY_MODULE_SYSTEM */ + #if JERRY_FUNCTION_TO_STRING if (!(context.global_status_flags & ECMA_PARSE_HAS_SOURCE_VALUE)) { @@ -2274,8 +2287,11 @@ parser_parse_source (void *source_p, /**< source code */ ecma_deref_ecma_string (ecma_get_string_from_value (context.script_p->resource_name)); #endif /* JERRY_RESOURCE_NAME */ - JERRY_ASSERT (context.script_p->refs_and_type >= CBC_SCRIPT_REF_ONE); - jmem_heap_free_block (context.script_p, script_size); + if (context.script_p != NULL) + { + JERRY_ASSERT (context.script_p->refs_and_type >= CBC_SCRIPT_REF_ONE); + jmem_heap_free_block (context.script_p, script_size); + } } PARSER_TRY_END diff --git a/jerry-core/parser/js/js-parser.h b/jerry-core/parser/js/js-parser.h index 0496b5670..b85da7107 100644 --- a/jerry-core/parser/js/js-parser.h +++ b/jerry-core/parser/js/js-parser.h @@ -182,6 +182,8 @@ typedef enum 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 */ + PARSER_ERR_META_EXPECTED, /**< meta keyword expected */ + PARSER_ERR_IMPORT_META_REQUIRE_MODULE, /**< cannot use 'import.meta' outside a module */ #endif /* JERRY_MODULE_SYSTEM */ PARSER_ERR_NON_STRICT_ARG_DEFINITION /**< non-strict argument definition */ diff --git a/jerry-core/parser/js/js-scanner-internal.h b/jerry-core/parser/js/js-scanner-internal.h index e4cecf811..ba488857d 100644 --- a/jerry-core/parser/js/js-scanner-internal.h +++ b/jerry-core/parser/js/js-scanner-internal.h @@ -437,6 +437,9 @@ void scanner_check_arrow_arg (parser_context_t *context_p, scanner_context_t *sc bool scanner_check_async_function (parser_context_t *context_p, scanner_context_t *scanner_context_p); void scanner_check_function_after_if (parser_context_t *context_p, scanner_context_t *scanner_context_p); #endif /* JERRY_ESNEXT */ +#if JERRY_MODULE_SYSTEM +void scanner_check_import_meta (parser_context_t *context_p); +#endif /* JERRY_MODULE_SYSTEM */ void scanner_scan_bracket (parser_context_t *context_p, scanner_context_t *scanner_context_p); void scanner_check_directives (parser_context_t *context_p, scanner_context_t *scanner_context_p); diff --git a/jerry-core/parser/js/js-scanner-ops.c b/jerry-core/parser/js/js-scanner-ops.c index f1b1450b1..2996e5bd2 100644 --- a/jerry-core/parser/js/js-scanner-ops.c +++ b/jerry-core/parser/js/js-scanner-ops.c @@ -357,6 +357,35 @@ scanner_check_function_after_if (parser_context_t *context_p, /**< context */ } } /* scanner_check_function_after_if */ +#endif /* JERRY_ESNEXT */ + +#if JERRY_MODULE_SYSTEM + +/** + * Check whether the next token is meta. + */ +void +scanner_check_import_meta (parser_context_t *context_p) /**< context */ +{ + lexer_next_token (context_p); + + if (context_p->token.type != LEXER_LITERAL + || context_p->token.lit_location.type != LEXER_IDENT_LITERAL + || context_p->token.keyword_type != LEXER_KEYW_META + || (context_p->token.lit_location.status_flags & LEXER_LIT_LOCATION_HAS_ESCAPE)) + { + scanner_raise_error (context_p); + } + + lexer_next_token (context_p); + + context_p->global_status_flags |= ECMA_PARSE_INTERNAL_HAS_IMPORT_META; +} /* scanner_check_import_meta */ + +#endif /* JERRY_MODULE_SYSTEM */ + +#if JERRY_ESNEXT + /** * Arrow types for scanner_scan_bracket() function. */ diff --git a/jerry-core/parser/js/js-scanner.c b/jerry-core/parser/js/js-scanner.c index c04761f2d..0a552432d 100644 --- a/jerry-core/parser/js/js-scanner.c +++ b/jerry-core/parser/js/js-scanner.c @@ -316,7 +316,11 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */ { lexer_next_token (context_p); - if (context_p->token.type != LEXER_LEFT_PAREN) + if (context_p->token.type == LEXER_DOT) + { + scanner_check_import_meta (context_p); + } + else if (context_p->token.type != LEXER_LEFT_PAREN) { scanner_raise_error (context_p); } @@ -1697,6 +1701,13 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ { lexer_next_token (context_p); + if (context_p->token.type == LEXER_DOT) + { + scanner_check_import_meta (context_p); + scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION; + return SCAN_KEEP_TOKEN; + } + if (context_p->token.type == LEXER_LEFT_PAREN) { scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION; diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index e428f60dc..dfd1a98ef 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -4571,7 +4571,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ #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) + if (script_p->refs_and_type & CBC_SCRIPT_HAS_USER_VALUE) { user_value = CBC_SCRIPT_GET_USER_VALUE (script_p); } @@ -4590,6 +4590,40 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ *stack_top_p++ = result; continue; } + case VM_OC_MODULE_IMPORT_META: + { + ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_header_p)->script_value; + cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value); + + JERRY_ASSERT (script_p->refs_and_type & CBC_SCRIPT_HAS_IMPORT_META); + + ecma_value_t import_meta = CBC_SCRIPT_GET_IMPORT_META (script_p, script_p->refs_and_type); + ecma_object_t *import_meta_object_p = ecma_get_object_from_value (import_meta); + + if (ecma_get_object_type (import_meta_object_p) != ECMA_OBJECT_TYPE_GENERAL) + { + JERRY_ASSERT (ecma_object_class_is (import_meta_object_p, ECMA_OBJECT_CLASS_MODULE)); + + ecma_value_t module = import_meta; + import_meta_object_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL); + import_meta = ecma_make_object_value (import_meta_object_p); + + if (JERRY_CONTEXT (module_import_meta_callback_p) != NULL) + { + void *user_p = JERRY_CONTEXT (module_import_meta_callback_user_p); + JERRY_CONTEXT (module_import_meta_callback_p) (module, import_meta, user_p); + } + + CBC_SCRIPT_GET_IMPORT_META (script_p, script_p->refs_and_type) = import_meta; + } + else + { + ecma_ref_object (import_meta_object_p); + } + + *stack_top_p++ = import_meta; + continue; + } #endif /* JERRY_MODULE_SYSTEM */ #if JERRY_DEBUGGER case VM_OC_BREAKPOINT_ENABLED: diff --git a/jerry-core/vm/vm.h b/jerry-core/vm/vm.h index cc239ead9..978f1b453 100644 --- a/jerry-core/vm/vm.h +++ b/jerry-core/vm/vm.h @@ -302,6 +302,7 @@ typedef enum #endif /* JERRY_ESNEXT */ #if JERRY_MODULE_SYSTEM VM_OC_MODULE_IMPORT, /**< module dynamic import */ + VM_OC_MODULE_IMPORT_META, /**< module import.meta */ #endif /* JERRY_MODULE_SYSTEM */ VM_OC_NONE, /**< a special opcode for unsupported byte codes */ @@ -390,6 +391,7 @@ typedef enum #endif /* !JERRY_ESNEXT */ #if !JERRY_MODULE_SYSTEM VM_OC_MODULE_IMPORT = VM_OC_NONE, /**< module dynamic import */ + VM_OC_MODULE_IMPORT_META = VM_OC_NONE, /**< module import.meta */ #endif /* JERRY_MODULE_SYSTEM */ VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */ diff --git a/tests/test262-esnext-excludelist.xml b/tests/test262-esnext-excludelist.xml index a9bd0e592..6829dc443 100644 --- a/tests/test262-esnext-excludelist.xml +++ b/tests/test262-esnext-excludelist.xml @@ -127,10 +127,6 @@ - - - - diff --git a/tests/unit-core/CMakeLists.txt b/tests/unit-core/CMakeLists.txt index 6ec5a1c15..e782e8653 100644 --- a/tests/unit-core/CMakeLists.txt +++ b/tests/unit-core/CMakeLists.txt @@ -59,6 +59,7 @@ set(SOURCE_UNIT_TEST_MAIN_MODULES test-literal-storage.c test-mem-stats.c test-module-dynamic.c + test-module-import-meta.c test-module.c test-native-callback-nested.c test-native-instanceof.c diff --git a/tests/unit-core/test-module-import-meta.c b/tests/unit-core/test-module-import-meta.c new file mode 100644 index 000000000..6208ddd07 --- /dev/null +++ b/tests/unit-core/test-module-import-meta.c @@ -0,0 +1,159 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "jerryscript.h" +#include "jerryscript-port.h" +#include "test-common.h" + +static int counter = 0; +static jerry_value_t global_module_value; + +static jerry_value_t +global_assert (const jerry_call_info_t *call_info_p, /**< call information */ + const jerry_value_t args_p[], /**< arguments list */ + const jerry_length_t args_cnt) /**< arguments length */ +{ + JERRY_UNUSED (call_info_p); + + TEST_ASSERT (args_cnt == 1 && jerry_value_is_true (args_p[0])); + return jerry_create_boolean (true); +} /* global_assert */ + +static void +register_assert (void) +{ + jerry_value_t global_object_value = jerry_get_global_object (); + + jerry_value_t function_value = jerry_create_external_function (global_assert); + jerry_value_t function_name_value = jerry_create_string ((const jerry_char_t *) "assert"); + jerry_value_t result_value = jerry_set_property (global_object_value, function_name_value, function_value); + + jerry_release_value (function_name_value); + jerry_release_value (function_value); + jerry_release_value (global_object_value); + + TEST_ASSERT (jerry_value_is_true (result_value)); + jerry_release_value (result_value); +} /* register_assert */ + +static void +module_import_meta_callback (const jerry_value_t module, /**< module */ + const jerry_value_t meta_object, /**< import.meta object */ + void *user_p) /**< user pointer */ +{ + TEST_ASSERT (user_p == (void *) &counter); + TEST_ASSERT (module == global_module_value); + + jerry_value_t property_name_value = jerry_create_string ((const jerry_char_t *) "prop"); + jerry_value_t result_value = jerry_set_property (meta_object, property_name_value, property_name_value); + jerry_release_value (result_value); + jerry_release_value (property_name_value); + + counter++; +} /* module_import_meta_callback */ + +static void +test_syntax_error (const char *source_p, /**< source code */ + const jerry_parse_options_t *options_p) /**< parse options */ +{ + jerry_value_t result_value = jerry_parse ((const jerry_char_t *) source_p, strlen (source_p), options_p); + TEST_ASSERT (jerry_value_is_error (result_value) + && jerry_get_error_type (result_value) == JERRY_ERROR_SYNTAX); + jerry_release_value (result_value); +} /* test_syntax_error */ + +static void +run_module (const char *source_p, /* source code */ + jerry_parse_options_t *parse_options_p) /* parse options */ +{ + global_module_value = jerry_parse ((const jerry_char_t *) source_p, strlen (source_p), parse_options_p); + TEST_ASSERT (!jerry_value_is_error (global_module_value)); + + jerry_value_t result_value = jerry_module_link (global_module_value, NULL, NULL); + TEST_ASSERT (!jerry_value_is_error (result_value)); + jerry_release_value (result_value); + + result_value = jerry_module_evaluate (global_module_value); + + jerry_release_value (global_module_value); + + TEST_ASSERT (!jerry_value_is_error (result_value)); + jerry_release_value (result_value); +} /* run_module */ + +int +main (void) +{ + jerry_init (JERRY_INIT_EMPTY); + + if (!jerry_is_feature_enabled (JERRY_FEATURE_MODULE)) + { + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Module is disabled!\n"); + jerry_cleanup (); + return 0; + } + + register_assert (); + jerry_module_set_import_meta_callback (module_import_meta_callback, (void *) &counter); + + /* Syntax errors. */ + test_syntax_error ("import.meta", NULL); + test_syntax_error ("var a = import.meta", NULL); + + jerry_parse_options_t parse_options; + parse_options.options = JERRY_PARSE_MODULE; + + test_syntax_error ("import.m\\u0065ta", &parse_options); + test_syntax_error ("import.invalid", &parse_options); + + counter = 0; + + run_module (TEST_STRING_LITERAL ("assert(typeof import.meta === 'object')\n"), + &parse_options); + + run_module (TEST_STRING_LITERAL ("assert(Object.getPrototypeOf(import.meta) === null)\n"), + &parse_options); + + run_module (TEST_STRING_LITERAL ("var meta = import.meta\n" + "assert(import.meta === meta)\n" + "assert(import.meta === meta)\n" + "function f() {\n" + " assert(import.meta === meta)\n" + "}\n" + "f()\n"), + &parse_options); + + run_module (TEST_STRING_LITERAL ("import.meta.x = 5.5\n" + "assert(import.meta.x === 5.5)\n"), + &parse_options); + + run_module (TEST_STRING_LITERAL ("assert(import.meta.prop === 'prop')\n" + "function f() {\n" + " import.meta.prop = 6.25\n" + " import.meta.prop2 = 's'\n" + "\n" + " return function() {\n" + " assert(import.meta.prop === 6.25)\n" + " assert(import.meta.prop2 === 's')\n" + " }\n" + "}\n" + "f()()\n"), + &parse_options); + + TEST_ASSERT (counter == 5); + + jerry_cleanup (); + return 0; +} /* main */