2 * Copyright (C) 2003, 2008 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 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.
27 #include "runtime_array.h"
29 #include <runtime/ArrayPrototype.h>
30 #include <runtime/Error.h>
31 #include <runtime/PropertyNameArray.h>
32 #include "JSDOMBinding.h"
34 using namespace WebCore;
38 const ClassInfo RuntimeArray::s_info = { "RuntimeArray", &JSArray::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeArray) };
40 RuntimeArray::RuntimeArray(ExecState* exec, Structure* structure)
41 : JSArray(exec->globalData(), structure)
45 void RuntimeArray::finishCreation(JSGlobalData& globalData, Bindings::Array* array)
47 Base::finishCreation(globalData);
48 ASSERT(inherits(&s_info));
49 setSubclassData(array);
52 RuntimeArray::~RuntimeArray()
54 delete getConcreteArray();
57 JSValue RuntimeArray::lengthGetter(ExecState*, JSValue slotBase, const Identifier&)
59 RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slotBase));
60 return jsNumber(thisObj->getLength());
63 JSValue RuntimeArray::indexGetter(ExecState* exec, JSValue slotBase, unsigned index)
65 RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slotBase));
66 return thisObj->getConcreteArray()->valueAt(exec, index);
69 void RuntimeArray::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
71 unsigned length = getLength();
72 for (unsigned i = 0; i < length; ++i)
73 propertyNames.add(Identifier::from(exec, i));
75 if (mode == IncludeDontEnumProperties)
76 propertyNames.add(exec->propertyNames().length);
78 JSObject::getOwnPropertyNames(exec, propertyNames, mode);
81 bool RuntimeArray::getOwnPropertySlotVirtual(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
83 return getOwnPropertySlot(this, exec, propertyName, slot);
86 bool RuntimeArray::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
88 RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
89 if (propertyName == exec->propertyNames().length) {
90 slot.setCacheableCustom(thisObject, thisObject->lengthGetter);
95 unsigned index = propertyName.toArrayIndex(ok);
97 if (index < thisObject->getLength()) {
98 slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
103 return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
106 bool RuntimeArray::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
108 if (propertyName == exec->propertyNames().length) {
110 slot.setCustom(this, lengthGetter);
111 descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
116 unsigned index = propertyName.toArrayIndex(ok);
118 if (index < getLength()) {
120 slot.setCustomIndex(this, index, indexGetter);
121 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | DontEnum);
126 return JSObject::getOwnPropertyDescriptor(exec, propertyName, descriptor);
129 bool RuntimeArray::getOwnPropertySlotVirtual(ExecState *exec, unsigned index, PropertySlot& slot)
131 return getOwnPropertySlot(this, exec, index, slot);
134 bool RuntimeArray::getOwnPropertySlot(JSCell* cell, ExecState *exec, unsigned index, PropertySlot& slot)
136 RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
137 if (index < thisObject->getLength()) {
138 slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
142 return JSObject::getOwnPropertySlot(thisObject, exec, index, slot);
145 void RuntimeArray::putVirtual(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
147 put(this, exec, propertyName, value, slot);
150 void RuntimeArray::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
152 RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
153 if (propertyName == exec->propertyNames().length) {
154 throwError(exec, createRangeError(exec, "Range error"));
159 unsigned index = propertyName.toArrayIndex(ok);
161 thisObject->getConcreteArray()->setValueAt(exec, index, value);
165 JSObject::put(thisObject, exec, propertyName, value, slot);
168 void RuntimeArray::putVirtual(ExecState* exec, unsigned index, JSValue value)
170 put(this, exec, index, value);
173 void RuntimeArray::put(JSCell* cell, ExecState* exec, unsigned index, JSValue value)
175 RuntimeArray* thisObject = static_cast<RuntimeArray*>(cell);
176 if (index >= thisObject->getLength()) {
177 throwError(exec, createRangeError(exec, "Range error"));
181 thisObject->getConcreteArray()->setValueAt(exec, index, value);
184 bool RuntimeArray::deletePropertyVirtual(ExecState* exec, const Identifier& propertyName)
186 return deleteProperty(this, exec, propertyName);
189 bool RuntimeArray::deleteProperty(JSCell*, ExecState*, const Identifier&)
194 bool RuntimeArray::deletePropertyVirtual(ExecState* exec, unsigned propertyName)
196 return deleteProperty(this, exec, propertyName);
199 bool RuntimeArray::deleteProperty(JSCell*, ExecState*, unsigned)