Skip to content

Commit 60cbc93

Browse files
author
kisbg
authored
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 [email protected]
1 parent 053389d commit 60cbc93

File tree

8 files changed

+356
-131
lines changed

8 files changed

+356
-131
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,10 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
981981
static ecma_value_t
982982
ecma_builtin_array_prototype_object_sort_compare_helper (ecma_value_t lhs, /**< left value */
983983
ecma_value_t rhs, /**< right value */
984-
ecma_value_t compare_func) /**< compare function */
984+
ecma_value_t compare_func, /**< compare function */
985+
ecma_object_t *array_buffer_p) /**< arrayBuffer */
985986
{
987+
JERRY_UNUSED (array_buffer_p);
986988
/*
987989
* ECMA-262 v5, 15.4.4.11 NOTE1: Because non-existent property values always
988990
* 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
11711173
ecma_value_t sort_value = ecma_builtin_helper_array_merge_sort_helper (values_buffer,
11721174
(uint32_t) (copied_num),
11731175
arg1,
1174-
sort_cb);
1176+
sort_cb,
1177+
NULL);
11751178
if (ECMA_IS_VALUE_ERROR (sort_value))
11761179
{
11771180
goto clean_up;

jerry-core/ecma/builtin-objects/ecma-builtin-helpers-sort.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ ecma_builtin_helper_array_merge_sort_bottom_up (ecma_value_t *source_array_p, /*
3333
uint32_t end_idx, /**< second array end */
3434
ecma_value_t *output_array_p, /**< output array */
3535
ecma_value_t compare_func, /**< compare function */
36-
const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */
36+
const ecma_builtin_helper_sort_compare_fn_t sort_cb, /**< sorting cb */
37+
ecma_object_t *array_buffer_p) /* array_buffer_p */
3738
{
3839
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
3940
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, /*
4445

4546
if (i < right_idx && j < end_idx)
4647
{
47-
compare_value = sort_cb (source_array_p[i], source_array_p[j], compare_func);
48+
compare_value = sort_cb (source_array_p[i], source_array_p[j], compare_func, array_buffer_p);
4849
if (ECMA_IS_VALUE_ERROR (compare_value))
4950
{
5051
ret_value = ECMA_VALUE_ERROR;
@@ -78,7 +79,8 @@ ecma_value_t
7879
ecma_builtin_helper_array_merge_sort_helper (ecma_value_t *array_p, /**< array to sort */
7980
uint32_t length, /**< length */
8081
ecma_value_t compare_func, /**< compare function */
81-
const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */
82+
const ecma_builtin_helper_sort_compare_fn_t sort_cb, /**< sorting cb */
83+
ecma_object_t *array_buffer_p) /**< arrayBuffer */
8284
{
8385
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
8486
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
112114
e,
113115
dest_array_p,
114116
compare_func,
115-
sort_cb);
117+
sort_cb,
118+
array_buffer_p);
116119
if (ECMA_IS_VALUE_ERROR (ret_value))
117120
{
118121
break;

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,14 @@ ecma_builtin_helper_error_dispatch_call (jerry_error_t error_type, const ecma_va
239239
*/
240240
typedef ecma_value_t (*ecma_builtin_helper_sort_compare_fn_t) (ecma_value_t lhs, /**< left value */
241241
ecma_value_t rhs, /**< right value */
242-
ecma_value_t compare_func); /**< compare function */
242+
ecma_value_t compare_func, /**< compare function */
243+
ecma_object_t *array_buffer_p); /**< arrayBuffer */
243244

244245
ecma_value_t ecma_builtin_helper_array_merge_sort_helper (ecma_value_t *array_p,
245246
uint32_t length,
246247
ecma_value_t compare_func,
247-
const ecma_builtin_helper_sort_compare_fn_t sort_cb);
248+
const ecma_builtin_helper_sort_compare_fn_t sort_cb,
249+
ecma_object_t *array_buffer_p);
248250

