Index: JavaScriptCore/ChangeLog
===================================================================
--- JavaScriptCore/ChangeLog	(revision 192571)
+++ JavaScriptCore/ChangeLog	(working copy)
@@ -1,3 +1,24 @@
+2015-11-18  Dominic Szablewski  <dominic@phoboslab.org>
+
+        [JSC] Added Typed Array API
+        https://bugs.webkit.org/show_bug.cgi?id=120112
+
+        Reviewed by NOBODY (OOPS!).
+
+        * API/JSTypedArray.cpp: Added.
+        (OpaqueJSData::create):
+        (OpaqueJSData::length):
+        (OpaqueJSData::baseAddress):
+        (OpaqueJSData::OpaqueJSData):
+        (JSObjectMakeTypedArray):
+        (JSObjectGetRetainedTypedArrayData):
+        (JSDataRetain):
+        (JSDataRelease):
+        (JSDataGetBytesPtr):
+        (JSDataGetLength):
+        * API/JSTypedArray.h: Added.
+        * JavaScriptCore.xcodeproj/project.pbxproj:
+
 2015-11-17  Benjamin Poulain  <bpoulain@apple.com>
 
         [JSC] Untangle the dependencies on FTLAbbreviations a bit
Index: JavaScriptCore/API/JSTypedArray.cpp
===================================================================
--- JavaScriptCore/API/JSTypedArray.cpp	(revision 0)
+++ JavaScriptCore/API/JSTypedArray.cpp	(working copy)
@@ -0,0 +1,199 @@
+/*
+ * 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
+ * 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"
+#include "JSTypedArray.h"
+
+#include "JSObjectRef.h"
+#include "APICast.h"
+#include "InitializeThreading.h"
+#include "JSCallbackObject.h"
+#include "JSClassRef.h"
+#include "JSGlobalObject.h"
+
+#include "TypedArrayInlines.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 <wtf/RefPtr.h>
+
+
+using namespace JSC;
+
+struct OpaqueJSData : public ThreadSafeRefCounted<OpaqueJSData> {
+
+    static PassRefPtr<OpaqueJSData> create(PassRefPtr<ArrayBuffer> 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>;
+    
+     OpaqueJSData(
+        PassRefPtr<ArrayBuffer> buffer, void* baseAddress, size_t byteLength)
+            : m_byteLength(byteLength)
+            , m_baseAddress(baseAddress)
+            , m_buffer(buffer)
+    {}
+    
+    unsigned m_byteLength;
+    void* m_baseAddress;
+    PassRefPtr<ArrayBuffer> m_buffer;
+};
+
+
+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
+};
+
+JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object)
+{
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSObject* jsObject = toJS(object);
+    JSTypedArrayType type = kJSTypedArrayTypeNone;
+    if (jsObject->inherits(JSArrayBufferView::info()))
+        type = TypedArrayTypes[jsObject->classInfo()->typedArrayStorageType];
+    else if (jsObject->inherits(JSArrayBuffer::info()))
+        type = kJSTypedArrayTypeArrayBuffer;
+    
+    return type;
+}
+
+JSObjectRef JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements)
+{
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+    
+    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 = nullptr;
+        break;
+    }
+
+    return toRef(result);
+}
+
+JSDataRef JSObjectGetRetainedTypedArrayData(JSContextRef ctx, JSObjectRef object)
+{
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+    
+    JSObject* jsObject = toJS(object);
+    if (JSArrayBufferView * view = jsDynamicCast<JSArrayBufferView*>(jsObject))
+        return OpaqueJSData::create(view->buffer(), view->impl()->baseAddress(), view->impl()->byteLength()).leakRef();
+    
+    if (ArrayBuffer* buffer = toArrayBuffer(jsObject))
+        return OpaqueJSData::create(buffer, buffer->data(), buffer->byteLength()).leakRef();
+
+    return NULL;
+}
+
+JSDataRef JSDataRetain(JSDataRef data)
+{
+    if (data)
+        data->ref();
+    return data;
+}
+
+void JSDataRelease(JSDataRef data)
+{
+    if (data)
+        data->deref();
+}
+
+void* JSDataGetBytesPtr(JSDataRef data)
+{
+    return data->baseAddress();
+}
+
+size_t JSDataGetLength(JSDataRef data)
+{
+    return data->length();
+}
+
Index: JavaScriptCore/API/JSTypedArray.h
===================================================================
--- JavaScriptCore/API/JSTypedArray.h	(revision 0)
+++ JavaScriptCore/API/JSTypedArray.h	(working copy)
@@ -0,0 +1,134 @@
+/*
+ * 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
+ * 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
+
+#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, or kJSTypedArrayTypeNone if the object is not a Typed Array.
+*/
+JS_EXPORT JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object);
+
+/*!
+@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 JSObjectMakeTypedArray(JSContextRef ctx, JSTypedArrayType arrayType, size_t numElements);
+
+/*! @typedef JSDataRef A Typed Array data buffer. */
+typedef struct OpaqueJSData* JSDataRef;
+
+/*!
+@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);
+
+/*!
+@function
+@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 size_t JSDataGetLength(JSDataRef data);
+
+
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* JSTypedArray_h */
Index: JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
===================================================================
--- JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj	(revision 192564)
+++ JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj	(working copy)
@@ -1756,6 +1756,8 @@
 		ADDB1F6318D77DBE009B58A8 /* OpaqueRootSet.h in Headers */ = {isa = PBXBuildFile; fileRef = ADDB1F6218D77DB7009B58A8 /* OpaqueRootSet.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		ADE39FFF16DD144B0003CD4A /* PropertyTable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD1CF06816DCAB2D00B97123 /* PropertyTable.cpp */; };
 		B59F89391891F29F00D5CCDC /* UnlinkedInstructionStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B59F89381891ADB500D5CCDC /* UnlinkedInstructionStream.cpp */; };
