Bug 1716030 [wpt PR 29346] - Update wpt metadata, a=testonly
[gecko.git] / js / src / jsapi-tests / testTypedArrays.cpp
blob08b3af5d6254c8ae3135f663aa9218e0b3f0af5c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "js/ArrayBuffer.h" // JS::{NewArrayBuffer,IsArrayBufferObject,GetArrayBuffer{ByteLength,Data}}
9 #include "js/experimental/TypedData.h" // JS_GetArrayBufferViewBuffer, JS_GetTypedArray{Length,ByteOffset,ByteLength}, JS_Get{{Ui,I}nt{8,16,32},Float{32,64},Uint8Clamped}ArrayData, JS_IsTypedArrayObject, JS_New{{Ui,I}nt{8,16,32},Float{32,64},Uint8Clamped}Array{,FromArray,WithBuffer}
10 #include "js/PropertyAndElement.h" // JS_GetElement, JS_SetElement
11 #include "js/SharedArrayBuffer.h" // JS::{NewSharedArrayBuffer,GetSharedArrayBufferData}
12 #include "jsapi-tests/tests.h"
13 #include "vm/Realm.h"
15 using namespace js;
17 BEGIN_TEST(testTypedArrays) {
18 bool ok = true;
20 ok = ok &&
21 TestPlainTypedArray<JS_NewInt8Array, int8_t, JS_GetInt8ArrayData>(cx) &&
22 TestPlainTypedArray<JS_NewUint8Array, uint8_t, JS_GetUint8ArrayData>(
23 cx) &&
24 TestPlainTypedArray<JS_NewUint8ClampedArray, uint8_t,
25 JS_GetUint8ClampedArrayData>(cx) &&
26 TestPlainTypedArray<JS_NewInt16Array, int16_t, JS_GetInt16ArrayData>(
27 cx) &&
28 TestPlainTypedArray<JS_NewUint16Array, uint16_t, JS_GetUint16ArrayData>(
29 cx) &&
30 TestPlainTypedArray<JS_NewInt32Array, int32_t, JS_GetInt32ArrayData>(
31 cx) &&
32 TestPlainTypedArray<JS_NewUint32Array, uint32_t, JS_GetUint32ArrayData>(
33 cx) &&
34 TestPlainTypedArray<JS_NewFloat32Array, float, JS_GetFloat32ArrayData>(
35 cx) &&
36 TestPlainTypedArray<JS_NewFloat64Array, double, JS_GetFloat64ArrayData>(
37 cx);
39 size_t nbytes = sizeof(double) * 8;
40 RootedObject buffer(cx, JS::NewArrayBuffer(cx, nbytes));
41 CHECK(JS::IsArrayBufferObject(buffer));
43 RootedObject proto(cx);
44 JS_GetPrototype(cx, buffer, &proto);
45 CHECK(!JS::IsArrayBufferObject(proto));
48 JS::AutoCheckCannotGC nogc;
49 bool isShared;
50 CHECK_EQUAL(JS::GetArrayBufferByteLength(buffer), nbytes);
51 memset(JS::GetArrayBufferData(buffer, &isShared, nogc), 1, nbytes);
52 CHECK(!isShared); // Because ArrayBuffer
55 ok =
56 ok &&
57 TestArrayFromBuffer<JS_NewInt8ArrayWithBuffer, JS_NewInt8ArrayFromArray,
58 int8_t, false, JS_GetInt8ArrayData>(cx) &&
59 TestArrayFromBuffer<JS_NewUint8ArrayWithBuffer, JS_NewUint8ArrayFromArray,
60 uint8_t, false, JS_GetUint8ArrayData>(cx) &&
61 TestArrayFromBuffer<JS_NewUint8ClampedArrayWithBuffer,
62 JS_NewUint8ClampedArrayFromArray, uint8_t, false,
63 JS_GetUint8ClampedArrayData>(cx) &&
64 TestArrayFromBuffer<JS_NewInt16ArrayWithBuffer, JS_NewInt16ArrayFromArray,
65 int16_t, false, JS_GetInt16ArrayData>(cx) &&
66 TestArrayFromBuffer<JS_NewUint16ArrayWithBuffer,
67 JS_NewUint16ArrayFromArray, uint16_t, false,
68 JS_GetUint16ArrayData>(cx) &&
69 TestArrayFromBuffer<JS_NewInt32ArrayWithBuffer, JS_NewInt32ArrayFromArray,
70 int32_t, false, JS_GetInt32ArrayData>(cx) &&
71 TestArrayFromBuffer<JS_NewUint32ArrayWithBuffer,
72 JS_NewUint32ArrayFromArray, uint32_t, false,
73 JS_GetUint32ArrayData>(cx) &&
74 TestArrayFromBuffer<JS_NewFloat32ArrayWithBuffer,
75 JS_NewFloat32ArrayFromArray, float, false,
76 JS_GetFloat32ArrayData>(cx) &&
77 TestArrayFromBuffer<JS_NewFloat64ArrayWithBuffer,
78 JS_NewFloat64ArrayFromArray, double, false,
79 JS_GetFloat64ArrayData>(cx);
81 ok =
82 ok &&
83 TestArrayFromBuffer<JS_NewInt8ArrayWithBuffer, JS_NewInt8ArrayFromArray,
84 int8_t, true, JS_GetInt8ArrayData>(cx) &&
85 TestArrayFromBuffer<JS_NewUint8ArrayWithBuffer, JS_NewUint8ArrayFromArray,
86 uint8_t, true, JS_GetUint8ArrayData>(cx) &&
87 TestArrayFromBuffer<JS_NewUint8ClampedArrayWithBuffer,
88 JS_NewUint8ClampedArrayFromArray, uint8_t, true,
89 JS_GetUint8ClampedArrayData>(cx) &&
90 TestArrayFromBuffer<JS_NewInt16ArrayWithBuffer, JS_NewInt16ArrayFromArray,
91 int16_t, true, JS_GetInt16ArrayData>(cx) &&
92 TestArrayFromBuffer<JS_NewUint16ArrayWithBuffer,
93 JS_NewUint16ArrayFromArray, uint16_t, true,
94 JS_GetUint16ArrayData>(cx) &&
95 TestArrayFromBuffer<JS_NewInt32ArrayWithBuffer, JS_NewInt32ArrayFromArray,
96 int32_t, true, JS_GetInt32ArrayData>(cx) &&
97 TestArrayFromBuffer<JS_NewUint32ArrayWithBuffer,
98 JS_NewUint32ArrayFromArray, uint32_t, true,
99 JS_GetUint32ArrayData>(cx) &&
100 TestArrayFromBuffer<JS_NewFloat32ArrayWithBuffer,
101 JS_NewFloat32ArrayFromArray, float, true,
102 JS_GetFloat32ArrayData>(cx) &&
103 TestArrayFromBuffer<JS_NewFloat64ArrayWithBuffer,
104 JS_NewFloat64ArrayFromArray, double, true,
105 JS_GetFloat64ArrayData>(cx);
107 return ok;
110 // Shared memory can only be mapped by a TypedArray by creating the
111 // TypedArray with a SharedArrayBuffer explicitly, so no tests here.
113 template <JSObject* Create(JSContext*, size_t), typename Element,
114 Element* GetData(JSObject*, bool* isShared,
115 const JS::AutoRequireNoGC&)>
116 bool TestPlainTypedArray(JSContext* cx) {
118 RootedObject notArray(cx, Create(cx, SIZE_MAX));
119 CHECK(!notArray);
120 JS_ClearPendingException(cx);
123 RootedObject array(cx, Create(cx, 7));
124 CHECK(JS_IsTypedArrayObject(array));
125 RootedObject proto(cx);
126 JS_GetPrototype(cx, array, &proto);
127 CHECK(!JS_IsTypedArrayObject(proto));
129 CHECK_EQUAL(JS_GetTypedArrayLength(array), 7u);
130 CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0u);
131 CHECK_EQUAL(JS_GetTypedArrayByteLength(array), sizeof(Element) * 7);
134 JS::AutoCheckCannotGC nogc;
135 Element* data;
136 bool isShared;
137 CHECK(data = GetData(array, &isShared, nogc));
138 CHECK(!isShared); // Because ArrayBuffer
139 *data = 13;
141 RootedValue v(cx);
142 CHECK(JS_GetElement(cx, array, 0, &v));
143 CHECK_SAME(v, Int32Value(13));
145 return true;
148 template <
149 JSObject* CreateWithBuffer(JSContext*, JS::HandleObject, size_t, int64_t),
150 JSObject* CreateFromArray(JSContext*, JS::HandleObject), typename Element,
151 bool Shared, Element* GetData(JSObject*, bool*, const JS::AutoRequireNoGC&)>
152 bool TestArrayFromBuffer(JSContext* cx) {
153 if (Shared &&
154 !cx->realm()->creationOptions().getSharedMemoryAndAtomicsEnabled()) {
155 return true;
158 size_t elts = 8;
159 size_t nbytes = elts * sizeof(Element);
160 RootedObject buffer(cx, Shared ? JS::NewSharedArrayBuffer(cx, nbytes)
161 : JS::NewArrayBuffer(cx, nbytes));
163 JS::AutoCheckCannotGC nogc;
164 bool isShared;
165 void* data = Shared ? JS::GetSharedArrayBufferData(buffer, &isShared, nogc)
166 : JS::GetArrayBufferData(buffer, &isShared, nogc);
167 CHECK_EQUAL(Shared, isShared);
168 memset(data, 1, nbytes);
172 RootedObject notArray(cx, CreateWithBuffer(cx, buffer, UINT32_MAX, -1));
173 CHECK(!notArray);
174 JS_ClearPendingException(cx);
177 RootedObject array(cx, CreateWithBuffer(cx, buffer, 0, -1));
178 CHECK_EQUAL(JS_GetTypedArrayLength(array), elts);
179 CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0u);
180 CHECK_EQUAL(JS_GetTypedArrayByteLength(array), nbytes);
182 bool isShared;
183 CHECK_EQUAL(JS_GetArrayBufferViewBuffer(cx, array, &isShared),
184 (JSObject*)buffer);
185 CHECK_EQUAL(Shared, isShared);
189 JS::AutoCheckCannotGC nogc;
190 Element* data;
191 bool isShared;
193 CHECK(data = GetData(array, &isShared, nogc));
194 CHECK_EQUAL(Shared, isShared);
196 CHECK_EQUAL(
197 (void*)data,
198 Shared ? (void*)JS::GetSharedArrayBufferData(buffer, &isShared, nogc)
199 : (void*)JS::GetArrayBufferData(buffer, &isShared, nogc));
200 CHECK_EQUAL(Shared, isShared);
202 CHECK_EQUAL(*reinterpret_cast<uint8_t*>(data), 1u);
205 RootedObject shortArray(cx, CreateWithBuffer(cx, buffer, 0, elts / 2));
206 CHECK_EQUAL(JS_GetTypedArrayLength(shortArray), elts / 2);
207 CHECK_EQUAL(JS_GetTypedArrayByteOffset(shortArray), 0u);
208 CHECK_EQUAL(JS_GetTypedArrayByteLength(shortArray), nbytes / 2);
210 RootedObject ofsArray(cx, CreateWithBuffer(cx, buffer, nbytes / 2, -1));
211 CHECK_EQUAL(JS_GetTypedArrayLength(ofsArray), elts / 2);
212 CHECK_EQUAL(JS_GetTypedArrayByteOffset(ofsArray), nbytes / 2);
213 CHECK_EQUAL(JS_GetTypedArrayByteLength(ofsArray), nbytes / 2);
215 // Make sure all 3 views reflect the same buffer at the expected locations
216 JS::RootedValue v(cx, JS::Int32Value(39));
217 CHECK(JS_SetElement(cx, array, 0, v));
218 JS::RootedValue v2(cx);
219 CHECK(JS_GetElement(cx, array, 0, &v2));
220 CHECK_SAME(v, v2);
221 CHECK(JS_GetElement(cx, shortArray, 0, &v2));
222 CHECK_SAME(v, v2);
224 JS::AutoCheckCannotGC nogc;
225 Element* data;
226 bool isShared;
227 CHECK(data = GetData(array, &isShared, nogc));
228 CHECK_EQUAL(Shared, isShared);
229 CHECK_EQUAL(long(v.toInt32()), long(reinterpret_cast<Element*>(data)[0]));
232 v.setInt32(40);
233 CHECK(JS_SetElement(cx, array, elts / 2, v));
234 CHECK(JS_GetElement(cx, array, elts / 2, &v2));
235 CHECK_SAME(v, v2);
236 CHECK(JS_GetElement(cx, ofsArray, 0, &v2));
237 CHECK_SAME(v, v2);
239 JS::AutoCheckCannotGC nogc;
240 Element* data;
241 bool isShared;
242 CHECK(data = GetData(array, &isShared, nogc));
243 CHECK_EQUAL(Shared, isShared);
244 CHECK_EQUAL(long(v.toInt32()),
245 long(reinterpret_cast<Element*>(data)[elts / 2]));
248 v.setInt32(41);
249 CHECK(JS_SetElement(cx, array, elts - 1, v));
250 CHECK(JS_GetElement(cx, array, elts - 1, &v2));
251 CHECK_SAME(v, v2);
252 CHECK(JS_GetElement(cx, ofsArray, elts / 2 - 1, &v2));
253 CHECK_SAME(v, v2);
255 JS::AutoCheckCannotGC nogc;
256 Element* data;
257 bool isShared;
258 CHECK(data = GetData(array, &isShared, nogc));
259 CHECK_EQUAL(Shared, isShared);
260 CHECK_EQUAL(long(v.toInt32()),
261 long(reinterpret_cast<Element*>(data)[elts - 1]));
264 JS::RootedObject copy(cx, CreateFromArray(cx, array));
265 CHECK(JS_GetElement(cx, array, 0, &v));
266 CHECK(JS_GetElement(cx, copy, 0, &v2));
267 CHECK_SAME(v, v2);
269 /* The copy should not see changes in the original */
270 v2.setInt32(42);
271 CHECK(JS_SetElement(cx, array, 0, v2));
272 CHECK(JS_GetElement(cx, copy, 0, &v2));
273 CHECK_SAME(v2, v); /* v is still the original value from 'array' */
275 return true;
278 END_TEST(testTypedArrays)