249251
/**
250252
* @}

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ ecma_builtin_typedarray_prototype_exec_routine (ecma_value_t this_arg, /**< this
151151
return call_value;
152152
}
153153

154+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
155+
{
156+
ecma_free_value (call_value);
157+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
158+
}
159+
154160
bool to_bool_result = ecma_op_to_boolean (call_value);
155161
ecma_free_value (call_value);
156162

@@ -238,6 +244,13 @@ ecma_builtin_typedarray_prototype_map (ecma_value_t this_arg, /**< this object *
238244
return mapped_value;
239245
}
240246

247+
if (ecma_arraybuffer_is_detached (src_info_p->array_buffer_p))
248+
{
249+
ecma_free_value (mapped_value);
250+
ecma_free_value (new_typedarray);
251+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
252+
}
253+
241254
uint32_t target_byte_pos = index << target_info.shift;
242255
ecma_value_t set_element = target_typedarray_setter_cb (target_info.buffer_p + target_byte_pos, mapped_value);
243256
ecma_free_value (mapped_value);
@@ -341,6 +354,12 @@ ecma_builtin_typedarray_prototype_reduce_with_direction (ecma_value_t this_arg,
341354
return call_value;
342355
}
343356

357+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
358+
{
359+
ecma_free_value (call_value);
360+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
361+
}
362+
344363
accumulator = call_value;
345364

346365
if (is_right)
@@ -416,6 +435,13 @@ ecma_builtin_typedarray_prototype_filter (ecma_value_t this_arg, /**< this objec
416435
goto cleanup;
417436
}
418437

438+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
439+
{
440+
ecma_free_value (call_value);
441+
ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
442+
goto cleanup;
443+
}
444+
419445
if (ecma_op_to_boolean (call_value))
420446
{
421447
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
693719

694720
ecma_free_value (elem);
695721

722+
if (ecma_arraybuffer_is_detached (arraybuffer_p))
723+
{
724+
ecma_deref_object (source_obj_p);
725+
ecma_free_value (value_to_set);
726+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
727+
}
728+
696729
ecma_value_t set_element = target_typedarray_setter_cb (target_info.buffer_p + target_byte_index, value_to_set);
697730

698731
ecma_free_value (value_to_set);
@@ -1008,6 +1041,11 @@ ecma_builtin_typedarray_prototype_fill (ecma_value_t this_arg, /**< this object
10081041
uint32_t byte_index = begin_index_uint32 * info_p->element_size;
10091042
uint32_t limit = byte_index + subarray_length * info_p->element_size;
10101043

1044+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1045+
{
1046+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1047+
}
1048+
10111049
while (byte_index < limit)
10121050
{
10131051
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
10381076
static ecma_value_t
10391077
ecma_builtin_typedarray_prototype_sort_compare_helper (ecma_value_t lhs, /**< left value */
10401078
ecma_value_t rhs, /**< right value */
1041-
ecma_value_t compare_func) /**< compare function */
1079+
ecma_value_t compare_func, /**< compare function */
1080+
ecma_object_t *array_buffer_p) /**< array buffer */
10421081
{
10431082
if (ecma_is_value_undefined (compare_func))
10441083
{
@@ -1110,6 +1149,12 @@ ecma_builtin_typedarray_prototype_sort_compare_helper (ecma_value_t lhs, /**< le
11101149
return number_result;
11111150
}
11121151

1152+
if (ecma_arraybuffer_is_detached (array_buffer_p))
1153+
{
1154+
ecma_free_value (number_result);
1155+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1156+
}
1157+
11131158
// If the coerced value can't be represented as a Number, compare them as equals.
11141159
if (ecma_number_is_nan (ret_num))
11151160
{
@@ -1164,7 +1209,8 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
11641209
ecma_value_t sort_value = ecma_builtin_helper_array_merge_sort_helper (values_buffer,
11651210
(uint32_t) (info_p->length),
11661211
compare_func,
1167-
sort_cb);
1212+
sort_cb,
1213+
info_p->array_buffer_p);
11681214

11691215
if (ECMA_IS_VALUE_ERROR (sort_value))
11701216
{
@@ -1174,6 +1220,11 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
11741220

11751221
JERRY_ASSERT (sort_value == ECMA_VALUE_EMPTY);
11761222

1223+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1224+
{
1225+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1226+
}
1227+
11771228
ecma_typedarray_setter_fn_t typedarray_setter_cb = ecma_get_typedarray_setter_fn (info_p->id);
11781229

11791230
byte_index = 0;
@@ -1248,6 +1299,13 @@ ecma_builtin_typedarray_prototype_find_helper (ecma_value_t this_arg, /**< this
12481299
return call_value;
12491300
}
12501301

1302+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1303+
{
1304+
ecma_free_value (element_value);
1305+
ecma_free_value (call_value);
1306+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1307+
}
1308+
12511309
bool call_result = ecma_op_to_boolean (call_value);
12521310
ecma_free_value (call_value);
12531311

@@ -1466,6 +1524,11 @@ ecma_builtin_typedarray_prototype_copy_within (ecma_value_t this_arg, /**< this
14661524
}
14671525
}
14681526

1527+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1528+
{
1529+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1530+
}
1531+
14691532
if (relative_target >= info_p->length || relative_start >= relative_end || relative_end == 0)
14701533
{
14711534
return ecma_copy_value (this_arg);
@@ -1538,6 +1601,12 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
15381601
{
15391602
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
15401603

1604+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1605+
{
1606+
ecma_deref_object (new_typedarray_p);
1607+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1608+
}
1609+
15411610
lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
15421611
uint32_t src_byte_index = (relative_start * info_p->element_size);
15431612

@@ -1673,6 +1742,11 @@ ecma_builtin_typedarray_prototype_includes (ecma_typedarray_info_t *info_p, /**<
16731742

16741743
uint32_t search_pos = (uint32_t) from_index * info_p->element_size;
16751744

1745+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1746+
{
1747+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1748+
}
1749+
16761750
while (search_pos < limit)
16771751
{
16781752
ecma_value_t element = getter_cb (info_p->buffer_p + search_pos);

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,10 @@ ecma_builtin_typedarray_from (ecma_value_t this_arg, /**< 'this' argument */
9191
}
9292
}
9393

94-
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
95-
96-
const uint8_t builtin_id = ecma_get_object_builtin_id (obj_p);
97-
if (!ecma_typedarray_helper_is_typedarray (builtin_id))
98-
{
99-
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray constructor"));
100-
}
101-
102-
ecma_typedarray_type_t typedarray_id = ecma_typedarray_helper_builtin_to_typedarray_id (builtin_id);
103-
104-
ecma_object_t *proto_p = ecma_builtin_get (ecma_typedarray_helper_get_prototype_id (typedarray_id));
105-
const uint8_t element_size_shift = ecma_typedarray_helper_get_shift_size (typedarray_id);
106-
107-
return ecma_op_typedarray_from (source,
94+
return ecma_op_typedarray_from (this_arg,
95+
source,
10896
map_fn,
109-
this_in_fn,
110-
proto_p,
111-
element_size_shift,
112-
typedarray_id);
97+
this_in_fn);
11398

11499
} /* ecma_builtin_typedarray_from */
115100

0 commit comments

Comments
 (0)