summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testTypedArrays.cpp
blob: 7abcbbf854461c6f8b07f1248397baf427c98eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jscompartment.h"
#include "jsfriendapi.h"

#include "jsapi-tests/tests.h"

using namespace js;

BEGIN_TEST(testTypedArrays)
{
    bool ok = true;

    ok = ok &&
        TestPlainTypedArray<JS_NewInt8Array, int8_t, JS_GetInt8ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewUint8Array, uint8_t, JS_GetUint8ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewUint8ClampedArray, uint8_t, JS_GetUint8ClampedArrayData>(cx) &&
        TestPlainTypedArray<JS_NewInt16Array, int16_t, JS_GetInt16ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewUint16Array, uint16_t, JS_GetUint16ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewInt32Array, int32_t, JS_GetInt32ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewUint32Array, uint32_t, JS_GetUint32ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewFloat32Array, float, JS_GetFloat32ArrayData>(cx) &&
        TestPlainTypedArray<JS_NewFloat64Array, double, JS_GetFloat64ArrayData>(cx);

    size_t nbytes = sizeof(double) * 8;
    RootedObject buffer(cx, JS_NewArrayBuffer(cx, nbytes));
    CHECK(JS_IsArrayBufferObject(buffer));

    RootedObject proto(cx);
    JS_GetPrototype(cx, buffer, &proto);
    CHECK(!JS_IsArrayBufferObject(proto));

    {
        JS::AutoCheckCannotGC nogc;
        bool isShared;
        CHECK_EQUAL(JS_GetArrayBufferByteLength(buffer), nbytes);
        memset(JS_GetArrayBufferData(buffer, &isShared, nogc), 1, nbytes);
        CHECK(!isShared);  // Because ArrayBuffer
    }

    ok = ok &&
        TestArrayFromBuffer<JS_NewInt8ArrayWithBuffer, JS_NewInt8ArrayFromArray, int8_t, false, JS_GetInt8ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint8ArrayWithBuffer, JS_NewUint8ArrayFromArray, uint8_t, false, JS_GetUint8ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint8ClampedArrayWithBuffer, JS_NewUint8ClampedArrayFromArray, uint8_t, false, JS_GetUint8ClampedArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewInt16ArrayWithBuffer, JS_NewInt16ArrayFromArray, int16_t, false, JS_GetInt16ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint16ArrayWithBuffer, JS_NewUint16ArrayFromArray, uint16_t, false, JS_GetUint16ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewInt32ArrayWithBuffer, JS_NewInt32ArrayFromArray, int32_t, false, JS_GetInt32ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint32ArrayWithBuffer, JS_NewUint32ArrayFromArray, uint32_t, false, JS_GetUint32ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewFloat32ArrayWithBuffer, JS_NewFloat32ArrayFromArray, float, false, JS_GetFloat32ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewFloat64ArrayWithBuffer, JS_NewFloat64ArrayFromArray, double, false, JS_GetFloat64ArrayData>(cx);

    ok = ok &&
        TestArrayFromBuffer<JS_NewInt8ArrayWithBuffer, JS_NewInt8ArrayFromArray, int8_t, true, JS_GetInt8ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint8ArrayWithBuffer, JS_NewUint8ArrayFromArray, uint8_t, true, JS_GetUint8ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint8ClampedArrayWithBuffer, JS_NewUint8ClampedArrayFromArray, uint8_t, true, JS_GetUint8ClampedArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewInt16ArrayWithBuffer, JS_NewInt16ArrayFromArray, int16_t, true, JS_GetInt16ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint16ArrayWithBuffer, JS_NewUint16ArrayFromArray, uint16_t, true, JS_GetUint16ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewInt32ArrayWithBuffer, JS_NewInt32ArrayFromArray, int32_t, true, JS_GetInt32ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewUint32ArrayWithBuffer, JS_NewUint32ArrayFromArray, uint32_t, true, JS_GetUint32ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewFloat32ArrayWithBuffer, JS_NewFloat32ArrayFromArray, float, true, JS_GetFloat32ArrayData>(cx) &&
        TestArrayFromBuffer<JS_NewFloat64ArrayWithBuffer, JS_NewFloat64ArrayFromArray, double, true, JS_GetFloat64ArrayData>(cx);

    return ok;
}

// Shared memory can only be mapped by a TypedArray by creating the
// TypedArray with a SharedArrayBuffer explicitly, so no tests here.

template<JSObject* Create(JSContext*, uint32_t),
         typename Element,
         Element* GetData(JSObject*, bool* isShared, const JS::AutoCheckCannotGC&)>
bool
TestPlainTypedArray(JSContext* cx)
{
    {
        RootedObject notArray(cx, Create(cx, UINT32_MAX));
        CHECK(!notArray);
    }

    RootedObject array(cx, Create(cx, 7));
    CHECK(JS_IsTypedArrayObject(array));
    RootedObject proto(cx);
    JS_GetPrototype(cx, array, &proto);
    CHECK(!JS_IsTypedArrayObject(proto));

    CHECK_EQUAL(JS_GetTypedArrayLength(array), 7u);
    CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0u);
    CHECK_EQUAL(JS_GetTypedArrayByteLength(array), sizeof(Element) * 7);

    {
        JS::AutoCheckCannotGC nogc;
        Element* data;
        bool isShared;
        CHECK(data = GetData(array, &isShared, nogc));
        CHECK(!isShared);  // Because ArrayBuffer
        *data = 13;
    }
    RootedValue v(cx);
    CHECK(JS_GetElement(cx, array, 0, &v));
    CHECK_SAME(v, Int32Value(13));

    return true;
}

