]> git.saurik.com Git - apple/javascriptcore.git/blame - JSCTypedArrayStubs.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / JSCTypedArrayStubs.h
CommitLineData
6fe7ccc8
A
1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef JSCTypedArrayStubs_h
27#define JSCTypedArrayStubs_h
28
29#include "JSObject.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>
41
42namespace JSC {
43
44#define TYPED_ARRAY(name, type) \
45class JS##name##Array : public JSNonFinalObject { \
46public: \
47 typedef JSNonFinalObject Base; \
48 static JS##name##Array* create(JSC::Structure* structure, JSGlobalObject* globalObject, PassRefPtr<name##Array> impl) \
49 { \
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()); \
52 return ptr; \
53 }\
54\
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;\
61\
62 static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\
63 {\
64 return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\
65 }\
66\
67 static void getOwnPropertyNames(JSC::JSObject*, JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\
68 static JSC::JSValue getConstructor(JSC::ExecState*, JSC::JSGlobalObject*);\
69\
70 static const JSC::TypedArrayType TypedArrayStorageType = JSC::TypedArray##name;\
71 uint32_t m_storageLength;\
72 type* m_storage;\
73 RefPtr<name##Array> m_impl;\
74protected:\
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);\
80};\
81\
82const ClassInfo JS##name##Array::s_info = { #name "Array" , &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JS##name##Array) };\
83\
84JS##name##Array::JS##name##Array(Structure* structure, JSGlobalObject* globalObject, PassRefPtr<name##Array> impl)\
85 : Base(globalObject->globalData(), structure)\
86 , m_impl(impl)\
87{\
88}\
89\
90void JS##name##Array::finishCreation(JSGlobalData& globalData)\
91{\
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));\
99}\
100\
101bool JS##name##Array::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\
102{\
103 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
104 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
105 bool ok;\
106 unsigned index = propertyName.toUInt32(ok);\
107 if (ok && index < thisObject->m_storageLength) {\
108 slot.setValue(thisObject->getByIndex(exec, index));\
109 return true;\
110 }\
111 return Base::getOwnPropertySlot(cell, exec, propertyName, slot);\
112}\
113\
114bool JS##name##Array::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\
115{\
116 JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
117 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
118 bool ok;\
119 unsigned index = propertyName.toUInt32(ok);\
120 if (ok && index < thisObject->m_storageLength) {\
121 descriptor.setDescriptor(thisObject->getByIndex(exec, index), DontDelete);\
122 return true;\
123 }\
124 return Base::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);\
125}\
126\
127bool JS##name##Array::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)\
128{\
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));\
133 return true;\
134 }\
135 return thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);\
136}\
137\
138void JS##name##Array::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\
139{\
140 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
141 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
142 bool ok;\
143 unsigned index = propertyName.toUInt32(ok);\
144 if (ok) {\
145 thisObject->indexSetter(exec, index, value);\
146 return;\
147 }\
148 Base::put(thisObject, exec, propertyName, value, slot);\
149}\
150\
151void JS##name##Array::indexSetter(JSC::ExecState* exec, unsigned index, JSC::JSValue value) \
152{\
153 m_impl->set(index, value.toNumber(exec));\
154}\
155\
156void JS##name##Array::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value, bool)\
157{\
158 JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
159 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
160 thisObject->indexSetter(exec, propertyName, value);\
161 return;\
162}\
163\
164void JS##name##Array::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)\
165{\
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);\
171}\
172\
173JSValue JS##name##Array::getByIndex(ExecState*, unsigned index)\
174{\
175 ASSERT_GC_OBJECT_INHERITS(this, &s_info);\
176 type result = m_impl->item(index);\
177 if (isnan((double)result))\
178 return jsNaN();\
179 return JSValue(result);\
180}\
181static 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); \
185 if (length < 0) \
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)));\
189}
190
191TYPED_ARRAY(Uint8, uint8_t);
192TYPED_ARRAY(Uint8Clamped, uint8_t);
193TYPED_ARRAY(Uint16, uint16_t);
194TYPED_ARRAY(Uint32, uint32_t);
195TYPED_ARRAY(Int8, int8_t);
196TYPED_ARRAY(Int16, int16_t);
197TYPED_ARRAY(Int32, int32_t);
198TYPED_ARRAY(Float32, float);
199TYPED_ARRAY(Float64, double);
200
201}
202
203#endif