Update TypedArray Builtin function (#4633)

Also create TypedArray object with object method has been added.

JerryScript-DCO-1.0-Signed-off-by: bence gabor kis kisbg@inf.u-szeged.hu
This commit is contained in:
kisbg
2021-04-13 13:37:04 +02:00
committed by GitHub
parent 053389de80
commit 60cbc93cd7
8 changed files with 371 additions and 146 deletions
@@ -981,8 +981,10 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
static ecma_value_t
ecma_builtin_array_prototype_object_sort_compare_helper (ecma_value_t lhs, /**< left value */
ecma_value_t rhs, /**< right value */
ecma_value_t compare_func) /**< compare function */
ecma_value_t compare_func, /**< compare function */
ecma_object_t *array_buffer_p) /**< arrayBuffer */
{
JERRY_UNUSED (array_buffer_p);
/*
* ECMA-262 v5, 15.4.4.11 NOTE1: Because non-existent property values always
* compare greater than undefined property values, and undefined always
@@ -1171,7 +1173,8 @@ ecma_builtin_array_prototype_object_sort (ecma_value_t this_arg, /**< this argum
ecma_value_t sort_value = ecma_builtin_helper_array_merge_sort_helper (values_buffer,
(uint32_t) (copied_num),
arg1,
sort_cb);
sort_cb,
NULL);
if (ECMA_IS_VALUE_ERROR (sort_value))
{
goto clean_up;
@@ -33,7 +33,8 @@ ecma_builtin_helper_array_merge_sort_bottom_up (ecma_value_t *source_array_p, /*
uint32_t end_idx, /**< second array end */
ecma_value_t *output_array_p, /**< output array */
ecma_value_t compare_func, /**< compare function */
const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */
const ecma_builtin_helper_sort_compare_fn_t sort_cb, /**< sorting cb */
ecma_object_t *array_buffer_p) /* array_buffer_p */
{
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
uint32_t i = left_idx, j = right_idx;
@@ -44,7 +45,7 @@ ecma_builtin_helper_array_merge_sort_bottom_up (ecma_value_t *source_array_p, /*
if (i < right_idx && j < end_idx)
{
compare_value = sort_cb (source_array_p[i], source_array_p[j], compare_func);
compare_value = sort_cb (source_array_p[i], source_array_p[j], compare_func, array_buffer_p);
if (ECMA_IS_VALUE_ERROR (compare_value))
{
ret_value = ECMA_VALUE_ERROR;
@@ -78,7 +79,8 @@ ecma_value_t
ecma_builtin_helper_array_merge_sort_helper (ecma_value_t *array_p, /**< array to sort */
uint32_t length, /**< length */
ecma_value_t compare_func, /**< compare function */
const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */
const ecma_builtin_helper_sort_compare_fn_t sort_cb, /**< sorting cb */
ecma_object_t *array_buffer_p) /**< arrayBuffer */
{
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
JMEM_DEFINE_LOCAL_ARRAY (dest_array_p, length, ecma_value_t);
@@ -112,7 +114,8 @@ ecma_builtin_helper_array_merge_sort_helper (ecma_value_t *array_p, /**< array t
e,
dest_array_p,
compare_func,
sort_cb);
sort_cb,
array_buffer_p);
if (ECMA_IS_VALUE_ERROR (ret_value))
{
break;
@@ -239,12 +239,14 @@ ecma_builtin_helper_error_dispatch_call (jerry_error_t error_type, const ecma_va
*/
typedef ecma_value_t (*ecma_builtin_helper_sort_compare_fn_t) (ecma_value_t lhs, /**< left value */
ecma_value_t rhs, /**< right value */
ecma_value_t compare_func); /**< compare function */
ecma_value_t compare_func, /**< compare function */
ecma_object_t *array_buffer_p); /**< arrayBuffer */
ecma_value_t ecma_builtin_helper_array_merge_sort_helper (ecma_value_t *array_p,
uint32_t length,
ecma_value_t compare_func,
const ecma_builtin_helper_sort_compare_fn_t sort_cb);
const ecma_builtin_helper_sort_compare_fn_t sort_cb,
ecma_object_t *array_buffer_p);
/**
* @}
@@ -151,6 +151,12 @@ ecma_builtin_typedarray_prototype_exec_routine (ecma_value_t this_arg, /**< this
return call_value;
}
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
ecma_free_value (call_value);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
bool to_bool_result = ecma_op_to_boolean (call_value);
ecma_free_value (call_value);
@@ -238,6 +244,13 @@ ecma_builtin_typedarray_prototype_map (ecma_value_t this_arg, /**< this object *
return mapped_value;
}
if (ecma_arraybuffer_is_detached (src_info_p->array_buffer_p))
{
ecma_free_value (mapped_value);
ecma_free_value (new_typedarray);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
uint32_t target_byte_pos = index << target_info.shift;
ecma_value_t set_element = target_typedarray_setter_cb (target_info.buffer_p + target_byte_pos, mapped_value);
ecma_free_value (mapped_value);
@@ -341,6 +354,12 @@ ecma_builtin_typedarray_prototype_reduce_with_direction (ecma_value_t this_arg,
return call_value;
}
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
ecma_free_value (call_value);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
accumulator = call_value;
if (is_right)
@@ -416,6 +435,13 @@ ecma_builtin_typedarray_prototype_filter (ecma_value_t this_arg, /**< this objec
goto cleanup;
}
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
ecma_free_value (call_value);
ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
goto cleanup;
}
if (ecma_op_to_boolean (call_value))
{
memcpy (pass_value_p, info_p->buffer_p + byte_pos, info_p->element_size);
@@ -693,6 +719,13 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument
ecma_free_value (elem);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
ecma_deref_object (source_obj_p);
ecma_free_value (value_to_set);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
ecma_value_t set_element = target_typedarray_setter_cb (target_info.buffer_p + target_byte_index, value_to_set);
ecma_free_value (value_to_set);
@@ -1008,6 +1041,11 @@ ecma_builtin_typedarray_prototype_fill (ecma_value_t this_arg, /**< this object
uint32_t byte_index = begin_index_uint32 * info_p->element_size;
uint32_t limit = byte_index + subarray_length * info_p->element_size;
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
while (byte_index < limit)
{
ecma_value_t set_element = typedarray_setter_cb (info_p->buffer_p + byte_index, value_to_set);
@@ -1038,7 +1076,8 @@ ecma_builtin_typedarray_prototype_fill (ecma_value_t this_arg, /**< this object
static ecma_value_t
ecma_builtin_typedarray_prototype_sort_compare_helper (ecma_value_t lhs, /**< left value */
ecma_value_t rhs, /**< right value */
ecma_value_t compare_func) /**< compare function */
ecma_value_t compare_func, /**< compare function */
ecma_object_t *array_buffer_p) /**< array buffer */
{
if (ecma_is_value_undefined (compare_func))
{
@@ -1110,6 +1149,12 @@ ecma_builtin_typedarray_prototype_sort_compare_helper (ecma_value_t lhs, /**< le
return number_result;
}
if (ecma_arraybuffer_is_detached (array_buffer_p))
{
ecma_free_value (number_result);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
// If the coerced value can't be represented as a Number, compare them as equals.
if (ecma_number_is_nan (ret_num))
{
@@ -1164,7 +1209,8 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
ecma_value_t sort_value = ecma_builtin_helper_array_merge_sort_helper (values_buffer,
(uint32_t) (info_p->length),
compare_func,
sort_cb);
sort_cb,
info_p->array_buffer_p);
if (ECMA_IS_VALUE_ERROR (sort_value))
{
@@ -1174,6 +1220,11 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
JERRY_ASSERT (sort_value == ECMA_VALUE_EMPTY);
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
ecma_typedarray_setter_fn_t typedarray_setter_cb = ecma_get_typedarray_setter_fn (info_p->id);
byte_index = 0;
@@ -1248,6 +1299,13 @@ ecma_builtin_typedarray_prototype_find_helper (ecma_value_t this_arg, /**< this
return call_value;
}
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
ecma_free_value (element_value);
ecma_free_value (call_value);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
bool call_result = ecma_op_to_boolean (call_value);
ecma_free_value (call_value);
@@ -1466,6 +1524,11 @@ ecma_builtin_typedarray_prototype_copy_within (ecma_value_t this_arg, /**< this
}
}
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
if (relative_target >= info_p->length || relative_start >= relative_end || relative_end == 0)
{
return ecma_copy_value (this_arg);
@@ -1538,6 +1601,12 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
{
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
ecma_deref_object (new_typedarray_p);
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
uint32_t src_byte_index = (relative_start * info_p->element_size);
@@ -1673,6 +1742,11 @@ ecma_builtin_typedarray_prototype_includes (ecma_typedarray_info_t *info_p, /**<
uint32_t search_pos = (uint32_t) from_index * info_p->element_size;
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
}
while (search_pos < limit)
{
ecma_value_t element = getter_cb (info_p->buffer_p + search_pos);
@@ -91,25 +91,10 @@ ecma_builtin_typedarray_from (ecma_value_t this_arg, /**< 'this' argument */
}
}
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
const uint8_t builtin_id = ecma_get_object_builtin_id (obj_p);
if (!ecma_typedarray_helper_is_typedarray (builtin_id))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray constructor"));
}
ecma_typedarray_type_t typedarray_id = ecma_typedarray_helper_builtin_to_typedarray_id (builtin_id);
ecma_object_t *proto_p = ecma_builtin_get (ecma_typedarray_helper_get_prototype_id (typedarray_id));
const uint8_t element_size_shift = ecma_typedarray_helper_get_shift_size (typedarray_id);
return ecma_op_typedarray_from (source,
return ecma_op_typedarray_from (this_arg,
source,
map_fn,
this_in_fn,
proto_p,
element_size_shift,
typedarray_id);
this_in_fn);
} /* ecma_builtin_typedarray_from */