Skip to content

Fix TypedArray.slice with external array buffer #3080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1967,8 +1967,7 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume

ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);
lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
ecma_length_t src_byte_offset = ecma_typedarray_get_offset (typedarray_p);
uint32_t src_byte_index = (start * element_size) + src_byte_offset;
uint32_t src_byte_index = (start * element_size);

memcpy (new_typedarray_buffer_p,
typedarray_buffer_p + src_byte_index,
Expand Down
21 changes: 21 additions & 0 deletions tests/jerry/es2015/regression-test-issue-3078.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var arrb = new ArrayBuffer(13);
var arr = new Uint8Array(arrb, 9);
for (var idx = 0; idx < arr.length; idx++) {
arr[idx] = idx + 1;
}

assert(arr.slice(1).toString() == "2,3,4");
71 changes: 71 additions & 0 deletions tests/jerry/es2015/typedarray-prototype-slice-ext-arraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var typedarrays = [
Uint8ClampedArray,
Uint8Array,
Uint16Array,
Uint32Array,
Float32Array,
Float64Array,
Int8Array,
Int16Array,
Int32Array,
];

for (var typeIdx = 0; typeIdx < typedarrays.length; typeIdx++) {
var buffer = new ArrayBuffer(100);
var typed = new typedarrays[typeIdx](buffer, 8, 6);
try {
typed.prototype.slice.call(undefined);
assert(false);
} catch (err) {
assert(err instanceof TypeError);
}

for (var idx = 0; idx < typed.length; idx++) {
typed[idx] = idx;
}

// Test with normal inputs
assert(typed.slice(1, 3).toString() === "1,2");
assert(typed.slice(2, 5).toString() === "2,3,4");
assert(typed.slice(0, 6).toString() === "0,1,2,3,4,5");

// Test witn negative inputs
assert(typed.slice(-2, 5).toString() === "4");
assert(typed.slice(0, -3).toString() === "0,1,2");
assert(typed.slice(-1, -4).toString() === "");

// Test with bigger inputs then length
assert(typed.slice(7, 1).toString() === "");
assert(typed.slice(2, 9).toString() === "2,3,4,5");

// Test with undefined
assert(typed.slice(undefined, 4).toString() === "0,1,2,3");
assert(typed.slice(0, undefined).toString() === "0,1,2,3,4,5");
assert(typed.slice(undefined, undefined).toString() === "0,1,2,3,4,5");

// Test with NaN and +/-Infinity
assert(typed.slice(NaN, 3).toString() === "0,1,2");
assert(typed.slice(2, Infinity).toString() === "2,3,4,5");
assert(typed.slice(-Infinity, Infinity).toString() === "0,1,2,3,4,5");

// Test with default inputs
assert(typed.slice().toString() === "0,1,2,3,4,5");
assert(typed.slice(4).toString() === "4,5");

delete buffer;
}