diff --git a/jerry-core/include/jerryscript-snapshot.h b/jerry-core/include/jerryscript-snapshot.h index 253733b71..c6c096039 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 (58u) +#define JERRY_SNAPSHOT_VERSION (59u) /** * Flags for jerry_generate_snapshot and jerry_generate_function_snapshot. diff --git a/jerry-core/parser/js/byte-code.c b/jerry-core/parser/js/byte-code.c index 20bbabdb3..8261055b5 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 == 141, +JERRY_STATIC_ASSERT (CBC_EXT_END == 145, 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 e99e20699..9969ee251 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -652,8 +652,14 @@ VM_OC_SET_SETTER | VM_OC_GET_STACK_STACK) \ CBC_OPCODE (CBC_EXT_SET__PROTO__, CBC_NO_FLAG, -1, \ VM_OC_SET__PROTO__ | VM_OC_GET_STACK) \ + CBC_OPCODE (CBC_EXT_PUSH_STATIC_FIELD_FUNC, CBC_HAS_LITERAL_ARG, 1, \ + VM_OC_PUSH_STATIC_FIELD_FUNC | VM_OC_GET_LITERAL) \ + CBC_OPCODE (CBC_EXT_PUSH_STATIC_COMPUTED_FIELD_FUNC, CBC_HAS_LITERAL_ARG, 0, \ + VM_OC_PUSH_STATIC_FIELD_FUNC | VM_OC_GET_LITERAL) \ CBC_OPCODE (CBC_EXT_ADD_COMPUTED_FIELD, CBC_NO_FLAG, -1, \ VM_OC_ADD_COMPUTED_FIELD | VM_OC_GET_STACK) \ + CBC_OPCODE (CBC_EXT_ADD_STATIC_COMPUTED_FIELD, CBC_NO_FLAG, -1, \ + VM_OC_ADD_COMPUTED_FIELD | VM_OC_GET_STACK) \ \ /* Class related opcodes. */ \ CBC_OPCODE (CBC_EXT_PUSH_NAMED_CLASS_ENV, CBC_HAS_LITERAL_ARG, 1, \ @@ -670,10 +676,12 @@ VM_OC_FINALIZE_CLASS | VM_OC_GET_LITERAL) \ CBC_OPCODE (CBC_EXT_FINALIZE_ANONYMOUS_CLASS, CBC_NO_FLAG, -2, \ VM_OC_FINALIZE_CLASS) \ - CBC_OPCODE (CBC_EXT_SET_CLASS_FIELD_INIT, CBC_HAS_LITERAL_ARG, 0, \ - VM_OC_SET_CLASS_FIELD_INIT | VM_OC_GET_LITERAL) \ - CBC_OPCODE (CBC_EXT_RUN_CLASS_FIELD_INIT, CBC_NO_FLAG, 0, \ - VM_OC_RUN_CLASS_FIELD_INIT) \ + CBC_OPCODE (CBC_EXT_SET_FIELD_INIT, CBC_HAS_LITERAL_ARG, 0, \ + VM_OC_SET_FIELD_INIT | VM_OC_GET_LITERAL) \ + CBC_OPCODE (CBC_EXT_RUN_FIELD_INIT, CBC_NO_FLAG, 0, \ + VM_OC_RUN_FIELD_INIT) \ + CBC_OPCODE (CBC_EXT_RUN_STATIC_FIELD_INIT, CBC_NO_FLAG, -1, \ + VM_OC_RUN_STATIC_FIELD_INIT) \ CBC_OPCODE (CBC_EXT_SET_NEXT_COMPUTED_FIELD, CBC_NO_FLAG, -1, \ VM_OC_SET_NEXT_COMPUTED_FIELD | VM_OC_PUT_REFERENCE) \ CBC_OPCODE (CBC_EXT_PUSH_SUPER, CBC_NO_FLAG, 1, \ diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index 557331a25..aa0db527b 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -2848,15 +2848,9 @@ lexer_construct_function_object (parser_context_t *context_p, /**< context */ { compiled_code_p = parser_parse_function (context_p, extra_status_flags); } - else if (JERRY_LIKELY (!(extra_status_flags & PARSER_CLASS_CONSTRUCTOR))) - { - compiled_code_p = parser_parse_arrow_function (context_p, extra_status_flags); - } else { - /* Since PARSER_IS_ARROW_FUNCTION and PARSER_CLASS_CONSTRUCTOR bits cannot - * be set at the same time, this bit combination triggers class field parsing. */ - compiled_code_p = parser_parse_class_fields (context_p); + compiled_code_p = parser_parse_arrow_function (context_p, extra_status_flags); } #else /* !ENABLED (JERRY_ESNEXT) */ compiled_code_p = parser_parse_function (context_p, extra_status_flags); diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index 926f287c7..8d9fed3ef 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -515,14 +515,17 @@ parser_is_constructor_literal (parser_context_t *context_p) /**< context */ /** * Parse class literal. + * + * @return true - if the class has static fields, false - otherwise */ -static void -parser_parse_class_literal (parser_context_t *context_p, /**< context */ - parser_class_literal_opts_t opts) /**< class literal parsing options */ +static bool +parser_parse_class_body (parser_context_t *context_p, /**< context */ + parser_class_literal_opts_t opts) /**< class literal parsing options */ { JERRY_ASSERT (context_p->token.type == LEXER_LEFT_BRACE); lexer_literal_t *ctor_literal_p = NULL; + lexer_literal_t *static_fields_literal_p = NULL; if (opts & PARSER_CLASS_LITERAL_CTOR_PRESENT) { @@ -542,6 +545,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */ bool is_static = false; size_t fields_size = 0; + uint32_t computed_field_count = 0; while (true) { @@ -715,7 +719,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */ if (!(status_flags & (PARSER_IS_ASYNC_FUNCTION | PARSER_IS_GENERATOR_FUNCTION))) { - if (!is_static && !lexer_check_next_character (context_p, LIT_CHAR_LEFT_PAREN)) + if (!lexer_check_next_character (context_p, LIT_CHAR_LEFT_PAREN)) { /* Class field. */ if (fields_size == 0) @@ -724,14 +728,19 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */ } scanner_range_t range; - uint8_t class_field_type = 0; + uint8_t class_field_type = is_static ? PARSER_CLASS_FIELD_STATIC : 0; if (!is_computed) { + if (is_static && parser_is_constructor_literal (context_p)) + { + parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIST_EXPECTED); + } + range.start_location.source_p = context_p->token.lit_location.char_p; range.start_location.line = context_p->token.line; range.start_location.column = context_p->token.column; - class_field_type = PARSER_CLASS_FIELD_NORMAL; + class_field_type |= PARSER_CLASS_FIELD_NORMAL; if (context_p->token.lit_location.type == LEXER_STRING_LITERAL) { @@ -740,7 +749,23 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */ } else { - parser_emit_cbc_ext (context_p, CBC_EXT_ADD_COMPUTED_FIELD); + if (++computed_field_count > ECMA_INTEGER_NUMBER_MAX) + { + parser_raise_error (context_p, PARSER_ERR_TOO_MANY_CLASS_FIELDS); + } + + if (is_static && static_fields_literal_p == NULL) + { + static_fields_literal_p = lexer_construct_unused_literal (context_p); + parser_emit_cbc_ext_literal (context_p, + CBC_EXT_PUSH_STATIC_COMPUTED_FIELD_FUNC, + (uint16_t) (context_p->literal_count++)); + } + else + { + parser_emit_cbc_ext (context_p, (is_static ? CBC_EXT_ADD_STATIC_COMPUTED_FIELD + : CBC_EXT_ADD_COMPUTED_FIELD)); + } } if (lexer_consume_assign (context_p)) @@ -806,62 +831,6 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */ lexer_construct_number_object (context_p, false, false); } } - - if (is_static && !lexer_check_next_character (context_p, LIT_CHAR_LEFT_PAREN)) - { - if (!is_computed && parser_is_constructor_literal (context_p)) - { - parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIST_EXPECTED); - } - - uint16_t literal_index = context_p->lit_object.index; - context_p->status_flags |= PARSER_INSIDE_CLASS_FIELD; - - if (lexer_consume_assign (context_p)) - { - if (context_p->next_scanner_info_p->source_p != context_p->source_p) - { - lexer_next_token (context_p); - parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA); - parser_raise_error (context_p, PARSER_ERR_SEMICOLON_EXPECTED); - } - - JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_CLASS_FIELD_INITIALIZER_END); - - /* Changing the source_end_p prevents the lexer to process the name of the next class field - * as normal token which may cause issues if the name is also a keyword (e.g. var). */ - const uint8_t *source_end_p = context_p->source_end_p; - context_p->source_end_p = ((scanner_location_info_t *) context_p->next_scanner_info_p)->location.source_p; - scanner_release_next (context_p, sizeof (scanner_location_info_t)); - - lexer_next_token (context_p); - parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA); - - if (context_p->token.type != LEXER_EOS) - { - parser_raise_error (context_p, PARSER_ERR_SEMICOLON_EXPECTED); - } - - context_p->source_end_p = source_end_p; - } - else - { - parser_emit_cbc (context_p, CBC_PUSH_UNDEFINED); - } - - if (!is_computed) - { - parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_STATIC_PROPERTY, literal_index); - } - else - { - parser_emit_cbc_ext (context_p, CBC_EXT_SET_STATIC_COMPUTED_PROPERTY); - } - - context_p->status_flags &= (uint32_t) ~PARSER_INSIDE_CLASS_FIELD; - is_static = false; - continue; - } } uint16_t literal_index = context_p->lit_object.index; @@ -897,17 +866,49 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */ } } - if (fields_size > 0) + if (fields_size == 0) { - parser_reverse_class_fields (context_p, fields_size); - - /* Since PARSER_IS_ARROW_FUNCTION and PARSER_CLASS_CONSTRUCTOR bits cannot - * be set at the same time, this bit combination triggers class field parsing. */ - uint16_t function_literal_index = lexer_construct_function_object (context_p, (PARSER_IS_ARROW_FUNCTION - | PARSER_CLASS_CONSTRUCTOR)); - parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_CLASS_FIELD_INIT, function_literal_index); + return false; } -} /* parser_parse_class_literal */ + + parser_reverse_class_fields (context_p, fields_size); + + /* Since PARSER_IS_ARROW_FUNCTION and PARSER_CLASS_CONSTRUCTOR bits cannot + * be set at the same time, this bit combination triggers class field parsing. */ + + if (!(context_p->stack_top_uint8 & PARSER_CLASS_FIELD_STATIC)) + { + lexer_literal_t *literal_p = lexer_construct_unused_literal (context_p); + + uint16_t function_literal_index = (uint16_t) (context_p->literal_count++); + parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_FIELD_INIT, function_literal_index); + parser_flush_cbc (context_p); + + literal_p->u.bytecode_p = parser_parse_class_fields (context_p); + literal_p->type = LEXER_FUNCTION_LITERAL; + } + + bool has_static_field = false; + + if (context_p->stack_top_uint8 & PARSER_CLASS_FIELD_STATIC) + { + if (static_fields_literal_p == NULL) + { + static_fields_literal_p = lexer_construct_unused_literal (context_p); + uint16_t function_literal_index = (uint16_t) (context_p->literal_count++); + parser_emit_cbc_ext_literal (context_p, CBC_EXT_PUSH_STATIC_FIELD_FUNC, function_literal_index); + } + + parser_flush_cbc (context_p); + static_fields_literal_p->u.bytecode_p = parser_parse_class_fields (context_p); + static_fields_literal_p->type = LEXER_FUNCTION_LITERAL; + + has_static_field = true; + } + + parser_stack_pop_uint8 (context_p); + return has_static_field; +} /* parser_parse_class_body */ /** * Parse class statement or expression. @@ -999,7 +1000,7 @@ parser_parse_class (parser_context_t *context_p, /**< context */ } /* ClassDeclaration is parsed. Continue with class body. */ - parser_parse_class_literal (context_p, opts); + bool has_static_field = parser_parse_class_body (context_p, opts); if (class_name_index != PARSER_INVALID_LITERAL_INDEX) { @@ -1011,6 +1012,11 @@ parser_parse_class (parser_context_t *context_p, /**< context */ parser_emit_cbc_ext (context_p, CBC_EXT_FINALIZE_ANONYMOUS_CLASS); } + if (has_static_field) + { + parser_emit_cbc_ext (context_p, CBC_EXT_RUN_STATIC_FIELD_INIT); + } + if (is_statement) { cbc_opcode_t opcode = CBC_MOV_IDENT; diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index d337c62da..09c50c69e 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -140,6 +140,7 @@ typedef enum PARSER_CLASS_FIELD_END = (1u << 0), /**< last class field */ PARSER_CLASS_FIELD_NORMAL = (1u << 1), /**< normal (non-computed) class field */ PARSER_CLASS_FIELD_INITIALIZED = (1u << 2), /**< class field is initialized */ + PARSER_CLASS_FIELD_STATIC = (1u << 3), /**< static class field */ } parser_class_field_type_t; #endif /* ENABLED (JERRY_ESNEXT) */ diff --git a/jerry-core/parser/js/js-parser-util.c b/jerry-core/parser/js/js-parser-util.c index 02b9a0dd7..35a57d9c1 100644 --- a/jerry-core/parser/js/js-parser-util.c +++ b/jerry-core/parser/js/js-parser-util.c @@ -792,6 +792,27 @@ parser_set_continues_to_current_position (parser_context_t *context_p, /**< cont #if ENABLED (JERRY_ESNEXT) +/** + * Return the size of internal record corresponding to a class field + * + * @return internal record size + */ +static size_t +parser_get_class_field_info_size (uint8_t class_field_type) /**< class field type */ +{ + if (class_field_type & PARSER_CLASS_FIELD_INITIALIZED) + { + return sizeof (scanner_range_t) + 1; + } + + if (class_field_type & PARSER_CLASS_FIELD_NORMAL) + { + return sizeof (scanner_location_t) + 1; + } + + return 1; +} /* parser_get_class_field_info_size */ + /** * Reverse the field list of a class */ @@ -802,60 +823,87 @@ parser_reverse_class_fields (parser_context_t *context_p, /**< context */ uint8_t *data_p = (uint8_t *) parser_malloc (context_p, fields_size); uint8_t *data_end_p = data_p + fields_size; uint8_t *current_p = data_p; + bool has_fields = false; parser_stack_iterator_t iterator; + JERRY_ASSERT (!(context_p->stack_top_uint8 & PARSER_CLASS_FIELD_END)); + parser_stack_iterator_init (context_p, &iterator); do { uint8_t class_field_type = parser_stack_iterator_read_uint8 (&iterator); - parser_stack_iterator_skip (&iterator, 1); + size_t info_size = parser_get_class_field_info_size (class_field_type); - if (class_field_type & PARSER_CLASS_FIELD_INITIALIZED) - { - parser_stack_iterator_read (&iterator, current_p, sizeof (scanner_range_t)); - parser_stack_iterator_skip (&iterator, sizeof (scanner_range_t)); - current_p += sizeof (scanner_range_t); - } - else if (class_field_type & PARSER_CLASS_FIELD_NORMAL) - { - parser_stack_iterator_read (&iterator, current_p, sizeof (scanner_location_t)); - parser_stack_iterator_skip (&iterator, sizeof (scanner_location_t)); - current_p += sizeof (scanner_location_t); - } + parser_stack_iterator_read (&iterator, current_p, info_size); + parser_stack_iterator_skip (&iterator, info_size); + current_p += info_size; - *current_p++ = class_field_type; + if (!(class_field_type & PARSER_CLASS_FIELD_STATIC)) + { + has_fields = true; + context_p->stack_top_uint8 = class_field_type; + } } while (current_p < data_end_p); parser_stack_iterator_init (context_p, &iterator); current_p = data_end_p; - context_p->stack_top_uint8 = current_p[-1]; - do + bool has_static_fields = false; + + if (has_fields) { - uint8_t class_field_type = current_p[-1]; + do + { + uint8_t class_field_type = current_p[-1]; - if (class_field_type & PARSER_CLASS_FIELD_INITIALIZED) - { - current_p -= sizeof (scanner_range_t) + 1; - parser_stack_iterator_write (&iterator, current_p, sizeof (scanner_range_t) + 1); - parser_stack_iterator_skip (&iterator, sizeof (scanner_range_t) + 1); - } - else if (class_field_type & PARSER_CLASS_FIELD_NORMAL) - { - current_p -= sizeof (scanner_location_t) + 1; - parser_stack_iterator_write (&iterator, current_p, sizeof (scanner_location_t) + 1); - parser_stack_iterator_skip (&iterator, sizeof (scanner_location_t) + 1); - } - else - { - current_p--; - parser_stack_iterator_write (&iterator, current_p, 1); - parser_stack_iterator_skip (&iterator, 1); + size_t info_size = parser_get_class_field_info_size (class_field_type); + + if (!(class_field_type & PARSER_CLASS_FIELD_STATIC)) + { + current_p -= info_size; + parser_stack_iterator_write (&iterator, current_p, info_size); + parser_stack_iterator_skip (&iterator, info_size); + continue; + } + + if (!has_static_fields) + { + has_static_fields = true; + current_p[-1] |= PARSER_CLASS_FIELD_END; + } + current_p -= info_size; } + while (current_p > data_p); + } + else + { + /* All class fields are static. */ + has_static_fields = true; + JERRY_ASSERT (data_end_p[-1] & PARSER_CLASS_FIELD_STATIC); + context_p->stack_top_uint8 = data_end_p[-1]; + } + + if (has_static_fields) + { + current_p = data_end_p; + + do + { + uint8_t class_field_type = current_p[-1]; + + size_t info_size = parser_get_class_field_info_size (class_field_type); + current_p -= info_size; + + if (class_field_type & PARSER_CLASS_FIELD_STATIC) + { + parser_stack_iterator_write (&iterator, current_p, info_size); + parser_stack_iterator_skip (&iterator, info_size); + } + } + while (current_p > data_p); } - while (current_p > data_p); parser_free (data_p, fields_size); } /* parser_reverse_class_fields */ @@ -1260,6 +1308,10 @@ parser_error_to_string (parser_error_t error) /**< error code */ { return "Super is not allowed to be used here."; } + case PARSER_ERR_TOO_MANY_CLASS_FIELDS: + { + return "Too many computed class fields are declared."; + } case PARSER_ERR_ARGUMENTS_IN_CLASS_FIELD: { return "In class field declarations 'arguments' is not allowed."; diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index dc89d228e..43c9a7128 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -2594,7 +2594,7 @@ parser_parse_function (parser_context_t *context_p, /**< context */ #if ENABLED (JERRY_ESNEXT) if ((context_p->status_flags & (PARSER_CLASS_CONSTRUCTOR | PARSER_ALLOW_SUPER_CALL)) == PARSER_CLASS_CONSTRUCTOR) { - parser_emit_cbc_ext (context_p, CBC_EXT_RUN_CLASS_FIELD_INIT); + parser_emit_cbc_ext (context_p, CBC_EXT_RUN_FIELD_INIT); } #endif /* ENABLED (JERRY_ESNEXT) */ @@ -2743,12 +2743,14 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */ parser_saved_context_t saved_context; ecma_compiled_code_t *compiled_code_p; - JERRY_ASSERT (context_p->token.type == LEXER_RIGHT_BRACE); + uint32_t extra_status_flags = context_p->status_flags & PARSER_INSIDE_WITH; + parser_save_context (context_p, &saved_context); context_p->status_flags |= (PARSER_IS_FUNCTION | PARSER_ALLOW_SUPER | PARSER_INSIDE_CLASS_FIELD - | PARSER_ALLOW_NEW_TARGET); + | PARSER_ALLOW_NEW_TARGET + | extra_status_flags); #if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) if (context_p->is_show_opcodes) @@ -2852,14 +2854,13 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */ parser_emit_cbc_ext (context_p, CBC_EXT_SET_NEXT_COMPUTED_FIELD); } } - while (context_p->stack_top_uint8 != PARSER_CLASS_FIELD_END); + while (!(context_p->stack_top_uint8 & PARSER_CLASS_FIELD_END)); if (!first_computed_class_field) { parser_emit_cbc (context_p, CBC_POP); } - parser_stack_pop_uint8 (context_p); parser_flush_cbc (context_p); context_p->source_end_p = source_end_p; scanner_set_location (context_p, &end_location); diff --git a/jerry-core/parser/js/js-parser.h b/jerry-core/parser/js/js-parser.h index c9a8ec8b0..034b9be38 100644 --- a/jerry-core/parser/js/js-parser.h +++ b/jerry-core/parser/js/js-parser.h @@ -148,6 +148,7 @@ typedef enum PARSER_ERR_INVALID_CLASS_CONSTRUCTOR, /**< class constructor cannot be a generator or async function */ PARSER_ERR_CLASS_STATIC_PROTOTYPE, /**< static method name 'prototype' is not allowed */ PARSER_ERR_UNEXPECTED_SUPER_KEYWORD, /**< unexpected super keyword */ + PARSER_ERR_TOO_MANY_CLASS_FIELDS, /**< too many computed class fields */ PARSER_ERR_ARGUMENTS_IN_CLASS_FIELD, /**< arguments is not allowed in class fields */ PARSER_ERR_RIGHT_BRACE_EXPECTED, /**< right brace expected */ diff --git a/jerry-core/vm/opcodes.c b/jerry-core/vm/opcodes.c index 88c05c7ca..4da05373d 100644 --- a/jerry-core/vm/opcodes.c +++ b/jerry-core/vm/opcodes.c @@ -967,22 +967,21 @@ opfunc_async_create_and_await (vm_frame_ctx_t *frame_ctx_p, /**< frame context * } /* opfunc_async_create_and_await */ /** - * Initialize implicit class fields. + * Initialize class fields. * * @return ECMA_VALUE_ERROR - initialization fails * ECMA_VALUE_UNDEFINED - otherwise */ ecma_value_t -ecma_op_init_class_fields (ecma_value_t function_object, /**< the function itself */ - ecma_value_t this_val) /**< this_arg of the function */ +opfunc_init_class_fields (ecma_value_t class_object, /**< the function itself */ + ecma_value_t this_val) /**< this_arg of the function */ { - JERRY_ASSERT (ecma_is_value_object (function_object)); + JERRY_ASSERT (ecma_is_value_object (class_object)); JERRY_ASSERT (ecma_is_value_object (this_val)); ecma_string_t *name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_CLASS_FIELD_INIT); - ecma_object_t *function_object_p = ecma_get_object_from_value (function_object); - - ecma_property_t *property_p = ecma_find_named_property (function_object_p, name_p); + ecma_object_t *class_object_p = ecma_get_object_from_value (class_object); + ecma_property_t *property_p = ecma_find_named_property (class_object_p, name_p); if (property_p == NULL) { @@ -994,7 +993,7 @@ ecma_op_init_class_fields (ecma_value_t function_object, /**< the function itsel shared_class_fields.computed_class_fields_p = NULL; name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_CLASS_FIELD_COMPUTED); - ecma_property_t *class_field_property_p = ecma_find_named_property (function_object_p, name_p); + ecma_property_t *class_field_property_p = ecma_find_named_property (class_object_p, name_p); if (class_field_property_p != NULL) { @@ -1016,11 +1015,57 @@ ecma_op_init_class_fields (ecma_value_t function_object, /**< the function itsel JERRY_ASSERT (ECMA_IS_VALUE_ERROR (result) || result == ECMA_VALUE_UNDEFINED); return result; -} /* ecma_op_init_class_fields */ +} /* opfunc_init_class_fields */ +/** + * Initialize static class fields. + * + * @return ECMA_VALUE_ERROR - initialization fails + * ECMA_VALUE_UNDEFINED - otherwise + */ ecma_value_t -ecma_op_add_computed_field (ecma_value_t class_object, /**< class object */ - ecma_value_t name) /**< name of the property */ +opfunc_init_static_class_fields (ecma_value_t function_object, /**< the function itself */ + ecma_value_t this_val) /**< this_arg of the function */ +{ + JERRY_ASSERT (ecma_op_is_callable (function_object)); + JERRY_ASSERT (ecma_is_value_object (this_val)); + + vm_frame_ctx_shared_class_fields_t shared_class_fields; + shared_class_fields.header.status_flags = VM_FRAME_CTX_SHARED_HAS_CLASS_FIELDS; + shared_class_fields.computed_class_fields_p = NULL; + + ecma_string_t *name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_CLASS_FIELD_COMPUTED); + ecma_object_t *function_object_p = ecma_get_object_from_value (function_object); + ecma_property_t *class_field_property_p = ecma_find_named_property (function_object_p, name_p); + + if (class_field_property_p != NULL) + { + ecma_value_t value = ECMA_PROPERTY_VALUE_PTR (class_field_property_p)->value; + shared_class_fields.computed_class_fields_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_value_t, value); + } + + ecma_extended_object_t *ext_function_p; + ext_function_p = (ecma_extended_object_t *) ecma_get_object_from_value (function_object); + shared_class_fields.header.bytecode_header_p = ecma_op_function_get_compiled_code (ext_function_p); + + ecma_object_t *scope_p = ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t, + ext_function_p->u.function.scope_cp); + + ecma_value_t result = vm_run (&shared_class_fields.header, this_val, scope_p); + + JERRY_ASSERT (ECMA_IS_VALUE_ERROR (result) || result == ECMA_VALUE_UNDEFINED); + return result; +} /* opfunc_init_static_class_fields */ + +/** + * Add the name of a computed field to a name list + * + * @return ECMA_VALUE_ERROR - name is not a valid property name + * ECMA_VALUE_UNDEFINED - otherwise + */ +ecma_value_t +opfunc_add_computed_field (ecma_value_t class_object, /**< class object */ + ecma_value_t name) /**< name of the property */ { ecma_string_t *prop_name_p = ecma_op_to_property_key (name); @@ -1060,7 +1105,7 @@ ecma_op_add_computed_field (ecma_value_t class_object, /**< class object */ compact_collection_p = ecma_compact_collection_push_back (compact_collection_p, name); ECMA_SET_INTERNAL_VALUE_POINTER (property_value_p->value, compact_collection_p); return ECMA_VALUE_UNDEFINED; -} /* ecma_op_add_computed_field */ +} /* opfunc_add_computed_field */ /** * Implicit class constructor handler when the classHeritage is not present. @@ -1083,7 +1128,7 @@ ecma_op_implicit_constructor_handler_cb (const ecma_value_t function_obj, /**< t return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'.")); } - return ecma_op_init_class_fields (function_obj, this_val); + return opfunc_init_class_fields (function_obj, this_val); } /* ecma_op_implicit_constructor_handler_cb */ /** @@ -1139,7 +1184,7 @@ ecma_op_implicit_constructor_handler_heritage_cb (const ecma_value_t function_ob ecma_get_object_from_value (proto_value)); } - ecma_value_t fields_value = ecma_op_init_class_fields (function_obj, result); + ecma_value_t fields_value = opfunc_init_class_fields (function_obj, result); if (ECMA_IS_VALUE_ERROR (fields_value)) { diff --git a/jerry-core/vm/opcodes.h b/jerry-core/vm/opcodes.h index 068aa97df..97140cee4 100644 --- a/jerry-core/vm/opcodes.h +++ b/jerry-core/vm/opcodes.h @@ -147,10 +147,13 @@ ecma_value_t opfunc_async_create_and_await (vm_frame_ctx_t *frame_ctx_p, ecma_value_t value, uint16_t extra_flags); ecma_value_t -ecma_op_init_class_fields (ecma_value_t function_object, ecma_value_t this_val); +opfunc_init_class_fields (ecma_value_t class_object, ecma_value_t this_val); ecma_value_t -ecma_op_add_computed_field (ecma_value_t class_object, ecma_value_t name); +opfunc_init_static_class_fields (ecma_value_t function_object, ecma_value_t this_val); + +ecma_value_t +opfunc_add_computed_field (ecma_value_t class_object, ecma_value_t name); ecma_value_t opfunc_create_implicit_class_constructor (uint8_t opcode); diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index bb224fb90..46fcd68ec 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -625,7 +625,7 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ if (ecma_is_value_object (completion_value)) { ecma_value_t current_function = ecma_make_object_value (vm_get_class_function (frame_ctx_p)); - ecma_value_t fields_value = ecma_op_init_class_fields (current_function, completion_value); + ecma_value_t fields_value = opfunc_init_class_fields (current_function, completion_value); if (ECMA_IS_VALUE_ERROR (fields_value)) { @@ -1811,9 +1811,40 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ } goto free_left_value; } + case VM_OC_PUSH_STATIC_FIELD_FUNC: + { + JERRY_ASSERT (byte_code_start_p[0] == CBC_EXT_OPCODE + && (byte_code_start_p[1] == CBC_EXT_PUSH_STATIC_FIELD_FUNC + || byte_code_start_p[1] == CBC_EXT_PUSH_STATIC_COMPUTED_FIELD_FUNC)); + + bool push_computed = (byte_code_start_p[1] == CBC_EXT_PUSH_STATIC_COMPUTED_FIELD_FUNC); + ecma_value_t value = stack_top_p[-1]; + + if (!push_computed) + { + stack_top_p++; + } + + memmove (stack_top_p - 3, stack_top_p - 4, 3 * sizeof (ecma_value_t)); + stack_top_p[-4] = left_value; + + if (!push_computed) + { + continue; + } + + left_value = value; + /* FALLTHRU */ + } case VM_OC_ADD_COMPUTED_FIELD: { - result = ecma_op_add_computed_field (stack_top_p[-2], left_value); + JERRY_ASSERT (byte_code_start_p[0] == CBC_EXT_OPCODE + && (byte_code_start_p[1] == CBC_EXT_PUSH_STATIC_COMPUTED_FIELD_FUNC + || byte_code_start_p[1] == CBC_EXT_ADD_COMPUTED_FIELD + || byte_code_start_p[1] == CBC_EXT_ADD_STATIC_COMPUTED_FIELD)); + + int index = (byte_code_start_p[1] == CBC_EXT_ADD_COMPUTED_FIELD) ? -2 : -4; + result = opfunc_add_computed_field (stack_top_p[index], left_value); if (ECMA_IS_VALUE_ERROR (result)) { @@ -2025,7 +2056,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ opfunc_finalize_class (frame_ctx_p, &stack_top_p, left_value); goto free_left_value; } - case VM_OC_SET_CLASS_FIELD_INIT: + case VM_OC_SET_FIELD_INIT: { ecma_string_t *property_name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_CLASS_FIELD_INIT); ecma_object_t *object_p = ecma_get_object_from_value (stack_top_p[-2]); @@ -2050,11 +2081,11 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ goto free_left_value; } - case VM_OC_RUN_CLASS_FIELD_INIT: + case VM_OC_RUN_FIELD_INIT: { JERRY_ASSERT (frame_ctx_p->shared_p->status_flags & VM_FRAME_CTX_SHARED_NON_ARROW_FUNC); - result = ecma_op_init_class_fields (ecma_make_object_value (VM_FRAME_CTX_GET_FUNCTION_OBJECT (frame_ctx_p)), - frame_ctx_p->this_binding); + result = opfunc_init_class_fields (ecma_make_object_value (VM_FRAME_CTX_GET_FUNCTION_OBJECT (frame_ctx_p)), + frame_ctx_p->this_binding); if (ECMA_IS_VALUE_ERROR (result)) { @@ -2062,6 +2093,20 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ } continue; } + case VM_OC_RUN_STATIC_FIELD_INIT: + { + left_value = stack_top_p[-2]; + stack_top_p[-2] = stack_top_p[-1]; + stack_top_p--; + + result = opfunc_init_static_class_fields (left_value, stack_top_p[-1]); + + if (ECMA_IS_VALUE_ERROR (result)) + { + goto error; + } + goto free_left_value; + } case VM_OC_SET_NEXT_COMPUTED_FIELD: { ecma_integer_value_t next_index = ecma_get_integer_from_value (stack_top_p[-2]) + 1; diff --git a/jerry-core/vm/vm.h b/jerry-core/vm/vm.h index 012e9fc5e..6417f1b79 100644 --- a/jerry-core/vm/vm.h +++ b/jerry-core/vm/vm.h @@ -260,8 +260,10 @@ typedef enum VM_OC_PUSH_IMPLICIT_CTOR, /**< create implicit class constructor */ VM_OC_INIT_CLASS, /**< initialize class */ VM_OC_FINALIZE_CLASS, /**< finalize class */ - VM_OC_SET_CLASS_FIELD_INIT, /**< store the class field initializer function */ - VM_OC_RUN_CLASS_FIELD_INIT, /**< run the class field initializer function */ + VM_OC_SET_FIELD_INIT, /**< store the class field initializer function */ + VM_OC_SET_STATIC_FIELD_INIT, /**< store the static class field initializer function */ + VM_OC_RUN_FIELD_INIT, /**< run the class field initializer function */ + VM_OC_RUN_STATIC_FIELD_INIT, /**< run the static class field initializer function */ VM_OC_SET_NEXT_COMPUTED_FIELD, /**< set the next computed field of a class */ VM_OC_PUSH_SUPER_CONSTRUCTOR, /**< getSuperConstructor operation */ VM_OC_RESOLVE_LEXICAL_THIS, /**< resolve this_binding from from the lexical environment */ @@ -294,6 +296,7 @@ typedef enum VM_OC_REQUIRE_OBJECT_COERCIBLE,/**< RequireObjectCoercible opretaion */ VM_OC_ASSIGN_SUPER, /**< assign super reference */ VM_OC_SET__PROTO__, /**< set prototype when __proto__: form is used */ + VM_OC_PUSH_STATIC_FIELD_FUNC, /**< push static field initializer function */ VM_OC_ADD_COMPUTED_FIELD, /**< add computed field name */ #endif /* ENABLED (JERRY_ESNEXT) */ VM_OC_NONE, /**< a special opcode for unsupported byte codes */ @@ -342,8 +345,10 @@ typedef enum VM_OC_PUSH_IMPLICIT_CTOR = VM_OC_NONE, /**< create implicit class constructor */ VM_OC_INIT_CLASS = VM_OC_NONE, /**< initialize class */ VM_OC_FINALIZE_CLASS = VM_OC_NONE, /**< finalize class */ - VM_OC_SET_CLASS_FIELD_INIT = VM_OC_NONE, /**< store the class field initializer function */ - VM_OC_RUN_CLASS_FIELD_INIT = VM_OC_NONE, /**< run the class field initializer function */ + VM_OC_SET_FIELD_INIT = VM_OC_NONE, /**< store the class field initializer function */ + VM_OC_SET_STATIC_FIELD_INIT = VM_OC_NONE, /**< store the static class field initializer function */ + VM_OC_RUN_FIELD_INIT = VM_OC_NONE, /**< run the class field initializer function */ + VM_OC_RUN_STATIC_FIELD_INIT = VM_OC_NONE, /**< run the static class field initializer function */ VM_OC_SET_NEXT_COMPUTED_FIELD = VM_OC_NONE, /**< set the next computed field of a class */ VM_OC_PUSH_SUPER_CONSTRUCTOR = VM_OC_NONE, /**< getSuperConstructor operation */ VM_OC_RESOLVE_LEXICAL_THIS = VM_OC_NONE, /**< resolve this_binding from from the lexical environment */ @@ -376,6 +381,7 @@ typedef enum VM_OC_REQUIRE_OBJECT_COERCIBLE = VM_OC_NONE,/**< RequireObjectCoercible opretaion */ VM_OC_ASSIGN_SUPER = VM_OC_NONE, /**< assign super reference */ VM_OC_SET__PROTO__ = VM_OC_NONE, /**< set prototype when __proto__: form is used */ + 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 /* !ENABLED (JERRY_ESNEXT) */ diff --git a/tests/jerry/es.next/class-fields3.js b/tests/jerry/es.next/class-fields3.js index 29c9909a5..fac1fd697 100644 --- a/tests/jerry/es.next/class-fields3.js +++ b/tests/jerry/es.next/class-fields3.js @@ -51,3 +51,33 @@ check_property(C1, 23, 11) check_property(C1, "a b", undefined) assert(res === "msg") assert(counter === 2) + +counter = 0 +class C2 { + static a = (assert(++counter === 6), "x") + static [(assert(++counter === 1), "b")] + static [(assert(++counter === 2), "f")]() {} + static [(assert(++counter === 3), "c")] = (assert(++counter === 7), this); + [(assert(++counter === 4), "a")] + static [(assert(++counter === 5), "d")];static e = (assert(++counter === 8), C2) +} + +assert(counter === 8) +check_property(C2, "a", "x") +check_property(C2, "b", undefined) +check_property(C2, "c", C2) +check_property(C2, "d", undefined) +check_property(C2, "e", C2) + +res = new C2 +check_property(res, "a", undefined) + +let C3 = class C4 { + static f() {} + static xx = C4 + static yy = this +} + +assert(Reflect.ownKeys(C3).toString() === "length,prototype,f,name,xx,yy") +check_property(C3, "xx", C3) +check_property(C3, "yy", C3) diff --git a/tests/test262-esnext-excludelist.xml b/tests/test262-esnext-excludelist.xml index 90f51df6f..894b3bb3b 100644 --- a/tests/test262-esnext-excludelist.xml +++ b/tests/test262-esnext-excludelist.xml @@ -6082,9 +6082,7 @@ - - @@ -6098,7 +6096,6 @@ - @@ -6531,9 +6528,6 @@ - - - @@ -7705,7 +7699,6 @@ - @@ -8197,9 +8190,6 @@ - - -