Make TypedArrays be available in commandline jsc
authoroliver@apple.com <oliver@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 22 Feb 2012 00:49:48 +0000 (00:49 +0000)
committeroliver@apple.com <oliver@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 22 Feb 2012 00:49:48 +0000 (00:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=79163

Reviewed by Gavin Barraclough.

Adds a compile time option to have jsc support a basic implementation
of the TypedArrays available in WebCore.  This lets us test the typed
array logic in the JIT witout having to build webcore.

* JSCTypedArrayStubs.h: Added.
(JSC):
* JavaScriptCore.xcodeproj/project.pbxproj:
* jsc.cpp:
(GlobalObject::finishCreation):
(GlobalObject):
(GlobalObject::addConstructableFunction):
* runtime/JSGlobalData.h:
(JSGlobalData):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@108420 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JSCTypedArrayStubs.h [new file with mode: 0644]
Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
Source/JavaScriptCore/jsc.cpp
Source/JavaScriptCore/runtime/JSGlobalData.h

index c3f8fa6..2c2d697 100644 (file)
@@ -1,3 +1,24 @@
+2012-02-21  Oliver Hunt  <oliver@apple.com>
+
+        Make TypedArrays be available in commandline jsc
+        https://bugs.webkit.org/show_bug.cgi?id=79163
+
+        Reviewed by Gavin Barraclough.
+
+        Adds a compile time option to have jsc support a basic implementation
+        of the TypedArrays available in WebCore.  This lets us test the typed
+        array logic in the JIT witout having to build webcore.
+
+        * JSCTypedArrayStubs.h: Added.
+        (JSC):
+        * JavaScriptCore.xcodeproj/project.pbxproj:
+        * jsc.cpp:
+        (GlobalObject::finishCreation):
+        (GlobalObject):
+        (GlobalObject::addConstructableFunction):
+        * runtime/JSGlobalData.h:
+        (JSGlobalData):
+
 2012-02-21  Tom Sepez  <tsepez@chromium.org>
 
         equalIgnoringNullity() only comparing half the bytes for equality
diff --git a/Source/JavaScriptCore/JSCTypedArrayStubs.h b/Source/JavaScriptCore/JSCTypedArrayStubs.h
new file mode 100644 (file)
index 0000000..0030684
--- /dev/null
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2012 Apple Inc. 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 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 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 JSCTypedArrayStubs_h
+#define JSCTypedArrayStubs_h
+
+#include "Float32Array.h"
+#include "Float64Array.h"
+#include "Int16Array.h"
+#include "Int32Array.h"
+#include "Int8Array.h"
+#include "JSObject.h"
+#include "ObjectPrototype.h"
+#include "Uint16Array.h"
+#include "Uint32Array.h"
+#include "Uint8Array.h"
+
+namespace JSC {
+
+#define TYPED_ARRAY(name, type) \
+class JS##name##Array : public JSNonFinalObject { \
+public: \
+    typedef JSNonFinalObject Base; \
+    static JS##name##Array* create(JSC::Structure* structure, JSGlobalObject* globalObject, PassRefPtr<name##Array> impl) \
+    { \
+        JS##name##Array* ptr = new (NotNull, JSC::allocateCell<JS##name##Array>(globalObject->globalData().heap)) JS##name##Array(structure, globalObject, impl); \
+        ptr->finishCreation(globalObject->globalData()); \
+        return ptr; \
+    }\
+\
+    static bool getOwnPropertySlot(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);\
+    static bool getOwnPropertyDescriptor(JSC::JSObject*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&);\
+    static bool getOwnPropertySlotByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);\
+    static void put(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\
+    static void putByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::JSValue);\
+    static const JSC::ClassInfo s_info;\
+\
+    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\
+    {\
+        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\
+    }\
+\
+    static void getOwnPropertyNames(JSC::JSObject*, JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\
+    static JSC::JSValue getConstructor(JSC::ExecState*, JSC::JSGlobalObject*);\
+\
+    static const JSC::TypedArrayType TypedArrayStorageType = JSC::TypedArrayNone;\
+    intptr_t m_storageLength;\
+    type* m_storage;\
+    RefPtr<name##Array> m_impl;\
+protected:\
+    JS##name##Array(JSC::Structure*, JSGlobalObject*, PassRefPtr<name##Array>);\
+    void finishCreation(JSC::JSGlobalData&);\
+    static const unsigned StructureFlags = JSC::OverridesGetPropertyNames | JSC::OverridesGetOwnPropertySlot | Base::StructureFlags;\
+    JSC::JSValue getByIndex(JSC::ExecState*, unsigned index);\
+    void indexSetter(JSC::ExecState*, unsigned index, JSC::JSValue);\
+};\
+\
+const ClassInfo JS##name##Array::s_info = { #name "Array" , &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JS##name##Array) };\
+\
+JS##name##Array::JS##name##Array(Structure* structure, JSGlobalObject* globalObject, PassRefPtr<name##Array> impl)\
+    : Base(globalObject->globalData(), structure)\
+    , m_impl(impl)\
+{\
+}\
+\
+void JS##name##Array::finishCreation(JSGlobalData& globalData)\
+{\
+    Base::finishCreation(globalData);\
+    TypedArrayDescriptor descriptor(&JS##name##Array::s_info, OBJECT_OFFSETOF(JS##name##Array, m_storage), OBJECT_OFFSETOF(JS##name##Array, m_storageLength));\
+    globalData.registerTypedArrayDescriptor(m_impl.get(), descriptor);\
+    m_storage = m_impl->data();\
+    m_storageLength = m_impl->length();\
+    putDirect(globalData, globalData.propertyNames->length, jsNumber(m_storageLength), DontDelete | ReadOnly | DontEnum); \
+    ASSERT(inherits(&s_info));\
+}\
+\
+bool JS##name##Array::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\
+{\
+    JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
+    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
+    bool ok;\
+    unsigned index = propertyName.toUInt32(ok);\
+    if (ok && index < thisObject->m_storageLength) {\
+        slot.setValue(thisObject->getByIndex(exec, index));\
+        return true;\
+    }\
+    return Base::getOwnPropertySlot(cell, exec, propertyName, slot);\
+}\
+\
+bool JS##name##Array::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\
+{\
+    JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
+    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
+    bool ok;\
+    unsigned index = propertyName.toUInt32(ok);\
+    if (ok && index < thisObject->m_storageLength) {\
+        descriptor.setDescriptor(thisObject->getByIndex(exec, index), DontDelete);\
+        return true;\
+    }\
+    return Base::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);\
+}\
+\
+bool JS##name##Array::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)\
+{\
+    JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
+    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
+    if (propertyName < thisObject->m_storageLength) {\
+        slot.setValue(thisObject->getByIndex(exec, propertyName));\
+        return true;\
+    }\
+    return thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);\
+}\
+\
+void JS##name##Array::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\
+{\
+    JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
+    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
+    bool ok;\
+    unsigned index = propertyName.toUInt32(ok);\
+    if (ok) {\
+        thisObject->indexSetter(exec, index, value);\
+        return;\
+    }\
+    Base::put(thisObject, exec, propertyName, value, slot);\
+}\
+\
+void JS##name##Array::indexSetter(JSC::ExecState* exec, unsigned index, JSC::JSValue value) \
+{\
+    m_impl->set(index, value.toNumber(exec));\
+}\
+\
+void JS##name##Array::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value)\
+{\
+    JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
+    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
+    thisObject->indexSetter(exec, propertyName, value);\
+    return;\
+}\
+\
+void JS##name##Array::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)\
+{\
+    JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
+    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
+    for (unsigned i = 0; i < thisObject->m_storageLength; ++i)\
+        propertyNames.add(Identifier::from(exec, i));\
+    Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);\
+}\
+\
+JSValue JS##name##Array::getByIndex(ExecState*, unsigned index)\
+{\
+    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\
+    type result = m_impl->item(index);\
+    if (isnan((double)result))\
+        return jsNaN();\
+    return JSValue(result);\
+}\
+static EncodedJSValue JSC_HOST_CALL constructJS##name##Array(ExecState* callFrame) { \
+    if (callFrame->argumentCount() < 1) \
+        return JSValue::encode(jsUndefined()); \
+    int32_t length = callFrame->argument(0).toInt32(callFrame); \
+    if (length < 0) \
+        return JSValue::encode(jsUndefined()); \
+    Structure* structure = JS##name##Array::createStructure(callFrame->globalData(), callFrame->lexicalGlobalObject(), callFrame->lexicalGlobalObject()->objectPrototype()); \
+    return JSValue::encode(JS##name##Array::create(structure, callFrame->lexicalGlobalObject(), name##Array::create(length)));\
+}
+
+#if ENABLE(COMMANDLINE_TYPEDARRAYS)
+TYPED_ARRAY(Uint8, uint8_t);
+TYPED_ARRAY(Uint16, uint16_t);
+TYPED_ARRAY(Uint32, uint32_t);
+TYPED_ARRAY(Int8, int8_t);
+TYPED_ARRAY(Int16, int16_t);
+TYPED_ARRAY(Int32, int32_t);
+TYPED_ARRAY(Float32, float);
+TYPED_ARRAY(Float64, double);
+#endif
+
+}
+
+#endif
index b8488af..71fa151 100644 (file)
                A74DE1CB120B86D600D40D5B /* ARMv7Assembler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ARMv7Assembler.cpp; sourceTree = "<group>"; };
                A7521E121429169A003C8D0C /* CardSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CardSet.h; sourceTree = "<group>"; };
                A75706DD118A2BCF0057F88F /* JITArithmetic32_64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JITArithmetic32_64.cpp; sourceTree = "<group>"; };
+               A767FF9F14F4502900789059 /* JSCTypedArrayStubs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSCTypedArrayStubs.h; sourceTree = "<group>"; };
                A76C51741182748D00715B05 /* JSInterfaceJIT.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSInterfaceJIT.h; sourceTree = "<group>"; };
                A76F54A213B28AAB00EF2BCE /* JITWriteBarrier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JITWriteBarrier.h; sourceTree = "<group>"; };
                A781E358141970C700094D90 /* StorageBarrier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StorageBarrier.h; sourceTree = "<group>"; };
                                F692A8540255597D01FF60F7 /* create_hash_table */,
                                F5C290E60284F98E018635CA /* JavaScriptCorePrefix.h */,
                                45E12D8806A49B0F00E9DF84 /* jsc.cpp */,
