diff --git a/jerry-core/ecma/operations/ecma-get-put-value.c b/jerry-core/ecma/operations/ecma-get-put-value.c index 6affcc33b..08a51c733 100644 --- a/jerry-core/ecma/operations/ecma-get-put-value.c +++ b/jerry-core/ecma/operations/ecma-get-put-value.c @@ -238,6 +238,12 @@ ecma_op_put_value_lex_env_base (ecma_object_t *lex_env_p, /**< lexical environme #if ENABLED (JERRY_ESNEXT) else if (ecma_is_property_enumerable (*property_p)) { + if (JERRY_UNLIKELY (ECMA_PROPERTY_VALUE_PTR (property_p)->value == ECMA_VALUE_UNINITIALIZED)) + { + return ecma_raise_reference_error (ECMA_ERR_MSG ("Variables declared by let/const must be" + " initialized before writing their value.")); + } + return ecma_raise_type_error (ECMA_ERR_MSG ("Constant bindings cannot be reassigned.")); } #endif /* ENABLED (JERRY_ESNEXT) */ diff --git a/jerry-core/parser/js/byte-code.c b/jerry-core/parser/js/byte-code.c index 8261055b5..21021eaca 100644 --- a/jerry-core/parser/js/byte-code.c +++ b/jerry-core/parser/js/byte-code.c @@ -27,7 +27,7 @@ JERRY_STATIC_ASSERT ((sizeof (cbc_uint16_arguments_t) % sizeof (jmem_cpointer_t) */ JERRY_STATIC_ASSERT (CBC_END == 238, number_of_cbc_opcodes_changed); -JERRY_STATIC_ASSERT (CBC_EXT_END == 145, +JERRY_STATIC_ASSERT (CBC_EXT_END == 144, number_of_cbc_ext_opcodes_changed); #if ENABLED (JERRY_PARSER) diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index 9969ee251..7e6455b16 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -663,8 +663,6 @@ \ /* Class related opcodes. */ \ CBC_OPCODE (CBC_EXT_PUSH_NAMED_CLASS_ENV, CBC_HAS_LITERAL_ARG, 1, \ - VM_OC_PUSH_CLASS_ENVIRONMENT | VM_OC_GET_LITERAL) \ - CBC_OPCODE (CBC_EXT_PUSH_ANONYMOUS_CLASS_ENV, CBC_NO_FLAG, 1, \ VM_OC_PUSH_CLASS_ENVIRONMENT) \ CBC_OPCODE (CBC_EXT_PUSH_IMPLICIT_CONSTRUCTOR, CBC_NO_FLAG, 1, \ VM_OC_PUSH_IMPLICIT_CTOR | VM_OC_PUT_STACK) \ @@ -673,7 +671,7 @@ CBC_OPCODE (CBC_EXT_INIT_CLASS, CBC_NO_FLAG, 0, \ VM_OC_INIT_CLASS | VM_OC_PUT_STACK) \ CBC_OPCODE (CBC_EXT_FINALIZE_NAMED_CLASS, CBC_HAS_LITERAL_ARG, -2, \ - VM_OC_FINALIZE_CLASS | VM_OC_GET_LITERAL) \ + VM_OC_FINALIZE_CLASS) \ CBC_OPCODE (CBC_EXT_FINALIZE_ANONYMOUS_CLASS, CBC_NO_FLAG, -2, \ VM_OC_FINALIZE_CLASS) \ CBC_OPCODE (CBC_EXT_SET_FIELD_INIT, CBC_HAS_LITERAL_ARG, 0, \ diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index 8d9fed3ef..10a6bba1e 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -945,7 +945,8 @@ parser_parse_class (parser_context_t *context_p, /**< context */ } class_ident_index = context_p->lit_object.index; - lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL); + lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_NEW_IDENT_LITERAL); + context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_USED; class_name_index = context_p->lit_object.index; #if ENABLED (JERRY_MODULE_SYSTEM) @@ -962,7 +963,8 @@ parser_parse_class (parser_context_t *context_p, /**< context */ /* Class expression may contain an identifier. */ if (context_p->token.type == LEXER_LITERAL && context_p->token.lit_location.type == LEXER_IDENT_LITERAL) { - lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL); + lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_NEW_IDENT_LITERAL); + context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_USED; class_name_index = context_p->lit_object.index; lexer_next_token (context_p); } @@ -970,11 +972,23 @@ parser_parse_class (parser_context_t *context_p, /**< context */ if (class_name_index != PARSER_INVALID_LITERAL_INDEX) { + if (JERRY_UNLIKELY (context_p->scope_stack_top >= context_p->scope_stack_size)) + { + JERRY_ASSERT (context_p->scope_stack_size == PARSER_MAXIMUM_DEPTH_OF_SCOPE_STACK); + parser_raise_error (context_p, PARSER_ERR_SCOPE_STACK_LIMIT_REACHED); + } + + parser_scope_stack_t *scope_stack_p = context_p->scope_stack_p + context_p->scope_stack_top; + + PARSER_PLUS_EQUAL_U16 (context_p->scope_stack_top, 1); + scope_stack_p->map_from = class_name_index; + scope_stack_p->map_to = 0; + parser_emit_cbc_ext_literal (context_p, CBC_EXT_PUSH_NAMED_CLASS_ENV, class_name_index); } else { - parser_emit_cbc_ext (context_p, CBC_EXT_PUSH_ANONYMOUS_CLASS_ENV); + parser_emit_cbc (context_p, CBC_PUSH_UNDEFINED); } bool is_strict = (context_p->status_flags & PARSER_IS_STRICT) != 0; @@ -1005,7 +1019,8 @@ parser_parse_class (parser_context_t *context_p, /**< context */ if (class_name_index != PARSER_INVALID_LITERAL_INDEX) { parser_emit_cbc_ext_literal (context_p, CBC_EXT_FINALIZE_NAMED_CLASS, class_name_index); - parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_FUNCTION_NAME, class_name_index); + parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_CLASS_NAME, class_name_index); + PARSER_MINUS_EQUAL_U16 (context_p->scope_stack_top, 1); } else { diff --git a/jerry-core/parser/js/js-scanner-internal.h b/jerry-core/parser/js/js-scanner-internal.h index ee7ca7014..4d9d95147 100644 --- a/jerry-core/parser/js/js-scanner-internal.h +++ b/jerry-core/parser/js/js-scanner-internal.h @@ -275,17 +275,17 @@ typedef struct scanner_binding_list_t typedef enum { SCANNER_LITERAL_POOL_FUNCTION = (1 << 0), /**< literal pool represents a function */ - SCANNER_LITERAL_POOL_BLOCK = (1 << 1), /**< literal pool represents a code block */ - SCANNER_LITERAL_POOL_IS_STRICT = (1 << 2), /**< literal pool represents a strict mode code block */ - SCANNER_LITERAL_POOL_CAN_EVAL = (1 << 3), /**< prepare for executing eval in this block */ - SCANNER_LITERAL_POOL_NO_ARGUMENTS = (1 << 4), /**< arguments object must not be constructed */ #if ENABLED (JERRY_ESNEXT) - SCANNER_LITERAL_POOL_HAS_COMPLEX_ARGUMENT = (1 << 5), /**< function has complex (ES2015+) argument definition */ + SCANNER_LITERAL_POOL_CLASS_NAME = (1 << 1), /**< literal pool which contains a class name */ + SCANNER_LITERAL_POOL_CLASS_FIELD = (1 << 2), /**< literal pool is created for a class field initializer */ #endif /* ENABLED (JERRY_ESNEXT) */ - SCANNER_LITERAL_POOL_IN_WITH = (1 << 6), /**< literal pool is in a with statement */ -#if ENABLED (JERRY_MODULE_SYSTEM) - SCANNER_LITERAL_POOL_IN_EXPORT = (1 << 7), /**< the declared variables are exported by the module system */ -#endif /* ENABLED (JERRY_MODULE_SYSTEM) */ + SCANNER_LITERAL_POOL_IS_STRICT = (1 << 3), /**< literal pool represents a strict mode code block */ + SCANNER_LITERAL_POOL_CAN_EVAL = (1 << 4), /**< prepare for executing eval in this block */ + SCANNER_LITERAL_POOL_NO_ARGUMENTS = (1 << 5), /**< arguments object must not be constructed */ +#if ENABLED (JERRY_ESNEXT) + SCANNER_LITERAL_POOL_HAS_COMPLEX_ARGUMENT = (1 << 6), /**< function has complex (ES2015+) argument definition */ +#endif /* ENABLED (JERRY_ESNEXT) */ + SCANNER_LITERAL_POOL_IN_WITH = (1 << 7), /**< literal pool is in a with statement */ #if ENABLED (JERRY_ESNEXT) SCANNER_LITERAL_POOL_FUNCTION_STATEMENT = (1 << 8), /**< function statement */ SCANNER_LITERAL_POOL_ARROW = (1 << 9), /**< arrow function */ @@ -293,6 +293,9 @@ typedef enum SCANNER_LITERAL_POOL_ASYNC = (1 << 11), /**< async function */ SCANNER_LITERAL_POOL_HAS_SUPER_REFERENCE = (1 << 12), /**< function body contains super reference */ #endif /* ENABLED (JERRY_ESNEXT) */ +#if ENABLED (JERRY_MODULE_SYSTEM) + SCANNER_LITERAL_POOL_IN_EXPORT = (1 << 13), /**< the declared variables are exported by the module system */ +#endif /* ENABLED (JERRY_MODULE_SYSTEM) */ } scanner_literal_pool_flags_t; /** @@ -319,6 +322,28 @@ typedef enum #define SCANNER_FROM_COMPUTED_TO_LITERAL_POOL(mode) \ (((mode) - SCAN_STACK_COMPUTED_PROPERTY) << 10) +#if ENABLED (JERRY_ESNEXT) + +/** + * Literal pool which may contains function argument identifiers + */ +#define SCANNER_LITERAL_POOL_MAY_HAVE_ARGUMENTS(status_flags) \ + (!((status_flags) & (SCANNER_LITERAL_POOL_CLASS_NAME | SCANNER_LITERAL_POOL_CLASS_FIELD))) + +/** + * The class name is the *default* class name + */ +#define SCANNER_LITERAL_POOL_DEFAULT_CLASS_NAME SCANNER_LITERAL_POOL_HAS_SUPER_REFERENCE + +#else /* !ENABLED (JERRY_ESNEXT) */ + +/** + * Literal pool which may contains function argument identifiers + */ +#define SCANNER_LITERAL_POOL_MAY_HAVE_ARGUMENTS(status_flags) true + +#endif /* ENABLED (JERRY_ESNEXT) */ + /** * Local literal pool. */ @@ -387,8 +412,8 @@ void scanner_detect_invalid_let (parser_context_t *context_p, lexer_lit_location void scanner_detect_eval_call (parser_context_t *context_p, scanner_context_t *scanner_context_p); #if ENABLED (JERRY_ESNEXT) -void scanner_push_class_declaration (parser_context_t *context_p, scanner_context_t *scanner_context_p, - uint8_t stack_mode); +lexer_lit_location_t *scanner_push_class_declaration (parser_context_t *context_p, + scanner_context_t *scanner_context_p, uint8_t stack_mode); void scanner_push_class_field_initializer (parser_context_t *context_p, scanner_context_t *scanner_context_p); void scanner_push_destructuring_pattern (parser_context_t *context_p, scanner_context_t *scanner_context_p, uint8_t binding_type, bool is_nested); diff --git a/jerry-core/parser/js/js-scanner-ops.c b/jerry-core/parser/js/js-scanner-ops.c index ffc9484ca..38b048f39 100644 --- a/jerry-core/parser/js/js-scanner-ops.c +++ b/jerry-core/parser/js/js-scanner-ops.c @@ -347,9 +347,7 @@ scanner_check_function_after_if (parser_context_t *context_p, /**< context */ if (JERRY_UNLIKELY (context_p->token.type == LEXER_KEYW_FUNCTION)) { scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, - scanner_context_p, - SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = context_p->source_p; parser_stack_push_uint8 (context_p, SCAN_STACK_PRIVATE_BLOCK); diff --git a/jerry-core/parser/js/js-scanner-util.c b/jerry-core/parser/js/js-scanner-util.c index 0e03e5743..8577c0a9b 100644 --- a/jerry-core/parser/js/js-scanner-util.c +++ b/jerry-core/parser/js/js-scanner-util.c @@ -483,7 +483,7 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ } #endif /* ENABLED (JERRY_ESNEXT) */ - if (literal_pool_p->source_p == NULL) + if (JERRY_UNLIKELY (literal_pool_p->source_p == NULL)) { JERRY_ASSERT (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_FUNCTION); JERRY_ASSERT (literal_pool_p->literal_pool.data.first_p == NULL @@ -528,6 +528,19 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ size_t compressed_size = 1; uint32_t no_declarations = literal_pool_p->no_declarations; +#if ENABLED (JERRY_ESNEXT) + if (JERRY_UNLIKELY (status_flags & SCANNER_LITERAL_POOL_CLASS_NAME)) + { + literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator); + + if ((literal_p != NULL || (status_flags & SCANNER_LITERAL_POOL_DEFAULT_CLASS_NAME)) + && no_declarations < PARSER_MAXIMUM_DEPTH_OF_SCOPE_STACK) + { + no_declarations++; + } + } +#endif /* ENABLED (JERRY_ESNEXT) */ + while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) { uint8_t type = literal_p->type; @@ -654,7 +667,13 @@ scanner_pop_literal_pool (parser_context_t *context_p, /**< context */ literal_p); uint8_t extended_type = literal_location_p->type; - if ((status_flags & SCANNER_LITERAL_POOL_FUNCTION) || (type & SCANNER_LITERAL_NO_REG)) +#if ENABLED (JERRY_ESNEXT) + const uint16_t no_reg_flags = (SCANNER_LITERAL_POOL_FUNCTION | SCANNER_LITERAL_POOL_CLASS_FIELD); +#else /* !ENABLED (JERRY_ESNEXT) */ + const uint16_t no_reg_flags = SCANNER_LITERAL_POOL_FUNCTION; +#endif /* ENABLED (JERRY_ESNEXT) */ + + if ((status_flags & no_reg_flags) || (type & SCANNER_LITERAL_NO_REG)) { extended_type |= SCANNER_LITERAL_NO_REG; } @@ -974,6 +993,8 @@ scanner_filter_arguments (parser_context_t *context_p, /**< context */ bool can_eval = (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_CAN_EVAL) != 0; bool has_arguments = (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_NO_ARGUMENTS) == 0; + JERRY_ASSERT (SCANNER_LITERAL_POOL_MAY_HAVE_ARGUMENTS (literal_pool_p->status_flags)); + if (can_eval && prev_literal_pool_p != NULL) { prev_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_CAN_EVAL; @@ -1126,53 +1147,64 @@ scanner_add_custom_literal (parser_context_t *context_p, /**< context */ scanner_literal_pool_t *literal_pool_p, /**< literal pool */ const lexer_lit_location_t *literal_location_p) /**< literal */ { - parser_list_iterator_t literal_iterator; - parser_list_iterator_init (&literal_pool_p->literal_pool, &literal_iterator); - lexer_lit_location_t *literal_p; - - const uint8_t *char_p = literal_location_p->char_p; - prop_length_t length = literal_location_p->length; - - if (JERRY_LIKELY (!literal_location_p->has_escape)) + while (true) { - while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) + parser_list_iterator_t literal_iterator; + parser_list_iterator_init (&literal_pool_p->literal_pool, &literal_iterator); + lexer_lit_location_t *literal_p; + + const uint8_t *char_p = literal_location_p->char_p; + prop_length_t length = literal_location_p->length; + + if (JERRY_LIKELY (!literal_location_p->has_escape)) { - if (literal_p->length == length) + while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) { - if (JERRY_LIKELY (!literal_p->has_escape)) + if (literal_p->length == length) { - if (memcmp (literal_p->char_p, char_p, length) == 0) + if (JERRY_LIKELY (!literal_p->has_escape)) { + if (memcmp (literal_p->char_p, char_p, length) == 0) + { + return literal_p; + } + } + else if (lexer_compare_identifier_to_string (literal_p, char_p, length)) + { + /* The non-escaped version is preferred. */ + literal_p->char_p = char_p; + literal_p->has_escape = 0; return literal_p; } } - else if (lexer_compare_identifier_to_string (literal_p, char_p, length)) + } + } + else + { + while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) + { + if (lexer_compare_identifiers (context_p, literal_p, literal_location_p)) { - /* The non-escaped version is preferred. */ - literal_p->char_p = char_p; - literal_p->has_escape = 0; return literal_p; } } } - } - else - { - while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) + +#if ENABLED (JERRY_ESNEXT) + if (JERRY_UNLIKELY (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_CLASS_NAME)) { - if (lexer_compare_identifiers (context_p, literal_p, literal_location_p)) - { - return literal_p; - } + literal_pool_p = literal_pool_p->prev_p; + continue; } +#endif /* ENABLED (JERRY_ESNEXT) */ + + literal_p = (lexer_lit_location_t *) parser_list_append (context_p, &literal_pool_p->literal_pool); + *literal_p = *literal_location_p; + + literal_p->type = 0; + + return literal_p; } - - literal_p = (lexer_lit_location_t *) parser_list_append (context_p, &literal_pool_p->literal_pool); - *literal_p = *literal_location_p; - - literal_p->type = 0; - - return literal_p; } /* scanner_add_custom_literal */ /** @@ -1232,6 +1264,8 @@ scanner_append_argument (parser_context_t *context_p, /**< context */ const uint8_t *char_p = literal_location_p->char_p; prop_length_t length = literal_location_p->length; + JERRY_ASSERT (SCANNER_LITERAL_POOL_MAY_HAVE_ARGUMENTS (literal_pool_p->status_flags)); + if (JERRY_LIKELY (!context_p->token.lit_location.has_escape)) { while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) @@ -1383,9 +1417,9 @@ scanner_detect_invalid_var (parser_context_t *context_p, /**< context */ scanner_literal_pool_t *literal_pool_p = scanner_context_p->active_literal_pool_p; - if (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_BLOCK + if (!(literal_pool_p->status_flags & SCANNER_LITERAL_POOL_FUNCTION) && (var_literal_p->type & (SCANNER_LITERAL_IS_FUNC | SCANNER_LITERAL_IS_FUNC_DECLARATION)) - == (SCANNER_LITERAL_IS_FUNC | SCANNER_LITERAL_IS_FUNC_DECLARATION)) + == (SCANNER_LITERAL_IS_FUNC | SCANNER_LITERAL_IS_FUNC_DECLARATION)) { scanner_raise_redeclaration_error (context_p); } @@ -1405,10 +1439,10 @@ scanner_detect_invalid_var (parser_context_t *context_p, /**< context */ { while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) { - if (literal_p->type & SCANNER_LITERAL_IS_LOCAL + if ((literal_p->type & SCANNER_LITERAL_IS_LOCAL) && !(literal_p->type & SCANNER_LITERAL_IS_ARG) && !((literal_p->type & SCANNER_LITERAL_IS_FUNC) - && (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_BLOCK) == 0) + && (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_FUNCTION)) && (literal_p->type & SCANNER_LITERAL_IS_LOCAL) != SCANNER_LITERAL_IS_LOCAL && literal_p->length == length) { @@ -1432,10 +1466,10 @@ scanner_detect_invalid_var (parser_context_t *context_p, /**< context */ { while ((literal_p = (lexer_lit_location_t *) parser_list_iterator_next (&literal_iterator)) != NULL) { - if (literal_p->type & SCANNER_LITERAL_IS_LOCAL + if ((literal_p->type & SCANNER_LITERAL_IS_LOCAL) && !(literal_p->type & SCANNER_LITERAL_IS_ARG) && !((literal_p->type & SCANNER_LITERAL_IS_FUNC) - && (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_BLOCK) == 0) + && (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_FUNCTION)) && (literal_p->type & SCANNER_LITERAL_IS_LOCAL) != SCANNER_LITERAL_IS_LOCAL && lexer_compare_identifiers (context_p, literal_p, var_literal_p)) { @@ -1475,23 +1509,44 @@ scanner_detect_invalid_let (parser_context_t *context_p, /**< context */ /** * Push the values required for class declaration parsing. + * + * @return literal reference created for class statements, NULL otherwise */ -void +lexer_lit_location_t * scanner_push_class_declaration (parser_context_t *context_p, /**< context */ scanner_context_t *scanner_context_p, /* scanner context */ uint8_t stack_mode) /**< stack mode */ { JERRY_ASSERT (context_p->token.type == LEXER_KEYW_CLASS); - parser_stack_push_uint8 (context_p, stack_mode); - scanner_source_start_t source_start; - source_start.source_p = context_p->source_p; + const uint8_t *source_p = context_p->source_p; + lexer_lit_location_t *literal_p = NULL; + + parser_stack_push_uint8 (context_p, stack_mode); + lexer_next_token (context_p); + + bool class_has_name = (context_p->token.type == LEXER_LITERAL + && context_p->token.lit_location.type == LEXER_IDENT_LITERAL); + + if (stack_mode == SCAN_STACK_CLASS_STATEMENT && class_has_name) + { + literal_p = scanner_add_literal (context_p, scanner_context_p); + } + + scanner_literal_pool_t *literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); + + if (class_has_name) + { + scanner_add_literal (context_p, scanner_context_p); + } + + literal_pool_p->source_p = source_p; + literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_CLASS_NAME; - parser_stack_push (context_p, &source_start, sizeof (scanner_source_start_t)); parser_stack_push_uint8 (context_p, SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR); scanner_context_p->mode = SCAN_MODE_CLASS_DECLARATION; - lexer_next_token (context_p); + return literal_p; } /* scanner_push_class_declaration */ /** @@ -1506,6 +1561,11 @@ scanner_push_class_field_initializer (parser_context_t *context_p, /**< context parser_stack_push (context_p, &source_start, sizeof (scanner_source_start_t)); parser_stack_push_uint8 (context_p, SCAN_STACK_CLASS_FIELD_INITIALIZER); + + scanner_literal_pool_t *literal_pool_p; + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, SCANNER_LITERAL_POOL_CLASS_FIELD); + literal_pool_p->source_p = context_p->source_p; + scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION; } /* scanner_push_class_field_initializer */ diff --git a/jerry-core/parser/js/js-scanner.c b/jerry-core/parser/js/js-scanner.c index bed7b0a81..414dad995 100644 --- a/jerry-core/parser/js/js-scanner.c +++ b/jerry-core/parser/js/js-scanner.c @@ -771,7 +771,7 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context * #if ENABLED (JERRY_ESNEXT) scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = context_p->source_p - 1; #endif /* ENABLED (JERRY_ESNEXT) */ @@ -982,6 +982,8 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context * if (stack_top == SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR || stack_top == SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR) { + JERRY_ASSERT (scanner_context_p->active_literal_pool_p->status_flags & SCANNER_LITERAL_POOL_CLASS_NAME); + if (context_p->token.type == LEXER_LEFT_PAREN) { scanner_push_literal_pool (context_p, scanner_context_p, SCANNER_LITERAL_POOL_FUNCTION); @@ -1108,12 +1110,11 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context * case SCAN_STACK_CLASS_FIELD_INITIALIZER: { scanner_source_start_t source_start; + const uint8_t *source_p = NULL; parser_stack_pop_uint8 (context_p); parser_stack_pop (context_p, &source_start, sizeof (scanner_source_start_t)); - - const uint8_t *source_p = NULL; - + scanner_pop_literal_pool (context_p, scanner_context_p); scanner_context_p->mode = SCAN_MODE_CLASS_BODY_NO_SCAN; switch (type) @@ -1228,9 +1229,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ { #if ENABLED (JERRY_ESNEXT) scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, - scanner_context_p, - SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = context_p->source_p; #endif /* ENABLED (JERRY_ESNEXT) */ @@ -1255,9 +1254,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ #if ENABLED (JERRY_ESNEXT) scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, - scanner_context_p, - SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = context_p->source_p; #endif /* ENABLED (JERRY_ESNEXT) */ @@ -1407,7 +1404,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ case LEXER_KEYW_CONST: { scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = source_p; if (scanner_context_p->mode == SCAN_MODE_PRIMARY_EXPRESSION) @@ -1563,7 +1560,7 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ scanner_literal_pool_t *literal_pool_p = scanner_context_p->active_literal_pool_p; - if (literal_pool_p->status_flags & SCANNER_LITERAL_POOL_BLOCK + if (!(literal_pool_p->status_flags & SCANNER_LITERAL_POOL_FUNCTION) && (literal_p->type & (SCANNER_LITERAL_IS_VAR))) { scanner_raise_redeclaration_error (context_p); @@ -1587,18 +1584,22 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ #if ENABLED (JERRY_ESNEXT) case LEXER_KEYW_CLASS: { - scanner_push_class_declaration (context_p, scanner_context_p, SCAN_STACK_CLASS_STATEMENT); + lexer_lit_location_t *literal_p; + literal_p = scanner_push_class_declaration (context_p, scanner_context_p, SCAN_STACK_CLASS_STATEMENT); - if (context_p->token.type != LEXER_LITERAL || context_p->token.lit_location.type != LEXER_IDENT_LITERAL) + if (literal_p == NULL) { scanner_raise_error (context_p); } - lexer_lit_location_t *literal_p = scanner_add_literal (context_p, scanner_context_p); - scanner_detect_invalid_let (context_p, literal_p); literal_p->type |= SCANNER_LITERAL_IS_LET; + if (literal_p->type & SCANNER_LITERAL_IS_USED) + { + literal_p->type |= SCANNER_LITERAL_EARLY_CREATE; + } + #if ENABLED (JERRY_MODULE_SYSTEM) if (scanner_context_p->active_literal_pool_p->status_flags & SCANNER_LITERAL_POOL_IN_EXPORT) { @@ -1810,25 +1811,31 @@ scanner_scan_statement (parser_context_t *context_p, /**< context */ scanner_context_p->mode = SCAN_MODE_FUNCTION_ARGUMENTS; return SCAN_KEEP_TOKEN; } + if (context_p->token.type == LEXER_KEYW_CLASS) { - scanner_push_class_declaration (context_p, scanner_context_p, SCAN_STACK_CLASS_STATEMENT); + lexer_lit_location_t *literal_p; + literal_p = scanner_push_class_declaration (context_p, scanner_context_p, SCAN_STACK_CLASS_STATEMENT); - if (context_p->token.type == LEXER_LITERAL && context_p->token.lit_location.type == LEXER_IDENT_LITERAL) + if (literal_p != NULL) { - lexer_lit_location_t *literal_p = scanner_add_literal (context_p, scanner_context_p); - scanner_detect_invalid_let (context_p, literal_p); + if (literal_p->type & SCANNER_LITERAL_IS_USED) + { + literal_p->type |= SCANNER_LITERAL_EARLY_CREATE; + } + literal_p->type |= SCANNER_LITERAL_IS_LET | SCANNER_LITERAL_NO_REG; return SCAN_NEXT_TOKEN; } - lexer_lit_location_t *literal_p; literal_p = scanner_add_custom_literal (context_p, scanner_context_p->active_literal_pool_p, &lexer_default_literal); + literal_p->type |= SCANNER_LITERAL_IS_LET | SCANNER_LITERAL_NO_REG; + scanner_context_p->active_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_DEFAULT_CLASS_NAME; return SCAN_KEEP_TOKEN; } @@ -2299,9 +2306,7 @@ scanner_scan_statement_end (parser_context_t *context_p, /**< context */ #if ENABLED (JERRY_ESNEXT) scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, - scanner_context_p, - SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = context_p->source_p; #endif /* ENABLED (JERRY_ESNEXT) */ @@ -2325,7 +2330,7 @@ scanner_scan_statement_end (parser_context_t *context_p, /**< context */ lexer_next_token (context_p); scanner_literal_pool_t *literal_pool_p; - literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, SCANNER_LITERAL_POOL_BLOCK); + literal_pool_p = scanner_push_literal_pool (context_p, scanner_context_p, 0); literal_pool_p->source_p = context_p->source_p; parser_stack_push_uint8 (context_p, SCAN_STACK_CATCH_STATEMENT); @@ -2556,20 +2561,15 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ { JERRY_ASSERT (stack_top == SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR || stack_top == SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR); + JERRY_ASSERT (scanner_context.active_literal_pool_p->status_flags & SCANNER_LITERAL_POOL_CLASS_NAME); if (context_p->token.type == LEXER_RIGHT_BRACE) { - scanner_source_start_t source_start; - parser_stack_pop_uint8 (context_p); - - if (stack_top == SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR) - { - parser_stack_pop (context_p, &source_start, sizeof (scanner_source_start_t)); - } - stack_top = context_p->stack_top_uint8; + scanner_pop_literal_pool (context_p, &scanner_context); + JERRY_ASSERT (stack_top == SCAN_STACK_CLASS_STATEMENT || stack_top == SCAN_STACK_CLASS_EXPRESSION); if (stack_top == SCAN_STACK_CLASS_STATEMENT) @@ -2592,13 +2592,11 @@ scanner_scan_all (parser_context_t *context_p, /**< context */ { if (stack_top == SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR) { - scanner_source_start_t source_start; - parser_stack_pop_uint8 (context_p); - parser_stack_pop (context_p, &source_start, sizeof (scanner_source_start_t)); + const uint8_t *class_source_p = scanner_context.active_literal_pool_p->source_p; + scanner_info_t *info_p = scanner_insert_info (context_p, class_source_p, sizeof (scanner_info_t)); - scanner_info_t *info_p = scanner_insert_info (context_p, source_start.source_p, sizeof (scanner_info_t)); info_p->type = SCANNER_TYPE_CLASS_CONSTRUCTOR; - parser_stack_push_uint8 (context_p, SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR); + parser_stack_change_last_uint8 (context_p, SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR); } } else if (lexer_token_is_identifier (context_p, "static", 6)) diff --git a/jerry-core/vm/opcodes.c b/jerry-core/vm/opcodes.c index 4da05373d..0b839f515 100644 --- a/jerry-core/vm/opcodes.c +++ b/jerry-core/vm/opcodes.c @@ -1269,16 +1269,17 @@ opfunc_push_class_environment (vm_frame_ctx_t *frame_ctx_p, /**< frame context * ecma_value_t **vm_stack_top, /**< VM stack top */ ecma_value_t class_name) /**< class name */ { - JERRY_ASSERT (ecma_is_value_undefined (class_name) || ecma_is_value_string (class_name)); + JERRY_ASSERT (ecma_is_value_string (class_name)); ecma_object_t *class_env_p = ecma_create_decl_lex_env (frame_ctx_p->lex_env_p); /* 4.a */ - if (!ecma_is_value_undefined (class_name)) - { - ecma_op_create_immutable_binding (class_env_p, - ecma_get_string_from_value (class_name), - ECMA_VALUE_UNINITIALIZED); - } + ecma_property_value_t *property_value_p; + property_value_p = ecma_create_named_data_property (class_env_p, + ecma_get_string_from_value (class_name), + ECMA_PROPERTY_FLAG_ENUMERABLE, + NULL); + + property_value_p->value = ECMA_VALUE_UNINITIALIZED; frame_ctx_p->lex_env_p = class_env_p; *(*vm_stack_top)++ = ECMA_VALUE_RELEASE_LEX_ENV; @@ -1518,13 +1519,17 @@ opfunc_finalize_class (vm_frame_ctx_t *frame_ctx_p, /**< frame context */ ecma_deref_object (proto_env_p); ecma_deref_object (ctor_env_p); - - opfunc_pop_lexical_environment (frame_ctx_p); - ecma_deref_object (proto_p); + JERRY_ASSERT ((ecma_is_value_undefined (class_name) ? stack_top_p[-3] == ECMA_VALUE_UNDEFINED + : stack_top_p[-3] == ECMA_VALUE_RELEASE_LEX_ENV)); + /* only the current class remains on the stack */ - JERRY_ASSERT (stack_top_p[-3] == ECMA_VALUE_RELEASE_LEX_ENV); + if (stack_top_p[-3] == ECMA_VALUE_RELEASE_LEX_ENV) + { + opfunc_pop_lexical_environment (frame_ctx_p); + } + stack_top_p[-3] = stack_top_p[-2]; *vm_stack_top_p -= 2; } /* opfunc_finalize_class */ diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index 06cabc3e2..d98248c4a 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -2033,8 +2033,11 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ } case VM_OC_PUSH_CLASS_ENVIRONMENT: { - opfunc_push_class_environment (frame_ctx_p, &stack_top_p, left_value); - goto free_left_value; + uint16_t literal_index; + + READ_LITERAL_INDEX (literal_index); + opfunc_push_class_environment (frame_ctx_p, &stack_top_p, literal_start_p[literal_index]); + continue; } case VM_OC_PUSH_IMPLICIT_CTOR: { @@ -2053,8 +2056,17 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ } case VM_OC_FINALIZE_CLASS: { + JERRY_ASSERT (opcode == CBC_EXT_FINALIZE_NAMED_CLASS || opcode == CBC_EXT_FINALIZE_ANONYMOUS_CLASS); + + if (opcode == CBC_EXT_FINALIZE_NAMED_CLASS) + { + uint16_t literal_index; + READ_LITERAL_INDEX (literal_index); + left_value = literal_start_p[literal_index]; + } + opfunc_finalize_class (frame_ctx_p, &stack_top_p, left_value); - goto free_left_value; + continue; } case VM_OC_SET_FIELD_INIT: { diff --git a/tests/jerry/es.next/class-fields4.js b/tests/jerry/es.next/class-fields4.js new file mode 100644 index 000000000..4a34bc27b --- /dev/null +++ b/tests/jerry/es.next/class-fields4.js @@ -0,0 +1,82 @@ +// 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. + +try { + { + A; + class A { } + } + assert(false) +} catch (e) { + assert(e instanceof ReferenceError) +} + +try { + { + class A { [A] () {} } + } + assert(false) +} catch (e) { + assert(e instanceof ReferenceError) +} + +try { + { + var a = class A { [A] () {} } + } + assert(false) +} catch (e) { + assert(e instanceof ReferenceError) +} + +{ + class C { + a = C + static b = C + } + + var X = C + C = 6 + var c = new X + + assert(X.b === X) + assert(c.a === X) +} + +{ + let a = 6 + let b = 7 + class C { + p = a + b + } + assert((new C).p === 13) +} + +try { + { + class C { static a = C = 5 } + } + assert(false) +} catch (e) { + assert(e instanceof TypeError) +} + +try { + { + class C { static [C = 5] = 6 } + } + assert(false) +} catch (e) { + assert(e instanceof ReferenceError) +} diff --git a/tests/test262-es6-excludelist.xml b/tests/test262-es6-excludelist.xml index d48395bfa..7ad1de832 100644 --- a/tests/test262-es6-excludelist.xml +++ b/tests/test262-es6-excludelist.xml @@ -308,8 +308,6 @@ - - ES2018 change: next method must be cached diff --git a/tests/test262-esnext-excludelist.xml b/tests/test262-esnext-excludelist.xml index 05154322c..7f83a8841 100644 --- a/tests/test262-esnext-excludelist.xml +++ b/tests/test262-esnext-excludelist.xml @@ -4161,8 +4161,6 @@ - -