Add @@species accessor to Array and Promise builtin objects (#3440)

Fully support @@species and SpeciesConstructor in Array and Promise builtins.
Also added partial support to TypedArrays, but a rework is needed in %TypedArray%.prototype functions' typedarray constructor, which is out of this patch's scope.

JerryScript-DCO-1.0-Signed-off-by: Daniel Balla dballa@inf.u-szeged.hu
This commit is contained in:
Daniel Balla
2019-12-16 11:44:15 +01:00
committed by Robert Fancsik
parent 40d930d62c
commit e0d8c4ca10
28 changed files with 743 additions and 116 deletions
+60 -19
View File
@@ -663,27 +663,68 @@ ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, /**< list of arguments that
* are passed to
* Array constructor */
ecma_length_t arguments_list_len, /**< length of the arguments' list */
bool is_treat_single_arg_as_length, /**< if the value is true,
* arguments_list_len is 1
* and single argument is Number,
* then treat the single argument
* as new Array's length rather
* than as single item of the
* Array */
ecma_object_t *object_p) /**< The object from whom the new array object
* is being created */
ecma_op_array_species_create (ecma_object_t *original_array_p, /**< The object from whom the new array object
* is being created */
ecma_length_t length) /**< length of the array */
{
/* TODO: Use @@species after Symbol has been implemented */
JERRY_UNUSED (object_p);
ecma_value_t constructor = ECMA_VALUE_UNDEFINED;
ecma_value_t original_array = ecma_make_object_value (original_array_p);
return ecma_op_create_array_object (arguments_list_p,
arguments_list_len,
is_treat_single_arg_as_length);
} /* ecma_op_create_array_object_by_constructor */
if (ecma_is_value_array (original_array))
{
constructor = ecma_op_object_get_by_magic_id (original_array_p, LIT_MAGIC_STRING_CONSTRUCTOR);
if (ECMA_IS_VALUE_ERROR (constructor))
{
return constructor;
}
if (ecma_is_constructor (constructor)
&& ecma_get_object_from_value (constructor) == ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY))
{
ecma_free_value (constructor);
constructor = ECMA_VALUE_UNDEFINED;
}
else if (ecma_is_value_object (constructor))
{
ecma_object_t *ctor_object_p = ecma_get_object_from_value (constructor);
constructor = ecma_op_object_get_by_symbol_id (ctor_object_p, LIT_MAGIC_STRING_SPECIES);
ecma_deref_object (ctor_object_p);
if (ECMA_IS_VALUE_ERROR (constructor))
{
return constructor;
}
if (ecma_is_value_null (constructor))
{
constructor = ECMA_VALUE_UNDEFINED;
}
}
}
if (ecma_is_value_undefined (constructor))
{
return ecma_make_object_value (ecma_op_new_fast_array_object (length));
}
if (!ecma_is_constructor (constructor))
{
ecma_free_value (constructor);
return ecma_raise_type_error (ECMA_ERR_MSG ("Invalid species constructor"));
}
ecma_value_t len_val = ecma_make_uint32_value (length);
ecma_object_t *ctor_object_p = ecma_get_object_from_value (constructor);
ecma_value_t ret_val = ecma_op_function_construct (ctor_object_p,
ECMA_VALUE_UNDEFINED,
&len_val,
1);
ecma_deref_object (ctor_object_p);
ecma_free_value (len_val);
return ret_val;
} /* ecma_op_array_species_create */
#endif /* ENABLED (JERRY_ES2015) */
/**
@@ -99,8 +99,8 @@ ecma_op_create_array_object (const ecma_value_t *arguments_list_p, ecma_length_t
#if ENABLED (JERRY_ES2015)
ecma_value_t
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
bool is_treat_single_arg_as_length, ecma_object_t *object_p);
ecma_op_array_species_create (ecma_object_t *original_array_p,
ecma_length_t length);
#endif /* ENABLED (JERRY_ES2015) */
ecma_value_t
+56
View File
@@ -2614,6 +2614,62 @@ ecma_op_is_regexp (ecma_value_t arg) /**< argument */
return ecma_make_boolean_value (ecma_object_is_regexp_object (arg));
} /* ecma_op_is_regexp */
/**
* SpeciesConstructor operation
* See also:
* ECMA-262 v6, 7.3.20;
*
* @return ecma_value
* returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_op_species_constructor (ecma_object_t *this_value, /**< This Value */
ecma_builtin_id_t default_constructor_id) /**< Builtin ID of default constructor */
{
ecma_object_t *default_constructor_p = ecma_builtin_get (default_constructor_id);
ecma_value_t constructor = ecma_op_object_get_by_magic_id (this_value, LIT_MAGIC_STRING_CONSTRUCTOR);
if (ECMA_IS_VALUE_ERROR (constructor))
{
return constructor;
}
if (ecma_is_value_undefined (constructor))
{
ecma_ref_object (default_constructor_p);
return ecma_make_object_value (default_constructor_p);
}
if (!ecma_is_value_object (constructor))
{
ecma_free_value (constructor);
return ecma_raise_type_error (ECMA_ERR_MSG ("Constructor must be an Object"));
}
ecma_object_t *ctor_object_p = ecma_get_object_from_value (constructor);
ecma_value_t species = ecma_op_object_get_by_symbol_id (ctor_object_p, LIT_MAGIC_STRING_SPECIES);
ecma_deref_object (ctor_object_p);
if (ECMA_IS_VALUE_ERROR (species))
{
return species;
}
if (ecma_is_value_undefined (species) || ecma_is_value_null (species))
{
ecma_ref_object (default_constructor_p);
return ecma_make_object_value (default_constructor_p);
}
if (!ecma_is_constructor (species))
{
ecma_free_value (species);
return ecma_raise_type_error (ECMA_ERR_MSG ("Species must be a Constructor"));
}
return species;
} /* ecma_op_species_constructor */
#endif /* ENABLED (JERRY_ES2015) */
/**
+3 -1
View File
@@ -16,6 +16,7 @@
#ifndef ECMA_OBJECTS_H
#define ECMA_OBJECTS_H
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-globals.h"
@@ -70,7 +71,8 @@ bool ecma_object_is_regexp_object (ecma_value_t arg);
#if ENABLED (JERRY_ES2015)
ecma_value_t ecma_op_is_concat_spreadable (ecma_value_t arg);
ecma_value_t ecma_op_is_regexp (ecma_value_t arg);
#endif /* !ENABLED (JERRY_ES2015) */
ecma_value_t ecma_op_species_constructor (ecma_object_t *this_value, ecma_builtin_id_t default_constructor_id);
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
@@ -573,6 +573,71 @@ ecma_op_create_promise_object (ecma_value_t executor, /**< the executor function
return ecma_make_object_value (object_p);
} /* ecma_op_create_promise_object */
/**
* 25.4.1.5.1 GetCapabilitiesExecutor Functions
*
* Checks and sets a promiseCapability's resolve and reject properties.
*
* @return ECMA_VALUE_UNDEFINED or TypeError
* returned value must be freed with ecma_free_value
*/
static ecma_value_t
ecma_op_get_capabilities_executor_cb (const ecma_value_t function_obj, /**< the function itself */
const ecma_value_t this_val, /**< this_arg of the function */
const ecma_value_t args_p[], /**< argument list */
const ecma_length_t args_count) /**< argument number */
{
JERRY_UNUSED (this_val);
/* 1. */
ecma_value_t capability = ecma_op_object_get_by_magic_id (ecma_get_object_from_value (function_obj),
LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_CAPABILITY);
JERRY_ASSERT (ecma_is_value_object (capability));
/* 2. */
ecma_object_t *capability_obj_p = ecma_get_object_from_value (capability);
/* 3. */
ecma_value_t resolve = ecma_op_object_get_by_magic_id (capability_obj_p,
LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_RESOLVE);
if (!ecma_is_value_undefined (resolve))
{
ecma_free_value (resolve);
ecma_deref_object (capability_obj_p);
return ecma_raise_type_error (ECMA_ERR_MSG ("Resolve must be undefined"));
}
/* 4. */
ecma_value_t reject = ecma_op_object_get_by_magic_id (capability_obj_p,
LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_REJECT);
if (!ecma_is_value_undefined (reject))
{
ecma_free_value (reject);
ecma_deref_object (capability_obj_p);
return ecma_raise_type_error (ECMA_ERR_MSG ("Reject must be undefined"));
}
/* 5. */
ecma_op_object_put (capability_obj_p,
ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_RESOLVE),
args_count > 0 ? args_p[0] : ECMA_VALUE_UNDEFINED,
false);
/* 6. */
ecma_op_object_put (capability_obj_p,
ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_REJECT),
args_count > 1 ? args_p[1] : ECMA_VALUE_UNDEFINED,
false);
ecma_deref_object (capability_obj_p);
/* 7. */
return ECMA_VALUE_UNDEFINED;
} /* ecma_op_get_capabilities_executor_cb */
/**
* Create a new PromiseCapability.
*
@@ -582,16 +647,22 @@ ecma_op_create_promise_object (ecma_value_t executor, /**< the executor function
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_promise_new_capability (void)
ecma_promise_new_capability (ecma_value_t constructor)
{
/* 1. */
if (!ecma_is_constructor (constructor))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Invalid capability"));
}
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor);
/* 3. */
ecma_object_t *capability_p = ecma_op_create_object_object_noarg ();
ecma_string_t *capability_str_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_CAPABILITY);
ecma_string_t *promise_str_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_PROMISE);
/* 4. */
ecma_object_t *executor_p;
executor_p = ecma_op_create_object_object_noarg ();
ecma_object_t *executor_p = ecma_op_create_external_function_object (ecma_op_get_capabilities_executor_cb);
ecma_value_t executor = ecma_make_object_value (executor_p);
/* 5. */
ecma_op_object_put (executor_p,
@@ -600,7 +671,18 @@ ecma_promise_new_capability (void)
false);
/* 6. */
ecma_value_t promise = ecma_op_create_promise_object (executor, ECMA_PROMISE_EXECUTOR_OBJECT);
ecma_value_t promise = ecma_op_function_construct (constructor_obj_p,
ECMA_VALUE_UNDEFINED,
&executor,
1);
ecma_deref_object (executor_p);
/* 7. */
if (ECMA_IS_VALUE_ERROR (promise))
{
ecma_deref_object (capability_p);
return promise;
}
/* 10. */
ecma_op_object_put (capability_p,
@@ -608,17 +690,8 @@ ecma_promise_new_capability (void)
promise,
false);
ecma_deref_object (executor_p);
/* 7. */
if (ECMA_IS_VALUE_ERROR (promise))
{
ecma_free_value (promise);
ecma_deref_object (capability_p);
return promise;
}
ecma_free_value (promise);
/* 8. */
ecma_string_t *resolve_str_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_RESOLVE);
ecma_value_t resolve = ecma_op_object_get (capability_p, resolve_str_p);
@@ -755,7 +828,14 @@ ecma_promise_then (ecma_value_t promise, /**< the promise which call 'then' */
return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not a Promise."));
}
ecma_value_t result_capability = ecma_promise_new_capability ();
ecma_value_t species = ecma_op_species_constructor (obj, ECMA_BUILTIN_ID_PROMISE);
if (ECMA_IS_VALUE_ERROR (species))
{
return species;
}
ecma_value_t result_capability = ecma_promise_new_capability (species);
ecma_free_value (species);
if (ECMA_IS_VALUE_ERROR (result_capability))
{
@@ -73,7 +73,7 @@ ecma_value_t
ecma_op_create_promise_object (ecma_value_t executor, ecma_promise_executor_type_t type);
uint8_t ecma_promise_get_state (ecma_object_t *promise_p);
ecma_value_t ecma_promise_get_result (ecma_object_t *promise_p);
ecma_value_t ecma_promise_new_capability (void);
ecma_value_t ecma_promise_new_capability (ecma_value_t constructor);
ecma_value_t
ecma_promise_then (ecma_value_t promise,
ecma_value_t on_fulfilled,
@@ -458,6 +458,17 @@ ecma_typedarray_helper_get_prototype_id (ecma_typedarray_type_t typedarray_id) /
return (ecma_builtin_id_t) (ECMA_FIRST_TYPEDARRAY_BUILTIN_PROTOTYPE_ID + typedarray_id);
} /* ecma_typedarray_helper_get_prototype_id */
/**
* Get the constructor ID of a TypedArray type.
*
* @return ecma_builtin_id_t
*/
ecma_builtin_id_t
ecma_typedarray_helper_get_constructor_id (ecma_typedarray_type_t typedarray_id) /**< the id of the typedarray **/
{
return (ecma_builtin_id_t) (ECMA_FIRST_TYPEDARRAY_BUILTIN_ROUTINE_ID + typedarray_id);
} /* ecma_typedarray_helper_get_constructor_id */
/**
* Get the built-in TypedArray type of the given object.
*
@@ -496,6 +507,7 @@ ecma_typedarray_helper_builtin_to_typedarray_id (ecma_builtin_id_t builtin_id)
*/
ecma_value_t
ecma_typedarray_create_object_with_length (ecma_length_t array_length, /**< length of the typedarray */
ecma_object_t *src_buffer_p, /**< source buffer */
ecma_object_t *proto_p, /**< prototype object */
uint8_t element_size_shift, /**< the size shift of the element length */
ecma_typedarray_type_t typedarray_id) /**< id of the typedarray */
@@ -512,7 +524,32 @@ ecma_typedarray_create_object_with_length (ecma_length_t array_length, /**< leng
return ecma_raise_range_error (ECMA_ERR_MSG ("Maximum typedarray size is reached."));
}
ecma_object_t *new_arraybuffer_p = ecma_arraybuffer_new_object (byte_length);
ecma_object_t *new_arraybuffer_p = NULL;
if (src_buffer_p == NULL)
{
new_arraybuffer_p = ecma_arraybuffer_new_object (byte_length);
}
else
{
ecma_value_t ctor_proto = ecma_op_species_constructor (src_buffer_p, ECMA_BUILTIN_ID_ARRAYBUFFER);
if (ECMA_IS_VALUE_ERROR (ctor_proto))
{
return ctor_proto;
}
ecma_object_t *ctor_proto_p = ecma_get_object_from_value (ctor_proto);
ecma_value_t byte_length_val = ecma_make_uint32_value (byte_length);
ecma_value_t new_arraybuffer = ecma_op_function_construct (ctor_proto_p,
ECMA_VALUE_UNDEFINED,
&byte_length_val,
1);
ecma_deref_object (ctor_proto_p);
ecma_free_value (byte_length_val);
new_arraybuffer_p = ecma_get_object_from_value (new_arraybuffer);
}
ecma_object_t *object_p = ecma_create_object (proto_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_PSEUDO_ARRAY);
@@ -597,6 +634,7 @@ ecma_typedarray_create_object_with_typedarray (ecma_object_t *typedarray_p, /**<
}
ecma_value_t new_typedarray = ecma_typedarray_create_object_with_length (array_length,
src_arraybuffer_p,
proto_p,
element_size_shift,
typedarray_id);
@@ -691,6 +729,7 @@ ecma_op_typedarray_from (ecma_value_t items_val, /**< the source array-like obje
/* 14 */
ecma_value_t new_typedarray = ecma_typedarray_create_object_with_length (len,
NULL,
proto_p,
element_size_shift,
typedarray_id);
@@ -899,7 +938,7 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
if (arguments_list_len == 0)
{
/* 22.2.1.1 */
ret = ecma_typedarray_create_object_with_length (0, proto_p, element_size_shift, typedarray_id);
ret = ecma_typedarray_create_object_with_length (0, NULL, proto_p, element_size_shift, typedarray_id);
}
else if (!ecma_is_value_object (arguments_list_p[0]))
{
@@ -920,6 +959,7 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
else
{
ret = ecma_typedarray_create_object_with_length (length,
NULL,
proto_p,
element_size_shift,
typedarray_id);
@@ -1154,51 +1194,19 @@ ecma_op_typedarray_define_index_prop (ecma_object_t *obj_p, /**< a TypedArray ob
* @return ecma_value_t
*/
ecma_value_t
ecma_op_create_typedarray_with_type_and_length (ecma_object_t *obj_p, /**< TypedArray object
* indicates the type */
ecma_op_create_typedarray_with_type_and_length (ecma_typedarray_type_t typedarray_id, /** TypedArray id */
ecma_length_t array_length) /**< length of the typedarray */
{
JERRY_ASSERT (ecma_object_is_typedarray (obj_p));
#if ENABLED (JERRY_ES2015)
ecma_value_t constructor_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_CONSTRUCTOR);
if (ECMA_IS_VALUE_ERROR (constructor_value)
|| !ecma_is_constructor (constructor_value))
{
ecma_free_value (constructor_value);
return ecma_raise_type_error (ECMA_ERR_MSG ("object.constructor is not a constructor."));
}
ecma_object_t *constructor_object_p = ecma_get_object_from_value (constructor_value);
ecma_value_t constructor_prototype = ecma_op_object_get_by_magic_id (constructor_object_p,
LIT_MAGIC_STRING_PROTOTYPE);
ecma_deref_object (constructor_object_p);
if (ECMA_IS_VALUE_ERROR (constructor_prototype))
{
return constructor_prototype;
}
#endif /* ENABLED (JERRY_ES2015) */
ecma_typedarray_type_t typedarray_id = ecma_get_typedarray_id (obj_p);
// TODO: assert validate typedarray_id
ecma_object_t *proto_p = ecma_builtin_get (ecma_typedarray_helper_get_prototype_id (typedarray_id));
uint8_t element_size_shift = ecma_typedarray_helper_get_shift_size (typedarray_id);
ecma_value_t new_obj = ecma_typedarray_create_object_with_length (array_length,
NULL,
proto_p,
element_size_shift,
typedarray_id);
#if ENABLED (JERRY_ES2015)
ecma_object_t *constructor_prototype_object_p = ecma_get_object_from_value (constructor_prototype);
ecma_object_t *new_obj_p = ecma_get_object_from_value (new_obj);
ECMA_SET_NON_NULL_POINTER (new_obj_p->u2.prototype_cp, constructor_prototype_object_p);
ecma_deref_object (constructor_prototype_object_p);
#endif /* ENABLED (JERRY_ES2015) */
return new_obj;
} /* ecma_op_create_typedarray_with_type_and_length */
@@ -39,6 +39,7 @@ void ecma_set_typedarray_element (lit_utf8_byte_t *dst_p,
bool ecma_typedarray_helper_is_typedarray (ecma_builtin_id_t builtin_id);
ecma_typedarray_type_t ecma_get_typedarray_id (ecma_object_t *obj_p);
ecma_builtin_id_t ecma_typedarray_helper_get_prototype_id (ecma_typedarray_type_t typedarray_id);
ecma_builtin_id_t ecma_typedarray_helper_get_constructor_id (ecma_typedarray_type_t typedarray_id);
ecma_typedarray_type_t ecma_typedarray_helper_builtin_to_typedarray_id (ecma_builtin_id_t builtin_id);
ecma_value_t ecma_op_typedarray_from (ecma_value_t items_val,
@@ -64,10 +65,11 @@ void ecma_op_typedarray_list_lazy_property_names (ecma_object_t *obj_p,
bool ecma_op_typedarray_define_index_prop (ecma_object_t *obj_p,
uint32_t index,
const ecma_property_descriptor_t *property_desc_p);
ecma_value_t ecma_op_create_typedarray_with_type_and_length (ecma_object_t *obj_p,
ecma_value_t ecma_op_create_typedarray_with_type_and_length (ecma_typedarray_type_t typedarray_id,
ecma_length_t array_length);
ecma_typedarray_info_t ecma_typedarray_get_info (ecma_object_t *typedarray_p);
ecma_value_t ecma_typedarray_create_object_with_length (ecma_length_t array_length,
ecma_object_t *src_arraybuffer_p,
ecma_object_t *proto_p,
uint8_t element_size_shift,
ecma_typedarray_type_t typedarray_id);