Merge "[Release] Webkit2-efl-123997_0.11.86" into tizen_2.2
[framework/web/webkit-efl.git] / Source / JavaScriptCore / JSCTypedArrayStubs.h
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
42 namespace JSC {
43     
44 #define TYPED_ARRAY(name, type) \
45 class JS##name##Array : public JSNonFinalObject { \
46 public: \
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*, JSC::PropertyName propertyName, JSC::PropertySlot&);\
56     static bool getOwnPropertyDescriptor(JSC::JSObject*, JSC::ExecState*, JSC::PropertyName propertyName, JSC::PropertyDescriptor&);\
57     static bool getOwnPropertySlotByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);\
58     static void put(JSC::JSCell*, JSC::ExecState*, JSC::PropertyName 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;\
74 protected:\
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 \
82 const ClassInfo JS##name##Array::s_info = { #name "Array" , &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JS##name##Array) };\
83 \
84 JS##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 \
90 void 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 \
101 bool JS##name##Array::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)\
102 {\
103     JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
104     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
105     unsigned index = propertyName.asIndex();\
106     if (index < thisObject->m_storageLength) {\
107         ASSERT(index != PropertyName::NotAnIndex);\
108         slot.setValue(thisObject->getByIndex(exec, index));\
109         return true;\
110     }\
111     return Base::getOwnPropertySlot(cell, exec, propertyName, slot);\
112 }\
113 \
114 bool JS##name##Array::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)\
115 {\
116     JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
117     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
118     unsigned index = propertyName.asIndex();\
119     if (index < thisObject->m_storageLength) {\
120         ASSERT(index != PropertyName::NotAnIndex);\
121         descriptor.setDescriptor(thisObject->getByIndex(exec, index), DontDelete);\
122         return true;\
123     }\
124     return Base::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);\
125 }\
126 \
127 bool 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 \
138 void JS##name##Array::put(JSCell* cell, ExecState* exec, PropertyName 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     unsigned index = propertyName.asIndex();\
143     if (index != PropertyName::NotAnIndex) {\
144         thisObject->indexSetter(exec, index, value);\
145         return;\
146     }\
147     Base::put(thisObject, exec, propertyName, value, slot);\
148 }\
149 \
150 void JS##name##Array::indexSetter(JSC::ExecState* exec, unsigned index, JSC::JSValue value) \
151 {\
152     m_impl->set(index, value.toNumber(exec));\
153 }\
154 \
155 void JS##name##Array::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value, bool)\
156 {\
157     JS##name##Array* thisObject = jsCast<JS##name##Array*>(cell);\
158     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
159     thisObject->indexSetter(exec, propertyName, value);\
160     return;\
161 }\
162 \
163 void JS##name##Array::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)\
164 {\
165     JS##name##Array* thisObject = jsCast<JS##name##Array*>(object);\
166     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\
167     for (unsigned i = 0; i < thisObject->m_storageLength; ++i)\
168         propertyNames.add(Identifier::from(exec, i));\
169     Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);\
170 }\
171 \
172 JSValue JS##name##Array::getByIndex(ExecState*, unsigned index)\
173 {\
174     ASSERT_GC_OBJECT_INHERITS(this, &s_info);\
175     type result = m_impl->item(index);\
176     if (isnan((double)result))\
177         return jsNaN();\
178     return JSValue(result);\
179 }\
180 static EncodedJSValue JSC_HOST_CALL constructJS##name##Array(ExecState* callFrame) { \
181     if (callFrame->argumentCount() < 1) \
182         return JSValue::encode(jsUndefined()); \
183     int32_t length = callFrame->argument(0).toInt32(callFrame); \
184     if (length < 0) \
185         return JSValue::encode(jsUndefined()); \
186     Structure* structure = JS##name##Array::createStructure(callFrame->globalData(), callFrame->lexicalGlobalObject(), callFrame->lexicalGlobalObject()->objectPrototype()); \
187     return JSValue::encode(JS##name##Array::create(structure, callFrame->lexicalGlobalObject(), name##Array::create(length)));\
188 }
189
190 TYPED_ARRAY(Uint8, uint8_t);
191 TYPED_ARRAY(Uint8Clamped, uint8_t);
192 TYPED_ARRAY(Uint16, uint16_t);
193 TYPED_ARRAY(Uint32, uint32_t);
194 TYPED_ARRAY(Int8, int8_t);
195 TYPED_ARRAY(Int16, int16_t);
196 TYPED_ARRAY(Int32, int32_t);
197 TYPED_ARRAY(Float32, float);
198 TYPED_ARRAY(Float64, double);
199
200 }
201
202 #endif