Skip to content

Commit f54f2d3

Browse files
authored
Implement optional chaining (jerryscript-project#5164)
The work is based on PR jerryscript-project#4843, only fixed some conflicts and cppcheck errors. Co-authored-by: Robert Fancsik [email protected] JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi [email protected]
1 parent e9f08a7 commit f54f2d3

14 files changed

+977
-377
lines changed

jerry-core/parser/js/byte-code.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ JERRY_STATIC_ASSERT (offsetof (cbc_uint8_arguments_t, script_value) == offsetof
2828
* whenever new bytecodes are introduced or existing ones have been deleted.
2929
*/
3030
JERRY_STATIC_ASSERT (CBC_END == 238, number_of_cbc_opcodes_changed);
31-
JERRY_STATIC_ASSERT (CBC_EXT_END == 167, number_of_cbc_ext_opcodes_changed);
31+
JERRY_STATIC_ASSERT (CBC_EXT_END == 170, number_of_cbc_ext_opcodes_changed);
3232

3333
/** \addtogroup parser Parser
3434
* @{

jerry-core/parser/js/byte-code.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,9 @@
497497
CBC_FORWARD_BRANCH (CBC_EXT_DEFAULT_INITIALIZER, -1, VM_OC_DEFAULT_INITIALIZER) \
498498
CBC_OPCODE (CBC_EXT_ERROR, CBC_NO_FLAG, 0, VM_OC_ERROR) \
499499
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_IF_NULLISH, -1, VM_OC_BRANCH_IF_NULLISH) \
500-
\
501-
/* Basic opcodes. */ \
502500
CBC_OPCODE (CBC_EXT_POP_REFERENCE, CBC_NO_FLAG, -2, VM_OC_POP_REFERENCE) \
501+
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_OPTIONAL_CHAIN, 0, VM_OC_BRANCH_OPTIONAL_CHAIN) \
502+
/* Basic opcodes. */ \
503503
CBC_OPCODE (CBC_EXT_CREATE_ARGUMENTS, CBC_HAS_LITERAL_ARG, 0, VM_OC_CREATE_ARGUMENTS) \
504504
CBC_OPCODE (CBC_EXT_CREATE_VAR_EVAL, CBC_HAS_LITERAL_ARG, 0, VM_OC_EXT_VAR_EVAL) \
505505
CBC_OPCODE (CBC_EXT_CREATE_VAR_FUNC_EVAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, VM_OC_EXT_VAR_EVAL) \

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

+30
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,29 @@ lexer_skip_spaces (parser_context_t *context_p) /**< context */
381381
}
382382
} /* lexer_skip_spaces */
383383

384+
/**
385+
* Checks the next token start character.
386+
*
387+
* @return LIT_INVALID_CP - if there is no more characters to read
388+
* next byte - otherwise
389+
*/
390+
lit_code_point_t
391+
lexer_peek_next_character (parser_context_t *context_p) /**< context */
392+
{
393+
if (!(context_p->token.flags & LEXER_NO_SKIP_SPACES))
394+
{
395+
lexer_skip_spaces (context_p);
396+
context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES);
397+
}
398+
399+
if (context_p->source_p < context_p->source_end_p)
400+
{
401+
return context_p->source_p[0];
402+
}
403+
404+
return LIT_INVALID_CP;
405+
} /* lexer_check_next_character */
406+
384407
/**
385408
* Skip all the continuous empty statements.
386409
*/
@@ -1738,6 +1761,13 @@ lexer_next_token (parser_context_t *context_p) /**< context */
17381761
length = 2;
17391762
break;
17401763
}
1764+
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_DOT
1765+
&& (length < 3 || !lit_char_is_decimal_digit (context_p->source_p[2])))
1766+
{
1767+
context_p->token.type = LEXER_QUESTION_MARK_DOT;
1768+
length = 2;
1769+
break;
1770+
}
17411771
}
17421772

17431773
context_p->token.type = LEXER_QUESTION_MARK;

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

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ typedef enum
154154
LEXER_RIGHT_PAREN, /**< ")" */
155155
LEXER_RIGHT_SQUARE, /**< "]" */
156156
LEXER_DOT, /**< "." */
157+
LEXER_QUESTION_MARK_DOT, /**< "?." */
157158
LEXER_SEMICOLON, /**< ";" */
158159
LEXER_COLON, /**< ":" */
159160
LEXER_COMMA, /**< "," */

0 commit comments

Comments
 (0)