Skip to content

Commit dae234f

Browse files
authored
Correctly convert data in case of TypedArray slice (#4796)
When executin the TypedArray's slice method it is possible to have the a different element sized output TypedArray. In such case the data must be converted to the desired element type/size. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent 4912e3b commit dae234f

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,28 +1589,55 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
15891589
ecma_value_t new_typedarray = ecma_typedarray_species_create (this_arg, &len, 1);
15901590
ecma_free_value (len);
15911591

1592-
if (ECMA_IS_VALUE_ERROR (new_typedarray))
1592+
if (ECMA_IS_VALUE_ERROR (new_typedarray) || count == 0)
15931593
{
15941594
return new_typedarray;
15951595
}
15961596

1597-
if (count > 0)
1597+
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
1598+
1599+
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
15981600
{
1599-
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
1601+
ecma_deref_object (new_typedarray_p);
1602+
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1603+
}
16001604

1601-
if (ecma_arraybuffer_is_detached (info_p->array_buffer_p))
1602-
{
1603-
ecma_deref_object (new_typedarray_p);
1604-
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
1605-
}
1605+
ecma_typedarray_info_t new_typedarray_info = ecma_typedarray_get_info (new_typedarray_p);
16061606

1607-
lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
1607+
if (info_p->id == new_typedarray_info.id)
1608+
{
1609+
// 22.2.3.23. Step 22. h-i.
16081610
uint32_t src_byte_index = (relative_start * info_p->element_size);
16091611

1610-
memcpy (new_typedarray_buffer_p,
1612+
memcpy (new_typedarray_info.buffer_p,
16111613
info_p->buffer_p + src_byte_index,
16121614
count * info_p->element_size);
16131615
}
1616+
else
1617+
{
1618+
// 22.2.3.23. Step 21. b.
1619+
ecma_typedarray_getter_fn_t src_typedarray_getter_cb = ecma_get_typedarray_getter_fn (info_p->id);
1620+
ecma_typedarray_setter_fn_t new_typedarray_setter_cb = ecma_get_typedarray_setter_fn (new_typedarray_info.id);
1621+
1622+
uint32_t src_byte_index = (relative_start * info_p->element_size);
1623+
uint32_t dst_byte_index = 0;
1624+
1625+
for (uint32_t idx = 0; idx < count; idx++)
1626+
{
1627+
ecma_value_t element = src_typedarray_getter_cb (info_p->buffer_p + src_byte_index);
1628+
ecma_value_t set_element = new_typedarray_setter_cb (new_typedarray_info.buffer_p + dst_byte_index, element);
1629+
ecma_free_value (element);
1630+
1631+
if (ECMA_IS_VALUE_ERROR (set_element))
1632+
{
1633+
ecma_deref_object (new_typedarray_p);
1634+
return set_element;
1635+
}
1636+
1637+
src_byte_index += info_p->element_size;
1638+
dst_byte_index += new_typedarray_info.element_size;
1639+
}
1640+
}
16141641

16151642
return new_typedarray;
16161643
} /* ecma_builtin_typedarray_prototype_slice */

tests/test262-esnext-excludelist.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<test id="built-ins/String/prototype/toLocaleLowerCase/special_casing_conditional.js"><reason></reason></test>
5353
<test id="built-ins/String/prototype/toLowerCase/Final_Sigma_U180E.js"><reason></reason></test>
5454
<test id="built-ins/String/prototype/toLowerCase/special_casing_conditional.js"><reason></reason></test>
55-
<test id="built-ins/TypedArray/prototype/slice/set-values-from-different-ctor-type.js"><reason></reason></test>
5655
<test id="language/block-scope/syntax/redeclaration/async-function-name-redeclaration-attempt-with-async-function.js"><reason></reason></test>
5756
<test id="language/block-scope/syntax/redeclaration/async-function-name-redeclaration-attempt-with-function.js"><reason></reason></test>
5857
<test id="language/block-scope/syntax/redeclaration/async-function-name-redeclaration-attempt-with-generator.js"><reason></reason></test>

0 commit comments

Comments
 (0)