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:
Robert Fancsik
2019-11-19 14:06:12 +01:00
committed by Dániel Bátyai
parent bf630c0c54
commit 22766a855e
9 changed files with 442 additions and 21 deletions
+78
View File
@@ -382,6 +382,84 @@ opfunc_append_to_spread_array (ecma_value_t *stack_top_p, /**< current stack top
return ECMA_VALUE_EMPTY;
} /* opfunc_append_to_spread_array */
/**
* Spread function call/construct arguments into an ecma-collection
*
* @return NULL - if the operation failed
* pointer to the ecma-collection with the spreaded arguments, otherwise
*/
ecma_collection_t * JERRY_ATTR_NOINLINE
opfunc_spread_arguments (ecma_value_t *arguments_list_p, /**< arguments list */
uint8_t arguments_list_len) /**< number of arguments */
{
ecma_collection_t *buff_p = ecma_new_collection ();
for (uint8_t i = 0; i < arguments_list_len; i++)
{
ecma_value_t arg = arguments_list_p[i];
if (!ecma_op_is_spread_object (arg))
{
ecma_collection_push_back (buff_p, arg);
continue;
}
ecma_value_t ret_value = ECMA_VALUE_ERROR;
ecma_object_t *spread_object_p = ecma_get_object_from_value (arguments_list_p[i]);
ecma_value_t spread_value = ecma_op_spread_object_get_spreaded_element (spread_object_p);
ecma_value_t iterator = ecma_op_get_iterator (spread_value, ECMA_VALUE_EMPTY);
if (!ECMA_IS_VALUE_ERROR (iterator))
{
while (true)
{
ecma_value_t next = ecma_op_iterator_step (iterator);
if (ECMA_IS_VALUE_ERROR (next))
{
break;
}
if (ecma_is_value_false (next))
{
ret_value = ECMA_VALUE_EMPTY;
break;
}
ecma_value_t value = ecma_op_iterator_value (next);
ecma_free_value (next);
if (ECMA_IS_VALUE_ERROR (value))
{
break;
}
ecma_collection_push_back (buff_p, value);
}
}
ecma_free_value (iterator);
ecma_free_value (spread_value);
if (ECMA_IS_VALUE_ERROR (ret_value))
{
for (uint32_t k = i; k < arguments_list_len; k++)
{
ecma_free_value (arguments_list_p[k]);
}
ecma_collection_free (buff_p);
return NULL;
}
ecma_deref_object (spread_object_p);
}
return buff_p;
} /* opfunc_spread_arguments */
#endif /* ENABLED (JERRY_ES2015) */
/**
+5
View File
@@ -104,6 +104,11 @@ opfunc_for_in (ecma_value_t left_value, ecma_value_t *result_obj_p);
ecma_value_t
opfunc_append_array (ecma_value_t *stack_top_p, uint16_t values_length);
#if ENABLED (JERRY_ES2015)
ecma_collection_t *
opfunc_spread_arguments (ecma_value_t *arguments_list_p, uint8_t argument_list_len);
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
* @}
+168 -10
View File
@@ -478,24 +478,40 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
uint8_t *byte_code_p = frame_ctx_p->byte_code_p + 3;
uint8_t opcode = byte_code_p[-2];
uint32_t arguments_list_len = byte_code_p[-1];
uint32_t arguments_list_len;
ecma_value_t *stack_top_p = frame_ctx_p->stack_top_p - arguments_list_len;
bool spread_arguments = opcode >= CBC_EXT_SPREAD_SUPER_CALL;
ecma_value_t func_value = stack_top_p[-1];
ecma_collection_t *collection_p = NULL;
ecma_value_t *arguments_p;
if (spread_arguments)
{
ecma_value_t collection = *(--frame_ctx_p->stack_top_p);
collection_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, collection);
arguments_p = collection_p->buffer_p;
arguments_list_len = collection_p->item_count;
}
else
{
arguments_list_len = byte_code_p[-1];
arguments_p = frame_ctx_p->stack_top_p;
}
ecma_value_t func_value = *(--frame_ctx_p->stack_top_p);
ecma_value_t completion_value;
ecma_op_set_super_called (frame_ctx_p->lex_env_p);
ecma_value_t this_value = ecma_op_get_class_this_binding (frame_ctx_p->lex_env_p);
if (!ecma_is_constructor (func_value))
{
completion_value = ecma_raise_type_error ("Class extends value is not a constructor.");
completion_value = ecma_raise_type_error (ECMA_ERR_MSG ("Class extends value is not a constructor."));
}
else
{
completion_value = ecma_op_function_construct (ecma_get_object_from_value (func_value),
this_value,
stack_top_p,
arguments_p,
arguments_list_len);
if (this_value != completion_value && ecma_is_value_object (completion_value))
@@ -508,9 +524,16 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
/* Free registers. */
for (uint32_t i = 0; i < arguments_list_len; i++)
{
ecma_fast_free_value (stack_top_p[i]);
ecma_fast_free_value (arguments_p[i]);
}
if (collection_p != NULL)
{
ecma_collection_destroy (collection_p);
}
ecma_free_value (func_value);
if (JERRY_UNLIKELY (ECMA_IS_VALUE_ERROR (completion_value)))
{
#if ENABLED (JERRY_DEBUGGER)
@@ -521,7 +544,6 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
else
{
frame_ctx_p->byte_code_p = byte_code_p;
ecma_free_value (*(--stack_top_p));
uint32_t opcode_data = vm_decode_table[(CBC_END + 1) + opcode];
if (!(opcode_data & (VM_OC_PUT_STACK | VM_OC_PUT_BLOCK)))
@@ -530,7 +552,7 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
else if (opcode_data & VM_OC_PUT_STACK)
{
*stack_top_p++ = completion_value;
*frame_ctx_p->stack_top_p++ = completion_value;
}
else
{
@@ -538,9 +560,102 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
frame_ctx_p->block_result = completion_value;
}
}
frame_ctx_p->stack_top_p = stack_top_p;
} /* vm_super_call */
/**
* Perform one of the following call/construct operation with spreaded argument list
* - f(...args)
* - o.f(...args)
* - new O(...args)
*/
static void
vm_spread_operation (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
JERRY_ASSERT (frame_ctx_p->byte_code_p[0] == CBC_EXT_OPCODE);
uint8_t opcode = frame_ctx_p->byte_code_p[1];
ecma_value_t completion_value;
ecma_value_t collection = *(--frame_ctx_p->stack_top_p);
ecma_collection_t *collection_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, collection);
ecma_value_t func_value = *(--frame_ctx_p->stack_top_p);
bool is_call_prop = opcode >= CBC_EXT_SPREAD_CALL_PROP;
if (frame_ctx_p->byte_code_p[1] == CBC_EXT_SPREAD_NEW)
{
if (!ecma_is_value_object (func_value)
|| !ecma_object_is_constructor (ecma_get_object_from_value (func_value)))
{
completion_value = ecma_raise_type_error (ECMA_ERR_MSG ("Expected a constructor."));
}
else
{
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (func_value);
completion_value = ecma_op_function_construct (constructor_obj_p,
ECMA_VALUE_UNDEFINED,
collection_p->buffer_p,
collection_p->item_count);
}
}
else
{
ecma_value_t this_value = is_call_prop ? frame_ctx_p->stack_top_p[-2] : ECMA_VALUE_UNDEFINED;
if (!ecma_is_value_object (func_value)
|| !ecma_op_object_is_callable (ecma_get_object_from_value (func_value)))
{
completion_value = ecma_raise_type_error (ECMA_ERR_MSG ("Expected a function."));
}
else
{
ecma_object_t *func_obj_p = ecma_get_object_from_value (func_value);
completion_value = ecma_op_function_call (func_obj_p,
this_value,
collection_p->buffer_p,
collection_p->item_count);
}
if (is_call_prop)
{
ecma_free_value (*(--frame_ctx_p->stack_top_p));
ecma_free_value (*(--frame_ctx_p->stack_top_p));
}
}
ecma_collection_free (collection_p);
ecma_free_value (func_value);
if (JERRY_UNLIKELY (ECMA_IS_VALUE_ERROR (completion_value)))
{
#if ENABLED (JERRY_DEBUGGER)
JERRY_CONTEXT (debugger_exception_byte_code_p) = frame_ctx_p->byte_code_p;
#endif /* ENABLED (JERRY_DEBUGGER) */
frame_ctx_p->byte_code_p = (uint8_t *) vm_error_byte_code_p;
}
else
{
uint32_t opcode_data = vm_decode_table[(CBC_END + 1) + opcode];
if (!(opcode_data & (VM_OC_PUT_STACK | VM_OC_PUT_BLOCK)))
{
ecma_fast_free_value (completion_value);
}
else if (opcode_data & VM_OC_PUT_STACK)
{
*frame_ctx_p->stack_top_p++ = completion_value;
}
else
{
ecma_fast_free_value (frame_ctx_p->block_result);
frame_ctx_p->block_result = completion_value;
}
/* EXT_OPCODE, SPREAD_OPCODE, BYTE_ARG */
frame_ctx_p->byte_code_p += 3;
}
} /* vm_spread_operation */
#endif /* ENABLED (JERRY_ES2015) */
/**
@@ -1465,6 +1580,23 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
#if ENABLED (JERRY_ES2015)
case VM_OC_SUPER_CALL:
{
uint8_t arguments_list_len = *byte_code_p++;
stack_top_p -= arguments_list_len;
if (opcode >= CBC_EXT_SPREAD_SUPER_CALL)
{
ecma_collection_t *arguments_p = opfunc_spread_arguments (stack_top_p, arguments_list_len);
if (JERRY_UNLIKELY (arguments_p == NULL))
{
result = ECMA_VALUE_ERROR;
goto error;
}
stack_top_p++;
ECMA_SET_INTERNAL_VALUE_POINTER (stack_top_p[-1], arguments_p);
}
frame_ctx_p->call_operation = VM_EXEC_SUPER_CALL;
frame_ctx_p->byte_code_p = byte_code_start_p;
frame_ctx_p->stack_top_p = stack_top_p;
@@ -1894,6 +2026,27 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
*stack_top_p++ = result;
goto free_left_value;
}
case VM_OC_SPREAD_ARGUMENTS:
{
uint8_t arguments_list_len = *byte_code_p++;
stack_top_p -= arguments_list_len;
ecma_collection_t *arguments_p = opfunc_spread_arguments (stack_top_p, arguments_list_len);
if (JERRY_UNLIKELY (arguments_p == NULL))
{
result = ECMA_VALUE_ERROR;
goto error;
}
stack_top_p++;
ECMA_SET_INTERNAL_VALUE_POINTER (stack_top_p[-1], arguments_p);
frame_ctx_p->call_operation = VM_EXEC_SPREAD_OP;
frame_ctx_p->byte_code_p = byte_code_start_p;
frame_ctx_p->stack_top_p = stack_top_p;
return ECMA_VALUE_UNDEFINED;
}
#endif /* ENABLED (JERRY_ES2015) */
case VM_OC_PUSH_ELISON:
{
@@ -3859,6 +4012,11 @@ vm_execute (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
vm_super_call (frame_ctx_p);
break;
}
case VM_EXEC_SPREAD_OP:
{
vm_spread_operation (frame_ctx_p);
break;
}
#endif /* ENABLED (JERRY_ES2015) */
case VM_EXEC_CONSTRUCT:
{
+5
View File
@@ -1,3 +1,4 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -252,6 +253,7 @@ typedef enum
VM_OC_DEFAULT_INITIALIZER, /**< default initializer inside a pattern */
VM_OC_REST_INITIALIZER, /**< create rest object inside an array pattern */
VM_OC_INITIALIZER_PUSH_PROP, /**< push property for object initializer */
VM_OC_SPREAD_ARGUMENTS, /**< perform function call/construct with spreaded arguments */
#endif /* ENABLED (JERRY_ES2015) */
VM_OC_NONE, /**< a special opcode for unsupported byte codes */
} vm_oc_types;
@@ -300,7 +302,9 @@ typedef enum
VM_OC_DEFAULT_INITIALIZER = VM_OC_NONE, /**< default initializer inside a pattern */
VM_OC_REST_INITIALIZER = VM_OC_NONE, /**< create rest object inside an array pattern */
VM_OC_INITIALIZER_PUSH_PROP = VM_OC_NONE, /**< push property for object initializer */
VM_OC_SPREAD_ARGUMENTS = VM_OC_NONE, /**< perform function call/construct with spreaded arguments */
#endif /* !ENABLED (JERRY_ES2015) */
VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */
} vm_oc_unused_types;
@@ -381,6 +385,7 @@ typedef enum
VM_NO_EXEC_OP, /**< do nothing */
VM_EXEC_CALL, /**< invoke a function */
VM_EXEC_SUPER_CALL, /**< invoke a function through 'super' keyword */
VM_EXEC_SPREAD_OP, /**< call/construct operation with spreaded argument list */
VM_EXEC_CONSTRUCT, /**< construct a new object */
} vm_call_operation;