Skip to content

Commit 0c78251

Browse files
author
Robert Fancsik
committed
Fix Typedarray.slice fastpath when the content type is matching
This patch fixes #4888. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 55acdf2 commit 0c78251

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,14 +1628,30 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
16281628
return ECMA_VALUE_ERROR;
16291629
}
16301630

1631-
JERRY_ASSERT (new_typedarray_info.offset == 0);
1632-
16331631
src_buffer_p += relative_start << info_p->shift;
16341632

16351633
if (info_p->id == new_typedarray_info.id)
16361634
{
16371635
// 22.2.3.23. Step 22. h-i.
1638-
memcpy (dst_buffer_p, src_buffer_p, count << info_p->shift);
1636+
1637+
if (JERRY_LIKELY (new_typedarray_info.offset == 0))
1638+
{
1639+
memcpy (dst_buffer_p, src_buffer_p, count << info_p->shift);
1640+
}
1641+
else if (count >= new_typedarray_info.offset)
1642+
{
1643+
uint32_t byte_shift = (uint32_t) (1 << info_p->shift);
1644+
1645+
while (count)
1646+
{
1647+
memmove (dst_buffer_p, src_buffer_p, byte_shift);
1648+
1649+
dst_buffer_p += byte_shift;
1650+
src_buffer_p += byte_shift;
1651+
1652+
count--;
1653+
}
1654+
}
16391655
}
16401656
else
16411657
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ab = new Int8Array(20).map((v, i) => i + 1).buffer;
16+
var ta = new Int8Array(ab, 0, 10);
17+
ta.constructor = {
18+
[Symbol.species]: function (len) {
19+
return new Int8Array(ab, 1, len);
20+
}
21+
};
22+
23+
var tb = ta.slice();
24+
25+
for (let e of ta) {
26+
assert(e === 1);
27+
}
28+
29+
for (let e of tb) {
30+
assert(e === 1);
31+
}

0 commit comments

Comments
 (0)