Skip to content

Commit d69ac0e

Browse files
author
Robert Fancsik
authored
Implement logical assignment operators (jerryscript-project#4834)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 70e275e commit d69ac0e

File tree

9 files changed

+656
-222
lines changed

9 files changed

+656
-222
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 == 165, number_of_cbc_ext_opcodes_changed);
31+
JERRY_STATIC_ASSERT (CBC_EXT_END == 166, number_of_cbc_ext_opcodes_changed);
3232

3333
#if JERRY_PARSER || JERRY_PARSER_DUMP_BYTE_CODE
3434

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

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@
506506
CBC_FORWARD_BRANCH (CBC_EXT_BRANCH_IF_NULLISH, -1, VM_OC_BRANCH_IF_NULLISH) \
507507
\
508508
/* Basic opcodes. */ \
509+
CBC_OPCODE (CBC_EXT_POP_REFERENCE, CBC_NO_FLAG, -2, VM_OC_POP_REFERENCE) \
509510
CBC_OPCODE (CBC_EXT_CREATE_ARGUMENTS, CBC_HAS_LITERAL_ARG, 0, VM_OC_CREATE_ARGUMENTS) \
510511
CBC_OPCODE (CBC_EXT_CREATE_VAR_EVAL, CBC_HAS_LITERAL_ARG, 0, VM_OC_EXT_VAR_EVAL) \
511512
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

+63-4
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,55 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
15091509
break; \
15101510
}
15111511

1512+
/**
1513+
* Four tokens, where the first is the prefix of the other three
1514+
* and the second is prefix of the fourth (e.g. &, &&, &=, &&= ).
1515+
*
1516+
* @param char1 first character
1517+
* @param type1 type of the first character
1518+
* @param char2 second character
1519+
* @param type2 type of the second character
1520+
* @param char3 third character
1521+
* @param type3 type of the third character
1522+
* @param char4 fourth character
1523+
* @param type4 type of the fourth character
1524+
*/
1525+
#if JERRY_ESNEXT
1526+
#define LEXER_TYPE_D_TOKEN(char1, type1, char2, type2, char3, type3, char4, type4) \
1527+
case (uint8_t) (char1): \
1528+
{ \
1529+
if (length >= 2) \
1530+
{ \
1531+
if (context_p->source_p[1] == (uint8_t) (char2)) \
1532+
{ \
1533+
context_p->token.type = (type2); \
1534+
length = 2; \
1535+
break; \
1536+
} \
1537+
\
1538+
if (context_p->source_p[1] == (uint8_t) (char3)) \
1539+
{ \
1540+
if (length >= 3 && context_p->source_p[2] == (uint8_t) (char4)) \
1541+
{ \
1542+
context_p->token.type = (type4); \
1543+
length = 3; \
1544+
break; \
1545+
} \
1546+
context_p->token.type = (type3); \
1547+
length = 2; \
1548+
break; \
1549+
} \
1550+
} \
1551+
\
1552+
context_p->token.type = (type1); \
1553+
length = 1; \
1554+
break; \
1555+
}
1556+
#else /* !JERRY_ESNEXT */
1557+
#define LEXER_TYPE_D_TOKEN(char1, type1, char2, type2, char3, type3, char4, type4) \
1558+
LEXER_TYPE_C_TOKEN (char1, type1, char2, type2, char3, type3)
1559+
#endif /* JERRY_ESNEXT */
1560+
15121561
/**
15131562
* Get next token.
15141563
*/
@@ -1759,18 +1808,22 @@ lexer_next_token (parser_context_t *context_p) /**< context */
17591808
LEXER_TYPE_B_TOKEN (LIT_CHAR_SLASH, LEXER_DIVIDE, LIT_CHAR_EQUALS, LEXER_ASSIGN_DIVIDE)
17601809
LEXER_TYPE_B_TOKEN (LIT_CHAR_PERCENT, LEXER_MODULO, LIT_CHAR_EQUALS, LEXER_ASSIGN_MODULO)
17611810

1762-
LEXER_TYPE_C_TOKEN (LIT_CHAR_AMPERSAND,
1811+
LEXER_TYPE_D_TOKEN (LIT_CHAR_AMPERSAND,
17631812
LEXER_BIT_AND,
17641813
LIT_CHAR_EQUALS,
17651814
LEXER_ASSIGN_BIT_AND,
17661815
LIT_CHAR_AMPERSAND,
1767-
LEXER_LOGICAL_AND)
1768-
LEXER_TYPE_C_TOKEN (LIT_CHAR_VLINE,
1816+
LEXER_LOGICAL_AND,
1817+
LIT_CHAR_EQUALS,
1818+
LEXER_ASSIGN_LOGICAL_AND)
1819+
LEXER_TYPE_D_TOKEN (LIT_CHAR_VLINE,
17691820
LEXER_BIT_OR,
17701821
LIT_CHAR_EQUALS,
17711822
LEXER_ASSIGN_BIT_OR,
17721823
LIT_CHAR_VLINE,
1773-
LEXER_LOGICAL_OR)
1824+
LEXER_LOGICAL_OR,
1825+
LIT_CHAR_EQUALS,
1826+
LEXER_ASSIGN_LOGICAL_OR)
17741827

17751828
LEXER_TYPE_B_TOKEN (LIT_CHAR_CIRCUMFLEX, LEXER_BIT_XOR, LIT_CHAR_EQUALS, LEXER_ASSIGN_BIT_XOR)
17761829

@@ -1782,6 +1835,12 @@ lexer_next_token (parser_context_t *context_p) /**< context */
17821835
{
17831836
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_QUESTION)
17841837
{
1838+
if (length >= 3 && context_p->source_p[2] == (uint8_t) LIT_CHAR_EQUALS)
1839+
{
1840+
context_p->token.type = LEXER_ASSIGN_NULLISH_COALESCING;
1841+
length = 3;
1842+
break;
1843+
}
17851844
context_p->token.type = LEXER_NULLISH_COALESCING;
17861845
length = 2;
17871846
break;

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

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ typedef enum
123123
LEXER_ASSIGN_MODULO, /**< "%=" (prec: 3) */
124124
#if JERRY_ESNEXT
125125
LEXER_ASSIGN_EXPONENTIATION, /**< "**=" (prec: 3) */
126+
LEXER_ASSIGN_NULLISH_COALESCING, /**< "??=" (prec: 3) */
127+
LEXER_ASSIGN_LOGICAL_OR, /**< "||=" (prec: 3) */
128+
LEXER_ASSIGN_LOGICAL_AND, /**< "&&=" (prec: 3) */
126129
#endif /* JERRY_ESNEXT */
127130
LEXER_ASSIGN_LEFT_SHIFT, /**< "<<=" (prec: 3) */
128131
LEXER_ASSIGN_RIGHT_SHIFT, /**< ">>=" (prec: 3) */
@@ -217,6 +220,7 @@ typedef enum
217220
LEXER_ASSIGN_CONST, /**< a const binding is reassigned */
218221
LEXER_INVALID_PATTERN, /**< special value for invalid destructuring pattern */
219222
LEXER_PRIVATE_PRIMARY_EXPR, /**< private field in primary expession position */
223+
LEXER_ASSIGN_REFERENCE, /**< special value for reference assignment */
220224
#endif /* JERRY_ESNEXT */
221225

222226
/* Keywords which are not keyword tokens. */

0 commit comments

Comments
 (0)