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(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
71 RuntimeArray* thisObject = jsCast<RuntimeArray*>(object);
72 unsigned length = thisObject->getLength();
73 for (unsigned i = 0; i < length; ++i)
74 propertyNames.add(Identifier::from(exec, i));
76 if (mode == IncludeDontEnumProperties)
77 propertyNames.add(exec->propertyNames().length);
79 JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
82 bool RuntimeArray::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
84 RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
85 if (propertyName == exec->propertyNames().length) {
86 slot.setCacheableCustom(thisObject, thisObject->lengthGetter);
91 unsigned index = propertyName.toArrayIndex(ok);
93 if (index < thisObject->getLength()) {
94 slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
99 return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
102 bool RuntimeArray::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
104 RuntimeArray* thisObject = jsCast<RuntimeArray*>(object);
105 if (propertyName == exec->propertyNames().length) {
107 slot.setCustom(thisObject, lengthGetter);
108 descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
113 unsigned index = propertyName.toArrayIndex(ok);
115 if (index < thisObject->getLength()) {
117 slot.setCustomIndex(thisObject, index, indexGetter);
118 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | DontEnum);
123 return JSObject::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
126 bool RuntimeArray::getOwnPropertySlotByIndex(JSCell* cell, ExecState *exec, unsigned index, PropertySlot& slot)
128 RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
129 if (index < thisObject->getLength()) {
130 slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
134 return JSObject::getOwnPropertySlotByIndex(thisObject, exec, index, slot);
137 void RuntimeArray::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
139 RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
140 if (propertyName == exec->propertyNames().length) {
141 throwError(exec, createRangeError(exec, "Range error"));
146 unsigned index = propertyName.toArrayIndex(ok);
148 thisObject->getConcreteArray()->setValueAt(exec, index, value);
152 JSObject::put(thisObject, exec, propertyName, value, slot);
155 void RuntimeArray::putByIndex(JSCell* cell, ExecState* exec, unsigned index, JSValue value)
157 RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
158 if (index >= thisObject->getLength()) {
159 throwError(exec, createRangeError(exec, "Range error"));
163 thisObject->getConcreteArray()->setValueAt(exec, index, value);
166 bool RuntimeArray::deleteProperty(JSCell*, ExecState*, const Identifier&)
171 bool RuntimeArray::deletePropertyByIndex(JSCell*, ExecState*, unsigned)