Skip to content

Commit 88d5198

Browse files
committed
Added API for ArrayBuffer
1 parent 82f9014 commit 88d5198

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

JavaScriptCore/API/JSTypedArray.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ const JSTypedArrayType TypedArrayTypes[] = {
3636
[TypedArrayUint16] = kJSTypedArrayTypeUint16Array,
3737
[TypedArrayUint32] = kJSTypedArrayTypeUint32Array,
3838
[TypedArrayFloat32] = kJSTypedArrayTypeFloat32Array,
39-
[TypedArrayFloat64] = kJSTypedArrayTypeFloat64Array
39+
[TypedArrayFloat64] = kJSTypedArrayTypeFloat64Array,
40+
/* not a TypedArray */ kJSTypedArrayTypeArrayBuffer
4041
};
4142

42-
const int kJSTypedArrayTypeLast = kJSTypedArrayTypeFloat64Array;
43+
const int kJSTypedArrayTypeLast = kJSTypedArrayTypeArrayBuffer;
4344

4445

4546
template <typename ArrayType>JSObject * CreateTypedArray(JSC::ExecState* exec, size_t length) {
@@ -50,6 +51,14 @@ template <typename ArrayType>JSObject * CreateTypedArray(JSC::ExecState* exec, s
5051
return asObject(toJS(exec, exec->lexicalGlobalObject(), array.get()));
5152
}
5253

54+
template <typename BufferType>JSObject * CreateArrayBuffer(JSC::ExecState* exec, size_t length) {
55+
RefPtr<BufferType> array = BufferType::create(length, 1);
56+
if( !array.get() ) {
57+
return NULL;
58+
}
59+
return asObject(toJS(exec, exec->lexicalGlobalObject(), array.get()));
60+
}
61+
5362
typedef JSObject*(*CreateTypedArrayFuncPtr)(JSC::ExecState*, size_t);
5463
const CreateTypedArrayFuncPtr CreateTypedArrayFunc[] = {
5564
[kJSTypedArrayTypeNone] = NULL,
@@ -61,7 +70,8 @@ const CreateTypedArrayFuncPtr CreateTypedArrayFunc[] = {
6170
[kJSTypedArrayTypeUint16Array] = CreateTypedArray<Uint16Array>,
6271
[kJSTypedArrayTypeUint32Array] = CreateTypedArray<Uint32Array>,
6372
[kJSTypedArrayTypeFloat32Array] = CreateTypedArray<Float32Array>,
64-
[kJSTypedArrayTypeFloat64Array] = CreateTypedArray<Float64Array>
73+
[kJSTypedArrayTypeFloat64Array] = CreateTypedArray<Float64Array>,
74+
[kJSTypedArrayTypeArrayBuffer] = CreateArrayBuffer<ArrayBuffer>,
6575
};
6676

6777

@@ -72,10 +82,15 @@ JSTypedArrayType JSTypedArrayGetType(JSContextRef ctx, JSValueRef value) {
7282
APIEntryShim entryShim(exec);
7383

7484
JSValue jsValue = toJS(exec, value);
75-
if( JSObject* object = jsValue.getObject() ) {
76-
return TypedArrayTypes[object->classInfo()->typedArrayStorageType];
85+
JSTypedArrayType type = kJSTypedArrayTypeNone;
86+
if( jsValue.inherits(&JSArrayBufferView::s_info) ) {
87+
JSObject* object = jsValue.getObject();
88+
type = TypedArrayTypes[object->classInfo()->typedArrayStorageType];
89+
}
90+
else if( jsValue.inherits(&JSArrayBuffer::s_info) ) {
91+
type = kJSTypedArrayTypeArrayBuffer;
7792
}
78-
return kJSTypedArrayTypeNone;
93+
return type;
7994
}
8095

8196
JSObjectRef JSTypedArrayMake(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements) {
@@ -101,6 +116,12 @@ void * JSTypedArrayGetDataPtr(JSContextRef ctx, JSValueRef value, size_t * byteL
101116
}
102117
return view->baseAddress();
103118
}
119+
else if( ArrayBuffer * buffer = toArrayBuffer(jsValue) ) {
120+
if( byteLength ) {
121+
*byteLength = buffer->byteLength();
122+
}
123+
return buffer->data();
124+
}
104125

105126
if( byteLength ) {
106127
*byteLength = 0;

JavaScriptCore/API/JSTypedArray.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ typedef enum {
3131
kJSTypedArrayTypeUint16Array,
3232
kJSTypedArrayTypeUint32Array,
3333
kJSTypedArrayTypeFloat32Array,
34-
kJSTypedArrayTypeFloat64Array
34+
kJSTypedArrayTypeFloat64Array,
35+
kJSTypedArrayTypeArrayBuffer
3536
} JSTypedArrayType;
3637

3738
/*!

0 commit comments

Comments
 (0)