From ba83a41a9ca5e3feb90e2be5470e9a74367f5ce5 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Thu, 22 Oct 2015 12:03:17 +0200 Subject: [PATCH 1/6] new api draft --- JavaScriptCore/API/JSTypedArray.cpp | 157 ++++++++++++++++++---------- JavaScriptCore/API/JSTypedArray.h | 10 +- 2 files changed, 111 insertions(+), 56 deletions(-) diff --git a/JavaScriptCore/API/JSTypedArray.cpp b/JavaScriptCore/API/JSTypedArray.cpp index 1ec2a0dc..23ecb8dc 100644 --- a/JavaScriptCore/API/JSTypedArray.cpp +++ b/JavaScriptCore/API/JSTypedArray.cpp @@ -27,7 +27,39 @@ using namespace JSC; -// Better be safe than sorry! +struct OpaqueJSData : public ThreadSafeRefCounted { + + static PassRefPtr create(PassRefPtr buffer, void* baseAddress, size_t byteLength) + { + return adoptRef(new OpaqueJSData(buffer, baseAddress, byteLength)); + } + + size_t length() { + return m_byteLength; + } + + void* baseAddress() { + return m_baseAddress; + } + +private: + friend class WTF::ThreadSafeRefCounted; + + OpaqueJSData( + PassRefPtr buffer, + void* baseAddress, + size_t byteLength) + : m_byteLength(byteLength) + , m_baseAddress(baseAddress) + , m_buffer(buffer) + {} + + unsigned m_byteLength; + void* m_baseAddress; + PassRefPtr m_buffer; +}; + + const JSTypedArrayType TypedArrayTypes[] = { [NotTypedArray] = kJSTypedArrayTypeNone, [TypeInt8] = kJSTypedArrayTypeInt8Array, @@ -42,43 +74,8 @@ const JSTypedArrayType TypedArrayTypes[] = { /* not a TypedArray */ kJSTypedArrayTypeArrayBuffer }; -const int kJSTypedArrayTypeLast = kJSTypedArrayTypeArrayBuffer; - - -template JSObject * CreateTypedArray(JSC::ExecState* exec, size_t length) { - return ArrayType::create(length)->wrap(exec, exec->lexicalGlobalObject()); -} - -template JSObject * CreateArrayBuffer(JSC::ExecState* exec, size_t length) { - RefPtr 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, - [kJSTypedArrayTypeInt16Array] = CreateTypedArray, - [kJSTypedArrayTypeInt32Array] = CreateTypedArray, - [kJSTypedArrayTypeUint8Array] = CreateTypedArray, - [kJSTypedArrayTypeUint8ClampedArray] = CreateTypedArray, - [kJSTypedArrayTypeUint16Array] = CreateTypedArray, - [kJSTypedArrayTypeUint32Array] = CreateTypedArray, - [kJSTypedArrayTypeFloat32Array] = CreateTypedArray, - [kJSTypedArrayTypeFloat64Array] = CreateTypedArray, - [kJSTypedArrayTypeArrayBuffer] = CreateArrayBuffer, -}; - - - - -JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object) { +JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object) +{ ExecState* exec = toJS(ctx); APIEntryShim entryShim(exec); @@ -93,38 +90,88 @@ JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object) return type; } -JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements) { +JSObjectRef JSObjectMakeTypedArray(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 ); + JSObject* result; + JSGlobalObject* jsGlobal = exec->lexicalGlobalObject(); + switch( arrayType ) { + case kJSTypedArrayTypeInt8Array: + result = Int8Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeInt16Array: + result = Int16Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeInt32Array: + result = Int8Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint8Array: + result = Int32Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint8ClampedArray: + result = Uint8ClampedArray::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint16Array: + result = Uint16Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint32Array: + result = Uint32Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeFloat32Array: + result = Float32Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeFloat64Array: + result = Float64Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeArrayBuffer: + result = JSArrayBuffer::create( + exec->vm(), jsGlobal->arrayBufferStructure(), ArrayBuffer::create(numElements, 1)); + break; + default: + result = NULL; + break; } return toRef(result); } -void* JSObjectGetTypedArrayDataPtr(JSContextRef ctx, JSObjectRef object, size_t* byteLength) { +JSDataRef JSObjectGetTypedArrayData(JSContextRef ctx, JSObjectRef object) +{ ExecState* exec = toJS(ctx); APIEntryShim entryShim(exec); JSObject* jsObject = toJS(object); if( JSArrayBufferView * view = jsDynamicCast(jsObject) ) { - if( byteLength ) { - *byteLength = view->impl()->byteLength(); - } - return view->impl()->baseAddress(); + return OpaqueJSData::create(view->buffer(), view->impl()->baseAddress(), view->impl()->byteLength()).leakRef(); } else if( ArrayBuffer* buffer = toArrayBuffer(jsObject) ) { - if( byteLength ) { - *byteLength = buffer->byteLength(); - } - return buffer->data(); - } - - if( byteLength ) { - *byteLength = 0; + return OpaqueJSData::create(buffer, buffer->data(), buffer->byteLength()).leakRef(); } + return NULL; } + +void JSDataRetain(JSDataRef data) +{ + if(data != nullptr) + data->ref(); +} + +void JSDataRelease(JSDataRef data) +{ + if(data != nullptr) + data->deref(); +} + +void* JSDataGetBytesPtr(JSDataRef data) +{ + return data->baseAddress(); +} + +size_t JSDataGetLength(JSDataRef data) +{ + return data->length(); +} + diff --git a/JavaScriptCore/API/JSTypedArray.h b/JavaScriptCore/API/JSTypedArray.h index b7c827b6..0bfefaa8 100644 --- a/JavaScriptCore/API/JSTypedArray.h +++ b/JavaScriptCore/API/JSTypedArray.h @@ -55,6 +55,14 @@ JS_EXPORT JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectR */ JS_EXPORT JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements); + +typedef struct OpaqueJSData* JSDataRef; + +JS_EXPORT void JSDataRetain(JSDataRef data); +JS_EXPORT void JSDataRelease(JSDataRef data); +JS_EXPORT void * JSDataGetBytesPtr(JSDataRef data); +JS_EXPORT size_t JSDataGetLength(JSDataRef data); + /*! @function @abstract Returns a pointer to a Typed Array's data in memory @@ -63,7 +71,7 @@ JS_EXPORT JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType @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 * JSObjectGetTypedArrayDataPtr(JSContextRef ctx, JSObjectRef object, size_t* byteLength); +JS_EXPORT JSDataRef JSObjectGetTypedArrayData(JSContextRef ctx, JSObjectRef object); #ifdef __cplusplus From f3a7c18f14b2a1d26942d42d97e22fc6552ab6b2 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Fri, 30 Oct 2015 13:15:54 +0100 Subject: [PATCH 2/6] Renamed function to clarify ownership --- JavaScriptCore/API/JSTypedArray.cpp | 2 +- JavaScriptCore/API/JSTypedArray.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/JavaScriptCore/API/JSTypedArray.cpp b/JavaScriptCore/API/JSTypedArray.cpp index 23ecb8dc..80d505a8 100644 --- a/JavaScriptCore/API/JSTypedArray.cpp +++ b/JavaScriptCore/API/JSTypedArray.cpp @@ -137,7 +137,7 @@ JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, return toRef(result); } -JSDataRef JSObjectGetTypedArrayData(JSContextRef ctx, JSObjectRef object) +JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object) { ExecState* exec = toJS(ctx); APIEntryShim entryShim(exec); diff --git a/JavaScriptCore/API/JSTypedArray.h b/JavaScriptCore/API/JSTypedArray.h index 0bfefaa8..b1d1457d 100644 --- a/JavaScriptCore/API/JSTypedArray.h +++ b/JavaScriptCore/API/JSTypedArray.h @@ -71,7 +71,7 @@ JS_EXPORT size_t JSDataGetLength(JSDataRef data); @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 JSDataRef JSObjectGetTypedArrayData(JSContextRef ctx, JSObjectRef object); +JS_EXPORT JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object); #ifdef __cplusplus From 69f4ef19e546b0bd9945301e467418051389f8ec Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Fri, 30 Oct 2015 17:53:23 +0100 Subject: [PATCH 3/6] Documentation --- JavaScriptCore/API/JSTypedArray.h | 47 ++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/JavaScriptCore/API/JSTypedArray.h b/JavaScriptCore/API/JSTypedArray.h index b1d1457d..069d3b4e 100644 --- a/JavaScriptCore/API/JSTypedArray.h +++ b/JavaScriptCore/API/JSTypedArray.h @@ -41,7 +41,7 @@ typedef enum { @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. +@result A value of type JSTypedArrayType that identifies value's Typed Array type, or kJSTypedArrayTypeNone if the object is not a Typed Array. */ JS_EXPORT JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object); @@ -55,23 +55,50 @@ JS_EXPORT JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectR */ JS_EXPORT JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements); - +/*! @typedef JSDataRef A Typed Array data buffer. */ typedef struct OpaqueJSData* JSDataRef; -JS_EXPORT void JSDataRetain(JSDataRef data); +/*! +@function +@abstract Returns a retained JSDataRef that encapsulates a pointer to a Typed Array's data in memory +@param ctx The execution context to use. +@param value The JSObjectRef whose Typed Array type data pointer you want to obtain. +@result A JSDataRef or NULL if the JSObjectRef is not a Typed Array. The return value is automatically retained and has to be released again with JSDataRelease +*/ +JS_EXPORT JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object); + +/*! +@function +@abstract Retains a JavaScript data object. +@param data The JSData to retain. +@result A JSDataRef that is the same as data. +*/ +JS_EXPORT JSDataRef JSDataRetain(JSDataRef data); + +/*! +@function +@abstract Releases a JavaScript data object. +@param data The JSData to release. +*/ JS_EXPORT void JSDataRelease(JSDataRef data); + +/*! +@function +@abstract Returns a pointer to the data buffer that serves as the backing store for a JavaScript data object. +@param data The JSData whose backing store you want to access. +@result A pointer to the raw data buffer that serves as data's backing store, which will be deallocated when the data is deallocated. +*/ JS_EXPORT void * JSDataGetBytesPtr(JSDataRef data); -JS_EXPORT size_t JSDataGetLength(JSDataRef data); /*! @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. +@abstract Returns the number of bytes in a JavaScript data object. +@param data The JSData whose length (in bytes) you want to know. +@result The number of bytes stored in the data object. */ -JS_EXPORT JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object); +JS_EXPORT size_t JSDataGetLength(JSDataRef data); + + #ifdef __cplusplus From 7d65ecb4f6afe886d29ef786835523ea6ad28ec5 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Fri, 30 Oct 2015 18:05:57 +0100 Subject: [PATCH 4/6] JSDataRetain did not return itself --- JavaScriptCore/API/JSTypedArray.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JavaScriptCore/API/JSTypedArray.cpp b/JavaScriptCore/API/JSTypedArray.cpp index 80d505a8..f8c1ff4a 100644 --- a/JavaScriptCore/API/JSTypedArray.cpp +++ b/JavaScriptCore/API/JSTypedArray.cpp @@ -153,10 +153,11 @@ JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object return NULL; } -void JSDataRetain(JSDataRef data) +JSDataRef JSDataRetain(JSDataRef data) { if(data != nullptr) data->ref(); + return data; } void JSDataRelease(JSDataRef data) From 896dea16bc40c374b2c655d139955f22e350db03 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Wed, 18 Nov 2015 12:06:29 +0100 Subject: [PATCH 5/6] Added copyright info as per Webkit spec --- JavaScriptCore/API/JSTypedArray.cpp | 25 +++++++++++++++++++++++++ JavaScriptCore/API/JSTypedArray.h | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/JavaScriptCore/API/JSTypedArray.cpp b/JavaScriptCore/API/JSTypedArray.cpp index f8c1ff4a..c0fde3c0 100644 --- a/JavaScriptCore/API/JSTypedArray.cpp +++ b/JavaScriptCore/API/JSTypedArray.cpp @@ -1,3 +1,28 @@ +/* + * Copyright (C) 2015 Dominic Szablewski. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include "config.h" diff --git a/JavaScriptCore/API/JSTypedArray.h b/JavaScriptCore/API/JSTypedArray.h index 069d3b4e..2697ddb0 100644 --- a/JavaScriptCore/API/JSTypedArray.h +++ b/JavaScriptCore/API/JSTypedArray.h @@ -1,3 +1,29 @@ +/* + * Copyright (C) 2015 Dominic Szablewski. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + #ifndef JSTypedArray_h #define JSTypedArray_h From 3b799eab01a7ed3a5349e183584fec5978a0f017 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Wed, 18 Nov 2015 13:07:47 +0100 Subject: [PATCH 6/6] Formatting --- JavaScriptCore/API/JSTypedArray.cpp | 102 +++++++++++++--------------- JavaScriptCore/API/JSTypedArray.h | 2 +- 2 files changed, 50 insertions(+), 54 deletions(-) diff --git a/JavaScriptCore/API/JSTypedArray.cpp b/JavaScriptCore/API/JSTypedArray.cpp index c0fde3c0..4eddc5a3 100644 --- a/JavaScriptCore/API/JSTypedArray.cpp +++ b/JavaScriptCore/API/JSTypedArray.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Dominic Szablewski. All rights reserved. + * Copyright (C) 2015 Dominic Szablewski (dominic@phoboslab.org) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -25,11 +25,8 @@ #include "config.h" - #include "JSTypedArray.h" -#include - #include "JSObjectRef.h" #include "APICast.h" #include "InitializeThreading.h" @@ -37,6 +34,7 @@ #include "JSClassRef.h" #include "JSGlobalObject.h" +#include "TypedArrayInlines.h" #include "JSArrayBuffer.h" #include "JSFloat32Array.h" #include "JSFloat64Array.h" @@ -48,7 +46,8 @@ #include "JSUint16Array.h" #include "JSUint32Array.h" -#include "TypedArrayInlines.h" +#include + using namespace JSC; @@ -71,9 +70,7 @@ struct OpaqueJSData : public ThreadSafeRefCounted { friend class WTF::ThreadSafeRefCounted; OpaqueJSData( - PassRefPtr buffer, - void* baseAddress, - size_t byteLength) + PassRefPtr buffer, void* baseAddress, size_t byteLength) : m_byteLength(byteLength) , m_baseAddress(baseAddress) , m_buffer(buffer) @@ -106,12 +103,11 @@ JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object) JSObject* jsObject = toJS(object); JSTypedArrayType type = kJSTypedArrayTypeNone; - if( jsObject->inherits(JSArrayBufferView::info()) ) { + if (jsObject->inherits(JSArrayBufferView::info())) type = TypedArrayTypes[jsObject->classInfo()->typedArrayStorageType]; - } - else if( jsObject->inherits(JSArrayBuffer::info()) ) { + else if (jsObject->inherits(JSArrayBuffer::info())) type = kJSTypedArrayTypeArrayBuffer; - } + return type; } @@ -122,41 +118,42 @@ JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, JSObject* result; JSGlobalObject* jsGlobal = exec->lexicalGlobalObject(); - switch( arrayType ) { - case kJSTypedArrayTypeInt8Array: - result = Int8Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeInt16Array: - result = Int16Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeInt32Array: - result = Int8Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeUint8Array: - result = Int32Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeUint8ClampedArray: - result = Uint8ClampedArray::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeUint16Array: - result = Uint16Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeUint32Array: - result = Uint32Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeFloat32Array: - result = Float32Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeFloat64Array: - result = Float64Array::create(numElements)->wrap(exec, jsGlobal); - break; - case kJSTypedArrayTypeArrayBuffer: - result = JSArrayBuffer::create( - exec->vm(), jsGlobal->arrayBufferStructure(), ArrayBuffer::create(numElements, 1)); - break; - default: - result = NULL; - break; + + switch (arrayType) { + case kJSTypedArrayTypeInt8Array: + result = Int8Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeInt16Array: + result = Int16Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeInt32Array: + result = Int8Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint8Array: + result = Int32Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint8ClampedArray: + result = Uint8ClampedArray::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint16Array: + result = Uint16Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeUint32Array: + result = Uint32Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeFloat32Array: + result = Float32Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeFloat64Array: + result = Float64Array::create(numElements)->wrap(exec, jsGlobal); + break; + case kJSTypedArrayTypeArrayBuffer: + result = JSArrayBuffer::create( + exec->vm(), jsGlobal->arrayBufferStructure(), ArrayBuffer::create(numElements, 1)); + break; + default: + result = nullptr; + break; } return toRef(result); @@ -168,26 +165,25 @@ JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object APIEntryShim entryShim(exec); JSObject* jsObject = toJS(object); - if( JSArrayBufferView * view = jsDynamicCast(jsObject) ) { + if (JSArrayBufferView * view = jsDynamicCast(jsObject)) return OpaqueJSData::create(view->buffer(), view->impl()->baseAddress(), view->impl()->byteLength()).leakRef(); - } - else if( ArrayBuffer* buffer = toArrayBuffer(jsObject) ) { + + if (ArrayBuffer* buffer = toArrayBuffer(jsObject)) return OpaqueJSData::create(buffer, buffer->data(), buffer->byteLength()).leakRef(); - } return NULL; } JSDataRef JSDataRetain(JSDataRef data) { - if(data != nullptr) + if (data) data->ref(); return data; } void JSDataRelease(JSDataRef data) { - if(data != nullptr) + if (data) data->deref(); } diff --git a/JavaScriptCore/API/JSTypedArray.h b/JavaScriptCore/API/JSTypedArray.h index 2697ddb0..fdbf3e1f 100644 --- a/JavaScriptCore/API/JSTypedArray.h +++ b/JavaScriptCore/API/JSTypedArray.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Dominic Szablewski. All rights reserved. + * Copyright (C) 2015 Dominic Szablewski (dominic@phoboslab.org) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions