Skip to content

Commit 24196b6

Browse files
szilagyiadamdbatyai
authored andcommitted
Add copyWithin and slice function to TypedArray (#2984)
The algorithm's are based on ECMA-262 v6, 22.2.3.5 and 22.2.3.23 Co-authored-by: Tibor Dusnoki [email protected] JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent 1c4cfe3 commit 24196b6

File tree

6 files changed

+326
-0
lines changed

6 files changed

+326
-0
lines changed

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

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,187 @@ ecma_builtin_typedarray_prototype_last_index_of (ecma_value_t this_arg, /**< thi
17971797
return ecma_builtin_typedarray_prototype_index_helper (this_arg, args, args_number, true);
17981798
} /* ecma_builtin_typedarray_prototype_last_index_of */
17991799

1800+
/**
1801+
* Helper function to get the uint32_t value from an argument.
1802+
*
1803+
* @return ecma value
1804+
* Returned value must be freed with ecma_free_value.
1805+
*/
1806+
static ecma_value_t
1807+
ecma_builtin_get_uint32_value_from_argument (ecma_value_t arg_value, /**< this argument */
1808+
uint32_t length, /**< length of the TypedArray */
1809+
uint32_t *index, /**< [out] pointer to the given index */
1810+
bool is_end_index) /**< true - normalize the end index */
1811+
{
1812+
ecma_number_t num_var;
1813+
ecma_value_t ret_value = ecma_get_number (arg_value, &num_var);
1814+
1815+
if (ECMA_IS_VALUE_ERROR (ret_value))
1816+
{
1817+
return ret_value;
1818+
}
1819+
1820+
if (is_end_index && ecma_number_is_nan (num_var))
1821+
{
1822+
*index = length;
1823+
}
1824+
else
1825+
{
1826+
*index = ecma_builtin_helper_array_index_normalize (num_var, length, false);
1827+
}
1828+
1829+
return ECMA_VALUE_EMPTY;
1830+
} /* ecma_builtin_get_uint32_value_from_argument */
1831+
1832+
/**
1833+
* The %TypedArray%.prototype object's 'copyWithin' routine
1834+
*
1835+
* See also:
1836+
* ECMA-262 v6, 22.2.3.5
1837+
*
1838+
* @return ecma value
1839+
* Returned value must be freed with ecma_free_value.
1840+
*/
1841+
static ecma_value_t
1842+
ecma_builtin_typedarray_prototype_copy_within (ecma_value_t this_arg, /**< this argument */
1843+
const ecma_value_t args[], /**< arguments list */
1844+
ecma_length_t args_number) /**< number of arguments */
1845+
{
1846+
if (!ecma_is_typedarray (this_arg))
1847+
{
1848+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
1849+
}
1850+
1851+
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
1852+
lit_utf8_byte_t *typedarray_buffer_p = ecma_typedarray_get_buffer (typedarray_p);
1853+
uint32_t length = ecma_typedarray_get_length (typedarray_p);
1854+
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
1855+
uint8_t element_size = (uint8_t) (1 << shift);
1856+
uint32_t target = 0;
1857+
uint32_t start = 0;
1858+
uint32_t end = length;
1859+
1860+
if (args_number > 0)
1861+
{
1862+
ecma_value_t target_value = ecma_builtin_get_uint32_value_from_argument (args[0], length, &target, false);
1863+
1864+
if (ECMA_IS_VALUE_ERROR (target_value))
1865+
{
1866+
return target_value;
1867+
}
1868+
1869+
if (args_number > 1)
1870+
{
1871+
ecma_value_t start_value = ecma_builtin_get_uint32_value_from_argument (args[1], length, &start, false);
1872+
1873+
if (ECMA_IS_VALUE_ERROR (start_value))
1874+
{
1875+
return start_value;
1876+
}
1877+
1878+
if (args_number > 2)
1879+
{
1880+
ecma_value_t end_value = ecma_builtin_get_uint32_value_from_argument (args[2], length, &end, true);
1881+
1882+
if (ECMA_IS_VALUE_ERROR (end_value))
1883+
{
1884+
return end_value;
1885+
}
1886+
}
1887+
}
1888+
}
1889+
1890+
int32_t distance = (int32_t) (end - start);
1891+
int32_t offset = (int32_t) (length - target);
1892+
int32_t count = JERRY_MIN (distance, offset);
1893+
1894+
if (target >= length || start >= length || end == 0)
1895+
{
1896+
return ecma_copy_value (this_arg);
1897+
}
1898+
else
1899+
{
1900+
memmove (typedarray_buffer_p + (target * element_size),
1901+
typedarray_buffer_p + (start * element_size),
1902+
(size_t) (count * element_size));
1903+
}
1904+
1905+
return ecma_copy_value (this_arg);
1906+
} /* ecma_builtin_typedarray_prototype_copy_within */
1907+
1908+
/**
1909+
* The %TypedArray%.prototype object's 'slice' routine
1910+
*
1911+
* See also:
1912+
* ECMA-262 v6, 22.2.3.23
1913+
*
1914+
* @return ecma value
1915+
* Returned value must be freed with ecma_free_value.
1916+
*/
1917+
static ecma_value_t
1918+
ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argument */
1919+
const ecma_value_t args[], /**< arguments list */
1920+
ecma_length_t args_number) /**< number of arguments */
1921+
{
1922+
if (!ecma_is_typedarray (this_arg))
1923+
{
1924+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
1925+
}
1926+
1927+
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
1928+
uint32_t length = ecma_typedarray_get_length (typedarray_p);
1929+
uint32_t start = 0;
1930+
uint32_t end = length;
1931+
1932+
if (args_number > 0)
1933+
{
1934+
ecma_value_t start_value = ecma_builtin_get_uint32_value_from_argument (args[0], length, &start, false);
1935+
1936+
if (ECMA_IS_VALUE_ERROR (start_value))
1937+
{
1938+
return start_value;
1939+
}
1940+
1941+
if (args_number > 1)
1942+
{
1943+
ecma_value_t end_value = ecma_builtin_get_uint32_value_from_argument (args[1], length, &end, true);
1944+
1945+
if (ECMA_IS_VALUE_ERROR (end_value))
1946+
{
1947+
return end_value;
1948+
}
1949+
}
1950+
}
1951+
1952+
int32_t distance = (int32_t) (end - start);
1953+
uint32_t count = distance > 0 ? (uint32_t) distance : 0;
1954+
1955+
ecma_value_t new_typedarray = ecma_op_create_typedarray_with_type_and_length (typedarray_p, count);
1956+
1957+
if (ECMA_IS_VALUE_ERROR (new_typedarray))
1958+
{
1959+
return new_typedarray;
1960+
}
1961+
1962+
if (count > 0)
1963+
{
1964+
lit_utf8_byte_t *typedarray_buffer_p = ecma_typedarray_get_buffer (typedarray_p);
1965+
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
1966+
uint8_t element_size = (uint8_t) (1 << shift);
1967+
1968+
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
1969+
lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
1970+
ecma_length_t src_byte_offset = ecma_typedarray_get_offset (typedarray_p);
1971+
uint32_t src_byte_index = (start * element_size) + src_byte_offset;
1972+
1973+
memcpy (new_typedarray_buffer_p,
1974+
typedarray_buffer_p + src_byte_index,
1975+
count * element_size);
1976+
}
1977+
1978+
return new_typedarray;
1979+
} /* ecma_builtin_typedarray_prototype_slice */
1980+
18001981
/**
18011982
* @}
18021983
* @}

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
@@ -72,6 +72,8 @@ 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)
7373
ROUTINE (LIT_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_typedarray_prototype_index_of, NON_FIXED, 1)
7474
ROUTINE (LIT_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_typedarray_prototype_last_index_of, NON_FIXED, 1)
75+
ROUTINE (LIT_MAGIC_STRING_COPY_WITHIN, ecma_builtin_typedarray_prototype_copy_within, NON_FIXED, 2)
76+
ROUTINE (LIT_MAGIC_STRING_SLICE, ecma_builtin_typedarray_prototype_slice, NON_FIXED, 2)
7577

7678
#if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR)
7779

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BYTE_OFFSET_UL, "byteOffset")
551551
#if ENABLED (JERRY_BUILTIN_STRING)
552552
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_CHAR_CODE_AT_UL, "charCodeAt")
553553
#endif
554+
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
555+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COPY_WITHIN, "copyWithin")
556+
#endif
554557
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ENUMERABLE, "enumerable")
555558
#if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW)
556559
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_FLOAT_32_UL, "getFloat32")
@@ -804,6 +807,8 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_TYPED_ARRAY_UL)
804807
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_BYTE_LENGTH_UL)
805808
#elif ENABLED (JERRY_BUILTIN_STRING)
806809
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_CHAR_CODE_AT_UL)
810+
#elif ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
811+
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_COPY_WITHIN)
807812
#else
808813
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_ENUMERABLE)
809814
#endif

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ LIT_MAGIC_STRING_UINT8_ARRAY_UL = "Uint8Array"
237237
LIT_MAGIC_STRING_BYTE_LENGTH_UL = "byteLength"
238238
LIT_MAGIC_STRING_BYTE_OFFSET_UL = "byteOffset"
239239
LIT_MAGIC_STRING_CHAR_CODE_AT_UL = "charCodeAt"
240+
LIT_MAGIC_STRING_COPY_WITHIN = "copyWithin"
240241
LIT_MAGIC_STRING_ENUMERABLE = "enumerable"
241242
LIT_MAGIC_STRING_GET_FLOAT_32_UL = "getFloat32"
242243
LIT_MAGIC_STRING_GET_FLOAT_64_UL = "getFloat64"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 typedarrays = [
17+
new Uint8ClampedArray ([0, 1, 2, 3, 4, 5]),
18+
new Uint8Array([0, 1, 2, 3, 4, 5]),
19+
new Uint16Array([0, 1, 2, 3, 4, 5]),
20+
new Uint32Array([0, 1, 2, 3, 4, 5]),
21+
new Float32Array([0, 1, 2, 3, 4, 5]),
22+
new Float64Array([0, 1, 2, 3, 4, 5]),
23+
new Int8Array([0, 1, 2, 3, 4, 5]),
24+
new Int16Array([0, 1, 2, 3, 4, 5]),
25+
new Int32Array([0, 1, 2, 3, 4, 5])
26+
];
27+
28+
typedarrays.forEach(function(e){
29+
try {
30+
e.prototype.copyWithin.call (undefined);
31+
assert (false);
32+
} catch (err) {
33+
assert (err instanceof TypeError);
34+
}
35+
36+
// Test with normal inputs
37+
assert(e.copyWithin(2, 1 ,4).toString() === '0,1,1,2,3,5');
38+
assert(e.copyWithin(3, 4, 6).toString() === '0,1,1,3,5,5');
39+
assert(e.copyWithin(4, 1, 3).toString() === '0,1,1,3,1,1');
40+
41+
e.set([5, 2, 1, 3, 4, 4]);
42+
43+
// Test with negative inputs
44+
assert(e.copyWithin(2, -10, 3).toString() === '5,2,5,2,1,4');
45+
assert(e.copyWithin(-3, 1, 6).toString() === '5,2,5,2,5,2');
46+
assert(e.copyWithin(2, 0, -3).toString() === '5,2,5,2,5,2');
47+
48+
e.set([9, 3, 4, 5, 1, 7]);
49+
50+
// Test with default inputs
51+
assert(e.copyWithin().toString() === '9,3,4,5,1,7');
52+
assert(e.copyWithin(3).toString() === '9,3,4,9,3,4');
53+
assert(e.copyWithin(1, 5).toString() === '9,4,4,9,3,4');
54+
55+
e.set([12, 3, 1, 43, 2, 9]);
56+
57+
// Test with too big inputs
58+
assert(e.copyWithin(2, 12, 21).toString() === '12,3,1,43,2,9');
59+
assert(e.copyWithin(4, 5, 13).toString() === '12,3,1,43,9,9');
60+
61+
e.set([1, 2, 3, 1, 2, 1]);
62+
63+
// Test with undefined inputs
64+
assert(e.copyWithin(undefined, 2, 4).toString() === '3,1,3,1,2,1');
65+
assert(e.copyWithin(undefined, undefined, 2).toString() === '3,1,3,1,2,1');
66+
assert(e.copyWithin(3, undefined, 5).toString() === '3,1,3,3,1,3');
67+
68+
e.set([0, 2, 4, 2, 1, 5]);
69+
70+
// Test with NaN/+-Infinity
71+
assert(e.copyWithin(NaN, 3, 5).toString() === '2,1,4,2,1,5');
72+
assert(e.copyWithin(3, Infinity, 5).toString() === '2,1,4,2,1,5');
73+
assert(e.copyWithin(1, 3, -Infinity).toString() === '2,1,4,2,1,5');
74+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 typedarrays = [
17+
new Uint8ClampedArray([0, 1, 2, 3, 4, 5]),
18+
new Uint8Array([0, 1, 2, 3, 4, 5]),
19+
new Uint16Array([0, 1, 2, 3, 4, 5]),
20+
new Uint32Array([0, 1, 2, 3, 4, 5]),
21+
new Float32Array([0, 1, 2, 3, 4, 5]),
22+
new Float64Array([0, 1, 2, 3, 4, 5]),
23+
new Int8Array([0, 1, 2, 3, 4, 5]),
24+
new Int16Array([0, 1, 2, 3, 4, 5]),
25+
new Int32Array([0, 1, 2, 3, 4, 5])
26+
];
27+
28+
typedarrays.forEach(function(e){
29+
try {
30+
e.prototype.slice.call (undefined);
31+
assert (false);
32+
} catch (err) {
33+
assert (err instanceof TypeError);
34+
}
35+
36+
// Test with normal inputs
37+
assert(e.slice(1, 3).toString() === "1,2");
38+
assert(e.slice(2, 5).toString() === "2,3,4");
39+
assert(e.slice(0, 6).toString() === "0,1,2,3,4,5");
40+
41+
// Test witn negative inputs
42+
assert(e.slice(-2, 5).toString() === "4");
43+
assert(e.slice(0, -3).toString() === "0,1,2");
44+
assert(e.slice(-1, -4).toString() === "");
45+
46+
// Test with bigger inputs then length
47+
assert(e.slice(7, 1).toString() === "");
48+
assert(e.slice(2, 9).toString() === "2,3,4,5");
49+
50+
// Test with undefined
51+
assert(e.slice(undefined, 4).toString() === "0,1,2,3");
52+
assert(e.slice(0, undefined).toString() === "0,1,2,3,4,5");
53+
assert(e.slice(undefined, undefined).toString() === "0,1,2,3,4,5");
54+
55+
// Test with NaN and +/-Infinity
56+
assert(e.slice(NaN, 3).toString() === "0,1,2");
57+
assert(e.slice(2, Infinity).toString() === "2,3,4,5");
58+
assert(e.slice(-Infinity, Infinity).toString() === "0,1,2,3,4,5");
59+
60+
// Test with default inputs
61+
assert(e.slice().toString() === "0,1,2,3,4,5");
62+
assert(e.slice(4).toString() === "4,5");
63+
});

0 commit comments

Comments
 (0)