+                               A767FF9F14F4502900789059 /* JSCTypedArrayStubs.h */,
                                F68EBB8C0255D4C601FF60F7 /* config.h */,
                                1432EBD70A34CAD400717B9F /* API */,
                                9688CB120ED12B4E001D649F /* assembler */,
index 25245e6..9f207c5 100644 (file)
@@ -29,6 +29,7 @@
 #include "InitializeThreading.h"
 #include "Interpreter.h"
 #include "JSArray.h"
+#include "JSCTypedArrayStubs.h"
 #include "JSFunction.h"
 #include "JSLock.h"
 #include "JSString.h"
@@ -193,6 +194,17 @@ protected:
         addFunction(globalData, "setSamplingFlags", functionSetSamplingFlags, 1);
         addFunction(globalData, "clearSamplingFlags", functionClearSamplingFlags, 1);
 #endif
+        
+#if ENABLE(COMMANDLINE_TYPEDARRAYS)
+        addConstructableFunction(globalData, "Uint8Array", constructJSUint8Array, 1);
+        addConstructableFunction(globalData, "Uint16Array", constructJSUint16Array, 1);
+        addConstructableFunction(globalData, "Uint32Array", constructJSUint32Array, 1);
+        addConstructableFunction(globalData, "Int8Array", constructJSInt8Array, 1);
+        addConstructableFunction(globalData, "Int16Array", constructJSInt16Array, 1);
+        addConstructableFunction(globalData, "Int32Array", constructJSInt32Array, 1);
+        addConstructableFunction(globalData, "Float32Array", constructJSFloat32Array, 1);
+        addConstructableFunction(globalData, "Float64Array", constructJSFloat64Array, 1);
+#endif
 
         JSObject* array = constructEmptyArray(globalExec());
         for (size_t i = 0; i < arguments.size(); ++i)
