Implement optional chaining (#5164)

The work is based on PR #4843, only fixed some conflicts and cppcheck errors.

Co-authored-by: Robert Fancsik robert.fancsik@h-lab.eu
JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi gergocs@inf.u-szeged.hu
This commit is contained in:
Gergo Csizi 2024-11-20 11:57:58 +01:00 committed by GitHub
parent e9f08a7879
commit f54f2d3a7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 1012 additions and 412 deletions

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 == 167, number_of_cbc_ext_opcodes_changed);
JERRY_STATIC_ASSERT (CBC_EXT_END == 170, number_of_cbc_ext_opcodes_changed);
/** \addtogroup parser Parser
* @{

View File

@ -497,9 +497,9 @@
CBC_FORWARD_BRANCH (CBC_EXT_DEFAULT_INITIALIZER, -1, VM_OC_DEFAULT_INITIALIZER) \
CBC_OPCODE (CBC_EXT_ERROR, CBC_NO_FLAG, 0, VM_OC_ERROR) \
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_IF_NULLISH, -1, VM_OC_BRANCH_IF_NULLISH) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_POP_REFERENCE, CBC_NO_FLAG, -2, VM_OC_POP_REFERENCE) \
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_OPTIONAL_CHAIN, 0, VM_OC_BRANCH_OPTIONAL_CHAIN) \
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_CREATE_ARGUMENTS, CBC_HAS_LITERAL_ARG, 0, VM_OC_CREATE_ARGUMENTS) \
CBC_OPCODE (CBC_EXT_CREATE_VAR_EVAL, CBC_HAS_LITERAL_ARG, 0, VM_OC_EXT_VAR_EVAL) \
CBC_OPCODE (CBC_EXT_CREATE_VAR_FUNC_EVAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, VM_OC_EXT_VAR_EVAL) \

View File

@ -381,6 +381,29 @@ lexer_skip_spaces (parser_context_t *context_p) /**< context */
}
} /* lexer_skip_spaces */
/**
* Checks the next token start character.
*
* @return LIT_INVALID_CP - if there is no more characters to read
* next byte - otherwise
*/
lit_code_point_t
lexer_peek_next_character (parser_context_t *context_p) /**< context */
{
if (!(context_p->token.flags & LEXER_NO_SKIP_SPACES))
{
lexer_skip_spaces (context_p);
context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES);
}
if (context_p->source_p < context_p->source_end_p)
{
return context_p->source_p[0];
}
return LIT_INVALID_CP;
} /* lexer_check_next_character */
/**
* Skip all the continuous empty statements.
*/
@ -1738,6 +1761,13 @@ lexer_next_token (parser_context_t *context_p) /**< context */
length = 2;
break;
}
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_DOT
&& (length < 3 || !lit_char_is_decimal_digit (context_p->source_p[2])))
{
context_p->token.type = LEXER_QUESTION_MARK_DOT;
length = 2;
break;
}
}
context_p->token.type = LEXER_QUESTION_MARK;

View File

@ -154,6 +154,7 @@ typedef enum
LEXER_RIGHT_PAREN, /**< ")" */
LEXER_RIGHT_SQUARE, /**< "]" */
LEXER_DOT, /**< "." */
LEXER_QUESTION_MARK_DOT, /**< "?." */
LEXER_SEMICOLON, /**< ";" */
LEXER_COLON, /**< ":" */
LEXER_COMMA, /**< "," */

File diff suppressed because it is too large Load Diff

View File

@ -736,6 +736,7 @@ bool lexer_check_next_character (parser_context_t *context_p, lit_utf8_byte_t ch
bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1, lit_utf8_byte_t character2);
uint8_t lexer_consume_next_character (parser_context_t *context_p);
bool lexer_check_post_primary_exp (parser_context_t *context_p);
lit_code_point_t lexer_peek_next_character (parser_context_t *context_p);
void lexer_skip_empty_statements (parser_context_t *context_p);
bool lexer_check_arrow (parser_context_t *context_p);
bool lexer_check_arrow_param (parser_context_t *context_p);

View File