+		B617DCCB1BFCA6F60079938B /* JSTypedArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B617DCC91BFCA6F60079938B /* JSTypedArray.cpp */; };
+		B617DCCC1BFCA6F60079938B /* JSTypedArray.h in Headers */ = {isa = PBXBuildFile; fileRef = B617DCCA1BFCA6F60079938B /* JSTypedArray.h */; };
 		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 */; };
@@ -3859,6 +3861,8 @@
 		ADDB1F6218D77DB7009B58A8 /* OpaqueRootSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpaqueRootSet.h; sourceTree = "<group>"; };
 		B59F89371891AD3300D5CCDC /* UnlinkedInstructionStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnlinkedInstructionStream.h; sourceTree = "<group>"; };
 		B59F89381891ADB500D5CCDC /* UnlinkedInstructionStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UnlinkedInstructionStream.cpp; sourceTree = "<group>"; };
+		B617DCC91BFCA6F60079938B /* JSTypedArray.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSTypedArray.cpp; sourceTree = "<group>"; };
+		B617DCCA1BFCA6F60079938B /* 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>"; };
@@ -4978,6 +4982,8 @@
 				146AAB370B66A94400E55F16 /* JSStringRefCF.cpp */,
 				146AAB2A0B66A84900E55F16 /* JSStringRefCF.h */,
 				1A28D4A7177B71C80007FA3C /* JSStringRefPrivate.h */,
+				B617DCC91BFCA6F60079938B /* JSTypedArray.cpp */,
+				B617DCCA1BFCA6F60079938B /* JSTypedArray.h */,
 				86E3C606167BAB87006D760A /* JSValue.h */,
 				86E3C60D167BAB87006D760A /* JSValue.mm */,
 				86E3C60E167BAB87006D760A /* JSValueInternal.h */,
@@ -7600,6 +7606,7 @@
 				BC18C4630E16F5CD00B34460 /* SourceProvider.h in Headers */,
 				E49DC16C12EF294E00184A1F /* SourceProviderCache.h in Headers */,
 				E49DC16D12EF295300184A1F /* SourceProviderCacheItem.h in Headers */,
+				B617DCCC1BFCA6F60079938B /* JSTypedArray.h in Headers */,
 				0FB7F39E15ED8E4600F167B2 /* SparseArrayValueMap.h in Headers */,
 				A7386554118697B400540279 /* SpecializedThunkJIT.h in Headers */,
 				0F5541B21613C1FB00CE3E25 /* SpecialPointer.h in Headers */,
@@ -8471,6 +8478,7 @@
 				A78A977A179738B8009DF744 /* DFGPlan.cpp in Sources */,
 				0FBE0F7416C1DB090082C5E8 /* DFGPredictionInjectionPhase.cpp in Sources */,
 				0FFFC95D14EF90B300C72532 /* DFGPredictionPropagationPhase.cpp in Sources */,
+				B617DCCB1BFCA6F60079938B /* JSTypedArray.cpp in Sources */,
 				0F3E01AA19D353A500F61B7F /* DFGPrePostNumbering.cpp in Sources */,
 				0F2B9CEC19D0BA7D00B1D1B5 /* DFGPromotedHeapLocation.cpp in Sources */,
 				0FB17662196B8F9E0091052A /* DFGPureValue.cpp in Sources */,
