diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-02-17 16:04:46 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-02-17 16:04:46 +0100 |
commit | 14e46b8e225dc329f4e14777960b10abb8a09699 (patch) | |
tree | e55752bae52bf7eed38b91c42e990a8b116b6621 | |
parent | 6d7662f846e031ac52d609dc69349a1816d8e100 (diff) |
syntax: support ES2015 computed property names
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | compiler.c | 43 | ||||
-rw-r--r-- | tests/00_syntax/13_object_literals | 45 |
2 files changed, 76 insertions, 12 deletions
@@ -1620,10 +1620,12 @@ uc_compiler_compile_object(uc_compiler *compiler, bool assignable) /* parse initializer values */ do { - if (uc_compiler_parse_check(compiler, TK_RBRACE)) { + /* End of object literal */ + if (uc_compiler_parse_check(compiler, TK_RBRACE)) break; - } - else if (uc_compiler_parse_match(compiler, TK_ELLIP)) { + + /* Spread operator */ + if (uc_compiler_parse_match(compiler, TK_ELLIP)) { /* set items on stack so far... */ if (len > 0) { uc_compiler_emit_insn(compiler, compiler->parser->prev.pos, I_SOBJ); @@ -1636,7 +1638,24 @@ uc_compiler_compile_object(uc_compiler *compiler, bool assignable) /* emit merge operation */ uc_compiler_emit_insn(compiler, 0, I_MOBJ); + + continue; } + + /* Computed property name */ + if (uc_compiler_parse_match(compiler, TK_LBRACK)) { + /* parse property name expression */ + uc_compiler_parse_precedence(compiler, P_ASSIGN); + + /* cosume closing bracket and colon */ + uc_compiler_parse_consume(compiler, TK_RBRACK); + uc_compiler_parse_consume(compiler, TK_COLON); + + /* parse value expression */ + uc_compiler_parse_precedence(compiler, P_ASSIGN); + } + + /* Property/value tuple or property shorthand */ else { /* parse key expression */ if (!uc_compiler_parse_match(compiler, TK_LABEL) && @@ -1665,17 +1684,17 @@ uc_compiler_compile_object(uc_compiler *compiler, bool assignable) /* parse value expression */ uc_compiler_parse_precedence(compiler, P_ASSIGN); } + } - /* set items on stack so far... */ - if (len >= 0xfffffffe) { - uc_compiler_emit_insn(compiler, compiler->parser->prev.pos, I_SOBJ); - uc_compiler_emit_u32(compiler, 0, len); - len = 0; - } - - hint_count += 2; - len += 2; + /* set items on stack so far... */ + if (len >= 0xfffffffe) { + uc_compiler_emit_insn(compiler, compiler->parser->prev.pos, I_SOBJ); + uc_compiler_emit_u32(compiler, 0, len); + len = 0; } + + hint_count += 2; + len += 2; } while (uc_compiler_parse_match(compiler, TK_COMMA)); diff --git a/tests/00_syntax/13_object_literals b/tests/00_syntax/13_object_literals index e7926ad..18fbbed 100644 --- a/tests/00_syntax/13_object_literals +++ b/tests/00_syntax/13_object_literals @@ -127,3 +127,48 @@ In line 2, byte 14: o = { "foo" }; %} -- End -- + + +ES2015 computed property names are supported. + +-- Expect stdout -- +{ "test": true, "hello": false, "ABC": 123 } +-- End -- + +-- Testcase -- +{% + s = "test"; + o = { + [s]: true, + ["he" + "llo"]: false, + [uc("abc")]: 123 + }; + + print(o, "\n"); +%} +-- End -- + +-- Expect stderr -- +Syntax error: Expecting expression +In line 2, byte 10: + + ` o1 = { []: true };` + Near here --^ + + +Syntax error: Unexpected token +Expecting ']' +In line 3, byte 14: + + ` o2 = { [true, false]: 123 };` + Near here ------^ + + +-- End -- + +-- Testcase -- +{% + o1 = { []: true }; + o2 = { [true, false]: 123 }; +%} +-- End -- |