@@ -205,6 +217,12 @@ protected:
         Identifier identifier(globalExec(), name);
         putDirect(globalData, identifier, JSFunction::create(globalExec(), this, arguments, identifier, function));
     }
+    
+    void addConstructableFunction(JSGlobalData& globalData, const char* name, NativeFunction function, unsigned arguments)
+    {
+        Identifier identifier(globalExec(), name);
+        putDirect(globalData, identifier, JSFunction::create(globalExec(), this, arguments, identifier, function, function));
+    }
 };
 COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
 ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
index 120d216..b30d1bf 100644 (file)
@@ -332,7 +332,7 @@ namespace JSC {
             ASSERT(!m_##type##ArrayDescriptor.m_classInfo || m_##type##ArrayDescriptor.m_classInfo == descriptor.m_classInfo); \
             m_##type##ArrayDescriptor = descriptor; \
         } \
-        const TypedArrayDescriptor& type##ArrayDescriptor() const { return m_##type##ArrayDescriptor; }
+        const TypedArrayDescriptor& type##ArrayDescriptor() const { ASSERT(m_##type##ArrayDescriptor.m_classInfo); return m_##type##ArrayDescriptor; }
 
         registerTypedArrayFunction(int8, Int8);
         registerTypedArrayFunction(int16, Int16);