Revise internal array creation operations (#4291)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2020-10-16 15:24:50 +02:00
committed by GitHub
parent 841d536fce
commit d8955552d7
18 changed files with 350 additions and 314 deletions
@@ -188,18 +188,16 @@ ecma_builtin_array_prototype_object_concat (const ecma_value_t args[], /**< argu
{
/* 2. */
#if ENABLED (JERRY_ESNEXT)
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, 0);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
#else /* !ENABLED (JERRY_ESNEXT) */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
ecma_object_t *new_array_p = ecma_op_new_array_object (0);
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
ecma_length_t new_length = 0;
/* 5.b - 5.c for this_arg */
@@ -236,7 +234,7 @@ ecma_builtin_array_prototype_object_concat (const ecma_value_t args[], /**< argu
return set_length_value;
}
return new_array;
return ecma_make_object_value (new_array_p);
} /* ecma_builtin_array_prototype_object_concat */
/**
@@ -859,19 +857,17 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
bool use_fast_path = ecma_op_object_is_fast_array (obj_p);
ecma_length_t copied_length = (end > start) ? end - start : 0;
#if ENABLED (JERRY_ESNEXT)
ecma_value_t new_array = ecma_op_array_species_create (obj_p, copied_length);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, copied_length);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
use_fast_path &= ecma_op_object_is_fast_array (ecma_get_object_from_value (new_array));
#else /* !ENABLED (JERRY_ESNEXT) */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
use_fast_path &= ecma_op_object_is_fast_array (new_array_p);
#else /* !ENABLED (JERRY_ESNEXT) */
ecma_object_t *new_array_p = ecma_op_new_array_object (0);
#endif /* ENABLED (JERRY_ESNEXT) */
if (use_fast_path && copied_length > 0)
{
@@ -885,7 +881,7 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
* Very unlikely case: the buffer copied from is a fast buffer and the property list was deleted.
* There is no need to do any copy.
*/
return new_array;
return ecma_make_object_value (new_array_p);
}
ecma_extended_object_t *ext_to_obj_p = (ecma_extended_object_t *) new_array_p;
@@ -927,7 +923,7 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
ext_to_obj_p->u.array.u.hole_count &= ECMA_FAST_ARRAY_HOLE_ONE - 1;
return new_array;
return ecma_make_object_value (new_array_p);
}
}
@@ -983,7 +979,7 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
}
#endif /* ENABLED (JERRY_ESNEXT) */
return new_array;
return ecma_make_object_value (new_array_p);
} /* ecma_builtin_array_prototype_object_slice */
/**
@@ -1316,22 +1312,17 @@ ecma_builtin_array_prototype_object_splice (const ecma_value_t args[], /**< argu
}
/* ES11: 9. */
ecma_value_t new_array = ecma_op_array_species_create (obj_p, actual_delete_count);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, actual_delete_count);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
#else /* !ENABLED (JERRY_ESNEXT) */
/* ES5.1: 2. */
ecma_value_t length = ecma_make_length_value (actual_delete_count);
ecma_value_t new_array = ecma_op_create_array_object (&length, 1, true);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
ecma_free_value (length);
ecma_object_t *new_array_p = ecma_op_new_array_object (actual_delete_count);
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
/* ES5.1: 8, ES11: 10. */
ecma_length_t k = 0;
@@ -1498,7 +1489,7 @@ ecma_builtin_array_prototype_object_splice (const ecma_value_t args[], /**< argu
}
/* ES5.1: 17, ES11: 20. */
return new_array;
return ecma_make_object_value (new_array_p);
} /* ecma_builtin_array_prototype_object_splice */
/**
@@ -1960,21 +1951,16 @@ ecma_builtin_array_prototype_object_map (ecma_value_t arg1, /**< callbackfn */
/* 6. */
#if ENABLED (JERRY_ESNEXT)
ecma_value_t new_array = ecma_op_array_species_create (obj_p, len);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, len);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
#else /* !ENABLED (JERRY_ESNEXT) */
ecma_value_t length_value = ecma_make_uint32_value (len);
ecma_value_t new_array = ecma_op_create_array_object (&length_value, 1, true);
ecma_free_value (length_value);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
ecma_object_t *new_array_p = ecma_op_new_array_object (len);
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
JERRY_ASSERT (ecma_is_value_object (arg1));
ecma_object_t *func_object_p = ecma_get_object_from_value (arg1);
@@ -2059,25 +2045,22 @@ ecma_builtin_array_prototype_object_filter (ecma_value_t arg1, /**< callbackfn *
/* 6. */
#if ENABLED (JERRY_ESNEXT)
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, 0);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
/* ES11: 22.1.3.7. 7.c.iii.1 */
const uint32_t prop_flags = ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE | ECMA_IS_THROW;
#else /* !ENABLED (JERRY_ESNEXT) */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
ecma_object_t *new_array_p = ecma_op_new_array_object (0);
/* ES5.1: 15.4.4.20. 9.c.iii.1 */
const uint32_t prop_flags = ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE;
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
/* We already checked that arg1 is callable, so it will always be an object. */
JERRY_ASSERT (ecma_is_value_object (arg1));
ecma_object_t *func_object_p = ecma_get_object_from_value (arg1);
@@ -2142,7 +2125,7 @@ ecma_builtin_array_prototype_object_filter (ecma_value_t arg1, /**< callbackfn *
}
}
return new_array;
return ecma_make_object_value (new_array_p);
} /* ecma_builtin_array_prototype_object_filter */
/**
@@ -2825,15 +2808,15 @@ ecma_builtin_array_prototype_object_flat (const ecma_value_t args[], /**< argume
}
/* 5. */
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, 0);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
/* 6. */
ecma_value_t flatten_val = ecma_builtin_array_flatten_into_array (new_array,
ecma_value_t flatten_val = ecma_builtin_array_flatten_into_array (ecma_make_object_value (new_array_p),
obj_p,
len,
0,
@@ -2843,12 +2826,12 @@ ecma_builtin_array_prototype_object_flat (const ecma_value_t args[], /**< argume
if (ECMA_IS_VALUE_ERROR (flatten_val))
{
ecma_free_value (new_array);
ecma_deref_object (new_array_p);
return flatten_val;
}
/* 7. */
return new_array;
return ecma_make_object_value (new_array_p);
} /* ecma_builtin_array_prototype_object_flat */
/**
@@ -2872,15 +2855,15 @@ ecma_builtin_array_prototype_object_flat_map (ecma_value_t callback, /**< callba
}
/* 4. */
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
ecma_object_t *new_array_p = ecma_op_array_species_create (obj_p, 0);
if (ECMA_IS_VALUE_ERROR (new_array))
if (JERRY_UNLIKELY (new_array_p == NULL))
{
return new_array;
return ECMA_VALUE_ERROR;
}
/* 5. */
ecma_value_t flatten_val = ecma_builtin_array_flatten_into_array (new_array,
ecma_value_t flatten_val = ecma_builtin_array_flatten_into_array (ecma_make_object_value (new_array_p),
obj_p,
len,
0,
@@ -2889,12 +2872,12 @@ ecma_builtin_array_prototype_object_flat_map (ecma_value_t callback, /**< callba
this_arg);
if (ECMA_IS_VALUE_ERROR (flatten_val))
{
ecma_free_value (new_array);
ecma_deref_object (new_array_p);
return flatten_val;
}
/* 6. */
return new_array;
return ecma_make_object_value (new_array_p);
} /* ecma_builtin_array_prototype_object_flat_map */
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -122,36 +122,36 @@ ecma_builtin_array_object_from (ecma_value_t this_arg, /**< 'this' argument */
/* 6. */
if (!ecma_is_value_undefined (using_iterator))
{
ecma_value_t array;
ecma_object_t *array_obj_p;
/* 6.a */
if (ecma_is_constructor (constructor))
{
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor);
array = ecma_op_function_construct (constructor_obj_p, constructor_obj_p, NULL, 0);
ecma_value_t array = ecma_op_function_construct (constructor_obj_p, constructor_obj_p, NULL, 0);
if (ecma_is_value_undefined (array) || ecma_is_value_null (array))
{
ecma_free_value (using_iterator);
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert undefined or null to object"));
}
/* 6.c */
if (ECMA_IS_VALUE_ERROR (array))
{
ecma_free_value (using_iterator);
return array;
}
array_obj_p = ecma_get_object_from_value (array);
}
else
{
/* 6.b */
array = ecma_op_create_array_object (NULL, 0, false);
array_obj_p = ecma_op_new_array_object (0);
}
/* 6.c */
if (ECMA_IS_VALUE_ERROR (array))
{
ecma_free_value (using_iterator);
return array;
}
ecma_object_t *array_obj_p = ecma_get_object_from_value (array);
/* 6.d */
ecma_value_t next_method;
ecma_value_t iterator = ecma_op_get_iterator (items, using_iterator, &next_method);
@@ -160,7 +160,7 @@ ecma_builtin_array_object_from (ecma_value_t this_arg, /**< 'this' argument */
/* 6.e */
if (ECMA_IS_VALUE_ERROR (iterator))
{
ecma_free_value (array);
ecma_deref_object (array_obj_p);
return iterator;
}
@@ -199,7 +199,7 @@ ecma_builtin_array_object_from (ecma_value_t this_arg, /**< 'this' argument */
ecma_free_value (iterator);
ecma_free_value (next_method);
/* 6.g.iv.3 */
return array;
return ecma_make_object_value (array_obj_p);
}
/* 6.g.v */
@@ -257,7 +257,7 @@ ecma_builtin_array_object_from (ecma_value_t this_arg, /**< 'this' argument */
iterator_cleanup:
ecma_free_value (iterator);
ecma_free_value (next_method);
ecma_free_value (array);
ecma_deref_object (array_obj_p);
return ret_value;
}
@@ -283,41 +283,43 @@ iterator_cleanup:
goto cleanup;
}
len_value = ecma_make_length_value (len);
/* 12. */
ecma_value_t array;
ecma_object_t *array_obj_p;
/* 12.a */
if (ecma_is_constructor (constructor))
{
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor);
array = ecma_op_function_construct (constructor_obj_p, constructor_obj_p, &len_value, 1);
len_value = ecma_make_length_value (len);
ecma_value_t array = ecma_op_function_construct (constructor_obj_p, constructor_obj_p, &len_value, 1);
ecma_free_value (len_value);
if (ecma_is_value_undefined (array) || ecma_is_value_null (array))
{
ecma_free_value (len_value);
ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert undefined or null to object"));
goto cleanup;
}
/* 14. */
if (ECMA_IS_VALUE_ERROR (array))
{
goto cleanup;
}
array_obj_p = ecma_get_object_from_value (array);
}
else
{
/* 13.a */
array = ecma_op_create_array_object (&len_value, 1, true);
array_obj_p = ecma_op_new_array_object_from_length (len);
if (JERRY_UNLIKELY (array_obj_p == NULL))
{
goto cleanup;
}
}
ecma_free_value (len_value);
/* 14. */
if (ECMA_IS_VALUE_ERROR (array))
{
goto cleanup;
}
ecma_object_t *array_obj_p = ecma_get_object_from_value (array);
/* 15. */
ecma_length_t k = 0;
@@ -412,7 +414,7 @@ ecma_builtin_array_object_of (ecma_value_t this_arg, /**< 'this' argument */
{
if (!ecma_is_constructor (this_arg))
{
return ecma_op_create_array_object (arguments_list_p, arguments_list_len, false);
return ecma_op_new_array_object_from_buffer (arguments_list_p, arguments_list_len);
}
ecma_value_t len = ecma_make_uint32_value (arguments_list_len);
@@ -481,7 +483,8 @@ ecma_builtin_array_species_get (ecma_value_t this_value) /**< This Value */
/**
* Handle calling [[Call]] of built-in Array object
*
* @return ecma value
* @return ECMA_VALUE_ERROR - if the array construction fails
* constructed array object - otherwise
*/
ecma_value_t
ecma_builtin_array_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
@@ -489,13 +492,28 @@ ecma_builtin_array_dispatch_call (const ecma_value_t *arguments_list_p, /**< arg
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
return ecma_op_create_array_object (arguments_list_p, arguments_list_len, true);
if (arguments_list_len != 1
|| !ecma_is_value_number (arguments_list_p[0]))
{
return ecma_op_new_array_object_from_buffer (arguments_list_p, arguments_list_len);
}
ecma_number_t num = ecma_get_number_from_value (arguments_list_p[0]);
uint32_t num_uint32 = ecma_number_to_uint32 (num);
if (num != ((ecma_number_t) num_uint32))
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid array length."));
}
return ecma_make_object_value (ecma_op_new_array_object (num_uint32));
} /* ecma_builtin_array_dispatch_call */
/**
* Handle calling [[Construct]] of built-in Array object
*
* @return ecma value
* @return ECMA_VALUE_ERROR - if the array construction fails
* constructed array object - otherwise
*/
ecma_value_t
ecma_builtin_array_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
@@ -504,7 +522,7 @@ ecma_builtin_array_dispatch_construct (const ecma_value_t *arguments_list_p, /**
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
#if !ENABLED (JERRY_ESNEXT)
return ecma_op_create_array_object (arguments_list_p, arguments_list_len, true);
return ecma_builtin_array_dispatch_call (arguments_list_p, arguments_list_len);
#else /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *proto_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target),
ECMA_BUILTIN_ID_ARRAY_PROTOTYPE);
@@ -514,12 +532,12 @@ ecma_builtin_array_dispatch_construct (const ecma_value_t *arguments_list_p, /**
return ECMA_VALUE_ERROR;
}
ecma_value_t result = ecma_op_create_array_object (arguments_list_p, arguments_list_len, true);
ecma_value_t result = ecma_builtin_array_dispatch_call (arguments_list_p, arguments_list_len);
if (ECMA_IS_VALUE_ERROR (result))
{
ecma_deref_object (proto_p);
return ECMA_VALUE_ERROR;
return result;
}
ecma_object_t *object_p = ecma_get_object_from_value (result);
@@ -543,11 +543,7 @@ ecma_builtin_json_parse_value (ecma_json_token_t *token_p) /**< token argument *
case TOKEN_LEFT_SQUARE:
{
uint32_t length = 0;
ecma_value_t array_construction = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (array_construction));
ecma_object_t *array_p = ecma_get_object_from_value (array_construction);
ecma_object_t *array_p = ecma_op_new_array_object (0);
ecma_builtin_json_parse_next_token (token_p, true);
@@ -694,12 +694,8 @@ ecma_builtin_object_object_keys_values_helper (ecma_object_t *obj_p, /**< routin
return ECMA_VALUE_ERROR;
}
ecma_value_t *names_buffer_p = props_p->buffer_p;
/* 3. */
ecma_value_t array_value = ecma_op_create_array_object (names_buffer_p, props_p->item_count, false);
ecma_collection_free (props_p);
return array_value;
return ecma_op_new_array_object_from_collection (props_p, option != ECMA_ENUMERABLE_PROPERTY_KEYS);
} /* ecma_builtin_object_object_keys_values_helper */
/**
@@ -1342,7 +1338,7 @@ ecma_op_object_get_own_property_keys (ecma_value_t this_arg, /**< this argument
}
/* 3. */
ecma_collection_t *name_list = ecma_new_collection ();
ecma_collection_t *name_list_p = ecma_new_collection ();
/* 4. */
for (uint32_t i = 0; i < props_p->item_count; i++)
@@ -1354,23 +1350,22 @@ ecma_op_object_get_own_property_keys (ecma_value_t this_arg, /**< this argument
|| (ecma_is_value_string (prop_name) && type == ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_NAMES))
{
ecma_ref_ecma_string (name_p);
ecma_collection_push_back (name_list, prop_name);
ecma_collection_push_back (name_list_p, prop_name);
}
}
ecma_value_t result_array = ecma_op_create_array_object (name_list->buffer_p, name_list->item_count, false);
ecma_value_t result_array = ecma_op_new_array_object_from_collection (name_list_p, false);
ecma_deref_object (obj_p);
ecma_collection_free (name_list);
ecma_collection_free (props_p);
return result_array;
#else /* !ENABLED (JERRY_ESNEXT) */
JERRY_UNUSED (type);
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_collection_t *props_p = ecma_op_object_own_property_keys (obj_p);
ecma_value_t result_array = ecma_op_create_array_object (props_p->buffer_p, props_p->item_count, false);
return ecma_op_new_array_object_from_collection (props_p, false);
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_collection_free (props_p);
return result_array;
} /* ecma_op_object_get_own_property_keys */
/**
@@ -1491,7 +1486,7 @@ ecma_builtin_object_dispatch_routine (uint16_t builtin_routine_id, /**< built-in
const int option = builtin_routine_id - ECMA_OBJECT_ROUTINE_KEYS;
result = ecma_builtin_object_object_keys_values_helper (obj_p,
(ecma_enumerable_property_names_options_t) option);
(ecma_enumerable_property_names_options_t) option);
break;
}
case ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_DESCRIPTOR:
@@ -253,7 +253,7 @@ ecma_builtin_promise_perform_all (ecma_value_t iterator, /**< iteratorRecord */
ecma_object_t *resolve_func_p = ecma_get_object_from_value (resolve);
/* 3. */
ecma_object_t *values_array_obj_p = ecma_op_new_fast_array_object (0);
ecma_object_t *values_array_obj_p = ecma_op_new_array_object (0);
ecma_value_t values_array = ecma_make_object_value (values_array_obj_p);
/* 4. */
ecma_value_t remaining = ecma_op_create_number_object (ecma_make_integer_value (1));
@@ -176,10 +176,7 @@ ecma_builtin_reflect_dispatch_routine (uint16_t builtin_routine_id, /**< built-i
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
/* 3. */
ecma_value_t new_array = ecma_op_create_array_object (prop_names->buffer_p, prop_names->item_count, false);
ecma_collection_free (prop_names);
return new_array;
return ecma_op_new_array_object_from_collection (prop_names, false);
}
if (builtin_routine_id == ECMA_REFLECT_OBJECT_CONSTRUCT)
@@ -909,7 +909,8 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_value, /**< this a
}
/* 6. */
result = ecma_op_create_array_object (NULL, 0, false);
ecma_object_t *array_p = ecma_op_new_array_object (0);
result = ecma_make_object_value (array_p);
/* 14. */
if (limit == 0)
@@ -917,7 +918,7 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_value, /**< this a
goto cleanup_separator;
}
ecma_object_t *array_p = ecma_get_object_from_value (result);
/* 6. */
lit_utf8_size_t array_length = 0;
/* 15. */