Implement spread operator for function call arguments (#3329)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
committed by
Dániel Bátyai
parent
bf630c0c54
commit
22766a855e
@@ -66,16 +66,16 @@
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
/**
|
||||
* Checks whether the current opcode is a super constructor call
|
||||
* CBC_NO_RESULT_OPERATION for ext opcodes
|
||||
*/
|
||||
#define CBC_SUPER_CALL_OPERATION(opcode) \
|
||||
#define CBC_EXT_NO_RESULT_OPERATION(opcode) \
|
||||
((opcode) >= PARSER_TO_EXT_OPCODE (CBC_EXT_SUPER_CALL) \
|
||||
&& (opcode) <= PARSER_TO_EXT_OPCODE (CBC_EXT_SUPER_CALL_BLOCK))
|
||||
&& (opcode) <= PARSER_TO_EXT_OPCODE (CBC_EXT_SPREAD_CALL_PROP_BLOCK))
|
||||
#else /* !ENABLED (JERRY_ES2015) */
|
||||
/**
|
||||
* Checks whether the current opcode is a super constructor call
|
||||
* CBC_NO_RESULT_OPERATION for ext opcodes
|
||||
*/
|
||||
#define CBC_SUPER_CALL_OPERATION(opcode) false
|
||||
#define CBC_EXT_NO_RESULT_OPERATION(opcode) false
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
/* Debug macro. */
|
||||
@@ -84,9 +84,9 @@
|
||||
|
||||
/* Debug macro. */
|
||||
#define CBC_SAME_ARGS(op1, op2) \
|
||||
(CBC_SUPER_CALL_OPERATION (op1) ? ((cbc_ext_flags[PARSER_GET_EXT_OPCODE (op1)] & CBC_ARG_TYPES) \
|
||||
== (cbc_ext_flags[PARSER_GET_EXT_OPCODE (op2)] & CBC_ARG_TYPES)) \
|
||||
: ((cbc_flags[op1] & CBC_ARG_TYPES) == (cbc_flags[op2] & CBC_ARG_TYPES)))
|
||||
(CBC_EXT_NO_RESULT_OPERATION (op1) ? ((cbc_ext_flags[PARSER_GET_EXT_OPCODE (op1)] & CBC_ARG_TYPES) \
|
||||
== (cbc_ext_flags[PARSER_GET_EXT_OPCODE (op2)] & CBC_ARG_TYPES)) \
|
||||
: ((cbc_flags[op1] & CBC_ARG_TYPES) == (cbc_flags[op2] & CBC_ARG_TYPES)))
|
||||
|
||||
#define CBC_UNARY_OPERATION(name, group) \
|
||||
CBC_OPCODE (name, CBC_NO_FLAG, 0, \
|
||||
@@ -140,7 +140,7 @@
|
||||
* cannot be true for an opcode which has a result
|
||||
*/
|
||||
#define CBC_NO_RESULT_OPERATION(opcode) \
|
||||
(((opcode) >= CBC_PRE_INCR && (opcode) < CBC_END) || CBC_SUPER_CALL_OPERATION ((opcode)))
|
||||
(((opcode) >= CBC_PRE_INCR && (opcode) < CBC_END) || CBC_EXT_NO_RESULT_OPERATION ((opcode)))
|
||||
|
||||
/**
|
||||
* Branch instructions are organized in group of 8 opcodes.
|
||||
@@ -598,6 +598,24 @@
|
||||
VM_OC_SUPER_CALL | VM_OC_PUT_STACK) \
|
||||
CBC_OPCODE (CBC_EXT_SUPER_CALL_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
|
||||
VM_OC_SUPER_CALL | VM_OC_PUT_BLOCK) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_SUPER_CALL, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
|
||||
VM_OC_SUPER_CALL) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_SUPER_CALL_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
|
||||
VM_OC_SUPER_CALL | VM_OC_PUT_STACK) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_SUPER_CALL_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
|
||||
VM_OC_SUPER_CALL | VM_OC_PUT_BLOCK) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_CALL, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
|
||||
VM_OC_SPREAD_ARGUMENTS) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_CALL_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
|
||||
VM_OC_SPREAD_ARGUMENTS | VM_OC_PUT_STACK) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_CALL_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
|
||||
VM_OC_SPREAD_ARGUMENTS | VM_OC_PUT_BLOCK) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_CALL_PROP, CBC_HAS_POP_STACK_BYTE_ARG, -3, \
|
||||
VM_OC_SPREAD_ARGUMENTS) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_CALL_PROP_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, -2, \
|
||||
VM_OC_SPREAD_ARGUMENTS | VM_OC_PUT_STACK) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_CALL_PROP_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -3, \
|
||||
VM_OC_SPREAD_ARGUMENTS | VM_OC_PUT_BLOCK) \
|
||||
CBC_OPCODE (CBC_EXT_PUSH_CONSTRUCTOR_SUPER, CBC_NO_FLAG, 1, \
|
||||
VM_OC_PUSH_CONSTRUCTOR_SUPER | VM_OC_PUT_STACK) \
|
||||
CBC_OPCODE (CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP, CBC_NO_FLAG, 1, \
|
||||
@@ -632,6 +650,8 @@
|
||||
VM_OC_REST_INITIALIZER) \
|
||||
CBC_OPCODE (CBC_EXT_INITIALIZER_PUSH_PROP_LITERAL, CBC_HAS_LITERAL_ARG, 1, \
|
||||
VM_OC_INITIALIZER_PUSH_PROP | VM_OC_GET_LITERAL) \
|
||||
CBC_OPCODE (CBC_EXT_SPREAD_NEW, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
|
||||
VM_OC_SPREAD_ARGUMENTS | VM_OC_PUT_STACK) \
|
||||
\
|
||||
/* Last opcode (not a real opcode). */ \
|
||||
CBC_OPCODE (CBC_EXT_END, CBC_NO_FLAG, 0, \
|
||||
|
||||
@@ -1638,6 +1638,10 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
|
||||
|
||||
lexer_next_token (context_p);
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
bool spread_arguments = false;
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
if (context_p->token.type != LEXER_RIGHT_PAREN)
|
||||
{
|
||||
while (true)
|
||||
@@ -1647,8 +1651,24 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
|
||||
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIMIT_REACHED);
|
||||
}
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
bool is_spread = false;
|
||||
if (context_p->token.type == LEXER_THREE_DOTS)
|
||||
{
|
||||
spread_arguments = true;
|
||||
is_spread = true;
|
||||
lexer_next_token (context_p);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
if (is_spread)
|
||||
{
|
||||
parser_emit_cbc_ext (context_p, CBC_EXT_CREATE_SPREAD_OBJECT);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
if (context_p->token.type != LEXER_COMMA)
|
||||
{
|
||||
break;
|
||||
@@ -1688,6 +1708,33 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
|
||||
parser_emit_cbc_ext (context_p, CBC_EXT_SUPER_PROP_CALL);
|
||||
context_p->status_flags &= (uint32_t) ~PARSER_CLASS_SUPER_PROP_REFERENCE;
|
||||
}
|
||||
|
||||
if (spread_arguments)
|
||||
{
|
||||
uint16_t spread_opcode;
|
||||
|
||||
if (opcode == CBC_CALL)
|
||||
{
|
||||
spread_opcode = CBC_EXT_SPREAD_CALL;
|
||||
}
|
||||
else if (opcode == CBC_CALL_PROP)
|
||||
{
|
||||
spread_opcode = CBC_EXT_SPREAD_CALL_PROP;
|
||||
}
|
||||
else if (opcode == CBC_NEW)
|
||||
{
|
||||
spread_opcode = CBC_EXT_SPREAD_NEW;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* opcode is unchanged */
|
||||
JERRY_ASSERT (opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_SUPER_CALL));
|
||||
spread_opcode = CBC_EXT_SPREAD_SUPER_CALL;
|
||||
}
|
||||
|
||||
parser_emit_cbc_ext_call (context_p, spread_opcode, call_arguments);
|
||||
continue;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
if (call_arguments <= 1)
|
||||
|
||||
@@ -599,6 +599,8 @@ void parser_set_continues_to_current_position (parser_context_t *context_p, pars
|
||||
parser_emit_cbc_literal ((context_p), PARSER_TO_EXT_OPCODE (opcode), (literal_index))
|
||||
#define parser_emit_cbc_ext_call(context_p, opcode, call_arguments) \
|
||||
parser_emit_cbc_call ((context_p), PARSER_TO_EXT_OPCODE (opcode), (call_arguments))
|
||||
#define parser_emit_cbc_ext_call(context_p, opcode, call_arguments) \
|
||||
parser_emit_cbc_call ((context_p), PARSER_TO_EXT_OPCODE (opcode), (call_arguments))
|
||||
#define parser_emit_cbc_ext_forward_branch(context_p, opcode, branch_p) \
|
||||
parser_emit_cbc_forward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (branch_p))
|
||||
#define parser_emit_cbc_ext_backward_branch(context_p, opcode, offset) \
|
||||
|
||||
@@ -443,8 +443,14 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
case LEXER_COMMA:
|
||||
{
|
||||
/* Elision. */
|
||||
if (stack_top != SCAN_STACK_ARRAY_LITERAL)
|
||||
/* Elision or spread arguments */
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
bool raise_error = (stack_top != SCAN_STACK_PAREN_EXPRESSION && stack_top != SCAN_STACK_ARRAY_LITERAL);
|
||||
#else /* !ENABLED (JERRY_ES2015) */
|
||||
bool raise_error = stack_top != SCAN_STACK_ARRAY_LITERAL;
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
if (raise_error)
|
||||
{
|
||||
scanner_raise_error (context_p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user