Implement ECMAScript 2022 private class methods and fields (#4831)

Co-authored-by: Robert Fancsik robert.fancsik@h-lab.eu
Co-authored-by: Martin Negyokru mnegyokru@inf.u-szeged.hu
JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam
2021-11-26 12:24:59 +01:00
committed by GitHub
parent 841e21a970
commit 70e275e92f
35 changed files with 2196 additions and 4341 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ JERRY_STATIC_ASSERT (offsetof (cbc_uint8_arguments_t, script_value) == offsetof
* whenever new bytecodes are introduced or existing ones have been deleted.
*/
JERRY_STATIC_ASSERT (CBC_END == 238, number_of_cbc_opcodes_changed);
JERRY_STATIC_ASSERT (CBC_EXT_END == 148, number_of_cbc_ext_opcodes_changed);
JERRY_STATIC_ASSERT (CBC_EXT_END == 165, number_of_cbc_ext_opcodes_changed);
#if JERRY_PARSER || JERRY_PARSER_DUMP_BYTE_CODE
+57 -1
View File
@@ -211,6 +211,8 @@
#define PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION \
(PARSER_FINALLY_CONTEXT_STACK_ALLOCATION - PARSER_TRY_CONTEXT_STACK_ALLOCATION)
#define PARSER_STATIC_PRIVATE_TO_PRIVATE_OFFSET (CBC_EXT_COLLECT_PRIVATE_STATIC_FIELD - CBC_EXT_COLLECT_PRIVATE_FIELD)
/**
* Opcode definitions.
*/
@@ -578,9 +580,56 @@
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 private property related opcodes */ \
CBC_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL_REFERENCE, \
CBC_HAS_LITERAL_ARG, \
2, \
VM_OC_PRIVATE_PROP_REFERENCE | VM_OC_GET_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL, \
CBC_HAS_LITERAL_ARG, \
0, \
VM_OC_PRIVATE_PROP_GET | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL_IN, \
CBC_HAS_LITERAL_ARG, \
0, \
VM_OC_PRIVATE_IN | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_PRIVATE_FIELD_ADD, CBC_HAS_LITERAL_ARG, -1, VM_OC_PRIVATE_FIELD_ADD | VM_OC_GET_STACK_LITERAL) \
/* These 8 opcodes must be in this order */ \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_FIELD, \
CBC_HAS_LITERAL_ARG, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_METHOD, \
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_GETTER, \
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_SETTER, \
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_STATIC_FIELD, \
CBC_HAS_LITERAL_ARG, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_STATIC_METHOD, \
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_STATIC_GETTER, \
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_COLLECT_PRIVATE_STATIC_SETTER, \
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, \
0, \
VM_OC_COLLECT_PRIVATE_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
/* Class related opcodes. */ \
CBC_OPCODE (CBC_EXT_PUSH_NAMED_CLASS_ENV, CBC_HAS_LITERAL_ARG, 1, VM_OC_PUSH_CLASS_ENVIRONMENT) \
CBC_OPCODE (CBC_EXT_DEFINE_FIELD, CBC_HAS_LITERAL_ARG, -1, VM_OC_DEFINE_FIELD | VM_OC_GET_STACK_LITERAL) \
CBC_OPCODE (CBC_EXT_PUSH_IMPLICIT_CONSTRUCTOR, CBC_NO_FLAG, 1, VM_OC_PUSH_IMPLICIT_CTOR | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_PUSH_IMPLICIT_CONSTRUCTOR_HERITAGE, CBC_NO_FLAG, 1, VM_OC_PUSH_IMPLICIT_CTOR | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_INIT_CLASS, CBC_NO_FLAG, 0, VM_OC_INIT_CLASS | VM_OC_PUT_STACK) \
@@ -589,6 +638,10 @@
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_ANONYMOUS_FUNC, \
CBC_NO_FLAG, \
-1, \
VM_OC_SET_NEXT_COMPUTED_FIELD | VM_OC_PUT_REFERENCE) \
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, VM_OC_NONE) \
CBC_OPCODE (CBC_EXT_PUSH_SUPER_CONSTRUCTOR, CBC_NO_FLAG, 1, VM_OC_PUSH_SUPER_CONSTRUCTOR) \
@@ -610,6 +663,9 @@
CBC_OPCODE (CBC_EXT_ASSIGN_SUPER, CBC_NO_FLAG, -3, VM_OC_ASSIGN_SUPER) \
CBC_OPCODE (CBC_EXT_ASSIGN_SUPER_PUSH_RESULT, CBC_NO_FLAG, -2, VM_OC_ASSIGN_SUPER | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_ASSIGN_SUPER_BLOCK, CBC_NO_FLAG, -3, VM_OC_ASSIGN_SUPER | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_EXT_ASSIGN_PRIVATE, CBC_NO_FLAG, -3, VM_OC_ASSIGN_PRIVATE) \
CBC_OPCODE (CBC_EXT_ASSIGN_PRIVATE_PUSH_RESULT, CBC_NO_FLAG, -2, VM_OC_ASSIGN_PRIVATE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_ASSIGN_PRIVATE_BLOCK, CBC_NO_FLAG, -3, VM_OC_ASSIGN_PRIVATE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_EXT_SUPER_CALL, CBC_HAS_POP_STACK_BYTE_ARG, -1, VM_OC_SUPER_CALL) \
CBC_OPCODE (CBC_EXT_SUPER_CALL_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, 0, VM_OC_SUPER_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EXT_SUPER_CALL_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -1, VM_OC_SUPER_CALL | VM_OC_PUT_BLOCK) \
+45 -15
View File
@@ -570,17 +570,6 @@ static const uint8_t keyword_lengths_list[] = {
#undef LEXER_KEYWORD
#undef LEXER_KEYWORD_LIST_LENGTH
/**
* Flags for lexer_parse_identifier.
*/
typedef enum
{
LEXER_PARSE_NO_OPTS = 0, /**< no options */
LEXER_PARSE_CHECK_KEYWORDS = (1 << 0), /**< check keywords */
LEXER_PARSE_CHECK_START_AND_RETURN = (1 << 1), /**< check identifier start and return */
LEXER_PARSE_CHECK_PART_AND_RETURN = (1 << 2), /**< check identifier part and return */
} lexer_parse_options_t;
JERRY_STATIC_ASSERT (LEXER_FIRST_NON_RESERVED_KEYWORD < LEXER_FIRST_FUTURE_STRICT_RESERVED_WORD,
lexer_first_non_reserved_keyword_must_be_before_lexer_first_future_strict_reserved_word);
@@ -855,8 +844,8 @@ lexer_parse_identifier (parser_context_t *context_p, /**< context */
}
#endif /* JERRY_ESNEXT */
if (keyword_p->type >= LEXER_FIRST_FUTURE_STRICT_RESERVED_WORD
&& (context_p->status_flags & PARSER_IS_STRICT))
if (keyword_p->type >= LEXER_FIRST_FUTURE_STRICT_RESERVED_WORD && (context_p->status_flags & PARSER_IS_STRICT)
&& !(options & LEXER_PARSE_NO_STRICT_IDENT_ERROR))
{
parser_raise_error (context_p, PARSER_ERR_STRICT_IDENT_NOT_ALLOWED);
}
@@ -1567,6 +1556,9 @@ lexer_next_token (parser_context_t *context_p) /**< context */
LEXER_TYPE_A_TOKEN (LIT_CHAR_RIGHT_SQUARE, LEXER_RIGHT_SQUARE);
LEXER_TYPE_A_TOKEN (LIT_CHAR_SEMICOLON, LEXER_SEMICOLON);
LEXER_TYPE_A_TOKEN (LIT_CHAR_COMMA, LEXER_COMMA);
#if JERRY_ESNEXT
LEXER_TYPE_A_TOKEN (LIT_CHAR_HASHMARK, LEXER_HASHMARK);
#endif /* JERRY_ESNEXT */
case (uint8_t) LIT_CHAR_DOT:
{
@@ -2115,6 +2107,21 @@ lexer_update_await_yield (parser_context_t *context_p, /**< context */
}
} /* lexer_update_await_yield */
/**
* Read next token without skipping whitespaces and checking keywords
*
* @return true if the next literal is private identifier, false otherwise
*/
bool
lexer_scan_private_identifier (parser_context_t *context_p) /**< context */
{
context_p->token.keyword_type = LEXER_EOS;
context_p->token.line = context_p->line;
context_p->token.column = context_p->column;
return (context_p->source_p < context_p->source_end_p && lexer_parse_identifier (context_p, LEXER_PARSE_NO_OPTS));
} /* lexer_scan_private_identifier */
#endif /* JERRY_ESNEXT */
/**
@@ -3142,6 +3149,12 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
create_literal_object = true;
}
#if JERRY_ESNEXT
else if (ident_opts & LEXER_OBJ_IDENT_CLASS_PRIVATE)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_CHARACTER);
}
#endif /* JERRY_ESNEXT */
else
{
switch (context_p->source_p[0])
@@ -3186,6 +3199,16 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
lexer_consume_next_character (context_p);
return;
}
case LIT_CHAR_HASHMARK:
{
if (ident_opts & LEXER_OBJ_IDENT_CLASS_IDENTIFIER)
{
context_p->token.type = LEXER_HASHMARK;
return;
}
break;
}
#endif /* JERRY_ESNEXT */
case LIT_CHAR_RIGHT_BRACE:
{
@@ -3250,6 +3273,12 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
{
return;
}
if (ident_opts & LEXER_OBJ_IDENT_CLASS_PRIVATE)
{
parser_resolve_private_identifier (context_p);
return;
}
#endif /* JERRY_ESNEXT */
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
@@ -3265,14 +3294,15 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
* @return true if the next literal is identifier, false otherwise
*/
bool
lexer_scan_identifier (parser_context_t *context_p) /**< context */
lexer_scan_identifier (parser_context_t *context_p, /**< context */
lexer_parse_options_t opts) /**< identifier parse options */
{
lexer_skip_spaces (context_p);
context_p->token.keyword_type = LEXER_EOS;
context_p->token.line = context_p->line;
context_p->token.column = context_p->column;
if (context_p->source_p < context_p->source_end_p && lexer_parse_identifier (context_p, LEXER_PARSE_NO_OPTS))
if (context_p->source_p < context_p->source_end_p && lexer_parse_identifier (context_p, opts))
{
return true;
}
+16 -1
View File
@@ -30,6 +30,18 @@
* @{
*/
/**
* Flags for lexer_parse_identifier.
*/
typedef enum
{
LEXER_PARSE_NO_OPTS = 0, /**< no options */
LEXER_PARSE_CHECK_KEYWORDS = (1 << 0), /**< check keywords */
LEXER_PARSE_NO_STRICT_IDENT_ERROR = (1 << 1), /**< do not throw exception for strict mode keywords */
LEXER_PARSE_CHECK_START_AND_RETURN = (1 << 2), /**< check identifier start and return */
LEXER_PARSE_CHECK_PART_AND_RETURN = (1 << 3), /**< check identifier part and return */
} lexer_parse_options_t;
/**
* Lexer token types.
*/
@@ -161,6 +173,7 @@ typedef enum
LEXER_COMMA, /**< "," */
#if JERRY_ESNEXT
LEXER_ARROW, /**< "=>" */
LEXER_HASHMARK, /**< "#" */
#endif /* JERRY_ESNEXT */
LEXER_KEYW_BREAK, /**< break */
@@ -203,6 +216,7 @@ typedef enum
LEXER_ASSIGN_GROUP_EXPR, /**< indetifier for the assignment is located in a group expression */
LEXER_ASSIGN_CONST, /**< a const binding is reassigned */
LEXER_INVALID_PATTERN, /**< special value for invalid destructuring pattern */
LEXER_PRIVATE_PRIMARY_EXPR, /**< private field in primary expession position */
#endif /* JERRY_ESNEXT */
/* Keywords which are not keyword tokens. */
@@ -278,8 +292,9 @@ typedef enum
LEXER_OBJ_IDENT_CLASS_IDENTIFIER = (1u << 1), /**< expect identifier inside a class body */
LEXER_OBJ_IDENT_CLASS_NO_STATIC = (1u << 2), /**< static keyword was not present before the identifier */
LEXER_OBJ_IDENT_OBJECT_PATTERN = (1u << 3), /**< parse "get"/"set" as string literal in object pattern */
LEXER_OBJ_IDENT_CLASS_PRIVATE = (1u << 4), /**< static keyword was not present before the identifier */
#if JERRY_FUNCTION_TO_STRING
LEXER_OBJ_IDENT_SET_FUNCTION_START = (1u << 4), /**< set function start */
LEXER_OBJ_IDENT_SET_FUNCTION_START = (1u << 5), /**< set function start */
#else /* !JERRY_FUNCTION_TO_STRING */
LEXER_OBJ_IDENT_SET_FUNCTION_START = 0, /**< set function start (disabled) */
#endif /* JERRY_FUNCTION_TO_STRING */
+259 -20
View File
@@ -285,6 +285,11 @@ parser_emit_unary_lvalue_opcode (parser_context_t *context_p, /**< context */
if (opcode == CBC_DELETE_PUSH_RESULT)
{
#if JERRY_ESNEXT
if (context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL))
{
parser_raise_error (context_p, PARSER_ERR_DELETE_PRIVATE_FIELD);
}
if (context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_SUPER_PROP_LITERAL)
|| context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_SUPER_PROP))
{
@@ -513,6 +518,40 @@ parser_is_constructor_literal (parser_context_t *context_p) /**< context */
&& lexer_compare_literal_to_string (context_p, "constructor", 11));
} /* parser_is_constructor_literal */
/**
* Checks if current private field is already declared
*/
static void
parser_check_duplicated_private_field (parser_context_t *context_p, /**< context */
uint8_t opts) /**< options */
{
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL);
JERRY_ASSERT (context_p->private_context_p);
scanner_class_private_member_t *iter = context_p->private_context_p->members_p;
bool search_for_property = (opts & SCANNER_PRIVATE_FIELD_PROPERTY);
while (iter != NULL)
{
if (lexer_compare_identifiers (context_p, &context_p->token.lit_location, &iter->loc) && (iter->u8_arg & opts))
{
if (iter->u8_arg & SCANNER_PRIVATE_FIELD_SEEN)
{
parser_raise_error (context_p, PARSER_ERR_DUPLICATED_PRIVATE_FIELD);
}
iter->u8_arg |= SCANNER_PRIVATE_FIELD_SEEN;
if (!search_for_property)
{
break;
}
}
iter = iter->prev_p;
}
} /* parser_check_duplicated_private_field */
/**
* Parse class literal.
*
@@ -550,6 +589,7 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
parser_emit_cbc_ext (context_p, CBC_EXT_INIT_CLASS);
bool is_static = false;
bool is_private = false;
size_t fields_size = 0;
uint32_t computed_field_count = 0;
@@ -560,9 +600,19 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
lexer_skip_empty_statements (context_p);
}
lexer_expect_object_literal_id (context_p,
(LEXER_OBJ_IDENT_CLASS_IDENTIFIER | LEXER_OBJ_IDENT_SET_FUNCTION_START
| (is_static ? 0 : LEXER_OBJ_IDENT_CLASS_NO_STATIC)));
uint32_t flags = (LEXER_OBJ_IDENT_CLASS_IDENTIFIER | LEXER_OBJ_IDENT_SET_FUNCTION_START);
if (!is_static)
{
flags |= LEXER_OBJ_IDENT_CLASS_NO_STATIC;
}
if (is_private)
{
flags |= LEXER_OBJ_IDENT_CLASS_PRIVATE;
}
lexer_expect_object_literal_id (context_p, flags);
if (context_p->token.type == LEXER_RIGHT_BRACE)
{
@@ -570,6 +620,14 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
break;
}
if (context_p->token.type == LEXER_HASHMARK)
{
is_private = true;
lexer_next_token (context_p);
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
continue;
}
if (context_p->token.type == LEXER_KEYW_STATIC)
{
JERRY_ASSERT (!is_static);
@@ -577,7 +635,19 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
continue;
}
if (!is_static && context_p->token.type == LEXER_LITERAL && parser_is_constructor_literal (context_p))
if (is_private)
{
parser_check_duplicated_private_field (context_p, SCANNER_PRIVATE_FIELD_PROPERTY_GETTER_SETTER);
}
bool is_constructor_literal = context_p->token.type == LEXER_LITERAL && parser_is_constructor_literal (context_p);
if (is_private && is_constructor_literal && lexer_check_next_character (context_p, LIT_CHAR_LEFT_PAREN))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_PRIVATE_CONSTRUCTOR);
}
if (!is_static && is_constructor_literal)
{
JERRY_ASSERT (!is_static);
JERRY_ASSERT (opts & PARSER_CLASS_LITERAL_CTOR_PRESENT);
@@ -619,14 +689,31 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
uint32_t accessor_status_flags = PARSER_FUNCTION_CLOSURE | PARSER_ALLOW_SUPER;
accessor_status_flags |= (is_getter ? PARSER_IS_PROPERTY_GETTER : PARSER_IS_PROPERTY_SETTER);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
uint8_t ident_opts = LEXER_OBJ_IDENT_ONLY_IDENTIFIERS;
if (lexer_check_next_character (context_p, LIT_CHAR_HASHMARK))
{
lexer_next_token (context_p);
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
ident_opts |= LEXER_OBJ_IDENT_CLASS_PRIVATE;
is_private = true;
}
lexer_expect_object_literal_id (context_p, ident_opts);
if (is_private)
{
parser_check_duplicated_private_field (context_p,
is_getter ? SCANNER_PRIVATE_FIELD_GETTER : SCANNER_PRIVATE_FIELD_SETTER);
}
literal_index = context_p->lit_object.index;
if (context_p->token.type == LEXER_RIGHT_SQUARE)
{
is_computed = true;
}
else if (is_static)
else if (is_static && !is_private)
{
if (LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type)
&& lexer_compare_identifier_to_string (&context_p->token.lit_location, (uint8_t *) "prototype", 9))
@@ -667,11 +754,13 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
if (is_getter)
{
opcode = is_static ? CBC_EXT_SET_STATIC_GETTER : CBC_EXT_SET_GETTER;
opcode = is_static ? (is_private ? CBC_EXT_COLLECT_PRIVATE_STATIC_GETTER : CBC_EXT_SET_STATIC_GETTER)
: (is_private ? CBC_EXT_COLLECT_PRIVATE_GETTER : CBC_EXT_SET_GETTER);
}
else
{
opcode = is_static ? CBC_EXT_SET_STATIC_SETTER : CBC_EXT_SET_SETTER;
opcode = is_static ? (is_private ? CBC_EXT_COLLECT_PRIVATE_STATIC_SETTER : CBC_EXT_SET_STATIC_SETTER)
: (is_private ? CBC_EXT_COLLECT_PRIVATE_SETTER : CBC_EXT_SET_SETTER);
}
}
@@ -683,11 +772,16 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
}
else
{
if (is_private)
{
accessor_status_flags |= PARSER_PRIVATE_FUNCTION_NAME;
}
parser_set_function_name (context_p, function_literal_index, literal_index, accessor_status_flags);
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (opcode);
}
is_static = false;
is_private = false;
continue;
}
@@ -697,16 +791,57 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
{
status_flags |= PARSER_IS_ASYNC_FUNCTION | PARSER_DISALLOW_AWAIT_YIELD;
uint8_t ident_opts = LEXER_OBJ_IDENT_ONLY_IDENTIFIERS;
if (lexer_check_next_character (context_p, LIT_CHAR_HASHMARK))
{
lexer_next_token (context_p);
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
ident_opts |= LEXER_OBJ_IDENT_CLASS_PRIVATE;
is_private = true;
}
if (!lexer_consume_generator (context_p))
{
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
lexer_expect_object_literal_id (context_p, ident_opts);
}
if (is_private)
{
if (context_p->token.type == LEXER_LITERAL && parser_is_constructor_literal (context_p))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_PRIVATE_CONSTRUCTOR);
}
parser_check_duplicated_private_field (context_p, SCANNER_PRIVATE_FIELD_PROPERTY_GETTER_SETTER);
}
}
if (context_p->token.type == LEXER_MULTIPLY)
{
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
uint8_t ident_opts = LEXER_OBJ_IDENT_ONLY_IDENTIFIERS;
if (lexer_check_next_character (context_p, LIT_CHAR_HASHMARK))
{
lexer_next_token (context_p);
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
ident_opts |= LEXER_OBJ_IDENT_CLASS_PRIVATE;
is_private = true;
}
lexer_expect_object_literal_id (context_p, ident_opts);
status_flags |= PARSER_IS_GENERATOR_FUNCTION | PARSER_DISALLOW_AWAIT_YIELD;
if (is_private)
{
if (context_p->token.type == LEXER_LITERAL && parser_is_constructor_literal (context_p))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_PRIVATE_CONSTRUCTOR);
}
parser_check_duplicated_private_field (context_p, SCANNER_PRIVATE_FIELD_PROPERTY_GETTER_SETTER);
}
}
if (context_p->token.type == LEXER_RIGHT_SQUARE)
@@ -715,7 +850,7 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
}
else if (LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type))
{
if (is_static)
if (is_static && !is_private)
{
if (lexer_compare_identifier_to_string (&context_p->token.lit_location, (uint8_t *) "prototype", 9))
{
@@ -744,6 +879,13 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
if (!is_computed)
{
if (is_private)
{
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
uint8_t field_opcode = is_static ? CBC_EXT_COLLECT_PRIVATE_STATIC_FIELD : CBC_EXT_COLLECT_PRIVATE_FIELD;
parser_emit_cbc_ext_literal_from_token (context_p, field_opcode);
}
if (is_static && parser_is_constructor_literal (context_p))
{
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIST_EXPECTED);
@@ -825,6 +967,7 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
parser_stack_push_uint8 (context_p, class_field_type);
fields_size++;
is_static = false;
is_private = false;
continue;
}
@@ -834,7 +977,14 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
{
JERRY_ASSERT (context_p->token.lit_location.type == LEXER_IDENT_LITERAL
|| context_p->token.lit_location.type == LEXER_STRING_LITERAL);
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
if (is_private)
{
parser_resolve_private_identifier (context_p);
}
else
{
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
}
}
else
{
@@ -856,7 +1006,14 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
continue;
}
parser_set_function_name (context_p, function_literal_index, literal_index, 0);
uint32_t function_name_status_flags = 0;
if (is_private)
{
function_name_status_flags = PARSER_PRIVATE_FUNCTION_NAME;
}
parser_set_function_name (context_p, function_literal_index, literal_index, function_name_status_flags);
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL);
@@ -864,13 +1021,20 @@ parser_parse_class_body (parser_context_t *context_p, /**< context */
if (is_static)
{
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_SET_STATIC_PROPERTY_LITERAL);
context_p->last_cbc_opcode = (is_private ? PARSER_TO_EXT_OPCODE (CBC_EXT_COLLECT_PRIVATE_STATIC_METHOD)
: PARSER_TO_EXT_OPCODE (CBC_EXT_SET_STATIC_PROPERTY_LITERAL));
is_static = false;
}
else if (is_private)
{
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_COLLECT_PRIVATE_METHOD);
}
else
{
context_p->last_cbc_opcode = CBC_SET_LITERAL_PROPERTY;
}
is_private = false;
}
if (fields_size == 0)
@@ -930,12 +1094,22 @@ parser_parse_class (parser_context_t *context_p, /**< context */
uint16_t class_ident_index = PARSER_INVALID_LITERAL_INDEX;
uint16_t class_name_index = PARSER_INVALID_LITERAL_INDEX;
parser_class_literal_opts_t opts = PARSER_CLASS_LITERAL_NO_OPTS;
scanner_info_t *scanner_info_p = context_p->next_scanner_info_p;
if (context_p->next_scanner_info_p->source_p == context_p->source_p)
scanner_class_info_t *class_info_p = (scanner_class_info_t *) scanner_info_p;
parser_private_context_t private_ctx;
if (scanner_info_p->source_p == context_p->source_p)
{
JERRY_ASSERT (context_p->next_scanner_info_p->type == SCANNER_TYPE_CLASS_CONSTRUCTOR);
scanner_release_next (context_p, sizeof (scanner_info_t));
opts |= PARSER_CLASS_LITERAL_CTOR_PRESENT;
JERRY_ASSERT (scanner_info_p->type == SCANNER_TYPE_CLASS_CONSTRUCTOR);
parser_save_private_context (context_p, &private_ctx, class_info_p);
if (scanner_info_p->u8_arg & SCANNER_CONSTRUCTOR_EXPLICIT)
{
opts |= PARSER_CLASS_LITERAL_CTOR_PRESENT;
}
scanner_release_next (context_p, sizeof (scanner_class_info_t));
}
if (is_statement)
@@ -1019,6 +1193,8 @@ parser_parse_class (parser_context_t *context_p, /**< context */
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
}
context_p->private_context_p->opts |= SCANNER_PRIVATE_FIELD_ACTIVE;
/* ClassDeclaration is parsed. Continue with class body. */
bool has_static_field = parser_parse_class_body (context_p, opts, class_name_index);
@@ -1057,6 +1233,8 @@ parser_parse_class (parser_context_t *context_p, /**< context */
}
context_p->status_flags &= (uint32_t) ~PARSER_ALLOW_SUPER;
parser_restore_private_context (context_p, &private_ctx);
lexer_next_token (context_p);
} /* parser_parse_class */
#endif /* JERRY_ESNEXT */
@@ -1943,6 +2121,26 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
switch (context_p->token.type)
{
#if JERRY_ESNEXT
case LEXER_HASHMARK:
{
if (!lexer_scan_private_identifier (context_p))
{
parser_raise_error (context_p, PARSER_ERR_INVALID_CHARACTER);
}
parser_resolve_private_identifier (context_p);
lexer_next_token (context_p);
if (context_p->token.type != LEXER_KEYW_IN)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_CHARACTER);
}
parser_stack_push_uint16 (context_p, context_p->lit_object.index);
parser_stack_push_uint8 (context_p, LEXER_PRIVATE_PRIMARY_EXPR);
return false;
}
case LEXER_TEMPLATE_LITERAL:
{
if (context_p->source_p[-1] != LIT_CHAR_GRAVE_ACCENT)
@@ -2345,7 +2543,30 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
{
parser_push_result (context_p);
#if JERRY_ESNEXT
if (lexer_check_next_character (context_p, LIT_CHAR_HASHMARK))
{
lexer_next_token (context_p);
if (context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_SUPER))
{
parser_raise_error (context_p, PARSER_ERR_UNEXPECTED_PRIVATE_FIELD);
}
if (!lexer_scan_private_identifier (context_p))
{
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
}
parser_resolve_private_identifier (context_p);
parser_emit_cbc_ext_literal (context_p, CBC_EXT_PUSH_PRIVATE_PROP_LITERAL, context_p->lit_object.index);
lexer_next_token (context_p);
continue;
}
#endif /* JERRY_ESNEXT */
lexer_expect_identifier (context_p, LEXER_STRING_LITERAL);
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL
&& context_p->lit_object.literal_p->type == LEXER_STRING_LITERAL);
context_p->token.lit_location.type = LEXER_STRING_LITERAL;
@@ -2466,6 +2687,11 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_SUPER_PROP_REFERENCE);
opcode = CBC_CALL_PROP;
}
else if (context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL))
{
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL_REFERENCE);
opcode = CBC_CALL_PROP;
}
#endif /* JERRY_ESNEXT */
else if (JERRY_UNLIKELY (context_p->status_flags & PARSER_INSIDE_WITH)
&& PARSER_IS_PUSH_LITERALS_WITH_THIS (context_p->last_cbc_opcode)
@@ -2649,7 +2875,6 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
parser_emit_cbc_call (context_p, opcode, call_arguments);
continue;
}
default:
{
if (context_p->stack_top_uint8 == LEXER_KEYW_NEW)
@@ -2893,6 +3118,12 @@ parser_append_binary_single_assignment_token (parser_context_t *context_p, /**<
parser_stack_push_uint8 (context_p, CBC_EXT_ASSIGN_SUPER);
assign_opcode = CBC_EXT_OPCODE;
}
else if (context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_PRIVATE_PROP_LITERAL))
{
context_p->last_cbc_opcode = CBC_PUSH_LITERAL;
parser_stack_push_uint8 (context_p, CBC_EXT_ASSIGN_PRIVATE);
assign_opcode = CBC_EXT_OPCODE;
}
#endif /* JERRY_ESNEXT */
else
{
@@ -3068,7 +3299,8 @@ parser_process_binary_opcodes (parser_context_t *context_p, /**< context */
if (JERRY_UNLIKELY (opcode == CBC_EXT_OPCODE))
{
parser_stack_pop_uint8 (context_p);
JERRY_ASSERT (context_p->stack_top_uint8 == CBC_EXT_ASSIGN_SUPER);
JERRY_ASSERT (context_p->stack_top_uint8 == CBC_EXT_ASSIGN_SUPER
|| context_p->stack_top_uint8 == CBC_EXT_ASSIGN_PRIVATE);
opcode = PARSER_TO_EXT_OPCODE (context_p->stack_top_uint8);
parser_stack_pop_uint8 (context_p);
}
@@ -3170,6 +3402,13 @@ parser_process_binary_opcodes (parser_context_t *context_p, /**< context */
parser_set_branch_to_current_position (context_p, &branch);
continue;
}
else if (token == LEXER_KEYW_IN && context_p->stack_top_uint8 == LEXER_PRIVATE_PRIMARY_EXPR)
{
parser_stack_pop_uint8 (context_p);
uint16_t lit_id = parser_stack_pop_uint16 (context_p);
parser_emit_cbc_ext_literal (context_p, CBC_EXT_PUSH_PRIVATE_PROP_LITERAL_IN, lit_id);
continue;
}
#endif /* JERRY_ESNEXT */
else
{
+23 -1
View File
@@ -76,6 +76,8 @@ typedef enum
PARSER_INSIDE_CLASS_FIELD = (1u << 23), /**< a class field is being parsed */
PARSER_ALLOW_NEW_TARGET = (1u << 24), /**< allow new.target parsing in the current context */
PARSER_IS_METHOD = (1u << 25), /**< method is parsed */
PARSER_PRIVATE_FUNCTION_NAME = PARSER_IS_FUNC_EXPRESSION, /**< represents private method for
* parser_set_function_name*/
#endif /* JERRY_ESNEXT */
#if JERRY_MODULE_SYSTEM
PARSER_MODULE_DEFAULT_CLASS_OR_FUNC = (1u << 26), /**< parsing a function or class default export */
@@ -481,6 +483,18 @@ typedef struct
#endif /* JERRY_LINE_INFO */
#if JERRY_ESNEXT
/**
* List of private field contexts
*/
typedef struct parser_private_context_t
{
scanner_class_private_member_t *members_p; /**< current private field context members */
struct parser_private_context_t *prev_p; /**< previous private field context */
uint8_t opts; /**< options */
} parser_private_context_t;
#endif /* JERRY_ESNEXT */
/**
* Those members of a context which needs
* to be saved when a sub-function is parsed.
@@ -605,6 +619,7 @@ typedef struct
#if JERRY_ESNEXT
uint16_t scope_stack_global_end; /**< end of global declarations of a function */
ecma_value_t tagged_template_literal_cp; /**< compessed pointer to the tagged template literal collection */
parser_private_context_t *private_context_p; /**< private context */
#endif /* JERRY_ESNEXT */
uint8_t stack_top_uint8; /**< top byte stored on the stack */
@@ -763,10 +778,11 @@ bool lexer_check_yield_no_arg (parser_context_t *context_p);
bool lexer_consume_generator (parser_context_t *context_p);
bool lexer_consume_assign (parser_context_t *context_p);
void lexer_update_await_yield (parser_context_t *context_p, uint32_t status_flags);
bool lexer_scan_private_identifier (parser_context_t *context_p);
#endif /* JERRY_ESNEXT */
void lexer_parse_string (parser_context_t *context_p, lexer_string_options_t opts);
void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type);
bool lexer_scan_identifier (parser_context_t *context_p);
bool lexer_scan_identifier (parser_context_t *context_p, lexer_parse_options_t opts);
void lexer_check_property_modifier (parser_context_t *context_p);
void lexer_convert_ident_to_cesu8 (uint8_t *destination_p, const uint8_t *source_p, prop_length_t length);
@@ -812,6 +828,11 @@ void parser_parse_block_expression (parser_context_t *context_p, int options);
void parser_parse_expression_statement (parser_context_t *context_p, int options);
void parser_parse_expression (parser_context_t *context_p, int options);
#if JERRY_ESNEXT
void parser_resolve_private_identifier (parser_context_t *context_p);
void parser_save_private_context (parser_context_t *context_p,
parser_private_context_t *private_ctx_p,
scanner_class_info_t *class_info_p);
void parser_restore_private_context (parser_context_t *context_p, parser_private_context_t *private_ctx_p);
void parser_parse_class (parser_context_t *context_p, bool is_statement);
void parser_parse_initializer (parser_context_t *context_p, parser_pattern_flags_t flags);
void parser_parse_initializer_by_next_char (parser_context_t *context_p, parser_pattern_flags_t flags);
@@ -829,6 +850,7 @@ void scanner_set_active (parser_context_t *context_p);
void scanner_revert_active (parser_context_t *context_p);
void scanner_release_active (parser_context_t *context_p, size_t size);
void scanner_release_switch_cases (scanner_case_info_t *case_p);
void scanner_release_private_fields (scanner_class_private_member_t *member_p);
void scanner_seek (parser_context_t *context_p);
void scanner_reverse_info_list (parser_context_t *context_p);
void scanner_cleanup (parser_context_t *context_p);
+241 -9
View File
@@ -22,6 +22,7 @@
#include "debugger.h"
#include "jcontext.h"
#include "js-parser-internal.h"
#include "lit-char-helpers.h"
#if JERRY_PARSER
@@ -1421,6 +1422,174 @@ parser_post_processing (parser_context_t *context_p) /**< context */
#undef PARSER_NEXT_BYTE
#undef PARSER_NEXT_BYTE_UPDATE
#if JERRY_ESNEXT
/**
* Resolve private identifier in direct eval context
*/
static bool
parser_resolve_private_identifier_eval (parser_context_t *context_p) /**< context */
{
ecma_string_t *search_key_p;
uint8_t *destination_p = (uint8_t *) parser_malloc (context_p, context_p->token.lit_location.length);
lexer_convert_ident_to_cesu8 (destination_p,
context_p->token.lit_location.char_p,
context_p->token.lit_location.length);
search_key_p = ecma_new_ecma_string_from_utf8 (destination_p, context_p->token.lit_location.length);
parser_free (destination_p, context_p->token.lit_location.length);
ecma_object_t *lex_env_p = JERRY_CONTEXT (vm_top_context_p)->lex_env_p;
while (true)
{
JERRY_ASSERT (lex_env_p != NULL);
if (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_CLASS
&& (lex_env_p->type_flags_refs & ECMA_OBJECT_FLAG_LEXICAL_ENV_HAS_DATA) != 0
&& !ECMA_LEX_ENV_CLASS_IS_MODULE (lex_env_p))
{
ecma_object_t *class_object_p = ((ecma_lexical_environment_class_t *) lex_env_p)->object_p;
ecma_string_t *internal_string_p = ecma_get_internal_string (LIT_INTERNAL_MAGIC_STRING_CLASS_PRIVATE_ELEMENTS);
ecma_property_t *prop_p = ecma_find_named_property (class_object_p, internal_string_p);
if (prop_p != NULL)
{
ecma_value_t *collection_p =
ECMA_GET_INTERNAL_VALUE_POINTER (ecma_value_t, ECMA_PROPERTY_VALUE_PTR (prop_p)->value);
ecma_value_t *current_p = collection_p + 1;
ecma_value_t *end_p = ecma_compact_collection_end (collection_p);
while (current_p < end_p)
{
current_p++; /* skip kind */
ecma_string_t *private_key_p = ecma_get_prop_name_from_value (*current_p++);
current_p++; /* skip value */
JERRY_ASSERT (ecma_prop_name_is_symbol (private_key_p));
ecma_string_t *private_key_desc_p =
ecma_get_string_from_value (((ecma_extended_string_t *) private_key_p)->u.symbol_descriptor);
if (ecma_compare_ecma_strings (private_key_desc_p, search_key_p))
{
ecma_deref_ecma_string (search_key_p);
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
return true;
}
}
}
}
if (lex_env_p->u2.outer_reference_cp == JMEM_CP_NULL)
{
break;
}
lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, lex_env_p->u2.outer_reference_cp);
}
ecma_deref_ecma_string (search_key_p);
return false;
} /* parser_resolve_private_identifier_eval */
/**
* Resolve private identifier
*/
void
parser_resolve_private_identifier (parser_context_t *context_p) /**< context */
{
if ((context_p->global_status_flags & ECMA_PARSE_DIRECT_EVAL) && parser_resolve_private_identifier_eval (context_p))
{
return;
}
parser_private_context_t *context_iter_p = context_p->private_context_p;
while (context_iter_p)
{
if (context_iter_p == NULL || !(context_iter_p->opts & SCANNER_PRIVATE_FIELD_ACTIVE))
{
parser_raise_error (context_p, PARSER_ERR_UNDECLARED_PRIVATE_FIELD);
}
if (!(context_iter_p->opts & SCANNER_SUCCESSFUL_CLASS_SCAN))
{
return;
}
parser_private_context_t *private_context_p = context_iter_p;
if (private_context_p == NULL)
{
parser_raise_error (context_p, PARSER_ERR_UNDECLARED_PRIVATE_FIELD);
}
scanner_class_private_member_t *ident_iter = private_context_p->members_p;
while (ident_iter)
{
if (lexer_compare_identifiers (context_p, &context_p->token.lit_location, &ident_iter->loc))
{
lexer_construct_literal_object (context_p, &context_p->token.lit_location, LEXER_STRING_LITERAL);
return;
}
ident_iter = ident_iter->prev_p;
}
context_iter_p = context_iter_p->prev_p;
}
parser_raise_error (context_p, PARSER_ERR_UNDECLARED_PRIVATE_FIELD);
} /* parser_resolve_private_identifier */
/**
* Save private field context
*/
void
parser_save_private_context (parser_context_t *context_p, /**< context */
parser_private_context_t *private_ctx_p, /**< private context */
scanner_class_info_t *class_info_p) /**< class scanner info */
{
private_ctx_p->prev_p = context_p->private_context_p;
context_p->private_context_p = private_ctx_p;
context_p->private_context_p->members_p = class_info_p->members;
context_p->private_context_p->opts = class_info_p->info.u8_arg;
class_info_p->members = NULL;
} /* parser_save_private_context */
/**
* Release contexts private fields
*/
static void
parser_free_private_fields (parser_context_t *context_p) /**< context */
{
parser_private_context_t *iter = context_p->private_context_p;
while (iter != NULL)
{
parser_private_context_t *prev_p = iter->prev_p;
scanner_release_private_fields (iter->members_p);
iter = prev_p;
}
} /* parser_free_private_fields */
/**
* Restore contexts private fields
*/
void
parser_restore_private_context (parser_context_t *context_p, /**< context */
parser_private_context_t *private_ctx_p) /**< private context */
{
scanner_release_private_fields (context_p->private_context_p->members_p);
context_p->private_context_p = private_ctx_p->prev_p;
} /* parser_restore_private_context */
#endif /* JERRY_ESNEXT */
/**
* Free identifiers and literals.
*/
@@ -1990,6 +2159,7 @@ parser_parse_source (void *source_p, /**< source code */
#if JERRY_ESNEXT
context.scope_stack_global_end = 0;
context.tagged_template_literal_cp = JMEM_CP_NULL;
context.private_context_p = NULL;
#endif /* JERRY_ESNEXT */
#ifndef JERRY_NDEBUG
@@ -2774,14 +2944,23 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */
}
uint16_t literal_index = 0;
bool is_private = false;
if (class_field_type & PARSER_CLASS_FIELD_NORMAL)
{
scanner_set_location (context_p, &range.start_location);
uint32_t ident_opts = LEXER_OBJ_IDENT_ONLY_IDENTIFIERS;
is_private = context_p->source_p[-1] == LIT_CHAR_HASHMARK;
if (is_private)
{
ident_opts |= LEXER_OBJ_IDENT_CLASS_PRIVATE;
}
context_p->source_end_p = source_end_p;
scanner_seek (context_p);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
lexer_expect_object_literal_id (context_p, ident_opts);
literal_index = context_p->lit_object.index;
@@ -2826,7 +3005,26 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */
if (class_field_type & PARSER_CLASS_FIELD_NORMAL)
{
parser_emit_cbc_literal (context_p, CBC_ASSIGN_PROP_THIS_LITERAL, literal_index);
uint16_t function_literal_index = parser_check_anonymous_function_declaration (context_p);
if (function_literal_index == PARSER_ANONYMOUS_CLASS)
{
parser_emit_cbc_ext_literal (context_p, CBC_EXT_SET_CLASS_NAME, literal_index);
}
else if (function_literal_index < PARSER_NAMED_FUNCTION)
{
uint32_t function_name_status_flags = is_private ? PARSER_PRIVATE_FUNCTION_NAME : 0;
parser_set_function_name (context_p, function_literal_index, literal_index, function_name_status_flags);
}
if (is_private)
{
parser_emit_cbc_ext_literal (context_p, CBC_EXT_PRIVATE_FIELD_ADD, literal_index);
}
else
{
parser_emit_cbc_ext_literal (context_p, CBC_EXT_DEFINE_FIELD, literal_index);
}
/* Prepare stack slot for assignment property reference base. Needed by vm.c */
if (context_p->stack_limit == context_p->stack_depth)
@@ -2837,6 +3035,14 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */
}
else
{
uint16_t function_literal_index = parser_check_anonymous_function_declaration (context_p);
uint16_t opcode = CBC_EXT_SET_NEXT_COMPUTED_FIELD;
if (function_literal_index < PARSER_NAMED_FUNCTION || function_literal_index == PARSER_ANONYMOUS_CLASS)
{
opcode = CBC_EXT_SET_NEXT_COMPUTED_FIELD_ANONYMOUS_FUNC;
}
parser_flush_cbc (context_p);
/* The next opcode pushes two more temporary values onto the stack */
@@ -2849,7 +3055,7 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */
}
}
parser_emit_cbc_ext (context_p, CBC_EXT_SET_NEXT_COMPUTED_FIELD);
parser_emit_cbc_ext (context_p, opcode);
}
} while (!(context_p->stack_top_uint8 & PARSER_CLASS_FIELD_END));
@@ -2947,6 +3153,26 @@ parser_set_function_name (parser_context_t *context_p, /**< context */
parser_compiled_code_set_function_name (context_p, bytecode_p, name_index, status_flags);
} /* parser_set_function_name */
/**
* Prepend the given prefix into the current function name literal
*
* @return pointer to the newly allocated buffer
*/
static uint8_t *
parser_add_function_name_prefix (parser_context_t *context_p, /**< context */
const char *prefix_p, /**< prefix */
uint32_t prefix_size, /**< prefix's length */
uint32_t *name_length_p, /**< [out] function name's size */
lexer_literal_t *name_lit_p) /**< function name literal */
{
*name_length_p += prefix_size;
uint8_t *name_buffer_p = (uint8_t *) parser_malloc (context_p, *name_length_p * sizeof (uint8_t));
memcpy (name_buffer_p, prefix_p, prefix_size);
memcpy (name_buffer_p + prefix_size, name_lit_p->u.char_p, name_lit_p->prop.length);
return name_buffer_p;
} /* parser_add_function_name_prefix */
/**
* Set the function name of the given compiled code
* to the given character buffer of literal corresponds to the given name index.
@@ -2989,13 +3215,17 @@ parser_compiled_code_set_function_name (parser_context_t *context_p, /**< contex
uint8_t *name_buffer_p = (uint8_t *) name_lit_p->u.char_p;
uint32_t name_length = name_lit_p->prop.length;
if (status_flags & (PARSER_IS_PROPERTY_GETTER | PARSER_IS_PROPERTY_SETTER))
if (status_flags & PARSER_PRIVATE_FUNCTION_NAME)
{
name_length += 4;
name_buffer_p = (uint8_t *) parser_malloc (context_p, name_length * sizeof (uint8_t));
char *prefix_p = (status_flags & PARSER_IS_PROPERTY_GETTER) ? "get " : "set ";
memcpy (name_buffer_p, prefix_p, 4);
memcpy (name_buffer_p + 4, name_lit_p->u.char_p, name_lit_p->prop.length);
name_buffer_p = parser_add_function_name_prefix (context_p, "#", 1, &name_length, name_lit_p);
}
else if (status_flags & (PARSER_IS_PROPERTY_GETTER | PARSER_IS_PROPERTY_SETTER))
{
name_buffer_p = parser_add_function_name_prefix (context_p,
(status_flags & PARSER_IS_PROPERTY_GETTER) ? "get " : "set ",
4,
&name_length,
name_lit_p);
}
*func_name_start_p =
@@ -3061,6 +3291,8 @@ parser_raise_error (parser_context_t *context_p, /**< context */
}
#if JERRY_ESNEXT
parser_free_private_fields (context_p);
if (context_p->tagged_template_literal_cp != JMEM_CP_NULL)
{
ecma_collection_t *collection =
@@ -410,6 +410,7 @@ lexer_lit_location_t *scanner_add_custom_literal (parser_context_t *context_p,
lexer_lit_location_t *scanner_add_literal (parser_context_t *context_p, scanner_context_t *scanner_context_p);
void scanner_add_reference (parser_context_t *context_p, scanner_context_t *scanner_context_p);
lexer_lit_location_t *scanner_append_argument (parser_context_t *context_p, scanner_context_t *scanner_context_p);
void scanner_add_private_identifier (parser_context_t *context_p, scanner_private_field_flags_t opts);
#if JERRY_ESNEXT
void scanner_detect_invalid_var (parser_context_t *context_p,
scanner_context_t *scanner_context_p,
+74 -3
View File
@@ -331,6 +331,21 @@ scanner_release_switch_cases (scanner_case_info_t *case_p) /**< case list */
}
} /* scanner_release_switch_cases */
/**
* Release private fields.
*/
void
scanner_release_private_fields (scanner_class_private_member_t *member_p) /**< private member list */
{
while (member_p != NULL)
{
scanner_class_private_member_t *prev_p = member_p->prev_p;
jmem_heap_free_block (member_p, sizeof (scanner_class_private_member_t));
member_p = prev_p;
}
} /* scanner_release_private_fields */
/**
* Seek to correct position in the scanner info list.
*/
@@ -406,7 +421,7 @@ scanner_scope_find_lexical_declaration (parser_context_t *context_p, /**< contex
if (JERRY_LIKELY (!(literal_p->status_flags & LEXER_LIT_LOCATION_HAS_ESCAPE)))
{
name_p = parser_new_ecma_string_from_literal ((lexer_literal_t *) literal_p);
name_p = ecma_new_ecma_string_from_utf8 (literal_p->char_p, literal_p->length);
}
else
{
@@ -414,7 +429,7 @@ scanner_scope_find_lexical_declaration (parser_context_t *context_p, /**< contex
lexer_convert_ident_to_cesu8 (destination_p, literal_p->char_p, literal_p->length);
name_p = parser_new_ecma_string_from_literal ((lexer_literal_t *) literal_p);
name_p = ecma_new_ecma_string_from_utf8 (destination_p, literal_p->length);
scanner_free (destination_p, literal_p->length);
}
@@ -1535,6 +1550,46 @@ scanner_append_argument (parser_context_t *context_p, /**< context */
return literal_p;
} /* scanner_append_argument */
/**
* Add private identifiers to private ident pool
*/
void
scanner_add_private_identifier (parser_context_t *context_p, /**< context */
scanner_private_field_flags_t opts) /**< options */
{
scan_stack_modes_t stack_top = (scan_stack_modes_t) context_p->stack_top_uint8;
parser_stack_pop_uint8 (context_p);
scanner_class_info_t *class_info_p;
parser_stack_pop (context_p, &class_info_p, sizeof (scanner_class_info_t *));
scanner_class_private_member_t *iter = class_info_p->members;
scanner_private_field_flags_t search_flag =
((opts & SCANNER_PRIVATE_FIELD_PROPERTY) ? SCANNER_PRIVATE_FIELD_PROPERTY_GETTER_SETTER
: (opts & SCANNER_PRIVATE_FIELD_GETTER_SETTER));
while (iter != NULL)
{
if (lexer_compare_identifiers (context_p, &context_p->token.lit_location, &iter->loc)
&& (iter->u8_arg & search_flag))
{
scanner_raise_error (context_p);
}
iter = iter->prev_p;
}
scanner_class_private_member_t *p_member;
p_member = (scanner_class_private_member_t *) scanner_malloc (context_p, sizeof (scanner_class_private_member_t));
p_member->loc = context_p->token.lit_location;
p_member->u8_arg = (uint8_t) opts;
p_member->prev_p = class_info_p->members;
class_info_p->members = p_member;
parser_stack_push (context_p, &class_info_p, sizeof (scanner_class_info_t *));
parser_stack_push_uint8 (context_p, (uint8_t) stack_top);
} /* scanner_add_private_identifier */
/**
* Check whether an eval call is performed and update the status flags accordingly.
*/
@@ -1729,6 +1784,15 @@ scanner_push_class_declaration (parser_context_t *context_p, /**< context */
literal_pool_p->source_p = source_p;
literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_CLASS_NAME;
const uint8_t *class_source_p = scanner_context_p->active_literal_pool_p->source_p;
scanner_class_info_t *class_info_p =
(scanner_class_info_t *) scanner_insert_info (context_p, class_source_p, sizeof (scanner_class_info_t));
class_info_p->info.type = SCANNER_TYPE_CLASS_CONSTRUCTOR;
class_info_p->members = NULL;
class_info_p->info.u8_arg = SCANNER_CONSTRUCTOR_IMPLICIT;
parser_stack_push (context_p, &class_info_p, sizeof (scanner_class_info_t *));
parser_stack_push_uint8 (context_p, SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR);
scanner_context_p->mode = SCAN_MODE_CLASS_DECLARATION;
@@ -1935,12 +1999,19 @@ scanner_cleanup (parser_context_t *context_p) /**< context */
size = sizeof (scanner_switch_info_t);
break;
}
#if JERRY_ESNEXT
case SCANNER_TYPE_CLASS_CONSTRUCTOR:
{
scanner_release_private_fields (((scanner_class_info_t *) scanner_info_p)->members);
size = sizeof (scanner_class_info_t);
break;
}
#endif /* JERRY_ESNEXT */
default:
{
#if JERRY_ESNEXT
JERRY_ASSERT (
scanner_info_p->type == SCANNER_TYPE_END_ARGUMENTS || scanner_info_p->type == SCANNER_TYPE_LITERAL_FLAGS
|| scanner_info_p->type == SCANNER_TYPE_CLASS_CONSTRUCTOR
|| scanner_info_p->type == SCANNER_TYPE_LET_EXPRESSION || scanner_info_p->type == SCANNER_TYPE_ERR_REDECLARED
|| scanner_info_p->type == SCANNER_TYPE_ERR_ASYNC_FUNCTION
|| scanner_info_p->type == SCANNER_TYPE_EXPORT_MODULE_SPECIFIER);
+143 -36
View File
@@ -64,6 +64,37 @@ JERRY_STATIC_ASSERT (SCANNER_FROM_COMPUTED_TO_LITERAL_POOL (SCAN_STACK_COMPUTED_
#endif /* JERRY_ESNEXT */
/**
* Change scanner mode from primary expression to post primary expression.
*
* @return SCAN_NEXT_TOKEN to read the next token, or SCAN_KEEP_TOKEN to do nothing
*/
static scan_return_types_t
scanner_primary_to_post_primary_expression (parser_context_t *context_p, /**< context */
scanner_context_t *scanner_context_p) /* scanner context */
{
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
#if JERRY_ESNEXT
if (JERRY_UNLIKELY (context_p->stack_top_uint8 == SCAN_STACK_CLASS_FIELD_INITIALIZER
&& (context_p->status_flags & PARSER_IS_STRICT)))
{
lexer_scan_identifier (context_p, LEXER_PARSE_CHECK_KEYWORDS | LEXER_PARSE_NO_STRICT_IDENT_ERROR);
if (context_p->token.type == LEXER_LITERAL && lexer_compare_literal_to_string (context_p, "static", 6))
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
}
return SCAN_KEEP_TOKEN;
}
#else /* !JERRY_ESNEXT */
JERRY_UNUSED (context_p);
#endif /* JERRY_ESNEXT */
return SCAN_NEXT_TOKEN;
} /* scanner_primary_to_post_primary_expression */
/**
* Scan primary expression.
*
@@ -84,7 +115,7 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
#if JERRY_ESNEXT
if (scanner_try_scan_new_target (context_p))
{
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
return scanner_primary_to_post_primary_expression (context_p, scanner_context_p);
}
#endif /* JERRY_ESNEXT */
break;
@@ -93,8 +124,7 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
case LEXER_ASSIGN_DIVIDE:
{
lexer_construct_regexp_object (context_p, true);
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
return scanner_primary_to_post_primary_expression (context_p, scanner_context_p);
}
case LEXER_KEYW_FUNCTION:
{
@@ -177,6 +207,15 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
return SCAN_KEEP_TOKEN;
}
#if JERRY_ESNEXT
case LEXER_HASHMARK:
{
if (!lexer_scan_private_identifier (context_p))
{
scanner_raise_error (context_p);
}
return SCAN_KEEP_TOKEN;
}
case LEXER_TEMPLATE_LITERAL:
{
if (context_p->source_p[-1] != LIT_CHAR_GRAVE_ACCENT)
@@ -217,8 +256,7 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
lexer_lit_location_t *location_p = scanner_add_literal (context_p, scanner_context_p);
location_p->type |= (SCANNER_LITERAL_IS_USED | SCANNER_LITERAL_IS_VAR);
scanner_detect_eval_call (context_p, scanner_context_p);
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
return scanner_primary_to_post_primary_expression (context_p, scanner_context_p);
}
#endif /* JERRY_MODULE_SYSTEM */
@@ -231,15 +269,13 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
case LEXER_LIT_FALSE:
case LEXER_LIT_NULL:
{
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
return scanner_primary_to_post_primary_expression (context_p, scanner_context_p);
}
#if JERRY_ESNEXT
case LEXER_KEYW_SUPER:
{
scanner_context_p->active_literal_pool_p->status_flags |= SCANNER_LITERAL_POOL_HAS_SUPER_REFERENCE;
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
return scanner_primary_to_post_primary_expression (context_p, scanner_context_p);
}
case LEXER_KEYW_CLASS:
{
@@ -338,8 +374,7 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
}
#endif /* JERRY_ESNEXT */
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
return scanner_primary_to_post_primary_expression (context_p, scanner_context_p);
}
/* FALLTHRU */
}
@@ -366,7 +401,15 @@ scanner_scan_post_primary_expression (parser_context_t *context_p, /**< context
{
case LEXER_DOT:
{
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
#if JERRY_ESNEXT
if (context_p->token.type == LEXER_HASHMARK)
{
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
}
#endif /* JERRY_ESNEXT */
if (context_p->token.type != LEXER_LITERAL || context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
@@ -1041,7 +1084,7 @@ scanner_scan_primary_expression_end (parser_context_t *context_p, /**< context *
break;
}
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
parser_stack_pop_uint8 (context_p);
stack_top = (scan_stack_modes_t) context_p->stack_top_uint8;
@@ -2568,7 +2611,7 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
case SCAN_MODE_CLASS_BODY:
{
lexer_skip_empty_statements (context_p);
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
/* FALLTHRU */
}
case SCAN_MODE_CLASS_BODY_NO_SCAN:
@@ -2580,13 +2623,18 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
if (context_p->token.type == LEXER_RIGHT_BRACE)
{
parser_stack_pop_uint8 (context_p);
stack_top = context_p->stack_top_uint8;
scanner_class_info_t *private_members_p;
parser_stack_pop (context_p, &private_members_p, sizeof (scanner_class_info_t *));
private_members_p->info.u8_arg |= SCANNER_SUCCESSFUL_CLASS_SCAN;
scanner_pop_literal_pool (context_p, &scanner_context);
JERRY_ASSERT (stack_top == SCAN_STACK_CLASS_STATEMENT || stack_top == SCAN_STACK_CLASS_EXPRESSION);
JERRY_ASSERT (context_p->stack_top_uint8 == SCAN_STACK_CLASS_STATEMENT
|| context_p->stack_top_uint8 == SCAN_STACK_CLASS_EXPRESSION);
if (stack_top == SCAN_STACK_CLASS_STATEMENT)
if (context_p->stack_top_uint8 == SCAN_STACK_CLASS_STATEMENT)
{
/* The token is kept to disallow consuming a semicolon after it. */
scanner_context.mode = SCAN_MODE_STATEMENT_END;
@@ -2608,37 +2656,58 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
break;
}
bool is_private = false;
scanner_private_field_flags_t private_field_flags = SCANNER_PRIVATE_FIELD_PROPERTY;
if (context_p->token.type == LEXER_HASHMARK)
{
is_private = true;
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
}
bool identifier_found = false;
if (context_p->token.type == LEXER_LITERAL && LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type)
&& lexer_compare_literal_to_string (context_p, "constructor", 11))
&& lexer_compare_literal_to_string (context_p, "constructor", 11)
&& stack_top == SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR)
{
if (stack_top == SCAN_STACK_IMPLICIT_CLASS_CONSTRUCTOR)
{
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));
info_p->type = SCANNER_TYPE_CLASS_CONSTRUCTOR;
parser_stack_change_last_uint8 (context_p, SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR);
}
parser_stack_pop_uint8 (context_p);
scanner_class_info_t *private_members_p;
parser_stack_pop (context_p, &private_members_p, sizeof (scanner_class_info_t *));
private_members_p->info.u8_arg = SCANNER_CONSTRUCTOR_EXPLICIT;
parser_stack_push (context_p, &private_members_p, sizeof (scanner_class_info_t *));
parser_stack_push_uint8 (context_p, SCAN_STACK_EXPLICIT_CLASS_CONSTRUCTOR);
}
else if (lexer_token_is_identifier (context_p, "static", 6))
{
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
identifier_found = true;
private_field_flags |= SCANNER_PRIVATE_FIELD_STATIC;
}
scanner_context.mode = SCAN_MODE_FUNCTION_ARGUMENTS;
uint16_t literal_pool_flags = SCANNER_LITERAL_POOL_FUNCTION;
if (lexer_token_is_identifier (context_p, "get", 3) || lexer_token_is_identifier (context_p, "set", 3))
private_field_flags |= lexer_token_is_identifier (context_p, "get", 3) ? SCANNER_PRIVATE_FIELD_GETTER : 0;
private_field_flags |= lexer_token_is_identifier (context_p, "set", 3) ? SCANNER_PRIVATE_FIELD_SETTER : 0;
if (private_field_flags & SCANNER_PRIVATE_FIELD_GETTER_SETTER)
{
lexer_scan_identifier (context_p);
private_field_flags &= ~(uint32_t) SCANNER_PRIVATE_FIELD_PROPERTY;
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
identifier_found = true;
if (context_p->token.type == LEXER_LEFT_PAREN)
{
if (is_private)
{
private_field_flags |= SCANNER_PRIVATE_FIELD_METHOD;
scanner_add_private_identifier (context_p, private_field_flags);
}
parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PROPERTY);
scanner_push_literal_pool (context_p, &scanner_context, SCANNER_LITERAL_POOL_FUNCTION);
continue;
@@ -2646,13 +2715,19 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
}
else if (lexer_token_is_identifier (context_p, "async", 5))
{
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
identifier_found = true;
if (!(context_p->token.flags & LEXER_WAS_NEWLINE))
{
if (context_p->token.type == LEXER_LEFT_PAREN)
{
if (is_private)
{
private_field_flags |= SCANNER_PRIVATE_FIELD_METHOD;
scanner_add_private_identifier (context_p, private_field_flags);
}
parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PROPERTY);
scanner_push_literal_pool (context_p, &scanner_context, SCANNER_LITERAL_POOL_FUNCTION);
continue;
@@ -2662,19 +2737,29 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
if (context_p->token.type == LEXER_MULTIPLY)
{
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
literal_pool_flags |= SCANNER_LITERAL_POOL_GENERATOR;
}
}
}
else if (context_p->token.type == LEXER_MULTIPLY)
{
lexer_scan_identifier (context_p);
if (is_private)
{
scanner_raise_error (context_p);
}
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
literal_pool_flags |= SCANNER_LITERAL_POOL_GENERATOR;
}
if (context_p->token.type == LEXER_LEFT_SQUARE)
{
if (is_private)
{
scanner_raise_error (context_p);
}
if (literal_pool_flags != SCANNER_LITERAL_POOL_FUNCTION)
{
parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PROPERTY);
@@ -2685,9 +2770,31 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
break;
}
if (context_p->token.type == LEXER_HASHMARK)
{
if (is_private)
{
scanner_raise_error (context_p);
}
is_private = true;
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
}
if (is_private)
{
if (lexer_check_next_character (context_p, LIT_CHAR_LEFT_PAREN))
{
private_field_flags |= SCANNER_PRIVATE_FIELD_METHOD;
}
scanner_add_private_identifier (context_p, private_field_flags);
}
if (context_p->token.type == LEXER_LITERAL)
{
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
identifier_found = true;
}
@@ -3103,7 +3210,7 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (stack_top == SCAN_STACK_OBJECT_LITERAL);
if (lexer_scan_identifier (context_p))
if (lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS))
{
lexer_check_property_modifier (context_p);
}
@@ -3160,7 +3267,7 @@ scanner_scan_all (parser_context_t *context_p) /**< context */
#endif /* JERRY_ESNEXT */
parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PROPERTY);
lexer_scan_identifier (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
#if JERRY_ESNEXT
if (context_p->token.type == LEXER_LEFT_SQUARE)
@@ -3740,7 +3847,7 @@ scan_completed:
}
case SCANNER_TYPE_CLASS_CONSTRUCTOR:
{
JERRY_DEBUG_MSG (" CLASS_CONSTRUCTOR: source:%d\n", (int) (info_p->source_p - source_start_p));
JERRY_DEBUG_MSG (" CLASS: source:%d\n", (int) (info_p->source_p - source_start_p));
print_location = false;
break;
}
+46
View File
@@ -87,6 +87,25 @@ typedef struct scanner_info_t
uint16_t u16_arg; /**< custom 16-bit value */
} scanner_info_t;
/**
* Scanner info for class private field
*/
typedef struct scanner_class_private_member_t
{
lexer_lit_location_t loc; /**< loc */
uint8_t u8_arg; /**< custom 8-bit value */
struct scanner_class_private_member_t *prev_p; /**< prev private field */
} scanner_class_private_member_t;
/**
* Scanner info extended with class private fields.
*/
typedef struct
{
scanner_info_t info; /**< header */
scanner_class_private_member_t *members; /**< first private field */
} scanner_class_info_t;
/**
* Scanner info extended with a location.
*/
@@ -259,6 +278,33 @@ typedef enum
SCANNER_FUNCTION_IS_STRICT = (1 << 5), /**< function is strict */
} scanner_function_flags_t;
/**
* Constants for u8_arg flags in scanner_class_info_t.
*/
typedef enum
{
SCANNER_CONSTRUCTOR_IMPLICIT = 0, /**< implicit constructor */
SCANNER_CONSTRUCTOR_EXPLICIT = (1 << 0), /**< explicit constructor */
SCANNER_SUCCESSFUL_CLASS_SCAN = (1 << 1), /**< class scan was successful */
SCANNER_PRIVATE_FIELD_ACTIVE = (1 << 2), /**< private field is active */
} scanner_constuctor_flags_t;
/**
* Constants for u8_arg flags in scanner_class_private_member_t.
*/
typedef enum
{
SCANNER_PRIVATE_FIELD_PROPERTY = (1 << 0), /**< private field initializer */
SCANNER_PRIVATE_FIELD_METHOD = (1 << 1), /**< private field method */
SCANNER_PRIVATE_FIELD_STATIC = (1 << 2), /**< static private property */
SCANNER_PRIVATE_FIELD_GETTER = (1 << 3), /**< private field getter */
SCANNER_PRIVATE_FIELD_SETTER = (1 << 4), /**< private field setter */
SCANNER_PRIVATE_FIELD_SEEN = (1 << 5), /**< private field has already been seen */
SCANNER_PRIVATE_FIELD_IGNORED = SCANNER_PRIVATE_FIELD_METHOD | SCANNER_PRIVATE_FIELD_STATIC,
SCANNER_PRIVATE_FIELD_GETTER_SETTER = (SCANNER_PRIVATE_FIELD_GETTER | SCANNER_PRIVATE_FIELD_SETTER),
SCANNER_PRIVATE_FIELD_PROPERTY_GETTER_SETTER = (SCANNER_PRIVATE_FIELD_PROPERTY | SCANNER_PRIVATE_FIELD_GETTER_SETTER),
} scanner_private_field_flags_t;
#if JERRY_ESNEXT
/**
@@ -16,9 +16,6 @@
/* This file is automatically generated by the gen-strings.py script
* from parser-error-messages.ini. Do not edit! */
#if JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_NO_ERROR, "No error")
#endif /* JERRY_PARSER */
#if JERRY_PARSER && !(JERRY_ESNEXT)
PARSER_ERROR_DEF (PARSER_ERR_INVALID_NUMBER, "Invalid number")
#endif /* JERRY_PARSER && !(JERRY_ESNEXT) */
@@ -95,6 +92,9 @@ PARSER_ERROR_DEF (PARSER_ERR_META_EXPECTED, "Expected 'meta' keyword")
#if JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_UNEXPECTED_END, "Unexpected end of input")
#endif /* JERRY_PARSER */
#if JERRY_ESNEXT && JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_UNEXPECTED_PRIVATE_FIELD, "Unexpected private field")
#endif /* JERRY_ESNEXT && JERRY_PARSER */
#if JERRY_MODULE_SYSTEM && JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_LEFT_BRACE_MULTIPLY_EXPECTED, "Expected '{' or '*' token")
#endif /* JERRY_MODULE_SYSTEM && JERRY_PARSER */
@@ -147,6 +147,7 @@ PARSER_ERROR_DEF (PARSER_ERR_DUPLICATED_IMPORT_BINDING, "Duplicated imported bin
PARSER_ERROR_DEF (PARSER_ERR_WHILE_EXPECTED, "While expected for do-while loop")
#endif /* JERRY_PARSER */
#if JERRY_ESNEXT && JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_DELETE_PRIVATE_FIELD, "Private fields can not be deleted")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_LHS_FOR_LOOP, "Invalid left-hand-side in for-loop")
#endif /* JERRY_ESNEXT && JERRY_PARSER */
#if JERRY_PARSER
@@ -185,6 +186,7 @@ PARSER_ERROR_DEF (PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED, "Expected ',' or ']'
#if JERRY_ESNEXT && JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_ILLEGAL_PROPERTY_IN_DECLARATION, "Illegal property in declaration context")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_DESTRUCTURING_PATTERN, "Invalid destructuring assignment target")
PARSER_ERROR_DEF (PARSER_ERR_DUPLICATED_PRIVATE_FIELD, "Private field has already been declared")
#endif /* JERRY_ESNEXT && JERRY_PARSER */
#if JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_NO_ARGUMENTS_EXPECTED, "Property getters must have no arguments")
@@ -231,6 +233,7 @@ PARSER_ERROR_DEF (PARSER_ERR_NEWLINE_NOT_ALLOWED, "Newline is not allowed in str
PARSER_ERROR_DEF (PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED, "Octal numbers are not allowed in strict mode")
#endif /* JERRY_PARSER */
#if JERRY_ESNEXT && JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_CLASS_PRIVATE_CONSTRUCTOR, "Class constructor may not be a private method")
PARSER_ERROR_DEF (PARSER_ERR_FOR_AWAIT_NO_OF, "only 'of' form is allowed for for-await loops")
#endif /* JERRY_ESNEXT && JERRY_PARSER */
#if JERRY_PARSER
@@ -262,6 +265,7 @@ PARSER_ERROR_DEF (PARSER_ERR_INVALID_CONTINUE_LABEL, "Labeled statement targeted
#endif /* JERRY_PARSER */
#if JERRY_ESNEXT && JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_LEXICAL_LET_BINDING, "Let binding cannot appear in let/const declarations")
PARSER_ERROR_DEF (PARSER_ERR_UNDECLARED_PRIVATE_FIELD, "Private field must be declared in an enclosing class")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_LHS_PREFIX_OP, "Invalid left-hand side expression in prefix operation")
#endif /* JERRY_ESNEXT && JERRY_PARSER */
#if JERRY_PARSER
@@ -137,4 +137,8 @@ PARSER_ERR_VARIABLE_REDECLARED = "Local variable is redeclared"
PARSER_ERR_WHILE_EXPECTED = "While expected for do-while loop"
PARSER_ERR_WITH_NOT_ALLOWED = "With statement not allowed in strict mode"
PARSER_ERR_YIELD_NOT_ALLOWED = "Yield expression is not allowed here"
PARSER_ERR_NO_ERROR = "No error"
PARSER_ERR_DUPLICATED_PRIVATE_FIELD = "Private field has already been declared"
PARSER_ERR_UNDECLARED_PRIVATE_FIELD = "Private field must be declared in an enclosing class"
PARSER_ERR_DELETE_PRIVATE_FIELD = "Private fields can not be deleted"
PARSER_ERR_UNEXPECTED_PRIVATE_FIELD = "Unexpected private field"
PARSER_ERR_CLASS_PRIVATE_CONSTRUCTOR = "Class constructor may not be a private method"
+1
View File
@@ -32,6 +32,7 @@ typedef enum
/** @endcond */
PARSER_ERR_OUT_OF_MEMORY,
PARSER_ERR_INVALID_REGEXP,
PARSER_ERR_NO_ERROR
} parser_error_msg_t;
const lit_utf8_byte_t* parser_get_error_utf8 (uint32_t id);