Skip to content

Commit 00ce4e4

Browse files
committed
Fix TypedArray initialization with another TypedArray
Fixes #3836 JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent d06c3a7 commit 00ce4e4

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

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

Lines changed: 1 addition & 4 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"
@@ -173,8 +174,6 @@ ecma_is_arraybuffer (ecma_value_t target) /**< the target value */
173174
ecma_length_t JERRY_ATTR_PURE
174175
ecma_arraybuffer_get_length (ecma_object_t *object_p) /**< pointer to the ArrayBuffer object */
175176
{
176-
JERRY_ASSERT (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));
177-
178177
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
179178
return ext_object_p->u.class_prop.u.length;
180179
} /* ecma_arraybuffer_get_length */
@@ -187,8 +186,6 @@ ecma_arraybuffer_get_length (ecma_object_t *object_p) /**< pointer to the ArrayB
187186
inline lit_utf8_byte_t * JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
188187
ecma_arraybuffer_get_buffer (ecma_object_t *object_p) /**< pointer to the ArrayBuffer object */
189188
{
190-
JERRY_ASSERT (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));
191-
192189
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
193190

194191
if (ECMA_ARRAYBUFFER_HAS_EXTERNAL_MEMORY (ext_object_p))

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 *data_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 (data_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, data_p);
550+
551+
ecma_deref_object (data_p);
546552
}
547553

548554
ecma_object_t *object_p = ecma_create_object (proto_p,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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]);

0 commit comments

Comments
 (0)