Implement import.meta object (#4766)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-09-15 17:44:16 +02:00
committed by GitHub
parent 386ec44d4d
commit 77c2602205
27 changed files with 528 additions and 93 deletions
+1 -1
View File
@@ -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
+20 -18
View File
@@ -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,
+3
View File
@@ -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),
+6 -5
View File
@@ -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
+28 -10
View File
@@ -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);
+1 -1
View File
@@ -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)
{
+8
View File
@@ -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:
{
+53 -37
View File
@@ -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
+2
View File
@@ -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 */
@@ -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);
+29
View File
@@ -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.
*/
+12 -1
View File
@@ -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;