@ -354,6 +354,29 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
return SCAN_NEXT_TOKEN;
} /* scanner_scan_primary_expression */
/**
* Consume the ?. token
*
* @return token type to continue the post primary expression parsing
*/
static lexer_token_type_t
scanner_consume_optional_chain (parser_context_t *context_p) /**< context */
{
switch (lexer_peek_next_character (context_p))
{
case LIT_CHAR_LEFT_PAREN:
case LIT_CHAR_LEFT_SQUARE:
{
lexer_next_token (context_p);
return context_p->token.type;
}
default:
{
return LEXER_DOT;
}
}
} /* scanner_consume_optional_chain */
/**
* Scan the tokens after the primary expression.
*
@ -365,75 +388,84 @@ scanner_scan_post_primary_expression (parser_context_t *context_p, /**< context
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top) /**< current stack top */
{
switch (type)
while (true)
{
case LEXER_DOT:
switch (type)
{
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
if (context_p->token.type == LEXER_HASHMARK)
case LEXER_QUESTION_MARK_DOT:
{
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
type = scanner_consume_optional_chain (context_p);
continue;
}
if (context_p->token.type != LEXER_LITERAL || context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
case LEXER_DOT:
{
scanner_raise_error (context_p);
lexer_scan_identifier (context_p, LEXER_PARSE_NO_OPTS);
if (context_p->token.type == LEXER_HASHMARK)
{
context_p->token.flags |= LEXER_NO_SKIP_SPACES;
lexer_next_token (context_p);
}
if (context_p->token.type != LEXER_LITERAL || context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
scanner_raise_error (context_p);
}
return true;
}
return true;
}
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_TEMPLATE_LITERAL:
{
if (JERRY_UNLIKELY (context_p->source_p[-1] != LIT_CHAR_GRAVE_ACCENT))
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
parser_stack_push_uint8 (context_p, SCAN_STACK_TAGGED_TEMPLATE_LITERAL);
return true;
}
return true;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PROPERTY_ACCESSOR);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_INCREASE:
case LEXER_DECREASE:
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
if (context_p->token.flags & LEXER_WAS_NEWLINE)
case LEXER_TEMPLATE_LITERAL:
{
return false;
if (JERRY_UNLIKELY (context_p->source_p[-1] != LIT_CHAR_GRAVE_ACCENT))
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
parser_stack_push_uint8 (context_p, SCAN_STACK_TAGGED_TEMPLATE_LITERAL);
}
return true;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PROPERTY_ACCESSOR);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_INCREASE:
case LEXER_DECREASE:
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
lexer_next_token (context_p);
type = (lexer_token_type_t) context_p->token.type;
if (context_p->token.flags & LEXER_WAS_NEWLINE)
{
return false;
}
if (type != LEXER_QUESTION_MARK)
lexer_next_token (context_p);
type = (lexer_token_type_t) context_p->token.type;
if (type != LEXER_QUESTION_MARK)
{
break;
}
/* FALLTHRU */
}
case LEXER_QUESTION_MARK:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
default:
{
break;
}
/* FALLTHRU */
}
case LEXER_QUESTION_MARK:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_EXPRESSION);
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
default:
{
break;
}
break;
}
if (LEXER_IS_BINARY_OP_TOKEN (type) && (type != LEXER_KEYW_IN || !SCANNER_IS_FOR_START (stack_top)))

View File

@ -169,6 +169,7 @@ PARSER_ERROR_DEF (PARSER_ERR_IMPORT_META_REQUIRE_MODULE, "Cannot use 'import.met
#if JERRY_PARSER
PARSER_ERROR_DEF (PARSER_ERR_INVALID_IDENTIFIER_PART, "Character cannot be part of an identifier")
PARSER_ERROR_DEF (PARSER_ERR_EVAL_CANNOT_ASSIGNED, "Eval cannot be assigned to in strict mode")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_TAGGED_TEMPLATE_OPTIONAL_CHAIN, "Invalid tagged template on optional chain")
PARSER_ERROR_DEF (PARSER_ERR_WITH_NOT_ALLOWED, "With statement not allowed in strict mode")
PARSER_ERROR_DEF (PARSER_ERR_NEW_TARGET_NOT_ALLOWED, "new.target expression is not allowed here")
PARSER_ERROR_DEF (PARSER_ERR_INVALID_IDENTIFIER_START, "Character cannot be start of an identifier")

View File

@ -140,3 +140,4 @@ PARSER_ERR_UNDECLARED_PRIVATE_FIELD = "Private field must be declared in an encl
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"
PARSER_ERR_INVALID_TAGGED_TEMPLATE_OPTIONAL_CHAIN = "Invalid tagged template on optional chain"

View File

