Skip to content

Commit a0a6eaa

Browse files
authored
Support identifier references for object initializers. (#2597)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent 3d33d32 commit a0a6eaa

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

jerry-core/parser/js/js-parser-expr.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
788788
parser_append_object_literal_item (context_p,
789789
literal_index,
790790
PARSER_OBJECT_PROPERTY_VALUE);
791+
#else /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
792+
parser_line_counter_t start_line = context_p->token.line;
793+
parser_line_counter_t start_column = context_p->token.column;
791794
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
792795

793796
lexer_next_token (context_p);
@@ -802,6 +805,35 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
802805
context_p->last_cbc.value = literal_index;
803806
break;
804807
}
808+
809+
if (context_p->token.type == LEXER_RIGHT_BRACE
810+
|| context_p->token.type == LEXER_COMMA)
811+
{
812+
/* Re-parse the literal as common identifier. */
813+
context_p->source_p = context_p->token.lit_location.char_p;
814+
context_p->line = start_line;
815+
context_p->column = start_column;
816+
817+
lexer_next_token (context_p);
818+
819+
if (context_p->token.type != LEXER_LITERAL
820+
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
821+
{
822+
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
823+
}
824+
825+
lexer_construct_literal_object (context_p,
826+
&context_p->token.lit_location,
827+
context_p->token.lit_location.type);
828+
829+
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
830+
831+
context_p->last_cbc_opcode = CBC_SET_LITERAL_PROPERTY;
832+
context_p->last_cbc.value = literal_index;
833+
834+
lexer_next_token (context_p);
835+
break;
836+
}
805837
#endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
806838

807839
if (context_p->token.type != LEXER_COLON)

jerry-core/parser/js/js-parser-scanner.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,18 @@ parser_scan_until (parser_context_t *context_p, /**< context */
959959
mode = SCAN_MODE_FUNCTION_ARGUMENTS;
960960
continue;
961961
}
962+
963+
if (context_p->token.type == LEXER_COMMA)
964+
{
965+
continue;
966+
}
967+
968+
if (context_p->token.type == LEXER_RIGHT_BRACE)
969+
{
970+
parser_stack_pop_uint8 (context_p);
971+
mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
972+
break;
973+
}
962974
#endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
963975

964976
if (context_p->token.type != LEXER_COLON)

tests/jerry/es2015/object-initializer.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,39 @@ default:
2727

2828
assert(o.func() === 244);
2929
assert(o.ab() === 446);
30+
31+
switch (1) {
32+
default:
33+
var ab = 5;
34+
var cd = 6;
35+
o = {
36+
ab,
37+
cd: 8,
38+
cd
39+
}
40+
}
41+
42+
assert(o.ab === 5);
43+
assert(o.cd === 6);
44+
45+
function exception_expected(str) {
46+
try {
47+
eval(str);
48+
assert(false);
49+
} catch (e) {
50+
assert(e instanceof SyntaxError);
51+
}
52+
}
53+
54+
// These forms are invalid.
55+
exception_expected('({ true })');
56+
exception_expected('({ 13 })');
57+
exception_expected('({ "x" })');
58+
59+
switch (1) {
60+
default:
61+
// These forms are valid.
62+
({ true: true });
63+
({ 13: 13 });
64+
({ "x": "x" });
65+
}

0 commit comments

Comments
 (0)