Skip to content

Commit a6208a7

Browse files
committed
Fix the TypedArray initialization where the internal buffer is another TypedArray
Fixes #3836 JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent d06c3a7 commit a6208a7

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

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

Lines changed: 29 additions & 2 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,7 +174,20 @@ 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+
/**
178+
* It is possible during the initialization of a TypedArray with another TypedArray,
179+
* that the buffer is a TypedArray object, not an ArrayBuffer object. In these
180+
* cases we should return with the internal TypedArray's buffer length.
181+
*/
182+
if (!ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
183+
{
184+
if (ecma_is_typedarray (ecma_make_object_value (object_p)))
185+
{
186+
ecma_extended_object_t *buffer_object_p = (ecma_extended_object_t *) object_p;
187+
ecma_value_t buffer = buffer_object_p->u.pseudo_array.u2.arraybuffer;
188+
object_p = ecma_get_object_from_value (buffer);
189+
}
190+
}
177191

178192
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
179193
return ext_object_p->u.class_prop.u.length;
@@ -187,7 +201,20 @@ ecma_arraybuffer_get_length (ecma_object_t *object_p) /**< pointer to the ArrayB
187201
inline lit_utf8_byte_t * JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
188202
ecma_arraybuffer_get_buffer (ecma_object_t *object_p) /**< pointer to the ArrayBuffer object */
189203
{
190-
JERRY_ASSERT (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));
204+
/**
205+
* It is possible during the initialization of a TypedArray with another TypedArray,
206+
* that the buffer is a TypedArray object, not an ArrayBuffer object. In these
207+
* cases we should return with the internal TypedArray's buffer.
208+
*/
209+
if (!ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
210+
{
211+
if (ecma_is_typedarray (ecma_make_object_value (object_p)))
212+
{
213+
ecma_extended_object_t *buffer_object_p = (ecma_extended_object_t *) object_p;
214+
ecma_value_t buffer = buffer_object_p->u.pseudo_array.u2.arraybuffer;
215+
object_p = ecma_get_object_from_value (buffer);
216+
}
217+
}
191218

192219
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
193220

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,16 @@ ecma_typedarray_get_length (ecma_object_t *typedarray_p) /**< the pointer to the
997997
ecma_length_t buffer_length = ecma_arraybuffer_get_length (arraybuffer_p);
998998
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
999999

1000+
if (!ecma_object_class_is (arraybuffer_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
1001+
{
1002+
if (ecma_is_typedarray (ecma_make_object_value (arraybuffer_p)))
1003+
{
1004+
uint8_t internal_shift = ecma_typedarray_get_element_size_shift (arraybuffer_p);
1005+
uint8_t element_size = (uint8_t) (1 << internal_shift);
1006+
return (buffer_length >> shift) / element_size;
1007+
}
1008+
}
1009+
10001010
return buffer_length >> shift;
10011011
}
10021012

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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]);

0 commit comments

Comments
 (0)