Skip to content

Improve lexer_expect_object_literal_id option handling #2436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions jerry-core/parser/js/js-lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2264,13 +2264,13 @@ lexer_expect_identifier (parser_context_t *context_p, /**< context */
*/
void
lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
bool must_be_identifier) /**< only identifiers are accepted */
uint32_t ident_opts) /**< lexer_obj_ident_opts_t option bits */
{
lexer_skip_spaces (context_p);

#ifndef CONFIG_DISABLE_ES2015_CLASS
int is_class_method = ((context_p->status_flags & PARSER_IS_CLASS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this flag still used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's no longer needed so it had been removed here.

&& !must_be_identifier
int is_class_method = ((ident_opts & LEXER_OBJ_IDENT_CLASS_METHOD)
&& !(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS)
&& (context_p->token.type != LEXER_KEYW_STATIC));
#endif /* !CONFIG_DISABLE_ES2015_CLASS */

Expand All @@ -2285,7 +2285,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
{
lexer_parse_identifier (context_p, false);

if (!must_be_identifier
if (!(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS)
&& context_p->token.lit_location.length == 3)
{
lexer_skip_spaces (context_p);
Expand Down Expand Up @@ -2323,7 +2323,7 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
lexer_parse_string (context_p);
create_literal_object = true;
}
else if (!must_be_identifier && context_p->source_p[0] == LIT_CHAR_RIGHT_BRACE)
else if (!(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS) && context_p->source_p[0] == LIT_CHAR_RIGHT_BRACE)
{
context_p->token.type = LEXER_RIGHT_BRACE;
context_p->source_p += 1;
Expand Down
11 changes: 11 additions & 0 deletions jerry-core/parser/js/js-lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ typedef enum
LEXER_NO_SKIP_SPACES = (1u << 1) /**< ignore skip spaces */
} lexer_newline_flags_t;

/**
* Lexer object identifier parse options.
*/
typedef enum
{
LEXER_OBJ_IDENT_NO_OPTS = (1u << 0), /**< no options */
LEXER_OBJ_IDENT_ONLY_IDENTIFIERS = (1u << 1), /**< only identifiers are accepted */
LEXER_OBJ_IDENT_CLASS_METHOD = (1u << 2), /**< expect identifier inside a class body */
} lexer_obj_ident_opts_t;


/**
* Lexer literal object types.
*/
Expand Down
12 changes: 5 additions & 7 deletions jerry-core/parser/js/js-parser-expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
lexer_skip_empty_statements (context_p);
}

lexer_expect_object_literal_id (context_p, false);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_CLASS_METHOD);

if (context_p->token.type == LEXER_RIGHT_BRACE)
{
Expand All @@ -403,7 +403,7 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
opcode = is_static ? CBC_EXT_SET_STATIC_SETTER : CBC_EXT_SET_SETTER;
}

lexer_expect_object_literal_id (context_p, true);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_CLASS_METHOD | LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
literal_index = context_p->lit_object.index;

if (!is_static && lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
Expand Down Expand Up @@ -556,7 +556,7 @@ parser_parse_class (parser_context_t *context_p, /**< context */
bool is_strict = context_p->status_flags & PARSER_IS_STRICT;

/* 14.5. A ClassBody is always strict code. */
context_p->status_flags |= PARSER_IS_STRICT | PARSER_IS_CLASS;
context_p->status_flags |= PARSER_IS_STRICT;

/* ClassDeclaration is parsed. Continue with class body. */
parser_parse_class_literal (context_p, constructor_literal_p);
Expand Down Expand Up @@ -586,8 +586,6 @@ parser_parse_class (parser_context_t *context_p, /**< context */

parser_flush_cbc (context_p);

context_p->status_flags &= (uint32_t) ~PARSER_IS_CLASS;

if (!is_strict)
{
/* Restore flag */
Expand All @@ -612,7 +610,7 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */

while (true)
{
lexer_expect_object_literal_id (context_p, false);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_NO_OPTS);

if (context_p->token.type == LEXER_RIGHT_BRACE)
{
Expand Down Expand Up @@ -640,7 +638,7 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
item_type = PARSER_OBJECT_PROPERTY_SETTER;
}

lexer_expect_object_literal_id (context_p, true);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
literal_index = context_p->lit_object.index;

parser_append_object_literal_item (context_p, literal_index, item_type);
Expand Down
5 changes: 2 additions & 3 deletions jerry-core/parser/js/js-parser-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ typedef enum
PARSER_ARROW_PARSE_ARGS = (1u << 19), /**< parse the argument list of an arrow function */
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
#ifndef CONFIG_DISABLE_ES2015_CLASS
PARSER_IS_CLASS = (1u << 20), /**< statements parsed inside class body */
PARSER_CLASS_CONSTRUCTOR = (1u << 21), /**< a class constructor is parsed */
PARSER_CLASS_CONSTRUCTOR = (1u << 20), /**< a class constructor is parsed */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
} parser_general_flags_t;

Expand Down Expand Up @@ -447,7 +446,7 @@ void lexer_parse_string (parser_context_t *context_p);
void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type);
void lexer_scan_identifier (parser_context_t *context_p, bool propety_name);
ecma_char_t lexer_hex_to_character (parser_context_t *context_p, const uint8_t *source_p, int length);
void lexer_expect_object_literal_id (parser_context_t *context_p, bool must_be_identifier);
void lexer_expect_object_literal_id (parser_context_t *context_p, uint32_t ident_opts);
void lexer_construct_literal_object (parser_context_t *context_p, lexer_lit_location_t *literal_p,
uint8_t literal_type);
bool lexer_construct_number_object (parser_context_t *context_p, bool is_expr, bool is_negative_number);
Expand Down