Implement Symbol.isConcatSpreadable (#3307)
Algorithm is based on ECMA-262 v6, 22.1.3.1.1 JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
committed by
Dániel Bátyai
parent
8bdb32cc88
commit
a7d129c8b2
@@ -405,6 +405,19 @@ ecma_check_value_type_is_spec_defined (ecma_value_t value) /**< ecma value */
|
|||||||
|| ecma_is_value_object (value));
|
|| ecma_is_value_object (value));
|
||||||
} /* ecma_check_value_type_is_spec_defined */
|
} /* ecma_check_value_type_is_spec_defined */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given argument is an array or not.
|
||||||
|
*
|
||||||
|
* @return true - if the given argument is an array object
|
||||||
|
* false - otherwise
|
||||||
|
*/
|
||||||
|
inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
|
||||||
|
ecma_is_value_array (ecma_value_t arg) /**< argument */
|
||||||
|
{
|
||||||
|
return (ecma_is_value_object (arg)
|
||||||
|
&& ecma_get_object_type (ecma_get_object_from_value (arg)) == ECMA_OBJECT_TYPE_ARRAY);
|
||||||
|
} /* ecma_is_value_array */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an ecma value from the given raw boolean.
|
* Creates an ecma value from the given raw boolean.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ bool JERRY_ATTR_CONST ecma_is_value_direct_string (ecma_value_t value);
|
|||||||
bool JERRY_ATTR_CONST ecma_is_value_non_direct_string (ecma_value_t value);
|
bool JERRY_ATTR_CONST ecma_is_value_non_direct_string (ecma_value_t value);
|
||||||
bool JERRY_ATTR_CONST ecma_is_value_object (ecma_value_t value);
|
bool JERRY_ATTR_CONST ecma_is_value_object (ecma_value_t value);
|
||||||
bool JERRY_ATTR_CONST ecma_is_value_error_reference (ecma_value_t value);
|
bool JERRY_ATTR_CONST ecma_is_value_error_reference (ecma_value_t value);
|
||||||
|
bool JERRY_ATTR_CONST ecma_is_value_array (ecma_value_t arg);
|
||||||
|
|
||||||
void ecma_check_value_type_is_spec_defined (ecma_value_t value);
|
void ecma_check_value_type_is_spec_defined (ecma_value_t value);
|
||||||
|
|
||||||
|
|||||||
@@ -59,15 +59,7 @@ ecma_builtin_array_object_is_array (ecma_value_t this_arg, /**< 'this' argument
|
|||||||
{
|
{
|
||||||
JERRY_UNUSED (this_arg);
|
JERRY_UNUSED (this_arg);
|
||||||
|
|
||||||
if (ecma_is_value_object (arg))
|
return ecma_make_boolean_value (ecma_is_value_array (arg));
|
||||||
{
|
|
||||||
if (ecma_get_object_type (ecma_get_object_from_value (arg)) == ECMA_OBJECT_TYPE_ARRAY)
|
|
||||||
{
|
|
||||||
return ECMA_VALUE_TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ECMA_VALUE_FALSE;
|
|
||||||
} /* ecma_builtin_array_object_is_array */
|
} /* ecma_builtin_array_object_is_array */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -416,45 +416,64 @@ ecma_builtin_helper_array_concat_value (ecma_object_t *array_obj_p, /**< array *
|
|||||||
ecma_value_t value) /**< value to concat */
|
ecma_value_t value) /**< value to concat */
|
||||||
{
|
{
|
||||||
/* 5.b */
|
/* 5.b */
|
||||||
if (ecma_is_value_object (value))
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
ecma_value_t is_spreadable = ecma_op_is_concat_spreadable (value);
|
||||||
|
|
||||||
|
if (ECMA_IS_VALUE_ERROR (is_spreadable))
|
||||||
|
{
|
||||||
|
return is_spreadable;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool spread_object = is_spreadable == ECMA_VALUE_TRUE;
|
||||||
|
#else /* !ENABLED (JERRY_ES2015) */
|
||||||
|
bool spread_object = ecma_is_value_array (value);
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
|
if (spread_object)
|
||||||
{
|
{
|
||||||
ecma_object_t *obj_p = ecma_get_object_from_value (value);
|
ecma_object_t *obj_p = ecma_get_object_from_value (value);
|
||||||
|
|
||||||
if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_ARRAY)
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
uint32_t arg_len;
|
||||||
|
ecma_value_t error = ecma_op_object_get_length (obj_p, &arg_len);
|
||||||
|
|
||||||
|
if (ECMA_IS_VALUE_ERROR (error))
|
||||||
{
|
{
|
||||||
/* 5.b.ii */
|
return error;
|
||||||
uint32_t arg_len = ecma_array_get_length (obj_p);
|
}
|
||||||
|
#else /* !ENABLED (JERRY_ES2015) */
|
||||||
|
/* 5.b.ii */
|
||||||
|
uint32_t arg_len = ecma_array_get_length (obj_p);
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
/* 5.b.iii */
|
||||||
|
for (uint32_t array_index = 0; array_index < arg_len; array_index++)
|
||||||
|
{
|
||||||
|
/* 5.b.iii.2 */
|
||||||
|
ecma_value_t get_value = ecma_op_object_find_by_uint32_index (obj_p, array_index);
|
||||||
|
|
||||||
/* 5.b.iii */
|
if (ECMA_IS_VALUE_ERROR (get_value))
|
||||||
for (uint32_t array_index = 0; array_index < arg_len; array_index++)
|
|
||||||
{
|
{
|
||||||
/* 5.b.iii.2 */
|
return get_value;
|
||||||
ecma_value_t get_value = ecma_op_object_find_by_uint32_index (obj_p, array_index);
|
|
||||||
|
|
||||||
if (ECMA_IS_VALUE_ERROR (get_value))
|
|
||||||
{
|
|
||||||
return get_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ecma_is_value_found (get_value))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 5.b.iii.3.b */
|
|
||||||
/* This will always be a simple value since 'is_throw' is false, so no need to free. */
|
|
||||||
ecma_value_t put_comp = ecma_builtin_helper_def_prop_by_index (array_obj_p,
|
|
||||||
*length_p + array_index,
|
|
||||||
get_value,
|
|
||||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE);
|
|
||||||
|
|
||||||
JERRY_ASSERT (ecma_is_value_true (put_comp));
|
|
||||||
ecma_free_value (get_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*length_p += arg_len;
|
if (!ecma_is_value_found (get_value))
|
||||||
return ECMA_VALUE_EMPTY;
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 5.b.iii.3.b */
|
||||||
|
/* This will always be a simple value since 'is_throw' is false, so no need to free. */
|
||||||
|
ecma_value_t put_comp = ecma_builtin_helper_def_prop_by_index (array_obj_p,
|
||||||
|
*length_p + array_index,
|
||||||
|
get_value,
|
||||||
|
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE);
|
||||||
|
|
||||||
|
JERRY_ASSERT (ecma_is_value_true (put_comp));
|
||||||
|
ecma_free_value (get_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*length_p += arg_len;
|
||||||
|
return ECMA_VALUE_EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 5.c.i */
|
/* 5.c.i */
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#include "ecma-exceptions.h"
|
#include "ecma-exceptions.h"
|
||||||
#include "ecma-gc.h"
|
#include "ecma-gc.h"
|
||||||
#include "ecma-globals.h"
|
#include "ecma-globals.h"
|
||||||
|
#include "ecma-helpers.h"
|
||||||
#include "ecma-function-object.h"
|
#include "ecma-function-object.h"
|
||||||
#include "ecma-lex-env.h"
|
#include "ecma-lex-env.h"
|
||||||
#include "ecma-string-object.h"
|
#include "ecma-string-object.h"
|
||||||
@@ -2474,6 +2475,43 @@ ecma_object_class_is (ecma_object_t *object_p, /**< object */
|
|||||||
return false;
|
return false;
|
||||||
} /* ecma_object_class_is */
|
} /* ecma_object_class_is */
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
/**
|
||||||
|
* Object's IsConcatSpreadable operation, used for Array.prototype.concat
|
||||||
|
* It checks the argument's [Symbol.isConcatSpreadable] property value
|
||||||
|
*
|
||||||
|
* See also:
|
||||||
|
* ECMA-262 v6, 22.1.3.1.1;
|
||||||
|
*
|
||||||
|
* @return ECMA_VALUE_ERROR - if the operation fails
|
||||||
|
* ECMA_VALUE_TRUE - if the argument is concatSpreadable
|
||||||
|
* ECMA_VALUE_FALSE - otherwise
|
||||||
|
*/
|
||||||
|
ecma_value_t
|
||||||
|
ecma_op_is_concat_spreadable (ecma_value_t arg) /**< argument */
|
||||||
|
{
|
||||||
|
if (!ecma_is_value_object (arg))
|
||||||
|
{
|
||||||
|
return ECMA_VALUE_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ecma_value_t spreadable = ecma_op_object_get_by_symbol_id (ecma_get_object_from_value (arg),
|
||||||
|
LIT_MAGIC_STRING_IS_CONCAT_SPREADABLE);
|
||||||
|
|
||||||
|
if (ECMA_IS_VALUE_ERROR (spreadable))
|
||||||
|
{
|
||||||
|
return spreadable;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ecma_is_value_undefined (spreadable))
|
||||||
|
{
|
||||||
|
return ecma_make_boolean_value (ecma_op_to_boolean (spreadable));
|
||||||
|
}
|
||||||
|
|
||||||
|
return (ecma_make_boolean_value (ecma_is_value_array (arg)));
|
||||||
|
} /* ecma_op_is_concat_spreadable */
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
* @}
|
* @}
|
||||||
|
|||||||
@@ -65,6 +65,9 @@ ecma_collection_t * ecma_op_object_get_property_names (ecma_object_t *obj_p, uin
|
|||||||
|
|
||||||
lit_magic_string_id_t ecma_object_get_class_name (ecma_object_t *obj_p);
|
lit_magic_string_id_t ecma_object_get_class_name (ecma_object_t *obj_p);
|
||||||
bool ecma_object_class_is (ecma_object_t *object_p, uint32_t class_id);
|
bool ecma_object_class_is (ecma_object_t *object_p, uint32_t class_id);
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
ecma_value_t ecma_op_is_concat_spreadable (ecma_value_t arg);
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
// Test with regular arrays
|
||||||
|
var alpha = ['a', 'b', 'c'];
|
||||||
|
var numeric = [1, 2, 3];
|
||||||
|
|
||||||
|
var alphaNumeric = alpha.concat(numeric);
|
||||||
|
assert(JSON.stringify(alphaNumeric) === '["a","b","c",1,2,3]');
|
||||||
|
assert(alphaNumeric.length === 6);
|
||||||
|
|
||||||
|
numeric[Symbol.isConcatSpreadable] = false;
|
||||||
|
alphaNumeric = alpha.concat(numeric);
|
||||||
|
assert(JSON.stringify(alphaNumeric) === '["a","b","c",[1,2,3]]');
|
||||||
|
assert(alphaNumeric.length === 4);
|
||||||
|
|
||||||
|
numeric[Symbol.isConcatSpreadable] = true;
|
||||||
|
|
||||||
|
// Test with array-like object
|
||||||
|
var fakeArray = {
|
||||||
|
[Symbol.isConcatSpreadable]: true,
|
||||||
|
length: 2,
|
||||||
|
0: 4,
|
||||||
|
1: 5
|
||||||
|
}
|
||||||
|
|
||||||
|
var numericArray = numeric.concat(fakeArray);
|
||||||
|
assert(JSON.stringify(numericArray) === '[1,2,3,4,5]');
|
||||||
|
assert(numericArray.length === 5);
|
||||||
|
|
||||||
|
fakeArray[Symbol.isConcatSpreadable] = false;
|
||||||
|
numericArray = numeric.concat(fakeArray);
|
||||||
|
assert(JSON.stringify(numericArray) === '[1,2,3,{"0":4,"1":5,"length":2}]');
|
||||||
|
assert(numericArray.length === 4);
|
||||||
|
|
||||||
|
// Test with object
|
||||||
|
var obj = { 0: 'd' };
|
||||||
|
|
||||||
|
var alphaObj = alpha.concat(obj);
|
||||||
|
assert(JSON.stringify(alphaObj) === '["a","b","c",{"0":"d"}]');
|
||||||
|
assert(alphaObj.length === 4);
|
||||||
|
|
||||||
|
obj[Symbol.isConcatSpreadable] = true;
|
||||||
|
alphaObj = alpha.concat(obj);
|
||||||
|
assert(JSON.stringify(alphaObj) === '["a","b","c"]');
|
||||||
|
assert(alphaObj.length === 3);
|
||||||
|
|
||||||
|
// Test with boolean
|
||||||
|
var bool = true;
|
||||||
|
var numericBool = numeric.concat(bool);
|
||||||
|
assert(JSON.stringify(numericBool) === '[1,2,3,true]');
|
||||||
|
assert(numericBool.length === 4);
|
||||||
|
|
||||||
|
bool[Symbol.isConcatSpreadable] = false;
|
||||||
|
numericBool = numeric.concat(bool);
|
||||||
|
assert(JSON.stringify(numericBool) === '[1,2,3,true]');
|
||||||
|
assert(numericBool.length === 4);
|
||||||
|
|
||||||
|
// Test when unable to concat
|
||||||
|
var array1 = [];
|
||||||
|
var array2 = [];
|
||||||
|
Object.defineProperty(array2, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||||
|
|
||||||
|
try {
|
||||||
|
array1.concat(array2);
|
||||||
|
assert(false);
|
||||||
|
} catch (e) {
|
||||||
|
assert(e.message === "foo");
|
||||||
|
assert(e instanceof ReferenceError);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user