|
| 1 | + |
| 2 | +#include "config.h" |
| 3 | + |
| 4 | +#include "JSTypedArray.h" |
| 5 | + |
| 6 | +#include "JSObjectRef.h" |
| 7 | +#include "APICast.h" |
| 8 | +#include "InitializeThreading.h" |
| 9 | +#include "JSCallbackObject.h" |
| 10 | +#include "JSClassRef.h" |
| 11 | +#include "JSGlobalObject.h" |
| 12 | + |
| 13 | +#include "JSArrayBuffer.h" |
| 14 | +#include "JSFloat32Array.h" |
| 15 | +#include "JSFloat64Array.h" |
| 16 | +#include "JSInt8Array.h" |
| 17 | +#include "JSInt16Array.h" |
| 18 | +#include "JSInt32Array.h" |
| 19 | +#include "JSUint8ClampedArray.h" |
| 20 | +#include "JSUint8Array.h" |
| 21 | +#include "JSUint16Array.h" |
| 22 | +#include "JSUint32Array.h" |
| 23 | + |
| 24 | +#include "TypedArrayInlines.h" |
| 25 | + |
| 26 | +using namespace JSC; |
| 27 | + |
| 28 | +// Better be safe than sorry! |
| 29 | +const JSTypedArrayType TypedArrayTypes[] = { |
| 30 | + [NotTypedArray] = kJSTypedArrayTypeNone, |
| 31 | + [TypeInt8] = kJSTypedArrayTypeInt8Array, |
| 32 | + [TypeUint8] = kJSTypedArrayTypeUint8Array, |
| 33 | + [TypeUint8Clamped] = kJSTypedArrayTypeUint8ClampedArray, |
| 34 | + [TypeInt16] = kJSTypedArrayTypeInt16Array, |
| 35 | + [TypeUint16] = kJSTypedArrayTypeUint16Array, |
| 36 | + [TypeInt32] = kJSTypedArrayTypeInt32Array, |
| 37 | + [TypeUint32] = kJSTypedArrayTypeUint32Array, |
| 38 | + [TypeFloat32] = kJSTypedArrayTypeFloat32Array, |
| 39 | + [TypeFloat64] = kJSTypedArrayTypeFloat64Array, |
| 40 | + /* not a TypedArray */ kJSTypedArrayTypeArrayBuffer |
| 41 | +}; |
| 42 | + |
| 43 | +const int kJSTypedArrayTypeLast = kJSTypedArrayTypeArrayBuffer; |
| 44 | + |
| 45 | + |
| 46 | +template <typename ArrayType>JSObject * CreateTypedArray(JSC::ExecState* exec, size_t length) { |
| 47 | + return ArrayType::create(length)->wrap(exec, exec->lexicalGlobalObject()); |
| 48 | +} |
| 49 | + |
| 50 | +template <typename BufferType>JSObject * CreateArrayBuffer(JSC::ExecState* exec, size_t length) { |
| 51 | + RefPtr<BufferType> buffer = BufferType::create(length, 1); |
| 52 | + if( !buffer ) { |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + |
| 56 | + JSArrayBuffer* result = JSArrayBuffer::create( |
| 57 | + exec->vm(), exec->lexicalGlobalObject()->arrayBufferStructure(), buffer); |
| 58 | + return result; |
| 59 | +} |
| 60 | + |
| 61 | +typedef JSObject*(*CreateTypedArrayFuncPtr)(JSC::ExecState*, size_t); |
| 62 | +const CreateTypedArrayFuncPtr CreateTypedArrayFunc[] = { |
| 63 | + [kJSTypedArrayTypeNone] = NULL, |
| 64 | + [kJSTypedArrayTypeInt8Array] = CreateTypedArray<Int8Array>, |
| 65 | + [kJSTypedArrayTypeInt16Array] = CreateTypedArray<Int16Array>, |
| 66 | + [kJSTypedArrayTypeInt32Array] = CreateTypedArray<Int32Array>, |
| 67 | + [kJSTypedArrayTypeUint8Array] = CreateTypedArray<Uint8Array>, |
| 68 | + [kJSTypedArrayTypeUint8ClampedArray] = CreateTypedArray<Uint8ClampedArray>, |
| 69 | + [kJSTypedArrayTypeUint16Array] = CreateTypedArray<Uint16Array>, |
| 70 | + [kJSTypedArrayTypeUint32Array] = CreateTypedArray<Uint32Array>, |
| 71 | + [kJSTypedArrayTypeFloat32Array] = CreateTypedArray<Float32Array>, |
| 72 | + [kJSTypedArrayTypeFloat64Array] = CreateTypedArray<Float64Array>, |
| 73 | + [kJSTypedArrayTypeArrayBuffer] = CreateArrayBuffer<ArrayBuffer>, |
| 74 | +}; |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +JSTypedArrayType JSTypedArrayGetType(JSContextRef ctx, JSValueRef value) { |
| 80 | + ExecState* exec = toJS(ctx); |
| 81 | + APIEntryShim entryShim(exec); |
| 82 | + |
| 83 | + JSValue jsValue = toJS(exec, value); |
| 84 | + JSTypedArrayType type = kJSTypedArrayTypeNone; |
| 85 | + if( jsValue.inherits(JSArrayBufferView::info()) ) { |
| 86 | + JSObject* object = jsValue.getObject(); |
| 87 | + type = TypedArrayTypes[object->classInfo()->typedArrayStorageType]; |
| 88 | + } |
| 89 | + else if( jsValue.inherits(JSArrayBuffer::info()) ) { |
| 90 | + type = kJSTypedArrayTypeArrayBuffer; |
| 91 | + } |
| 92 | + return type; |
| 93 | +} |
| 94 | + |
| 95 | +JSObjectRef JSTypedArrayMake(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements) { |
| 96 | + ExecState* exec = toJS(ctx); |
| 97 | + APIEntryShim entryShim(exec); |
| 98 | + |
| 99 | + JSObject* result = NULL; |
| 100 | + if( arrayType > kJSTypedArrayTypeNone && arrayType <= kJSTypedArrayTypeLast ) { |
| 101 | + result = CreateTypedArrayFunc[arrayType]( exec, numElements ); |
| 102 | + } |
| 103 | + |
| 104 | + return toRef(result); |
| 105 | +} |
| 106 | + |
| 107 | +void * JSTypedArrayGetDataPtr(JSContextRef ctx, JSValueRef value, size_t * byteLength) { |
| 108 | + ExecState* exec = toJS(ctx); |
| 109 | + APIEntryShim entryShim(exec); |
| 110 | + |
| 111 | + JSValue jsValue = toJS(exec, value); |
| 112 | + if( JSArrayBufferView * view = jsDynamicCast<JSArrayBufferView*>(jsValue) ) { |
| 113 | + if( byteLength ) { |
| 114 | + *byteLength = view->impl()->byteLength(); |
| 115 | + } |
| 116 | + return view->impl()->baseAddress(); |
| 117 | + } |
| 118 | + else if( ArrayBuffer * buffer = toArrayBuffer(jsValue) ) { |
| 119 | + if( byteLength ) { |
| 120 | + *byteLength = buffer->byteLength(); |
| 121 | + } |
| 122 | + return buffer->data(); |
| 123 | + } |
| 124 | + |
| 125 | + if( byteLength ) { |
| 126 | + *byteLength = 0; |
| 127 | + } |
| 128 | + return NULL; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments