Skip to content

Commit ee9d2ce

Browse files
committed
Added Typed Array API
1 parent 1ffba5d commit ee9d2ce

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+

JavaScriptCore/API/JSTypedArray.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#ifndef JSTypedArray_h
2+
#define JSTypedArray_h
3+
4+
#include <JavaScriptCore/JSValueRef.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
/*!
11+
@enum JSType
12+
@abstract A constant identifying the Typed Array type of a JSValue.
13+
@constant kJSTypedArrayTypeNone Not a Typed Array.
14+
@constant kJSTypedArrayTypeInt8Array Int8Array
15+
@constant kJSTypedArrayTypeInt16Array Int16Array
16+
@constant kJSTypedArrayTypeInt32Array Int32Array
17+
@constant kJSTypedArrayTypeUint8Array Int8Array
18+
@constant kJSTypedArrayTypeUint8ClampedArray Int8ClampedArray
19+
@constant kJSTypedArrayTypeUint16Array Uint16Array
20+
@constant kJSTypedArrayTypeUint32Array Uint32Array
21+
@constant kJSTypedArrayTypeFloat32Array Float32Array
22+
@constant kJSTypedArrayTypeFloat64Array Float64Array
23+
@constant kJSTypedArrayTypeArrayBuffer ArrayBuffer
24+
*/
25+
typedef enum {
26+
kJSTypedArrayTypeNone,
27+
kJSTypedArrayTypeInt8Array,
28+
kJSTypedArrayTypeInt16Array,
29+
kJSTypedArrayTypeInt32Array,
30+
kJSTypedArrayTypeUint8Array,
31+
kJSTypedArrayTypeUint8ClampedArray,
32+
kJSTypedArrayTypeUint16Array,
33+
kJSTypedArrayTypeUint32Array,
34+
kJSTypedArrayTypeFloat32Array,
35+
kJSTypedArrayTypeFloat64Array,
36+
kJSTypedArrayTypeArrayBuffer
37+
} JSTypedArrayType;
38+
39+
/*!
40+
@function
41+
@abstract Returns a JavaScript value's Typed Array type
42+
@param ctx The execution context to use.
43+
@param value The JSValue whose Typed Array type you want to obtain.
44+
@result A value of type JSTypedArrayType that identifies value's Typed Array type.
45+
*/
46+
JS_EXPORT JSTypedArrayType JSTypedArrayGetType(JSContextRef ctx, JSValueRef value);
47+
48+
/*!
49+
@function
50+
@abstract Creates a JavaScript Typed Array with the given number of elements
51+
@param ctx The execution context to use.
52+
@param arrayType A value of type JSTypedArrayType identifying the type of array you want to create
53+
@param numElements The number of elements for the array.
54+
@result A JSObjectRef that is a Typed Array or NULL if there was an error
55+
*/
56+
JS_EXPORT JSObjectRef JSTypedArrayMake(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements);
57+
58+
/*!
59+
@function
60+
@abstract Returns a pointer to a Typed Array's data in memory
61+
@param ctx The execution context to use.
62+
@param value The JSValue whose Typed Array type data pointer you want to obtain.
63+
@param byteLength A pointer to a size_t in which to store the byte length of the Typed Array
64+
@result A pointer to the Typed Array's data or NULL if the JSValue is not a Typed Array
65+
*/
66+
JS_EXPORT void * JSTypedArrayGetDataPtr(JSContextRef ctx, JSValueRef value, size_t * byteLength);
67+
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#endif /* JSTypedArray_h */

JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,7 @@
22142214
B65883601829170700CDA593 /* LLVMOverrides.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FCEFAC01805D94100472CE4 /* LLVMOverrides.cpp */; };
22152215
B65883651829174400CDA593 /* LLIntOffsetsExtractor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F4680A114BA7F8200BFE272 /* LLIntOffsetsExtractor.cpp */; };
22162216
B6CDA15418293D8C00190074 /* libWTF.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B6CDA15318293D8C00190074 /* libWTF.a */; };
2217+
B6CDA15E1829441F00190074 /* JSTypedArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6CDA15C1829441400190074 /* JSTypedArray.cpp */; };
22172218
BC02E90D0E1839DB000F9297 /* ErrorConstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9050E1839DB000F9297 /* ErrorConstructor.h */; };
22182219
BC02E90F0E1839DB000F9297 /* ErrorPrototype.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9070E1839DB000F9297 /* ErrorPrototype.h */; settings = {ATTRIBUTES = (Private, ); }; };
22192220
BC02E9110E1839DB000F9297 /* NativeErrorConstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = BC02E9090E1839DB000F9297 /* NativeErrorConstructor.h */; };
@@ -3532,6 +3533,8 @@
35323533
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>"; };
35333534
B658836C1829174400CDA593 /* libJSCLLIntOffsetsExtractor.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libJSCLLIntOffsetsExtractor.a; sourceTree = BUILT_PRODUCTS_DIR; };
35343535
B6CDA15318293D8C00190074 /* libWTF.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libWTF.a; path = ../Build/libWTF.a; sourceTree = "<group>"; };
3536+
B6CDA15C1829441400190074 /* JSTypedArray.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSTypedArray.cpp; sourceTree = "<group>"; };
3537+
B6CDA15D1829441400190074 /* JSTypedArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSTypedArray.h; sourceTree = "<group>"; };
35353538
BC021BF2136900C300FC5467 /* ToolExecutable.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ToolExecutable.xcconfig; sourceTree = "<group>"; };
35363539
BC02E9040E1839DB000F9297 /* ErrorConstructor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ErrorConstructor.cpp; sourceTree = "<group>"; };
35373540
BC02E9050E1839DB000F9297 /* ErrorConstructor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ErrorConstructor.h; sourceTree = "<group>"; };
@@ -4281,6 +4284,8 @@
42814284
1432EBD70A34CAD400717B9F /* API */ = {
42824285
isa = PBXGroup;
42834286
children = (
4287+
B6CDA15C1829441400190074 /* JSTypedArray.cpp */,
4288+
B6CDA15D1829441400190074 /* JSTypedArray.h */,
42844289
C211B574176A224D000E2A23 /* APICallbackFunction.h */,
42854290
1482B78A0A4305AB00517CFC /* APICast.h */,
42864291
865F408710E7D56300947361 /* APIShims.h */,
@@ -7734,6 +7739,7 @@
77347739
isa = PBXSourcesBuildPhase;
77357740
buildActionMask = 2147483647;
77367741
files = (
7742+
B6CDA15E1829441F00190074 /* JSTypedArray.cpp in Sources */,
77377743
B6479A0B1829146300CCF60F /* A64DOpcode.cpp in Sources */,
77387744
B658835E1829170700CDA593 /* LLVMAnchor.cpp in Sources */,
77397745
B658835F1829170700CDA593 /* LLVMExports.cpp in Sources */,

0 commit comments

Comments
 (0)