Skip to content

Commit 4f2b16f

Browse files
Support for shorthand object literal notation.
JerryScript-DCO-1.0-Signed-off-by: Anthony Calandra [email protected]
1 parent 76ff084 commit 4f2b16f

File tree

4 files changed

+109
-12
lines changed

4 files changed

+109
-12
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,6 @@ lexer_check_colon (parser_context_t *context_p) /**< context */
13261326
&& context_p->source_p[0] == (uint8_t) LIT_CHAR_COLON);
13271327
} /* lexer_check_colon */
13281328

1329-
#ifndef CONFIG_DISABLE_ES2015_CLASS
13301329
/**
13311330
* Checks whether the next token is a left parenthesis.
13321331
*
@@ -1343,7 +1342,6 @@ lexer_check_left_paren (parser_context_t *context_p) /**< context */
13431342
return (context_p->source_p < context_p->source_end_p
13441343
&& context_p->source_p[0] == (uint8_t) LIT_CHAR_LEFT_PAREN);
13451344
} /* lexer_check_left_paren */
1346-
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
13471345

13481346
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
13491347

@@ -2291,7 +2289,9 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
22912289
lexer_skip_spaces (context_p);
22922290

22932291
if (context_p->source_p < context_p->source_end_p
2294-
&& context_p->source_p[0] != LIT_CHAR_COLON)
2292+
&& context_p->source_p[0] != LIT_CHAR_COLON
2293+
&& context_p->source_p[0] != LIT_CHAR_RIGHT_BRACE
2294+
&& context_p->source_p[0] != LIT_CHAR_COMMA)
22952295
{
22962296
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3))
22972297
{

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,23 +666,51 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
666666
literal_index,
667667
PARSER_OBJECT_PROPERTY_VALUE);
668668

669-
lexer_next_token (context_p);
670-
if (context_p->token.type != LEXER_COLON)
669+
if (lexer_check_colon (context_p))
671670
{
672-
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
671+
/* If there's a colon ahead, then we have { prop: val } */
672+
lexer_next_token (context_p); /* Skip colon. */
673+
lexer_next_token (context_p);
674+
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
675+
676+
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
677+
{
678+
context_p->last_cbc_opcode = CBC_SET_LITERAL_PROPERTY;
679+
context_p->last_cbc.value = literal_index;
680+
}
681+
else
682+
{
683+
parser_emit_cbc_literal (context_p, CBC_SET_PROPERTY, literal_index);
684+
}
673685
}
686+
else if (context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
687+
{
688+
/* If we have an identifier literal, then we have { prop } or { prop() {...} }
689+
(a.k.a shorthand notation) */
690+
if (lexer_check_left_paren (context_p))
691+
{
692+
parser_flush_cbc (context_p);
693+
uint32_t status_flags = PARSER_IS_FUNCTION | PARSER_IS_FUNC_EXPRESSION | PARSER_IS_CLOSURE;
694+
uint16_t function_literal_index = lexer_construct_function_object (context_p, status_flags);
695+
parser_emit_cbc_literal (context_p, CBC_PUSH_LITERAL, function_literal_index);
696+
}
697+
else
698+
{
699+
lexer_construct_literal_object (context_p,
700+
&context_p->token.lit_location,
701+
context_p->token.lit_location.type);
674702

675-
lexer_next_token (context_p);
676-
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
703+
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
704+
}
677705

678-
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
679-
{
680706
context_p->last_cbc_opcode = CBC_SET_LITERAL_PROPERTY;
681707
context_p->last_cbc.value = literal_index;
708+
lexer_next_token (context_p);
682709
}
683710
else
684711
{
685-
parser_emit_cbc_literal (context_p, CBC_SET_PROPERTY, literal_index);
712+
/* Else, we have something that isn't a valid object literal. */
713+
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
686714
}
687715
}
688716

jerry-core/parser/js/js-parser-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ void parser_set_continues_to_current_position (parser_context_t *context_p, pars
436436

437437
void lexer_next_token (parser_context_t *context_p);
438438
bool lexer_check_colon (parser_context_t *context_p);
439-
#ifndef CONFIG_DISABLE_ES2015_CLASS
440439
bool lexer_check_left_paren (parser_context_t *context_p);
440+
#ifndef CONFIG_DISABLE_ES2015_CLASS
441441
void lexer_skip_empty_statements (parser_context_t *context_p);
442442
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
443443
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var x = 1, y = 2, z = 3;
16+
var obj = { x, y, z: { z } };
17+
assert(obj.x === 1);
18+
assert(obj.y === 2);
19+
assert(obj.z.z === 3);
20+
21+
try {
22+
var a = { get, set };
23+
assert(false);
24+
} catch (e) {
25+
assert(e instanceof ReferenceError);
26+
}
27+
28+
function checkReservedAsVars() {
29+
var get = 1, set = 2;
30+
var a = { get, set };
31+
assert(a.get + a.set === 3);
32+
}
33+
checkReservedAsVars();
34+
35+
var one = 1;
36+
assert({ one, one }.one === 1);
37+
assert({ one: 0, one }.one === 1);
38+
assert({ one, one: 0 }.one === 0);
39+
40+
var obj2 = { one };
41+
assert({ obj2 }.obj2.one === 1);
42+
43+
try {
44+
var a = { true, false, null };
45+
assert(false);
46+
} catch (e) {
47+
// FIXME
48+
/*assert(e instanceof SyntaxError);*/
49+
}
50+
51+
var obj3 = { f() { return 1; } };
52+
assert(obj3.f() === 1);
53+
54+
var obj4 = { one, one() { return one; } };
55+
assert(typeof obj4.one === 'function');
56+
assert(obj4.one() === 1);
57+
58+
var obj5 = { x: 123, getX() { return this.x; } };
59+
assert(obj5.getX() === 123);
60+
61+
var obj6 = {
62+
if() { return 0; }, else() { return 1; }, try() { return 2; }, catch() { return 3; },
63+
finally() { return 4; }, let() { return 5; }, true() { return 6; }, false() { return 7; },
64+
null() { return 8; }
65+
};
66+
assert(
67+
obj6.if() + obj6.else() + obj6.try() + obj6.catch() + obj6.finally() + obj6.let() +
68+
obj6.true() + obj6.false() + obj6.null() === 36
69+
);

0 commit comments

Comments
 (0)