This repository was archived by the owner on Jul 11, 2025. It is now read-only.
forked from phoboslab/JavaScriptCore-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSTypedArray.cpp
More file actions
133 lines (107 loc) · 4.2 KB
/
JSTypedArray.cpp
File metadata and controls
133 lines (107 loc) · 4.2 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
#include "config.h"
#include "JSTypedArray.h"
#include "JSObjectRef.h"
#include "APICast.h"
#include "InitializeThreading.h"
#include "JSCallbackObject.h"
#include "JSClassRef.h"
#include "JSGlobalObject.h"
#include "JSArrayBuffer.h"
#include "JSFloat32Array.h"
#include "JSFloat64Array.h"
#include "JSInt8Array.h"
#include "JSInt16Array.h"
#include "JSInt32Array.h"
#include "JSUint8ClampedArray.h"
#include "JSUint8Array.h"
#include "JSUint16Array.h"
#include "JSUint32Array.h"
using namespace JSC;
using namespace WebCore;
// Better be safe than sorry!
const JSTypedArrayType TypedArrayTypes[] = {
[TypedArrayNone] = kJSTypedArrayTypeNone,
[TypedArrayInt8] = kJSTypedArrayTypeInt8Array,
[TypedArrayInt16] = kJSTypedArrayTypeInt16Array,
[TypedArrayInt32] = kJSTypedArrayTypeInt32Array,
[TypedArrayUint8] = kJSTypedArrayTypeUint8Array,
[TypedArrayUint8Clamped] = kJSTypedArrayTypeUint8ClampedArray,
[TypedArrayUint16] = kJSTypedArrayTypeUint16Array,
[TypedArrayUint32] = kJSTypedArrayTypeUint32Array,
[TypedArrayFloat32] = kJSTypedArrayTypeFloat32Array,
[TypedArrayFloat64] = kJSTypedArrayTypeFloat64Array,
/* not a TypedArray */ kJSTypedArrayTypeArrayBuffer
};
const int kJSTypedArrayTypeLast = kJSTypedArrayTypeArrayBuffer;
template <typename ArrayType>JSObject * CreateTypedArray(JSC::ExecState* exec, size_t length) {
RefPtr<ArrayType> array = ArrayType::create(length);
if( !array.get() ) {
return NULL;
}
return asObject(toJS(exec, exec->lexicalGlobalObject(), array.get()));
}
template <typename BufferType>JSObject * CreateArrayBuffer(JSC::ExecState* exec, size_t length) {
RefPtr<BufferType> array = BufferType::create(length, 1);
if( !array.get() ) {
return NULL;
}
return asObject(toJS(exec, exec->lexicalGlobalObject(), array.get()));
}
typedef JSObject*(*CreateTypedArrayFuncPtr)(JSC::ExecState*, size_t);
const CreateTypedArrayFuncPtr CreateTypedArrayFunc[] = {
[kJSTypedArrayTypeNone] = NULL,
[kJSTypedArrayTypeInt8Array] = CreateTypedArray<Int8Array>,
[kJSTypedArrayTypeInt16Array] = CreateTypedArray<Int16Array>,
[kJSTypedArrayTypeInt32Array] = CreateTypedArray<Int32Array>,
[kJSTypedArrayTypeUint8Array] = CreateTypedArray<Uint8Array>,
[kJSTypedArrayTypeUint8ClampedArray] = CreateTypedArray<Uint8ClampedArray>,
[kJSTypedArrayTypeUint16Array] = CreateTypedArray<Uint16Array>,
[kJSTypedArrayTypeUint32Array] = CreateTypedArray<Uint32Array>,
[kJSTypedArrayTypeFloat32Array] = CreateTypedArray<Float32Array>,
[kJSTypedArrayTypeFloat64Array] = CreateTypedArray<Float64Array>,
[kJSTypedArrayTypeArrayBuffer] = CreateArrayBuffer<ArrayBuffer>,
};
JSTypedArrayType JSTypedArrayGetType(JSContextRef ctx, JSValueRef value) {
ExecState* exec = toJS(ctx);
APIEntryShim entryShim(exec);
JSValue jsValue = toJS(exec, value);
JSTypedArrayType type = kJSTypedArrayTypeNone;
if( jsValue.inherits(&JSArrayBufferView::s_info) ) {
JSObject* object = jsValue.getObject();
type = TypedArrayTypes[object->classInfo()->typedArrayStorageType];
}
else if( jsValue.inherits(&JSArrayBuffer::s_info) ) {
type = kJSTypedArrayTypeArrayBuffer;
}
return type;
}
JSObjectRef JSTypedArrayMake(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements) {
ExecState* exec = toJS(ctx);
APIEntryShim entryShim(exec);
JSObject* result;
if( arrayType > kJSTypedArrayTypeNone && arrayType <= kJSTypedArrayTypeLast ) {
result = CreateTypedArrayFunc[arrayType]( exec, numElements );
}
return toRef(result);
}
void * JSTypedArrayGetDataPtr(JSContextRef ctx, JSValueRef value, size_t * byteLength) {
ExecState* exec = toJS(ctx);
APIEntryShim entryShim(exec);
JSValue jsValue = toJS(exec, value);
if( ArrayBufferView * view = toArrayBufferView(jsValue) ) {
if( byteLength ) {
*byteLength = view->byteLength();
}
return view->baseAddress();
}
else if( ArrayBuffer * buffer = toArrayBuffer(jsValue) ) {
if( byteLength ) {
*byteLength = buffer->byteLength();
}
return buffer->data();
}
if( byteLength ) {
*byteLength = 0;
}
return NULL;
}