Skip to content

Commit 5d51fe8

Browse files
galpeterdbatyai
authored andcommitted
Fix TypedArray.slice with external array buffer (#3080)
In case of TypedArrays which were constructed with an ArrayBuffer the `slice` method incorrectly added the `byteOffset` value of when the elements were copied. There is no need to add the `byteOffset` value for the ArrayBuffer's contents pointer as it is already added by the `ecma_typedarray_get_buffer` call. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent 91818be commit 5d51fe8

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,8 +1967,7 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
19671967

19681968
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
19691969
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;
1970+
uint32_t src_byte_index = (start * element_size);
19721971

19731972
memcpy (new_typedarray_buffer_p,
19741973
typedarray_buffer_p + src_byte_index,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
var arrb = new ArrayBuffer(13);
16+
var arr = new Uint8Array(arrb, 9);
17+
for (var idx = 0; idx < arr.length; idx++) {
18+
arr[idx] = idx + 1;
19+
}
20+
21+
assert(arr.slice(1).toString() == "2,3,4");
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
Uint8ClampedArray,
18+
Uint8Array,
19+
Uint16Array,
20+
Uint32Array,
21+
Float32Array,
22+
Float64Array,
23+
Int8Array,
24+
Int16Array,
25+
Int32Array,
26+
];
27+
28+
for (var typeIdx = 0; typeIdx < typedarrays.length; typeIdx++) {
29+
var buffer = new ArrayBuffer(100);
30+
var typed = new typedarrays[typeIdx](buffer, 8, 6);
31+
try {
32+
typed.prototype.slice.call(undefined);
33+
assert(false);
34+
} catch (err) {
35+
assert(err instanceof TypeError);
36+
}
37+
38+
for (var idx = 0; idx < typed.length; idx++) {
39+
typed[idx] = idx;
40+
}
41+
42+
// Test with normal inputs
43+
assert(typed.slice(1, 3).toString() === "1,2");
44+
assert(typed.slice(2, 5).toString() === "2,3,4");
45+
assert(typed.slice(0, 6).toString() === "0,1,2,3,4,5");
46+
47+
// Test witn negative inputs
48+
assert(typed.slice(-2, 5).toString() === "4");
49+
assert(typed.slice(0, -3).toString() === "0,1,2");
50+
assert(typed.slice(-1, -4).toString() === "");
51+
52+
// Test with bigger inputs then length
53+
assert(typed.slice(7, 1).toString() === "");
54+
assert(typed.slice(2, 9).toString() === "2,3,4,5");
55+
56+
// Test with undefined
57+
assert(typed.slice(undefined, 4).toString() === "0,1,2,3");
58+
assert(typed.slice(0, undefined).toString() === "0,1,2,3,4,5");
59+
assert(typed.slice(undefined, undefined).toString() === "0,1,2,3,4,5");
60+
61+
// Test with NaN and +/-Infinity
62+
assert(typed.slice(NaN, 3).toString() === "0,1,2");
63+
assert(typed.slice(2, Infinity).toString() === "2,3,4,5");
64+
assert(typed.slice(-Infinity, Infinity).toString() === "0,1,2,3,4,5");
65+
66+
// Test with default inputs
67+
assert(typed.slice().toString() === "0,1,2,3,4,5");
68+
assert(typed.slice(4).toString() === "4,5");
69+
70+
delete buffer;
71+
}

0 commit comments

Comments
 (0)