6900cb7746412700afa60d310ad505467e0ae677
[framework/web/webkit-efl.git] / Source / WebCore / bridge / runtime_array.cpp
1 /*
2  * Copyright (C) 2003, 2008 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 COMPUTER, 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 COMPUTER, 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 #include "config.h"
27 #include "runtime_array.h"
28
29 #include <runtime/ArrayPrototype.h>
30 #include <runtime/Error.h>
31 #include <runtime/PropertyNameArray.h>
32 #include "JSDOMBinding.h"
33
34 using namespace WebCore;
35
36 namespace JSC {
37
38 const ClassInfo RuntimeArray::s_info = { "RuntimeArray", &JSArray::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeArray) };
39
40 RuntimeArray::RuntimeArray(ExecState* exec, Structure* structure)
41     : JSArray(exec->globalData(), structure)
42 {
43 }
44
45 void RuntimeArray::finishCreation(JSGlobalData& globalData, Bindings::Array* array)
46 {
47     Base::finishCreation(globalData);
48     ASSERT(inherits(&s_info));
49     setSubclassData(array);
50 }
51
52 RuntimeArray::~RuntimeArray()
53 {
54     delete getConcreteArray();
55 }
56
57 JSValue RuntimeArray::lengthGetter(ExecState*, JSValue slotBase, const Identifier&)
58 {
59     RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slotBase));
60     return jsNumber(thisObj->getLength());
61 }
62
63 JSValue RuntimeArray::indexGetter(ExecState* exec, JSValue slotBase, unsigned index)
64 {
65     RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slotBase));
66     return thisObj->getConcreteArray()->valueAt(exec, index);
67 }
68
69 void RuntimeArray::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
70 {
71     unsigned length = getLength();
72     for (unsigned i = 0; i < length; ++i)
73         propertyNames.add(Identifier::from(exec, i));
74
75     if (mode == IncludeDontEnumProperties)
76         propertyNames.add(exec->propertyNames().length);
77
78     JSObject::getOwnPropertyNames(exec, propertyNames, mode);
79 }
80
81 bool RuntimeArray::getOwnPropertySlotVirtual(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
82 {
83     return getOwnPropertySlot(this, exec, propertyName, slot);
84 }
85
86 bool RuntimeArray::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
87 {
88     RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
89     if (propertyName == exec->propertyNames().length) {
90         slot.setCacheableCustom(thisObject, thisObject->lengthGetter);
91         return true;
92     }
93     
94     bool ok;
95     unsigned index = propertyName.toArrayIndex(ok);
96     if (ok) {
97         if (index < thisObject->getLength()) {
98             slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
99             return true;
100         }
101     }
102     
103     return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
104 }
105
106 bool RuntimeArray::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
107 {
108     if (propertyName == exec->propertyNames().length) {
109         PropertySlot slot;
110         slot.setCustom(this, lengthGetter);
111         descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
112         return true;
113     }
114     
115     bool ok;
116     unsigned index = propertyName.toArrayIndex(ok);
117     if (ok) {
118         if (index < getLength()) {
119             PropertySlot slot;
120             slot.setCustomIndex(this, index, indexGetter);
121             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | DontEnum);
122             return true;
123         }
124     }
125     
126     return JSObject::getOwnPropertyDescriptor(exec, propertyName, descriptor);
127 }
128
129 bool RuntimeArray::getOwnPropertySlotVirtual(ExecState *exec, unsigned index, PropertySlot& slot)
130 {
131     return getOwnPropertySlot(this, exec, index, slot);
132 }
133
134 bool RuntimeArray::getOwnPropertySlot(JSCell* cell, ExecState *exec, unsigned index, PropertySlot& slot)
135 {
136     RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
137     if (index < thisObject->getLength()) {
138         slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
139         return true;
140     }
141     
142     return JSObject::getOwnPropertySlot(thisObject, exec, index, slot);
143 }
144
145 void RuntimeArray::putVirtual(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
146 {
147     put(this, exec, propertyName, value, slot);
148 }
149
150 void RuntimeArray::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
151 {
152     RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
153     if (propertyName == exec->propertyNames().length) {
154         throwError(exec, createRangeError(exec, "Range error"));
155         return;
156     }
157     
158     bool ok;
159     unsigned index = propertyName.toArrayIndex(ok);
160     if (ok) {
161         thisObject->getConcreteArray()->setValueAt(exec, index, value);
162         return;
163     }
164     
165     JSObject::put(thisObject, exec, propertyName, value, slot);
166 }
167
168 void RuntimeArray::putVirtual(ExecState* exec, unsigned index, JSValue value)
169 {
170     put(this, exec, index, value);
171 }
172
173 void RuntimeArray::put(JSCell* cell, ExecState* exec, unsigned index, JSValue value)
174 {
175     RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
176     if (index >= thisObject->getLength()) {
177         throwError(exec, createRangeError(exec, "Range error"));
178         return;
179     }
180     
181     thisObject->getConcreteArray()->setValueAt(exec, index, value);
182 }
183
184 bool RuntimeArray::deletePropertyVirtual(ExecState* exec, const Identifier& propertyName)
185 {
186     return deleteProperty(this, exec, propertyName);
187 }
188
189 bool RuntimeArray::deleteProperty(JSCell*, ExecState*, const Identifier&)
190 {
191     return false;
192 }
193
194 bool RuntimeArray::deletePropertyVirtual(ExecState* exec, unsigned propertyName)
195 {
196     return deleteProperty(this, exec, propertyName);
197 }
198
199 bool RuntimeArray::deleteProperty(JSCell*, ExecState*, unsigned)
200 {
201     return false;
202 }
203
204 }