Properly implement static class fields. (#4221)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-09-25 14:23:02 +02:00
committed by GitHub
parent e478640d80
commit bc64957d19
15 changed files with 344 additions and 162 deletions
+1 -1
View File
@@ -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)
+12 -4
View File
@@ -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, \
+1 -7
View File
@@ -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);
+79 -73
View File
@@ -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;
@@ -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) */
+87 -35
View File
@@ -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.";
+6 -5
View File
@@ -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);
+1
View File
@@ -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 */