@ -3174,9 +3174,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
case VM_OC_EVAL:
{
JERRY_CONTEXT (status_flags) |= ECMA_STATUS_DIRECT_EVAL;
JERRY_ASSERT ((*byte_code_p >= CBC_CALL && *byte_code_p <= CBC_CALL2_PROP_BLOCK)
|| (*byte_code_p == CBC_EXT_OPCODE && byte_code_p[1] >= CBC_EXT_SPREAD_CALL
&& byte_code_p[1] <= CBC_EXT_SPREAD_CALL_PROP_BLOCK));
continue;
}
case VM_OC_CALL:
@ -3307,6 +3304,31 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
ecma_fast_free_value (value);
continue;
}
case VM_OC_BRANCH_OPTIONAL_CHAIN:
{
left_value = stack_top_p[-1];
bool pop_reference = byte_code_p[0] == CBC_EXT_OPCODE && byte_code_p[1] == CBC_EXT_POP_REFERENCE;
if (!(ecma_is_value_null (left_value) || ecma_is_value_undefined (left_value)))
{
if (pop_reference)
{
byte_code_p += 2;
}
continue;
}
stack_top_p[-1] = ECMA_VALUE_UNDEFINED;
byte_code_p = byte_code_start_p + branch_offset;
if (!pop_reference)
{
continue;
}
/* FALLTHRU */
}
case VM_OC_POP_REFERENCE:
{
ecma_free_value (stack_top_p[-2]);

View File

@ -165,6 +165,7 @@ typedef enum
VM_OC_JUMP, /**< jump */
VM_OC_BRANCH_IF_NULLISH, /** branch if undefined or null */
VM_OC_BRANCH_OPTIONAL_CHAIN, /** branch if undefined or null and adjust stack */
VM_OC_POP_REFERENCE, /** prop identifier or property reference from the stack */
VM_OC_BRANCH_IF_STRICT_EQUAL, /**< branch if strict equal */

View File

@ -0,0 +1,167 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function expectSyntaxError(str) {
try {
eval(str);
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}
}
function expectTypeError(cb) {
try {
cb();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
}
expectSyntaxError("this?.a``");
expectSyntaxError("this?.['a']``");
expectSyntaxError("this?.``");
expectSyntaxError("this.a?.``");
expectSyntaxError("this['a']?.``");
expectSyntaxError("this?.a = 9");
expectSyntaxError("this.a.a.a.a.a?.a = 9");
expectSyntaxError("this?.a.a.a.a.a.a = 9");
expectSyntaxError("this?.a++");
expectSyntaxError("this?.a.a.a.a.a.a++");
expectSyntaxError("this.a.a.a.a.?a.a++");
expectSyntaxError("this?.a--");
expectSyntaxError("this?.a.a.a.a.a.a--");
expectSyntaxError("this.a.a.a.a.?a.a--");
var o = {
a: 4.1,
b() {
return 4.2;
},
c: {
a: 4.3,
b() {
return 4.4
}
},
e(...args) {
return args.reduce((p, c) => p + c);
}
}
assert(o?.a === 4.1);
assert(o?.a2 === undefined);
assert(this.o?.a === 4.1);
assert(this.o?.a2 === undefined);
assert(this.o?.['a'] === 4.1);
assert(this.o?.['a2'] === undefined);
assert(typeof o?.a === 'number');
assert(typeof o?.a2 === 'undefined');
assert(o?.c?.a === 4.3);
assert(o?.c?.a2 === undefined);
assert(this.o?.c?.a === 4.3);
assert(this.o?.c?.a2 === undefined);
assert(this.o?.c?.['a'] === 4.3);
assert(this.o?.c?.['a2'] === undefined);
assert(typeof o?.c?.a === 'number');
assert(typeof o?.c?.a2 === 'undefined');
assert(o?.d === undefined);
assert(o?.d?.d === undefined);
assert(o?.d?.d?.['d'] === undefined);
assert(o?.b?.() === 4.2);
assert(o?.b2?.() === undefined);
assert(o?.c?.b?.() === 4.4);
assert(o?.c?.b2?.() === undefined);
assert(o?.e(...[1.25, 2.25, 3.25]) === 6.75);
assert(o?.e(...[1.25, 2.25, 3.25], ...[0.25]) === 7);
assert(o?.['e'](...[1.25, 2.25, 3.25]) === 6.75);
assert(o?.['e'](...[1.25, 2.25, 3.25], ...[0.25]) === 7);
assert(o?.e?.(...[1.25, 2.25, 3.25]) === 6.75);
assert(o?.e?.(...[1.25, 2.25, 3.25], ...[0.25]) === 7);
assert(o?.['e']?.(...[1.25, 2.25, 3.25]) === 6.75);
assert(o?.['e']?.(...[1.25, 2.25, 3.25], ...[0.25]) === 7);
// Test short circuit
let count = 0;
assert(undefined?.[count++] === undefined);
assert(undefined?.[count++]() === undefined);
assert(undefined?.[count++]()() === undefined);
assert(null?.[count++] === undefined);
assert(null?.[count++]() === undefined);
assert(null?.[count++]()() === undefined);
assert(count === 0);
// Test optional call
var g = undefined;
function f () {
return 4.5;
}
assert(g?.() === undefined);
assert(this?.g?.() === undefined);
assert(this.g?.() === undefined);
expectTypeError(_ => {
this.g();
});
assert(f?.() === 4.5);
assert(this?.f?.() === 4.5);
assert(this.f?.() === 4.5);
assert(f() === 4.5);
// test direct eval
var a = 5.1;
eval('a = 5.2');
assert(a === 5.2);
eval?.('a = 5.3');
assert(a === 5.3);
eval?.(...['a = 5.4']);
assert(a === 5.4);
const saved_eval = eval;
eval = undefined;
eval?.('a = 5.5')
assert(a === 5.4);
eval?.(...['a = 5.5'])
assert(a === 5.4);
eval = saved_eval;
// test optional private property access
class A {
#a = 5.1;
test(o) {
return o?.#a;
}
}
let instance = new A;
assert(instance.test(instance) === 5.1);
assert(instance.test(undefined) === undefined);
assert(instance.test(null) === undefined);
expectTypeError(_ => {
instance.test({});
});

View File

@ -2003,16 +2003,12 @@
<test id="language/expressions/class/elements/async-gen-private-method/yield-promise-reject-next.js"><reason></reason></test>
<test id="language/expressions/class/elements/class-name-static-initializer-anonymous.js"><reason></reason></test>
<test id="language/expressions/class/elements/class-name-static-initializer-default-export.js"><reason></reason></test>
<test id="language/expressions/class/elements/grammar-private-field-optional-chaining.js"><reason></reason></test>
<test id="language/expressions/class/elements/private-field-after-optional-chain.js"><reason></reason></test>
<test id="language/statements/class/elements/async-gen-private-method-static/yield-promise-reject-next-catch.js"><reason></reason></test>
<test id="language/statements/class/elements/async-gen-private-method-static/yield-promise-reject-next-for-await-of-async-iterator.js"><reason></reason></test>
<test id="language/statements/class/elements/async-gen-private-method-static/yield-promise-reject-next.js"><reason></reason></test>
<test id="language/statements/class/elements/async-gen-private-method/yield-promise-reject-next-catch.js"><reason></reason></test>
<test id="language/statements/class/elements/async-gen-private-method/yield-promise-reject-next-for-await-of-async-iterator.js"><reason></reason></test>
<test id="language/statements/class/elements/async-gen-private-method/yield-promise-reject-next.js"><reason></reason></test>
<test id="language/statements/class/elements/grammar-private-field-optional-chaining.js"><reason></reason></test>
<test id="language/statements/class/elements/private-field-after-optional-chain.js"><reason></reason></test>
<!-- END - ESNext stage 3 proposals: class fields, private methods and static class features -->
<!-- ESNext stage 3 proposal: top level await
@ -2512,37 +2508,6 @@
<test id="language/statements/while/tco-body.js"><reason></reason></test>
<!-- END - ES2015: Proper Tail Call (PTC) Optimization -->
<!-- ES2020: Optional Chaining
features: [optional-chaining]
https://github.com/tc39/proposal-optional-chaining
-->
<test id="language/expressions/optional-chaining/call-expression.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/eval-optional-call.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/iteration-statement-do.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/iteration-statement-for-await-of.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/iteration-statement-for-in.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/iteration-statement-for-of-type-error.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/iteration-statement-for.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/iteration-statement-while.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/member-expression-async-identifier.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/member-expression-async-literal.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/member-expression-async-this.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/member-expression.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/new-target-optional-call.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-call-preserves-this.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain-async-optional-chain-square-brackets.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain-async-square-brackets.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain-expression-optional-expression.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain-prod-arguments.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain-prod-expression.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain-prod-identifiername.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-chain.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/optional-expression.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/runtime-semantics-evaluation.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/short-circuiting.js"><reason></reason></test>
<test id="language/expressions/optional-chaining/super-property-optional-call.js"><reason></reason></test>
<!-- END - ES2020: Optional Chaining -->
<!-- ES2017: Shared Memory and Atomics
features: [Atomics]
https://github.com/tc39/ecmascript_sharedmem

View File

@ -31,7 +31,6 @@ shiftNegativeLHS:jerry-math/*.c
shiftTooManyBits:jerry-core/*.c
shiftTooManyBitsSigned:jerry-math/*.c
signConversionCond:jerry-core/*.c
uninitvar:jerry-core/parser/js/js-parser-expr.c:3423
uninitvar:tests/unit-core/test-api-objecttype.c:119
unmatchedSuppression:jerry-core/*.inc.h
unreadVariable:jerry-core/*.c