Skip to content

Commit

Permalink
Added Typed Array API
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab committed Nov 5, 2013
1 parent 1ffba5d commit ee9d2ce
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
132 changes: 132 additions & 0 deletions JavaScriptCore/API/JSTypedArray.cpp
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;
}



73 changes: 73 additions & 0 deletions JavaScriptCore/API/JSTypedArray.h
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 */
6 changes: 6 additions & 0 deletions JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,7 @@
B65883601829170700CDA593 /* LLVMOverrides.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FCEFAC01805D94100472CE4 /* LLVMOverrides.cpp */; };
B65883651829174400CDA593 /* LLIntOffsetsExtractor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F4680A114BA7F8200BFE272 /* LLIntOffsetsExtractor.cpp */; };
B6CDA15418293D8C00190074 /* libWTF.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B6CDA15318293D8C00190074 /* libWTF.a */; };
B6CDA15E1829441F00190074 /* JSTypedArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6CDA15C1829441400190074 /* JSTypedArray.cpp */; };
BC02E90D0E1839DB000F9297 /* ErrorConstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9050E1839DB000F9297 /* ErrorConstructor.h */; };
BC02E90F0E1839DB000F9297 /* ErrorPrototype.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9070E1839DB000F9297 /* ErrorPrototype.h */; settings = {ATTRIBUTES = (Private, ); }; };
BC02E9110E1839DB000F9297 /* NativeErrorConstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9090E1839DB000F9297 /* NativeErrorConstructor.h */; };
Expand Down Expand Up @@ -3532,6 +3533,8 @@
B6479BDD1829146400CCF60F /* JavaScriptCore copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "JavaScriptCore copy-Info.plist"; path = "/Users/dominic/xcode/JavaScriptCore/JavaScriptCore/JavaScriptCore copy-Info.plist"; sourceTree = "<absolute>"; };
B658836C1829174400CDA593 /* libJSCLLIntOffsetsExtractor.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libJSCLLIntOffsetsExtractor.a; sourceTree = BUILT_PRODUCTS_DIR; };
B6CDA15318293D8C00190074 /* libWTF.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libWTF.a; path = ../Build/libWTF.a; sourceTree = "<group>"; };
B6CDA15C1829441400190074 /* JSTypedArray.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSTypedArray.cpp; sourceTree = "<group>"; };
B6CDA15D1829441400190074 /* JSTypedArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSTypedArray.h; sourceTree = "<group>"; };
BC021BF2136900C300FC5467 /* ToolExecutable.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ToolExecutable.xcconfig; sourceTree = "<group>"; };
BC02E9040E1839DB000F9297 /* ErrorConstructor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ErrorConstructor.cpp; sourceTree = "<group>"; };
BC02E9050E1839DB000F9297 /* ErrorConstructor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ErrorConstructor.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4281,6 +4284,8 @@
1432EBD70A34CAD400717B9F /* API */ = {
isa = PBXGroup;
children = (
B6CDA15C1829441400190074 /* JSTypedArray.cpp */,
B6CDA15D1829441400190074 /* JSTypedArray.h */,
C211B574176A224D000E2A23 /* APICallbackFunction.h */,
1482B78A0A4305AB00517CFC /* APICast.h */,
865F408710E7D56300947361 /* APIShims.h */,
Expand Down Expand Up @@ -7734,6 +7739,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B6CDA15E1829441F00190074 /* JSTypedArray.cpp in Sources */,
B6479A0B1829146300CCF60F /* A64DOpcode.cpp in Sources */,
B658835E1829170700CDA593 /* LLVMAnchor.cpp in Sources */,
B658835F1829170700CDA593 /* LLVMExports.cpp in Sources */,
Expand Down

0 comments on commit ee9d2ce

Please sign in to comment.