-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathJSTypedArray.cpp
More file actions
379 lines (317 loc) · 13.7 KB
/
JSTypedArray.cpp
File metadata and controls
379 lines (317 loc) · 13.7 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (C) 2015 Dominic Szablewski (dominic@phoboslab.org)
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JSTypedArray.h"
#include "APICast.h"
#include "APIUtils.h"
#include "ClassInfo.h"
#include "JSCInlines.h"
#include "JSGenericTypedArrayViewInlines.h"
#include "JSTypedArrays.h"
#include "TypedArrayController.h"
#include <wtf/RefPtr.h>
#if PLATFORM(IOS) || PLATFORM(VISION)
#include <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
#endif
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
using namespace JSC;
// Helper functions.
inline JSTypedArrayType toJSTypedArrayType(JSC::JSType type)
{
switch (type) {
case JSC::Int8ArrayType:
return kJSTypedArrayTypeInt8Array;
case JSC::Uint8ArrayType:
return kJSTypedArrayTypeUint8Array;
case JSC::Uint8ClampedArrayType:
return kJSTypedArrayTypeUint8ClampedArray;
case JSC::Int16ArrayType:
return kJSTypedArrayTypeInt16Array;
case JSC::Uint16ArrayType:
return kJSTypedArrayTypeUint16Array;
case JSC::Int32ArrayType:
return kJSTypedArrayTypeInt32Array;
case JSC::Uint32ArrayType:
return kJSTypedArrayTypeUint32Array;
case JSC::Float32ArrayType:
return kJSTypedArrayTypeFloat32Array;
case JSC::Float64ArrayType:
return kJSTypedArrayTypeFloat64Array;
case JSC::BigInt64ArrayType:
return kJSTypedArrayTypeBigInt64Array;
case JSC::BigUint64ArrayType:
return kJSTypedArrayTypeBigUint64Array;
default:
return kJSTypedArrayTypeNone;
}
RELEASE_ASSERT_NOT_REACHED();
}
inline TypedArrayType toTypedArrayType(JSTypedArrayType type)
{
switch (type) {
case kJSTypedArrayTypeArrayBuffer:
case kJSTypedArrayTypeNone:
return NotTypedArray;
case kJSTypedArrayTypeInt8Array:
return TypeInt8;
case kJSTypedArrayTypeUint8Array:
return TypeUint8;
case kJSTypedArrayTypeUint8ClampedArray:
return TypeUint8Clamped;
case kJSTypedArrayTypeInt16Array:
return TypeInt16;
case kJSTypedArrayTypeUint16Array:
return TypeUint16;
case kJSTypedArrayTypeInt32Array:
return TypeInt32;
case kJSTypedArrayTypeUint32Array:
return TypeUint32;
case kJSTypedArrayTypeFloat32Array:
return TypeFloat32;
case kJSTypedArrayTypeFloat64Array:
return TypeFloat64;
case kJSTypedArrayTypeBigInt64Array:
return TypeBigInt64;
case kJSTypedArrayTypeBigUint64Array:
return TypeBigUint64;
}
RELEASE_ASSERT_NOT_REACHED();
}
static JSObject* createTypedArray(JSGlobalObject* globalObject, JSTypedArrayType type, RefPtr<ArrayBuffer>&& buffer, size_t offset, std::optional<size_t> length)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!buffer) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
constexpr JSTypedArrayType kJSTypedArrayTypeFloat16Array = static_cast<JSTypedArrayType>(kJSTypedArrayTypeBigUint64Array + 1);
bool isResizableOrGrowableShared = buffer->isResizableOrGrowableShared();
switch (static_cast<int>(type)) {
#define JSC_TYPED_ARRAY_FACTORY(type) case kJSTypedArrayType##type##Array: { \
return JS##type##Array::create(globalObject, globalObject->typedArrayStructure(Type##type, isResizableOrGrowableShared), WTF::move(buffer), offset, length.value()); \
}
FOR_EACH_TYPED_ARRAY_TYPE_EXCLUDING_DATA_VIEW(JSC_TYPED_ARRAY_FACTORY)
#undef JSC_TYPED_ARRAY_CHECK
case kJSTypedArrayTypeArrayBuffer:
case kJSTypedArrayTypeNone:
default:
RELEASE_ASSERT_NOT_REACHED();
}
return nullptr;
}
// Implementations of the API functions.
JSTypedArrayType JSValueGetTypedArrayType(JSContextRef ctx, JSValueRef valueRef, JSValueRef*)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSValue value = toJS(globalObject, valueRef);
if (!value.isObject())
return kJSTypedArrayTypeNone;
JSObject* object = value.getObject();
if (jsDynamicCast<JSArrayBuffer*>(object))
return kJSTypedArrayTypeArrayBuffer;
return toJSTypedArrayType(object->type());
}
JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t length, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
if (arrayType == kJSTypedArrayTypeNone || arrayType == kJSTypedArrayTypeArrayBuffer)
return nullptr;
unsigned elementByteSize = elementSize(toTypedArrayType(arrayType));
auto buffer = ArrayBuffer::tryCreate(length, elementByteSize);
JSObject* result = createTypedArray(globalObject, arrayType, WTF::move(buffer), 0, length);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeTypedArrayWithBytesNoCopy(JSContextRef ctx, JSTypedArrayType arrayType, void* bytes, size_t length, JSTypedArrayBytesDeallocator destructor, void* destructorContext, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
if (arrayType == kJSTypedArrayTypeNone || arrayType == kJSTypedArrayTypeArrayBuffer)
return nullptr;
unsigned elementByteSize = elementSize(toTypedArrayType(arrayType));
auto buffer = ArrayBuffer::createFromBytes({ static_cast<const uint8_t*>(bytes), length }, createSharedTask<void(void*)>([=](void* p) {
if (destructor)
destructor(p, destructorContext);
}));
JSObject* result = createTypedArray(globalObject, arrayType, WTF::move(buffer), 0, length / elementByteSize);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeTypedArrayWithArrayBuffer(JSContextRef ctx, JSTypedArrayType arrayType, JSObjectRef jsBufferRef, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
if (arrayType == kJSTypedArrayTypeNone || arrayType == kJSTypedArrayTypeArrayBuffer)
return nullptr;
JSArrayBuffer* jsBuffer = jsDynamicCast<JSArrayBuffer*>(toJS(jsBufferRef));
if (!jsBuffer) {
setException(ctx, exception, createTypeError(globalObject, "JSObjectMakeTypedArrayWithArrayBuffer expects buffer to be an Array Buffer object"_s));
return nullptr;
}
RefPtr<ArrayBuffer> buffer = jsBuffer->impl();
unsigned elementByteSize = elementSize(toTypedArrayType(arrayType));
std::optional<size_t> length;
if (!buffer->isResizableOrGrowableShared())
length = buffer->byteLength() / elementByteSize;
JSObject* result = createTypedArray(globalObject, arrayType, WTF::move(buffer), 0, length);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(result);
}
JSObjectRef JSObjectMakeTypedArrayWithArrayBufferAndOffset(JSContextRef ctx, JSTypedArrayType arrayType, JSObjectRef jsBufferRef, size_t offset, size_t length, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
if (arrayType == kJSTypedArrayTypeNone || arrayType == kJSTypedArrayTypeArrayBuffer)
return nullptr;
JSArrayBuffer* jsBuffer = jsDynamicCast<JSArrayBuffer*>(toJS(jsBufferRef));
if (!jsBuffer) {
setException(ctx, exception, createTypeError(globalObject, "JSObjectMakeTypedArrayWithArrayBuffer expects buffer to be an Array Buffer object"_s));
return nullptr;
}
JSObject* result = createTypedArray(globalObject, arrayType, jsBuffer->impl(), offset, length);
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(result);
}
void* JSObjectGetTypedArrayBytesPtr(JSContextRef ctx, JSObjectRef objectRef, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* object = toJS(objectRef);
if (JSArrayBufferView* typedArray = jsDynamicCast<JSArrayBufferView*>(object)) {
if (ArrayBuffer* buffer = typedArray->possiblySharedBuffer()) {
buffer->pinAndLock();
return buffer->data();
}
setException(ctx, exception, createOutOfMemoryError(globalObject));
}
return nullptr;
}
size_t JSObjectGetTypedArrayLength(JSContextRef, JSObjectRef objectRef, JSValueRef*)
{
JSObject* object = toJS(objectRef);
if (JSArrayBufferView* typedArray = jsDynamicCast<JSArrayBufferView*>(object))
return typedArray->length();
return 0;
}
size_t JSObjectGetTypedArrayByteLength(JSContextRef, JSObjectRef objectRef, JSValueRef*)
{
JSObject* object = toJS(objectRef);
if (JSArrayBufferView* typedArray = jsDynamicCast<JSArrayBufferView*>(object))
return typedArray->byteLength();
return 0;
}
size_t JSObjectGetTypedArrayByteOffset(JSContextRef, JSObjectRef objectRef, JSValueRef*)
{
JSObject* object = toJS(objectRef);
if (JSArrayBufferView* typedArray = jsDynamicCast<JSArrayBufferView*>(object))
return typedArray->byteOffset();
return 0;
}
JSObjectRef JSObjectGetTypedArrayBuffer(JSContextRef ctx, JSObjectRef objectRef, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* object = toJS(objectRef);
if (JSArrayBufferView* typedArray = jsDynamicCast<JSArrayBufferView*>(object)) {
if (ArrayBuffer* buffer = typedArray->possiblySharedBuffer())
return toRef(vm.m_typedArrayController->toJS(globalObject, typedArray->globalObject(), *buffer));
setException(ctx, exception, createOutOfMemoryError(globalObject));
}
return nullptr;
}
JSObjectRef JSObjectMakeArrayBufferWithBytesNoCopy(JSContextRef ctx, void* bytes, size_t byteLength, JSTypedArrayBytesDeallocator bytesDeallocator, void* deallocatorContext, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
auto buffer = ArrayBuffer::createFromBytes({ static_cast<const uint8_t*>(bytes), byteLength }, createSharedTask<void(void*)>([=](void* p) {
if (bytesDeallocator)
bytesDeallocator(p, deallocatorContext);
}));
JSArrayBuffer* jsBuffer = JSArrayBuffer::create(vm, globalObject->arrayBufferStructure(ArrayBufferSharingMode::Default), WTF::move(buffer));
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
return nullptr;
return toRef(jsBuffer);
}
void* JSObjectGetArrayBufferBytesPtr(JSContextRef ctx, JSObjectRef objectRef, JSValueRef* exception)
{
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
JSObject* object = toJS(objectRef);
if (JSArrayBuffer* jsBuffer = jsDynamicCast<JSArrayBuffer*>(object)) {
ArrayBuffer* buffer = jsBuffer->impl();
if (buffer->isWasmMemory()) {
setException(ctx, exception, createTypeError(globalObject, "Cannot get the backing buffer for a WebAssembly.Memory"_s));
return nullptr;
}
buffer->pinAndLock();
return buffer->data();
}
return nullptr;
}
#if PLATFORM(IOS) || PLATFORM(VISION)
inline static bool isLinkedBeforeTypedArrayLengthQuirk()
{
return !linkedOnOrAfterSDKWithBehavior(SDKAlignedBehavior::NoTypedArrayAPIQuirk);
}
#else
inline static bool isLinkedBeforeTypedArrayLengthQuirk() { return false; }
#endif
size_t JSObjectGetArrayBufferByteLength(JSContextRef, JSObjectRef objectRef, JSValueRef*)
{
JSObject* object = toJS(objectRef);
if (!object) {
// For some reason prior to https://bugs.webkit.org/show_bug.cgi?id=235720 Clang would emit code
// to early return if objectRef is 0 but not after. Passing 0 should be invalid API use.
static bool shouldntCrash = isLinkedBeforeTypedArrayLengthQuirk();
RELEASE_ASSERT(shouldntCrash);
return 0;
}
if (JSArrayBuffer* jsBuffer = jsDynamicCast<JSArrayBuffer*>(object))
return jsBuffer->impl()->byteLength();
return 0;
}
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END