Skip to content

Commit 024b804

Browse files
committed
Add copyWithin and slice function to TypedArray
The algorithm's are based on ECMA-262 v6, 22.2.3.5 and 22.2.3.23 Co-authored-by: Tibor Dusnoki tdusnoki@inf.u-szeged.hu JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
1 parent 3c69dfa commit 024b804

File tree

6 files changed

+361
-0
lines changed

6 files changed

+361
-0
lines changed

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

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,224 @@ ecma_builtin_typedarray_prototype_find_index (ecma_value_t this_arg, /**< this a
16871687
return ecma_builtin_typedarray_prototype_find_helper (this_arg, predicate, predicate_this_arg, false);
16881688
} /* ecma_builtin_typedarray_prototype_find_index */
16891689

1690+
/**
1691+
* Helper function to get the uint32_t value from an argument.
1692+
*
1693+
* @return ecma value
1694+
* Returned value must be freed with ecma_free_value.
1695+
*/
1696+
static ecma_value_t
1697+
ecma_builtin_get_uint32_value_from_argument (ecma_value_t arg_value, /**< this argument */
1698+
uint32_t length, /**< length of the TypedArray */
1699+
uint32_t *index, /**< pointer to the given index */
1700+
bool is_end_index) /**< true - normalize the end index */
1701+
{
1702+
ecma_number_t num_var;
1703+
ecma_value_t ret_value = ecma_get_number (arg_value, &num_var);
1704+
1705+
if (ECMA_IS_VALUE_ERROR (ret_value))
1706+
{
1707+
return ret_value;
1708+
}
1709+
1710+
if (is_end_index && ecma_number_is_nan (num_var))
1711+
{
1712+
*index = length;
1713+
}
1714+
else
1715+
{
1716+
*index = ecma_builtin_helper_array_index_normalize (num_var, length);
1717+
}
1718+
1719+
return ECMA_VALUE_EMPTY;
1720+
} /* ecma_builtin_get_uint32_value_from_argument */
1721+
1722+
/**
1723+
* The %TypedArray%.prototype object's 'copyWithin' routine
1724+
*
1725+
* See also:
1726+
* ECMA-262 v6, 22.2.3.5
1727+
*
1728+
* @return ecma value
1729+
* Returned value must be freed with ecma_free_value.
1730+
*/
1731+
static ecma_value_t
1732+
ecma_builtin_typedarray_prototype_copy_within (ecma_value_t this_arg, /**< this argument */
1733+
const ecma_value_t args[], /**< arguments list */
1734+
ecma_length_t args_number) /**< number of arguments */
1735+
{
1736+
if (!ecma_is_typedarray (this_arg))
1737+
{
1738+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
1739+
}
1740+
1741+
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
1742+
uint32_t length = ecma_typedarray_get_length (typedarray_p);
1743+
lit_magic_string_id_t class_id = ecma_object_get_class_name (typedarray_p);
1744+
lit_utf8_byte_t *typedarray_buffer_p = ecma_typedarray_get_buffer (typedarray_p);
1745+
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
1746+
uint8_t element_size = (uint8_t) (1 << shift);
1747+
uint32_t limit = length * element_size;
1748+
uint32_t target = 0;
1749+
uint32_t start = 0;
1750+
uint32_t end = limit;
1751+
1752+
if (args_number > 0)
1753+
{
1754+
ecma_value_t target_value = ecma_builtin_get_uint32_value_from_argument (args[0], length, &target, false);
1755+
target *= element_size;
1756+
1757+
if (ECMA_IS_VALUE_ERROR (target_value))
1758+
{
1759+
return target_value;
1760+
}
1761+
1762+
if (args_number > 1)
1763+
{
1764+
ecma_value_t start_value = ecma_builtin_get_uint32_value_from_argument (args[1], length, &start, false);
1765+
start *= element_size;
1766+
1767+
if (ECMA_IS_VALUE_ERROR (start_value))
1768+
{
1769+
return start_value;
1770+
}
1771+
1772+
if (args_number > 2)
1773+
{
1774+
ecma_value_t end_value = ecma_builtin_get_uint32_value_from_argument (args[2], length, &end, true);
1775+
end *= element_size;
1776+
1777+
if (ECMA_IS_VALUE_ERROR (end_value))
1778+
{
1779+
return end_value;
1780+
}
1781+
}
1782+
}
1783+
}
1784+
1785+
int32_t distance = (int32_t) (end - start);
1786+
int32_t offset = (int32_t) (limit - target);
1787+
int32_t count = JERRY_MIN (distance, offset);
1788+
count >>= shift;
1789+
int32_t direction = 1;
1790+
uint32_t counter = (uint32_t) ((count - 1) * element_size);
1791+
1792+
if (start < target && target < start + (uint32_t) (count * element_size))
1793+
{
1794+
direction *= -1;
1795+
start += counter;
1796+
target += counter;
1797+
}
1798+
1799+
while (count > 0)
1800+
{
1801+
ecma_number_t target_element = ecma_get_typedarray_element (typedarray_buffer_p + start,
1802+
class_id);
1803+
1804+
ecma_set_typedarray_element (typedarray_buffer_p + target, target_element, class_id);
1805+
1806+
if (direction < 0)
1807+
{
1808+
start -= element_size;
1809+
target -= element_size;
1810+
}
1811+
else
1812+
{
1813+
start += element_size;
1814+
target += element_size;
1815+
}
1816+
1817+
count--;
1818+
}
1819+
1820+
return ecma_copy_value (this_arg);
1821+
} /* ecma_builtin_typedarray_prototype_copy_within */
1822+
1823+
/**
1824+
* The %TypedArray%.prototype object's 'slice' routine
1825+
*
1826+
* See also:
1827+
* ECMA-262 v6, 22.2.3.23
1828+
*
1829+
* @return ecma value
1830+
* Returned value must be freed with ecma_free_value.
1831+
*/
1832+
static ecma_value_t
1833+
ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argument */
1834+
const ecma_value_t args[], /**< arguments list */
1835+
ecma_length_t args_number) /**< number of arguments */
1836+
{
1837+
if (!ecma_is_typedarray (this_arg))
1838+
{
1839+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
1840+
}
1841+
1842+
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
1843+
uint32_t length = ecma_typedarray_get_length (typedarray_p);
1844+
lit_magic_string_id_t class_id = ecma_object_get_class_name (typedarray_p);
1845+
lit_utf8_byte_t *typedarray_buffer_p = ecma_typedarray_get_buffer (typedarray_p);
1846+
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
1847+
uint8_t element_size = (uint8_t) (1 << shift);
1848+
uint32_t start = 0;
1849+
uint32_t end = length;
1850+
1851+
if (args_number > 0)
1852+
{
1853+
ecma_value_t start_value = ecma_builtin_get_uint32_value_from_argument (args[0], length, &start, false);
1854+
1855+
if (ECMA_IS_VALUE_ERROR (start_value))
1856+
{
1857+
return start_value;
1858+
}
1859+
1860+
if (args_number > 1)
1861+
{
1862+
ecma_value_t end_value = ecma_builtin_get_uint32_value_from_argument (args[1], length, &end, true);
1863+
1864+
if (ECMA_IS_VALUE_ERROR (end_value))
1865+
{
1866+
return end_value;
1867+
}
1868+
}
1869+
}
1870+
1871+
int32_t distance = (int32_t) (end - start);
1872+
uint32_t count = distance > 0 ? (uint32_t) distance : 0;
1873+
1874+
ecma_value_t new_typedarray = ecma_op_create_typedarray_with_type_and_length (typedarray_p, count);
1875+
1876+
if (ECMA_IS_VALUE_ERROR (new_typedarray))
1877+
{
1878+
return new_typedarray;
1879+
}
1880+
1881+
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
1882+
lit_magic_string_id_t new_class_id = ecma_object_get_class_name (new_typedarray_p);
1883+
lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
1884+
1885+
if (count > 0)
1886+
{
1887+
ecma_length_t src_byte_offset = ecma_typedarray_get_offset (typedarray_p);
1888+
uint32_t target_byte_index = 0;
1889+
uint32_t src_byte_index = (start * element_size) + src_byte_offset;
1890+
1891+
while (target_byte_index < count * element_size)
1892+
{
1893+
if (class_id == new_class_id)
1894+
{
1895+
memcpy (new_typedarray_buffer_p + target_byte_index,
1896+
typedarray_buffer_p + src_byte_index,
1897+
element_size);
1898+
}
1899+
1900+
src_byte_index += element_size;
1901+
target_byte_index += element_size;
1902+
}
1903+
}
1904+
1905+
return new_typedarray;
1906+
} /* ecma_builtin_typedarray_prototype_slice */
1907+
16901908
/**
16911909
* @}
16921910
* @}

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ ROUTINE (LIT_MAGIC_STRING_FILL, ecma_builtin_typedarray_prototype_fill, 3, 1)
7070
ROUTINE (LIT_MAGIC_STRING_SORT, ecma_builtin_typedarray_prototype_sort, 1, 1)
7171
ROUTINE (LIT_MAGIC_STRING_FIND, ecma_builtin_typedarray_prototype_find, 2, 1)
7272
ROUTINE (LIT_MAGIC_STRING_FIND_INDEX, ecma_builtin_typedarray_prototype_find_index, 2, 1)
73+
ROUTINE (LIT_MAGIC_STRING_COPY_WITHIN, ecma_builtin_typedarray_prototype_copy_within, NON_FIXED, 2)
74+
ROUTINE (LIT_MAGIC_STRING_SLICE, ecma_builtin_typedarray_prototype_slice, NON_FIXED, 2)
7375

7476
#if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR)
7577

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BYTE_OFFSET_UL, "byteOffset")
547547
#if ENABLED (JERRY_BUILTIN_STRING)
548548
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_CHAR_CODE_AT_UL, "charCodeAt")
549549
#endif
550+
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
551+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COPY_WITHIN, "copyWithin")
552+
#endif
550553
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ENUMERABLE, "enumerable")
551554
#if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW)
552555
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_FLOAT_32_UL, "getFloat32")
@@ -796,6 +799,8 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_TYPED_ARRAY_UL)
796799
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_BYTE_LENGTH_UL)
797800
#elif ENABLED (JERRY_BUILTIN_STRING)
798801
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_CHAR_CODE_AT_UL)
802+
#elif ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
803+
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_COPY_WITHIN)
799804
#else
800805
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_ENUMERABLE)
801806
#endif

jerry-core/lit/lit-magic-strings.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ LIT_MAGIC_STRING_UINT8_ARRAY_UL = "Uint8Array"
236236
LIT_MAGIC_STRING_BYTE_LENGTH_UL = "byteLength"
237237
LIT_MAGIC_STRING_BYTE_OFFSET_UL = "byteOffset"
238238
LIT_MAGIC_STRING_CHAR_CODE_AT_UL = "charCodeAt"
239+
LIT_MAGIC_STRING_COPY_WITHIN = "copyWithin"
239240
LIT_MAGIC_STRING_ENUMERABLE = "enumerable"
240241
LIT_MAGIC_STRING_GET_FLOAT_32_UL = "getFloat32"
241242
LIT_MAGIC_STRING_GET_FLOAT_64_UL = "getFloat64"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var normal_typedarras = [
17+
new Uint8Array([0, 1, 2, 3, 4, 5]),
18+
new Uint16Array([0, 1, 2, 3, 4, 5]),
19+
new Uint32Array([0, 1, 2, 3, 4, 5]),
20+
new Float32Array([0, 1, 2, 3, 4, 5]),
21+
new Float64Array([0, 1, 2, 3, 4, 5]),
22+
new Int8Array([0, 1, 2, 3, 4, 5]),
23+
new Int16Array([0, 1, 2, 3, 4, 5]),
24+
new Int32Array([0, 1, 2, 3, 4, 5])
25+
];
26+
27+
normal_typedarras.forEach(function(e){
28+
try {
29+
e.prototype.copyWithin.call (undefined);
30+
assert (false);
31+
} catch (e) {
32+
assert (e instanceof TypeError);
33+
}
34+
35+
// Test with normal inputs
36+
assert(e.copyWithin(2, 1 ,4).toString() === '0,1,1,2,3,5');
37+
assert(e.copyWithin(3, 4, 6).toString() === '0,1,1,3,5,5');
38+
assert(e.copyWithin(4, 1, 3).toString() === '0,1,1,3,1,1');
39+
40+
e.set([5, 2, 1, 3, 4, 4]);
41+
42+
// Test with negative inputs
43+
assert(e.copyWithin(2, -10, 3).toString() === '5,2,5,2,1,4');
44+
assert(e.copyWithin(-3, 1, 6).toString() === '5,2,5,2,5,2');
45+
assert(e.copyWithin(2, 0, -3).toString() === '5,2,5,2,5,2');
46+
47+
e.set([9, 3, 4, 5, 1, 7]);
48+
49+
// Test with default inputs
50+
assert(e.copyWithin().toString() === '9,3,4,5,1,7');
51+
assert(e.copyWithin(3).toString() === '9,3,4,9,3,4');
52+
assert(e.copyWithin(1, 5).toString() === '9,4,4,9,3,4');
53+
54+
e.set([12, 3, 1, 43, 2, 9]);
55+
56+
// Test with too big inputs
57+
assert(e.copyWithin(2, 12, 21).toString() === '12,3,1,43,2,9');
58+
assert(e.copyWithin(4, 5, 13).toString() === '12,3,1,43,9,9');
59+
60+
e.set([1, 2, 3, 1, 2, 1]);
61+
62+
// Test with undefined inputs
63+
assert(e.copyWithin(undefined, 2, 4).toString() === '3,1,3,1,2,1');
64+
assert(e.copyWithin(undefined, undefined, 2).toString() === '3,1,3,1,2,1');
65+
assert(e.copyWithin(3, undefined, 5).toString() === '3,1,3,3,1,3');
66+
67+
e.set([0, 2, 4, 2, 1, 5]);
68+
69+
// Test with NaN/+-Infinity
70+
assert(e.copyWithin(NaN, 3, 5).toString() === '2,1,4,2,1,5');
71+
assert(e.copyWithin(3, Infinity, 5).toString() === '2,1,4,2,1,5');
72+
assert(e.copyWithin(1, 3, -Infinity).toString() === '2,1,4,2,1,5');
73+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var normal_typedarras = [
17+
new Uint8Array([0, 1, 2, 3, 4, 5]),
18+
new Uint16Array([0, 1, 2, 3, 4, 5]),
19+
new Uint32Array([0, 1, 2, 3, 4, 5]),
20+
new Float32Array([0, 1, 2, 3, 4, 5]),
21+
new Float64Array([0, 1, 2, 3, 4, 5]),
22+
new Int8Array([0, 1, 2, 3, 4, 5]),
23+
new Int16Array([0, 1, 2, 3, 4, 5]),
24+
new Int32Array([0, 1, 2, 3, 4, 5])
25+
];
26+
27+
normal_typedarras.forEach(function(e){
28+
try {
29+
e.prototype.slice.call (undefined);
30+
assert (false);
31+
} catch (e) {
32+
assert (e instanceof TypeError);
33+
}
34+
35+
// Test with normal inputs
36+
assert(e.slice(1, 3).toString() === "1,2");
37+
assert(e.slice(2, 5).toString() === "2,3,4");
38+
assert(e.slice(0, 6).toString() === "0,1,2,3,4,5");
39+
40+
// Test witn negative inputs
41+
assert(e.slice(-2, 5).toString() === "4");
42+
assert(e.slice(0, -3).toString() === "0,1,2");
43+
assert(e.slice(-1, -4).toString() === "");
44+
45+
// Test with bigger inputs then length
46+
assert(e.slice(7, 1).toString() === "");
47+
assert(e.slice(2, 9).toString() === "2,3,4,5");
48+
49+
// Test with undefined
50+
assert(e.slice(undefined, 4).toString() === "0,1,2,3");
51+
assert(e.slice(0, undefined).toString() === "0,1,2,3,4,5");
52+
assert(e.slice(undefined, undefined).toString() === "0,1,2,3,4,5");
53+
54+
// Test with NaN and +/-Infinity
55+
assert(e.slice(NaN, 3).toString() === "0,1,2");
56+
assert(e.slice(2, Infinity).toString() === "2,3,4,5");
57+
assert(e.slice(-Infinity, Infinity).toString() === "0,1,2,3,4,5");
58+
59+
// Test with default inputs
60+
assert(e.slice().toString() === "0,1,2,3,4,5");
61+
assert(e.slice(4).toString() === "4,5");
62+
});

0 commit comments

Comments
 (0)