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_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.
|
||||
*
|
||||
|
||||
@@ -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_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_array (ecma_value_t arg);
|
||||
|
||||
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);
|
||||
|
||||
if (ecma_is_value_object (arg))
|
||||
{
|
||||
if (ecma_get_object_type (ecma_get_object_from_value (arg)) == ECMA_OBJECT_TYPE_ARRAY)
|
||||
{
|
||||
return ECMA_VALUE_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return ECMA_VALUE_FALSE;
|
||||
return ecma_make_boolean_value (ecma_is_value_array (arg));
|
||||
} /* 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 */
|
||||
{
|
||||
/* 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);
|
||||
|
||||
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 */
|
||||
uint32_t arg_len = ecma_array_get_length (obj_p);
|
||||
return error;
|
||||
}
|
||||
#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 */
|
||||
for (uint32_t array_index = 0; array_index < arg_len; array_index++)
|
||||
if (ECMA_IS_VALUE_ERROR (get_value))
|
||||
{
|
||||
/* 5.b.iii.2 */
|
||||
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);
|
||||
return get_value;
|
||||
}
|
||||
|
||||
*length_p += arg_len;
|
||||
return ECMA_VALUE_EMPTY;
|
||||
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;
|
||||
return ECMA_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
/* 5.c.i */
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "ecma-exceptions.h"
|
||||
#include "ecma-gc.h"
|
||||
#include "ecma-globals.h"
|
||||
#include "ecma-helpers.h"
|
||||
#include "ecma-function-object.h"
|
||||
#include "ecma-lex-env.h"
|
||||
#include "ecma-string-object.h"
|
||||
@@ -2474,6 +2475,43 @@ ecma_object_class_is (ecma_object_t *object_p, /**< object */
|
||||
return false;
|
||||
} /* 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);
|
||||
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) */
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
||||
Reference in New Issue
Block a user