| 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/. */ |
| 7 |
|
| 8 |
#include "js/ArrayBuffer.h" |
| 9 |
#include "js/ArrayBufferMaybeShared.h" |
| 10 |
#include "js/experimental/TypedData.h" |
| 11 |
#include "js/SharedArrayBuffer.h" |
| 12 |
|
| 13 |
#include "jsapi-tests/tests.h" |
| 14 |
|
| 15 |
using namespace js; |
| 16 |
|
| 17 |
BEGIN_TEST(testLargeArrayBuffers) { |
| 18 |
#ifdef JS_64BIT |
| 19 |
constexpr size_t nbytes = size_t(6) * 1024 * 1024 * 1024; // 6 GB. |
| 20 |
|
| 21 |
RootedObject buffer(cx, JS::NewArrayBuffer(cx, nbytes)); |
| 22 |
CHECK(buffer); |
| 23 |
CHECK(JS::IsArrayBufferObject(buffer)); |
| 24 |
|
| 25 |
size_t length; |
| 26 |
bool isShared; |
| 27 |
uint8_t* data; |
| 28 |
|
| 29 |
// ArrayBuffer |
| 30 |
{ |
| 31 |
CHECK_EQUAL(JS::GetArrayBufferByteLength(buffer), nbytes); |
| 32 |
|
| 33 |
JS::GetArrayBufferLengthAndData(buffer, &length, &isShared, &data); |
| 34 |
CHECK_EQUAL(length, nbytes); |
| 35 |
|
| 36 |
length = 0; |
| 37 |
JS::GetArrayBufferMaybeSharedLengthAndData(buffer, &length, &isShared, |
| 38 |
&data); |
| 39 |
CHECK_EQUAL(length, nbytes); |
| 40 |
|
| 41 |
length = 0; |
| 42 |
CHECK(GetObjectAsArrayBuffer(buffer, &length, &data)); |
| 43 |
CHECK_EQUAL(length, nbytes); |
| 44 |
} |
| 45 |
|
| 46 |
// New Uint8Array |
| 47 |
{ |
| 48 |
RootedObject tarr(cx, JS_NewUint8Array(cx, nbytes)); |
| 49 |
CHECK(JS_IsTypedArrayObject(tarr)); |
| 50 |
CHECK_EQUAL(JS_GetArrayBufferViewByteOffset(tarr), 0u); |
| 51 |
CHECK_EQUAL(JS_GetArrayBufferViewByteLength(tarr), nbytes); |
| 52 |
CHECK_EQUAL(JS_GetTypedArrayByteOffset(tarr), 0u); |
| 53 |
CHECK_EQUAL(JS_GetTypedArrayByteLength(tarr), nbytes); |
| 54 |
CHECK_EQUAL(JS_GetTypedArrayLength(tarr), nbytes); |
| 55 |
|
| 56 |
length = 0; |
| 57 |
js::GetArrayBufferViewLengthAndData(tarr, &length, &isShared, &data); |
| 58 |
CHECK_EQUAL(length, nbytes); |
| 59 |
|
| 60 |
length = 0; |
| 61 |
js::GetUint8ArrayLengthAndData(tarr, &length, &isShared, &data); |
| 62 |
CHECK_EQUAL(length, nbytes); |
| 63 |
|
| 64 |
length = 0; |
| 65 |
JS::AutoCheckCannotGC nogc(cx); |
| 66 |
mozilla::Span<uint8_t> span = |
| 67 |
JS::TypedArray<Scalar::Uint8>::unwrap(tarr).getData(&isShared, nogc); |
| 68 |
CHECK_EQUAL(span.Length(), nbytes); |
| 69 |
|
| 70 |
length = 0; |
| 71 |
CHECK(JS_GetObjectAsArrayBufferView(tarr, &length, &isShared, &data)); |
| 72 |
CHECK_EQUAL(length, nbytes); |
| 73 |
} |
| 74 |
|
| 75 |
// Int16Array |
| 76 |
{ |
| 77 |
RootedObject tarr(cx, |
| 78 |
JS_NewInt16ArrayWithBuffer(cx, buffer, 0, nbytes / 2)); |
| 79 |
CHECK(JS_IsTypedArrayObject(tarr)); |
| 80 |
CHECK_EQUAL(JS_GetArrayBufferViewByteOffset(tarr), 0u); |
| 81 |
CHECK_EQUAL(JS_GetArrayBufferViewByteLength(tarr), nbytes); |
| 82 |
CHECK_EQUAL(JS_GetTypedArrayByteOffset(tarr), 0u); |
| 83 |
CHECK_EQUAL(JS_GetTypedArrayByteLength(tarr), nbytes); |
| 84 |
CHECK_EQUAL(JS_GetTypedArrayLength(tarr), nbytes / 2); |
| 85 |
|
| 86 |
length = 0; |
| 87 |
js::GetArrayBufferViewLengthAndData(tarr, &length, &isShared, &data); |
| 88 |
CHECK_EQUAL(length, nbytes); |
| 89 |
|
| 90 |
length = 0; |
| 91 |
int16_t* int16Data; |
| 92 |
js::GetInt16ArrayLengthAndData(tarr, &length, &isShared, &int16Data); |
| 93 |
CHECK_EQUAL(length, nbytes / 2); |
| 94 |
|
| 95 |
length = 0; |
| 96 |
JS::AutoCheckCannotGC nogc(cx); |
| 97 |
mozilla::Span<short> span = |
| 98 |
JS::TypedArray<Scalar::Int16>::unwrap(tarr).getData(&isShared, nogc); |
| 99 |
CHECK_EQUAL(span.Length(), nbytes / 2); |
| 100 |
|
| 101 |
length = 0; |
| 102 |
CHECK(JS_GetObjectAsArrayBufferView(tarr, &length, &isShared, &data)); |
| 103 |
CHECK_EQUAL(length, nbytes); |
| 104 |
} |
| 105 |
|
| 106 |
// DataView |
| 107 |
{ |
| 108 |
RootedObject dv(cx, JS_NewDataView(cx, buffer, 0, nbytes - 10)); |
| 109 |
CHECK(JS_IsArrayBufferViewObject(dv)); |
| 110 |
CHECK_EQUAL(JS_GetArrayBufferViewByteOffset(dv), 0u); |
| 111 |
CHECK_EQUAL(JS_GetArrayBufferViewByteLength(dv), nbytes - 10); |
| 112 |
|
| 113 |
length = 0; |
| 114 |
js::GetArrayBufferViewLengthAndData(dv, &length, &isShared, &data); |
| 115 |
CHECK_EQUAL(length, nbytes - 10); |
| 116 |
|
| 117 |
length = 0; |
| 118 |
CHECK(JS_GetObjectAsArrayBufferView(dv, &length, &isShared, &data)); |
| 119 |
CHECK_EQUAL(length, nbytes - 10); |
| 120 |
} |
| 121 |
|
| 122 |
// Int8Array with large byteOffset. |
| 123 |
{ |
| 124 |
RootedObject tarr(cx, |
| 125 |
JS_NewInt8ArrayWithBuffer(cx, buffer, nbytes - 200, 32)); |
| 126 |
CHECK(JS_IsTypedArrayObject(tarr)); |
| 127 |
CHECK_EQUAL(JS_GetArrayBufferViewByteOffset(tarr), nbytes - 200); |
| 128 |
CHECK_EQUAL(JS_GetArrayBufferViewByteLength(tarr), 32u); |
| 129 |
CHECK_EQUAL(JS_GetTypedArrayByteOffset(tarr), nbytes - 200); |
| 130 |
CHECK_EQUAL(JS_GetTypedArrayByteLength(tarr), 32u); |
| 131 |
CHECK_EQUAL(JS_GetTypedArrayLength(tarr), 32u); |
| 132 |
} |
| 133 |
|
| 134 |
// DataView with large byteOffset. |
| 135 |
{ |
| 136 |
RootedObject dv(cx, JS_NewDataView(cx, buffer, nbytes - 200, 32)); |
| 137 |
CHECK(JS_IsArrayBufferViewObject(dv)); |
| 138 |
CHECK_EQUAL(JS_GetArrayBufferViewByteOffset(dv), nbytes - 200); |
| 139 |
CHECK_EQUAL(JS_GetArrayBufferViewByteLength(dv), 32u); |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
return true; |
| 144 |
} |
| 145 |
END_TEST(testLargeArrayBuffers) |
| 146 |
|
| 147 |
BEGIN_TEST(testLargeSharedArrayBuffers) { |
| 148 |
#ifdef JS_64BIT |
| 149 |
constexpr size_t nbytes = size_t(5) * 1024 * 1024 * 1024; // 5 GB. |
| 150 |
|
| 151 |
RootedObject buffer(cx, JS::NewSharedArrayBuffer(cx, nbytes)); |
| 152 |
CHECK(buffer); |
| 153 |
CHECK(JS::IsSharedArrayBufferObject(buffer)); |
| 154 |
CHECK_EQUAL(GetSharedArrayBufferByteLength(buffer), nbytes); |
| 155 |
|
| 156 |
size_t length; |
| 157 |
bool isShared; |
| 158 |
uint8_t* data; |
| 159 |
JS::GetSharedArrayBufferLengthAndData(buffer, &length, &isShared, &data); |
| 160 |
CHECK_EQUAL(length, nbytes); |
| 161 |
#endif |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
END_TEST(testLargeSharedArrayBuffers) |