template<JSObject* CreateWithBuffer(JSContext*, JS::HandleObject, uint32_t, int32_t),
         JSObject* CreateFromArray(JSContext*, JS::HandleObject),
         typename Element,
         bool Shared,
         Element* GetData(JSObject*, bool*, const JS::AutoCheckCannotGC&)>
bool
TestArrayFromBuffer(JSContext* cx)
{
    if (Shared && !cx->compartment()->creationOptions().getSharedMemoryAndAtomicsEnabled())
        return true;

    size_t elts = 8;
    size_t nbytes = elts * sizeof(Element);
    RootedObject buffer(cx, Shared ? JS_NewSharedArrayBuffer(cx, nbytes)
                                   : JS_NewArrayBuffer(cx, nbytes));
    {
        JS::AutoCheckCannotGC nogc;
        bool isShared;
        void* data = Shared ? JS_GetSharedArrayBufferData(buffer, &isShared, nogc)
                            : JS_GetArrayBufferData(buffer, &isShared, nogc);
        CHECK_EQUAL(Shared, isShared);
        memset(data, 1, nbytes);
    }

    {
        RootedObject notArray(cx, CreateWithBuffer(cx, buffer, UINT32_MAX, -1));
        CHECK(!notArray);
    }

    RootedObject array(cx, CreateWithBuffer(cx, buffer, 0, -1));
    CHECK_EQUAL(JS_GetTypedArrayLength(array), elts);
    CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0u);
    CHECK_EQUAL(JS_GetTypedArrayByteLength(array), nbytes);
    {
        bool isShared;
        CHECK_EQUAL(JS_GetArrayBufferViewBuffer(cx, array, &isShared), (JSObject*) buffer);
        CHECK_EQUAL(Shared, isShared);
    }

    {
        JS::AutoCheckCannotGC nogc;
        Element* data;
        bool isShared;

        CHECK(data = GetData(array, &isShared, nogc));
        CHECK_EQUAL(Shared, isShared);

        CHECK_EQUAL((void*) data,
                    Shared ? (void*) JS_GetSharedArrayBufferData(buffer, &isShared, nogc)
                    : (void*) JS_GetArrayBufferData(buffer, &isShared, nogc));
        CHECK_EQUAL(Shared, isShared);

        CHECK_EQUAL(*reinterpret_cast<uint8_t*>(data), 1u);
    }

    RootedObject shortArray(cx, CreateWithBuffer(cx, buffer, 0, elts / 2));
    CHECK_EQUAL(JS_GetTypedArrayLength(shortArray), elts / 2);
    CHECK_EQUAL(JS_GetTypedArrayByteOffset(shortArray), 0u);
    CHECK_EQUAL(JS_GetTypedArrayByteLength(shortArray), nbytes / 2);

    RootedObject ofsArray(cx, CreateWithBuffer(cx, buffer, nbytes / 2, -1));
    CHECK_EQUAL(JS_GetTypedArrayLength(ofsArray), elts / 2);
    CHECK_EQUAL(JS_GetTypedArrayByteOffset(ofsArray), nbytes / 2);
    CHECK_EQUAL(JS_GetTypedArrayByteLength(ofsArray), nbytes / 2);

    // Make sure all 3 views reflect the same buffer at the expected locations
    JS::RootedValue v(cx, JS::Int32Value(39));
    CHECK(JS_SetElement(cx, array, 0, v));
    JS::RootedValue v2(cx);
    CHECK(JS_GetElement(cx, array, 0, &v2));
    CHECK_SAME(v, v2);
    CHECK(JS_GetElement(cx, shortArray, 0, &v2));
    CHECK_SAME(v, v2);
    {
        JS::AutoCheckCannotGC nogc;
        Element* data;
        bool isShared;
        CHECK(data = GetData(array, &isShared, nogc));
        CHECK_EQUAL(Shared, isShared);
        CHECK_EQUAL(long(v.toInt32()), long(reinterpret_cast<Element*>(data)[0]));
    }

    v.setInt32(40);
    CHECK(JS_SetElement(cx, array, elts / 2, v));
    CHECK(JS_GetElement(cx, array, elts / 2, &v2));
    CHECK_SAME(v, v2);
    CHECK(JS_GetElement(cx, ofsArray, 0, &v2));
    CHECK_SAME(v, v2);
    {
        JS::AutoCheckCannotGC nogc;
        Element* data;
        bool isShared;
        CHECK(data = GetData(array, &isShared, nogc));
        CHECK_EQUAL(Shared, isShared);
        CHECK_EQUAL(long(v.toInt32()), long(reinterpret_cast<Element*>(data)[elts / 2]));
    }

    v.setInt32(41);
    CHECK(JS_SetElement(cx, array, elts - 1, v));
    CHECK(JS_GetElement(cx, array, elts - 1, &v2));
    CHECK_SAME(v, v2);
    CHECK(JS_GetElement(cx, ofsArray, elts / 2 - 1, &v2));
    CHECK_SAME(v, v2);
    {
        JS::AutoCheckCannotGC nogc;
        Element* data;
        bool isShared;
        CHECK(data = GetData(array, &isShared, nogc));
        CHECK_EQUAL(Shared, isShared);
        CHECK_EQUAL(long(v.toInt32()), long(reinterpret_cast<Element*>(data)[elts - 1]));
    }

    JS::RootedObject copy(cx, CreateFromArray(cx, array));
    CHECK(JS_GetElement(cx, array, 0, &v));
    CHECK(JS_GetElement(cx, copy, 0, &v2));
    CHECK_SAME(v, v2);

    /* The copy should not see changes in the original */
    v2.setInt32(42);
    CHECK(JS_SetElement(cx, array, 0, v2));
    CHECK(JS_GetElement(cx, copy, 0, &v2));
    CHECK_SAME(v2, v); /* v is still the original value from 'array' */

    return true;
}

END_TEST(testTypedArrays)