]>
git.saurik.com Git - apple/javascriptcore.git/blob - JSCTypedArrayStubs.h
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef JSCTypedArrayStubs_h
27 #define JSCTypedArrayStubs_h
30 #include "ObjectPrototype.h"
31 #include <wtf/Float32Array.h>
32 #include <wtf/Float64Array.h>
33 #include <wtf/Forward.h>
34 #include <wtf/Int16Array.h>
35 #include <wtf/Int32Array.h>
36 #include <wtf/Int8Array.h>
37 #include <wtf/Uint16Array.h>
38 #include <wtf/Uint32Array.h>
39 #include <wtf/Uint8Array.h>
40 #include <wtf/Uint8ClampedArray.h>
44 #define TYPED_ARRAY(name, type) \
45 class JS##name##Array : public JSNonFinalObject { \
47 typedef JSNonFinalObject Base; \
48 static JS##name##Array* create(JSC::Structure* structure, JSGlobalObject* globalObject, PassRefPtr<name##Array> impl) \
50 JS##name##Array* ptr = new (NotNull, JSC::allocateCell<JS##name##Array>(globalObject->globalData().heap)) JS##name##Array(structure, globalObject, impl); \
51 ptr->finishCreation(globalObject->globalData()); \
55 static bool getOwnPropertySlot(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);\
56 static bool getOwnPropertyDescriptor(JSC::JSObject*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&);\
57 static bool getOwnPropertySlotByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);\
58 static void put(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\
59 static void putByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::JSValue, bool);\
60 static const JSC::ClassInfo s_info;\
62 static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\
64 return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\
67 static void getOwnPropertyNames(JSC::JSObject*, JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\
68 static JSC::JSValue getConstructor(JSC::ExecState*, JSC::JSGlobalObject*);\
70 static const JSC::TypedArrayType TypedArrayStorageType = JSC::TypedArray##name;\
71 uint32_t m_storageLength;\
73 RefPtr<name##Array> m_impl;\
75 JS##name##Array(JSC::Structure*, JSGlobalObject*, PassRefPtr<name##Array>);\
76 void finishCreation(JSC::JSGlobalData&);\
77 static const unsigned StructureFlags = JSC::OverridesGetPropertyNames | JSC::OverridesGetOwnPropertySlot | Base::StructureFlags;\
78 JSC::JSValue getByIndex(JSC::ExecState*, unsigned index);\
79 void indexSetter(JSC::ExecState*, unsigned index, JSC::JSValue);\
82 const ClassInfo JS##name##Array::s_info = { #name "Array" , &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JS##name##Array) };\
84 JS##name##Array::JS##name##Array(Structure* structure, JSGlobalObject* globalObject, PassRefPtr<name##Array> impl)\
85 : Base(globalObject->globalData(), structure)\
90 void JS##name##Array::finishCreation(JSGlobalData& globalData)\
92 Base::finishCreation(globalData);\
93 TypedArrayDescriptor descriptor(&JS##name##Array::s_info, OBJECT_OFFSETOF(JS##name##Array, m_storage), OBJECT_OFFSETOF(JS##name##Array, m_storageLength));\
94 globalData.registerTypedArrayDescriptor(m_impl.get(), descriptor);\
95 m_storage = m_impl->data();\
96 m_storageLength = m_impl->length();\
97 putDirect(globalData, globalData.propertyNames->length, jsNumber(m_storageLength), DontDelete | ReadOnly | DontEnum); \
98 ASSERT(inherits(&s_info));\
101 bool JS##name##Array::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\
103 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
104 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
106 unsigned index = propertyName.toUInt32(ok);\
107 if (ok && index < thisObject->m_storageLength) {\
108 slot.setValue(thisObject->getByIndex(exec, index));\
111 return Base::getOwnPropertySlot(cell, exec, propertyName, slot);\
114 bool JS##name##Array::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\
116 JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
117 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
119 unsigned index = propertyName.toUInt32(ok);\
120 if (ok && index < thisObject->m_storageLength) {\
121 descriptor.setDescriptor(thisObject->getByIndex(exec, index), DontDelete);\
124 return Base::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);\
127 bool JS##name##Array::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)\
129 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
130 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
131 if (propertyName < thisObject->m_storageLength) {\
132 slot.setValue(thisObject->getByIndex(exec, propertyName));\
135 return thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);\
138 void JS##name##Array::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\
140 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
141 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
143 unsigned index = propertyName.toUInt32(ok);\
145 thisObject->indexSetter(exec, index, value);\
148 Base::put(thisObject, exec, propertyName, value, slot);\
151 void JS##name##Array::indexSetter(JSC::ExecState* exec, unsigned index, JSC::JSValue value) \
153 m_impl->set(index, value.toNumber(exec));\
156 void JS##name##Array::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value, bool)\
158 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
159 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
160 thisObject->indexSetter(exec, propertyName, value);\
164 void JS##name##Array::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)\
166 JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
167 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
168 for (unsigned i = 0; i < thisObject->m_storageLength; ++i)\
169 propertyNames.add(Identifier::from(exec, i));\
170 Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);\
173 JSValue JS##name##Array::getByIndex(ExecState*, unsigned index)\
175 ASSERT_GC_OBJECT_INHERITS(this, &s_info);\
176 type result = m_impl->item(index);\
177 if (isnan((double)result))\
179 return JSValue(result);\
181 static EncodedJSValue JSC_HOST_CALL constructJS##name##Array(ExecState* callFrame) { \
182 if (callFrame->argumentCount() < 1) \
183 return JSValue::encode(jsUndefined()); \
184 int32_t length = callFrame->argument(0).toInt32(callFrame); \
186 return JSValue::encode(jsUndefined()); \
187 Structure* structure = JS##name##Array::createStructure(callFrame->globalData(), callFrame->lexicalGlobalObject(), callFrame->lexicalGlobalObject()->objectPrototype()); \
188 return JSValue::encode(JS##name##Array::create(structure, callFrame->lexicalGlobalObject(), name##Array::create(length)));\
191 TYPED_ARRAY(Uint8
, uint8_t);
192 TYPED_ARRAY(Uint8Clamped
, uint8_t);
193 TYPED_ARRAY(Uint16
, uint16_t);
194 TYPED_ARRAY(Uint32
, uint32_t);
195 TYPED_ARRAY(Int8
, int8_t);
196 TYPED_ARRAY(Int16
, int16_t);
197 TYPED_ARRAY(Int32
, int32_t);
198 TYPED_ARRAY(Float32
, float);
199 TYPED_ARRAY(Float64
, double);