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:
Robert Fancsik
2019-11-04 16:36:58 +01:00
committed by Dániel Bátyai
parent fc3cfc4fdc
commit 6f83da4c0b
12 changed files with 417 additions and 10 deletions
+20
View File
@@ -465,6 +465,17 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
break;
}
#endif /* ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) */
#if ENABLED (JERRY_ES2015)
case ECMA_PSEUDO_SPREAD_OBJECT:
{
ecma_value_t spread_value = ext_object_p->u.pseudo_array.u2.spread_value;
if (ecma_is_value_object (spread_value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (spread_value));
}
break;
}
#endif /* ENABLED (JERRY_ES2015) */
default:
{
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);
@@ -940,6 +951,15 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
return;
}
#endif /* ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) */
#if ENABLED (JERRY_ES2015)
case ECMA_PSEUDO_SPREAD_OBJECT:
{
ecma_value_t spread_value = ext_object_p->u.pseudo_array.u2.spread_value;
ecma_free_value_if_not_object (spread_value);
ecma_dealloc_extended_object (object_p, sizeof (ecma_extended_object_t));
return;
}
#endif /* ENABLED (JERRY_ES2015) */
default:
{
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);
+3 -1
View File
@@ -629,7 +629,8 @@ typedef enum
ECMA_PSEUDO_SET_ITERATOR = 4, /**< Set iterator object (ECMAScript v6, 23.2.5.1) */
ECMA_PSEUDO_MAP_ITERATOR = 5, /**< Map iterator object (ECMAScript v6, 23.1.5.1) */
ECMA_PSEUDO_STRING_ITERATOR = 6, /**< String iterator object (ECMAScript v6, 22.1.5.1) */
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_STRING_ITERATOR /**< maximum value */
ECMA_PSEUDO_SPREAD_OBJECT = 7, /**< spread object */
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_SPREAD_OBJECT /**< maximum value */
} ecma_pseudo_array_type_t;
/**
@@ -865,6 +866,7 @@ typedef struct
ecma_value_t lex_env_cp; /**< for arguments: lexical environment */
ecma_value_t arraybuffer; /**< for typedarray: internal arraybuffer */
ecma_value_t iterated_value; /**< for %Iterator%: [[IteratedObject]] property */
ecma_value_t spread_value; /**< for spread object: spreaded element */
} u2;
} pseudo_array;
@@ -0,0 +1,88 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-spread-object.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaspreadeanobject ECMA Spread object related routines
* @{
*/
/**
* Spread object creation operation.
*
* @return pseudo array object as an ecma value
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_op_create_spread_object (ecma_value_t element) /**< value to spread */
{
ecma_object_t *object_p = ecma_create_object (NULL,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_PSEUDO_ARRAY);
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
ext_object_p->u.pseudo_array.type = ECMA_PSEUDO_SPREAD_OBJECT;
ext_object_p->u.pseudo_array.u2.spread_value = ecma_copy_value_if_not_object (element);
return ecma_make_object_value (object_p);
} /* ecma_op_create_spread_object */
/**
* Spread object creation operation.
*
* @return true - if the argument is a spread object
* false, otherwise
*/
bool
ecma_op_is_spread_object (ecma_value_t value) /**< value to check */
{
if (ecma_is_value_object (value))
{
ecma_object_t *object_p = ecma_get_object_from_value (value);
if (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_PSEUDO_ARRAY)
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
return ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_SPREAD_OBJECT;
}
}
return false;
} /* ecma_op_is_spread_object */
/**
* Get the referenced element by the spread object
*
* @return element referenced by the spread object as an ecma-value
*/
ecma_value_t
ecma_op_spread_object_get_spreaded_element (ecma_object_t *object_p) /**< spread object */
{
JERRY_ASSERT (ecma_op_is_spread_object (ecma_make_object_value (object_p)));
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
return ecma_copy_value (ext_object_p->u.pseudo_array.u2.spread_value);
} /* ecma_op_spread_object_get_spreaded_element */
/**
* @}
* @}
*/
@@ -0,0 +1,42 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMA_SPREAD_OBJECT_H
#define ECMA_SPREAD_OBJECT_H
#include "ecma-globals.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaspreadobject ECMA Spread object related routines
* @{
*/
ecma_value_t
ecma_op_create_spread_object (ecma_value_t arg);
bool
ecma_op_is_spread_object (ecma_value_t value);
ecma_value_t
ecma_op_spread_object_get_spreaded_element (ecma_object_t *object_p);
/**
* @}
* @}
*/
#endif /* !ECMA_SPREAD_OBJECT_H */