summaryrefslogtreecommitdiff
path: root/Source/JavaScriptCore/JSCTypedArrayStubs.h
blob: cda55fc9b496417425eec0c321a080011295133e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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::TypedArray##name;\
    uint32_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