Implement the spread operator for array initialization (#3265)
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
fc3cfc4fdc
commit
6f83da4c0b
+122
-3
@@ -16,14 +16,17 @@
|
||||
#include "ecma-alloc.h"
|
||||
#include "ecma-array-object.h"
|
||||
#include "ecma-builtins.h"
|
||||
#include "ecma-builtin-helpers.h"
|
||||
#include "ecma-conversion.h"
|
||||
#include "ecma-exceptions.h"
|
||||
#include "ecma-function-object.h"
|
||||
#include "ecma-gc.h"
|
||||
#include "ecma-globals.h"
|
||||
#include "ecma-helpers.h"
|
||||
#include "ecma-iterator-object.h"
|
||||
#include "ecma-lex-env.h"
|
||||
#include "ecma-objects.h"
|
||||
#include "ecma-spread-object.h"
|
||||
#include "ecma-try-catch-macro.h"
|
||||
#include "jcontext.h"
|
||||
#include "opcodes.h"
|
||||
@@ -275,15 +278,129 @@ opfunc_for_in (ecma_value_t left_value, /**< left value */
|
||||
return NULL;
|
||||
} /* opfunc_for_in */
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
/**
|
||||
* 'VM_OC_APPEND_ARRAY' opcode handler specialized for spread objects
|
||||
*
|
||||
* @return ECMA_VALUE_ERROR - if the operation failed
|
||||
* ECMA_VALUE_EMPTY, otherwise
|
||||
*/
|
||||
static ecma_value_t JERRY_ATTR_NOINLINE
|
||||
opfunc_append_to_spread_array (ecma_value_t *stack_top_p, /**< current stack top */
|
||||
uint16_t values_length) /**< number of elements to set */
|
||||
{
|
||||
JERRY_ASSERT (!(values_length & OPFUNC_HAS_SPREAD_ELEMENT));
|
||||
|
||||
ecma_object_t *array_obj_p = ecma_get_object_from_value (stack_top_p[-1]);
|
||||
JERRY_ASSERT (ecma_get_object_type (array_obj_p) == ECMA_OBJECT_TYPE_ARRAY);
|
||||
|
||||
ecma_extended_object_t *ext_array_obj_p = (ecma_extended_object_t *) array_obj_p;
|
||||
uint32_t old_length = ext_array_obj_p->u.array.length;
|
||||
|
||||
for (uint32_t i = 0, j = old_length; i < values_length; i++)
|
||||
{
|
||||
if (ecma_is_value_array_hole (stack_top_p[i]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (ecma_op_is_spread_object (stack_top_p[i]))
|
||||
{
|
||||
ecma_value_t ret_value = ECMA_VALUE_ERROR;
|
||||
ecma_object_t *spread_object_p = ecma_get_object_from_value (stack_top_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))
|
||||
{
|
||||
j--;
|
||||
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_value_t put_comp;
|
||||
put_comp = ecma_builtin_helper_def_prop_by_index (array_obj_p,
|
||||
i + j,
|
||||
value,
|
||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE);
|
||||
|
||||
j++;
|
||||
JERRY_ASSERT (ecma_is_value_true (put_comp));
|
||||
ecma_free_value (value);
|
||||
}
|
||||
}
|
||||
|
||||
ecma_free_value (iterator);
|
||||
ecma_free_value (spread_value);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (ret_value))
|
||||
{
|
||||
for (uint32_t k = i; k < values_length; k++)
|
||||
{
|
||||
ecma_free_value (stack_top_p[k]);
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_deref_object (spread_object_p);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_value_t put_comp = ecma_builtin_helper_def_prop_by_index (array_obj_p,
|
||||
i + j,
|
||||
stack_top_p[i],
|
||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE);
|
||||
JERRY_ASSERT (ecma_is_value_true (put_comp));
|
||||
ecma_free_value (stack_top_p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return ECMA_VALUE_EMPTY;
|
||||
} /* opfunc_append_to_spread_array */
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
/**
|
||||
* 'VM_OC_APPEND_ARRAY' opcode handler, for setting array object properties
|
||||
*
|
||||
* @return ECMA_VALUE_ERROR - if the operation failed
|
||||
* ECMA_VALUE_EMPTY, otherwise
|
||||
*/
|
||||
void JERRY_ATTR_NOINLINE
|
||||
ecma_value_t JERRY_ATTR_NOINLINE
|
||||
opfunc_append_array (ecma_value_t *stack_top_p, /**< current stack top */
|
||||
uint8_t values_length) /**< number of elements to set */
|
||||
uint16_t values_length) /**< number of elements to set
|
||||
* with potential OPFUNC_HAS_SPREAD_ELEMENT flag */
|
||||
{
|
||||
ecma_object_t *array_obj_p = ecma_get_object_from_value (stack_top_p[-1]);
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
if (values_length >= OPFUNC_HAS_SPREAD_ELEMENT)
|
||||
{
|
||||
return opfunc_append_to_spread_array (stack_top_p, (uint16_t) (values_length & ~OPFUNC_HAS_SPREAD_ELEMENT));
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
ecma_object_t *array_obj_p = ecma_get_object_from_value (stack_top_p[-1]);
|
||||
JERRY_ASSERT (ecma_get_object_type (array_obj_p) == ECMA_OBJECT_TYPE_ARRAY);
|
||||
|
||||
ecma_extended_object_t *ext_array_obj_p = (ecma_extended_object_t *) array_obj_p;
|
||||
@@ -344,6 +461,8 @@ opfunc_append_array (ecma_value_t *stack_top_p, /**< current stack top */
|
||||
ext_array_obj_p->u.array.length = old_length + values_length;
|
||||
}
|
||||
}
|
||||
|
||||
return ECMA_VALUE_EMPTY;
|
||||
} /* opfunc_append_array */
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,6 +51,11 @@ typedef enum
|
||||
NUMBER_BITWISE_NOT, /**< bitwise NOT calculation */
|
||||
} number_bitwise_logic_op;
|
||||
|
||||
/**
|
||||
* The stack contains spread object during the upcoming APPEND_ARRAY operation
|
||||
*/
|
||||
#define OPFUNC_HAS_SPREAD_ELEMENT (1 << 8)
|
||||
|
||||
void
|
||||
vm_var_decl (ecma_object_t *lex_env_p, ecma_string_t *var_name_str_p, bool is_configurable_bindings);
|
||||
|
||||
@@ -96,8 +101,8 @@ vm_op_delete_var (ecma_value_t name_literal, ecma_object_t *lex_env_p);
|
||||
ecma_collection_t *
|
||||
opfunc_for_in (ecma_value_t left_value, ecma_value_t *result_obj_p);
|
||||
|
||||
void
|
||||
opfunc_append_array (ecma_value_t *stack_top_p, uint8_t values_length);
|
||||
ecma_value_t
|
||||
opfunc_append_array (ecma_value_t *stack_top_p, uint16_t values_length);
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
||||
+24
-2
@@ -31,6 +31,7 @@
|
||||
#include "ecma-objects-general.h"
|
||||
#include "ecma-regexp-object.h"
|
||||
#include "ecma-try-catch-macro.h"
|
||||
#include "ecma-spread-object.h"
|
||||
#include "jcontext.h"
|
||||
#include "opcodes.h"
|
||||
#include "vm.h"
|
||||
@@ -1774,6 +1775,11 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
|
||||
|
||||
goto error;
|
||||
}
|
||||
case VM_OC_CREATE_SPREAD_OBJECT:
|
||||
{
|
||||
*stack_top_p++ = ecma_op_create_spread_object (left_value);
|
||||
goto free_left_value;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
case VM_OC_PUSH_ELISON:
|
||||
{
|
||||
@@ -1782,9 +1788,25 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
|
||||
}
|
||||
case VM_OC_APPEND_ARRAY:
|
||||
{
|
||||
uint8_t values_length = *byte_code_p++;
|
||||
uint16_t values_length = *byte_code_p++;
|
||||
stack_top_p -= values_length;
|
||||
opfunc_append_array (stack_top_p, values_length);
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
if (*byte_code_start_p == CBC_EXT_OPCODE)
|
||||
{
|
||||
values_length = (uint16_t) (values_length | OPFUNC_HAS_SPREAD_ELEMENT);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
result = opfunc_append_array (stack_top_p, values_length);
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
#else /* !ENABLED (JERRY_ES2015) */
|
||||
JERRY_ASSERT (ecma_is_value_empty (result));
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
continue;
|
||||
}
|
||||
case VM_OC_PUSH_UNDEFINED_BASE:
|
||||
|
||||
@@ -245,6 +245,7 @@ typedef enum
|
||||
VM_OC_PUSH_CONSTRUCTOR_SUPER, /**< push 'super' inside a class constructor */
|
||||
VM_OC_PUSH_CONSTRUCTOR_THIS, /**< push 'this' inside a class constructor */
|
||||
VM_OC_CONSTRUCTOR_RET, /**< explicit return from a class constructor */
|
||||
VM_OC_CREATE_SPREAD_OBJECT, /**< create spread object */
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
VM_OC_NONE, /**< a special opcode for unsupported byte codes */
|
||||
} vm_oc_types;
|
||||
@@ -284,6 +285,7 @@ typedef enum
|
||||
VM_OC_PUSH_CONSTRUCTOR_SUPER = VM_OC_NONE, /**< push 'super' inside a class constructor */
|
||||
VM_OC_PUSH_CONSTRUCTOR_THIS = VM_OC_NONE, /**< push 'this' inside a class constructor */
|
||||
VM_OC_CONSTRUCTOR_RET = VM_OC_NONE, /**< explicit return from a class constructor */
|
||||
VM_OC_CREATE_SPREAD_OBJECT = VM_OC_NONE, /**< create spread object */
|
||||
#endif /* !ENABLED (JERRY_ES2015S) */
|
||||
|
||||
VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */
|
||||
|
||||
Reference in New Issue
Block a user