forked from phoboslab/JavaScriptCore-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
#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" | ||
|
||
#include "TypedArrayInlines.h" | ||
|
||
using namespace JSC; | ||
|
||
// Better be safe than sorry! | ||
const JSTypedArrayType TypedArrayTypes[] = { | ||
[NotTypedArray] = kJSTypedArrayTypeNone, | ||
[TypeInt8] = kJSTypedArrayTypeInt8Array, | ||
[TypeUint8] = kJSTypedArrayTypeUint8Array, | ||
[TypeUint8Clamped] = kJSTypedArrayTypeUint8ClampedArray, | ||
[TypeInt16] = kJSTypedArrayTypeInt16Array, | ||
[TypeUint16] = kJSTypedArrayTypeUint16Array, | ||
[TypeInt32] = kJSTypedArrayTypeInt32Array, | ||
[TypeUint32] = kJSTypedArrayTypeUint32Array, | ||
[TypeFloat32] = kJSTypedArrayTypeFloat32Array, | ||
[TypeFloat64] = kJSTypedArrayTypeFloat64Array, | ||
/* not a TypedArray */ kJSTypedArrayTypeArrayBuffer | ||
}; | ||
|
||
const int kJSTypedArrayTypeLast = kJSTypedArrayTypeArrayBuffer; | ||
|
||
|
||
template <typename ArrayType>JSObject * CreateTypedArray(JSC::ExecState* exec, size_t length) { | ||
return ArrayType::create(length)->wrap(exec, exec->lexicalGlobalObject()); | ||
} | ||
|
||
template <typename BufferType>JSObject * CreateArrayBuffer(JSC::ExecState* exec, size_t length) { | ||
RefPtr<BufferType> buffer = BufferType::create(length, 1); | ||
if( !buffer ) { | ||
return NULL; | ||
} | ||
|
||
JSArrayBuffer* result = JSArrayBuffer::create( | ||
exec->vm(), exec->lexicalGlobalObject()->arrayBufferStructure(), buffer); | ||
return result; | ||
} | ||
|
||
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::info()) ) { | ||
JSObject* object = jsValue.getObject(); | ||
type = TypedArrayTypes[object->classInfo()->typedArrayStorageType]; | ||
} | ||
else if( jsValue.inherits(JSArrayBuffer::info()) ) { | ||
type = kJSTypedArrayTypeArrayBuffer; | ||
} | ||
return type; | ||
} | ||
|
||
JSObjectRef JSTypedArrayMake(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements) { | ||
ExecState* exec = toJS(ctx); | ||
APIEntryShim entryShim(exec); | ||
|
||
JSObject* result = NULL; | ||
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( JSArrayBufferView * view = jsDynamicCast<JSArrayBufferView*>(jsValue) ) { | ||
if( byteLength ) { | ||
*byteLength = view->impl()->byteLength(); | ||
} | ||
return view->impl()->baseAddress(); | ||
} | ||
else if( ArrayBuffer * buffer = toArrayBuffer(jsValue) ) { | ||
if( byteLength ) { | ||
*byteLength = buffer->byteLength(); | ||
} | ||
return buffer->data(); | ||
} | ||
|
||
if( byteLength ) { | ||
*byteLength = 0; | ||
} | ||
return NULL; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifndef JSTypedArray_h | ||
#define JSTypedArray_h | ||
|
||
#include <JavaScriptCore/JSValueRef.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/*! | ||
@enum JSType | ||
@abstract A constant identifying the Typed Array type of a JSValue. | ||
@constant kJSTypedArrayTypeNone Not a Typed Array. | ||
@constant kJSTypedArrayTypeInt8Array Int8Array | ||
@constant kJSTypedArrayTypeInt16Array Int16Array | ||
@constant kJSTypedArrayTypeInt32Array Int32Array | ||
@constant kJSTypedArrayTypeUint8Array Int8Array | ||
@constant kJSTypedArrayTypeUint8ClampedArray Int8ClampedArray | ||
@constant kJSTypedArrayTypeUint16Array Uint16Array | ||
@constant kJSTypedArrayTypeUint32Array Uint32Array | ||
@constant kJSTypedArrayTypeFloat32Array Float32Array | ||
@constant kJSTypedArrayTypeFloat64Array Float64Array | ||
@constant kJSTypedArrayTypeArrayBuffer ArrayBuffer | ||
*/ | ||
typedef enum { | ||
kJSTypedArrayTypeNone, | ||
kJSTypedArrayTypeInt8Array, | ||
kJSTypedArrayTypeInt16Array, | ||
kJSTypedArrayTypeInt32Array, | ||
kJSTypedArrayTypeUint8Array, | ||
kJSTypedArrayTypeUint8ClampedArray, | ||
kJSTypedArrayTypeUint16Array, | ||
kJSTypedArrayTypeUint32Array, | ||
kJSTypedArrayTypeFloat32Array, | ||
kJSTypedArrayTypeFloat64Array, | ||
kJSTypedArrayTypeArrayBuffer | ||
} JSTypedArrayType; | ||
|
||
/*! | ||
@function | ||
@abstract Returns a JavaScript value's Typed Array type | ||
@param ctx The execution context to use. | ||
@param value The JSValue whose Typed Array type you want to obtain. | ||
@result A value of type JSTypedArrayType that identifies value's Typed Array type. | ||
*/ | ||
JS_EXPORT JSTypedArrayType JSTypedArrayGetType(JSContextRef ctx, JSValueRef value); | ||
|
||
/*! | ||
@function | ||
@abstract Creates a JavaScript Typed Array with the given number of elements | ||
@param ctx The execution context to use. | ||
@param arrayType A value of type JSTypedArrayType identifying the type of array you want to create | ||
@param numElements The number of elements for the array. | ||
@result A JSObjectRef that is a Typed Array or NULL if there was an error | ||
*/ | ||
JS_EXPORT JSObjectRef JSTypedArrayMake(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements); | ||
|
||
/*! | ||
@function | ||
@abstract Returns a pointer to a Typed Array's data in memory | ||
@param ctx The execution context to use. | ||
@param value The JSValue whose Typed Array type data pointer you want to obtain. | ||
@param byteLength A pointer to a size_t in which to store the byte length of the Typed Array | ||
@result A pointer to the Typed Array's data or NULL if the JSValue is not a Typed Array | ||
*/ | ||
JS_EXPORT void * JSTypedArrayGetDataPtr(JSContextRef ctx, JSValueRef value, size_t * byteLength); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* JSTypedArray_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters