Skip to content

Commit 90fd881

Browse files
authored
Fix TypedArray initialization with another TypedArray (#3850)
Fixes #3836 JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent 1230d45 commit 90fd881

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

jerry-core/ecma/operations/ecma-arraybuffer-object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "ecma-arraybuffer-object.h"
1717
#include "ecma-try-catch-macro.h"
18+
#include "ecma-typedarray-object.h"
1819
#include "ecma-objects.h"
1920
#include "ecma-builtins.h"
2021
#include "ecma-exceptions.h"

jerry-core/ecma/operations/ecma-typedarray-object.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,21 @@ ecma_typedarray_create_object_with_length (ecma_length_t array_length, /**< leng
534534

535535
ecma_object_t *ctor_proto_p = ecma_get_object_from_value (ctor_proto);
536536

537-
ecma_value_t byte_length_val = ecma_make_uint32_value (byte_length);
538-
ecma_value_t new_arraybuffer = ecma_op_function_construct (ctor_proto_p,
539-
ctor_proto_p,
540-
&byte_length_val,
541-
1);
537+
ecma_object_t *prototype_p = ecma_op_get_prototype_from_constructor (ctor_proto_p,
538+
ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE);
539+
542540
ecma_deref_object (ctor_proto_p);
543-
ecma_free_value (byte_length_val);
544541

545-
new_arraybuffer_p = ecma_get_object_from_value (new_arraybuffer);
542+
if (JERRY_UNLIKELY (prototype_p == NULL))
543+
{
544+
return ECMA_VALUE_ERROR;
545+
}
546+
547+
new_arraybuffer_p = ecma_arraybuffer_new_object (byte_length);
548+
549+
ECMA_SET_NON_NULL_POINTER (new_arraybuffer_p->u2.prototype_cp, prototype_p);
550+
551+
ecma_deref_object (prototype_p);
546552
}
547553

548554
ecma_object_t *object_p = ecma_create_object (proto_p,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
function validate_typedarray (typedarray, result) {
16+
assert(typedarray.length === result.length);
17+
for (var i = 0; i < typedarray.length; i++) {
18+
assert(typedarray[i] === result[i]);
19+
}
20+
}
21+
22+
var v1 = new Float64Array(6);
23+
v1.buffer.constructor = Uint8Array;
24+
var v2 = new Float64Array(v1);
25+
26+
assert(v2.buffer.constructor === Uint8Array);
27+
validate_typedarray(v2, [0, 0, 0, 0, 0, 0]);
28+
29+
var v3 = new Uint32Array(6);
30+
v3.buffer.constructor = Float64Array;
31+
var v4 = new Uint8Array(v3);
32+
33+
assert(v4.buffer.constructor === Float64Array);
34+
validate_typedarray(v4, [0, 0, 0, 0, 0, 0]);
35+
36+
var v5 = new Uint32Array(6);
37+
v5.buffer.constructor = Set;
38+
var v6 = new Uint8Array(v5);
39+
40+
assert(v6.buffer.constructor === Set);
41+
validate_typedarray(v6, [0, 0, 0, 0, 0, 0]);
42+
43+
var species_called = false;
44+
45+
var v7 = new Float64Array(6);
46+
var v8 = v7.buffer;
47+
v8.constructor = {
48+
get [Symbol.species] (){
49+
species_called = true;
50+
return Uint8Array;
51+
}
52+
}
53+
var v9 = new Float64Array(v7);
54+
55+
assert(species_called);
56+
assert(Reflect.getPrototypeOf(v9.buffer) === Uint8Array.prototype);

0 commit comments

Comments
 (0)