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::info
, 0, 0 };
40 RuntimeArray::RuntimeArray(ExecState
* exec
, Bindings::Array
* array
)
41 // FIXME: deprecatedGetDOMStructure uses the prototype off of the wrong global object
42 // We need to pass in the right global object for "array".
43 : JSObject(deprecatedGetDOMStructure
<RuntimeArray
>(exec
))
48 JSValue
RuntimeArray::lengthGetter(ExecState
* exec
, const Identifier
&, const PropertySlot
& slot
)
50 RuntimeArray
* thisObj
= static_cast<RuntimeArray
*>(asObject(slot
.slotBase()));
51 return jsNumber(exec
, thisObj
->getLength());
54 JSValue
RuntimeArray::indexGetter(ExecState
* exec
, const Identifier
&, const PropertySlot
& slot
)
56 RuntimeArray
* thisObj
= static_cast<RuntimeArray
*>(asObject(slot
.slotBase()));
57 return thisObj
->getConcreteArray()->valueAt(exec
, slot
.index());
60 void RuntimeArray::getOwnPropertyNames(ExecState
* exec
, PropertyNameArray
& propertyNames
)
62 unsigned length
= getLength();
63 for (unsigned i
= 0; i
< length
; ++i
)
64 propertyNames
.add(Identifier::from(exec
, i
));
66 JSObject::getOwnPropertyNames(exec
, propertyNames
);
69 bool RuntimeArray::getOwnPropertySlot(ExecState
* exec
, const Identifier
& propertyName
, PropertySlot
& slot
)
71 if (propertyName
== exec
->propertyNames().length
) {
72 slot
.setCustom(this, lengthGetter
);
77 unsigned index
= propertyName
.toArrayIndex(&ok
);
79 if (index
< getLength()) {
80 slot
.setCustomIndex(this, index
, indexGetter
);
85 return JSObject::getOwnPropertySlot(exec
, propertyName
, slot
);
88 bool RuntimeArray::getOwnPropertyDescriptor(ExecState
* exec
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
90 if (propertyName
== exec
->propertyNames().length
) {
92 slot
.setCustom(this, lengthGetter
);
93 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), ReadOnly
| DontDelete
| DontEnum
);
98 unsigned index
= propertyName
.toArrayIndex(&ok
);
100 if (index
< getLength()) {
102 slot
.setCustomIndex(this, index
, indexGetter
);
103 descriptor
.setDescriptor(slot
.getValue(exec
, propertyName
), DontDelete
| DontEnum
);
108 return JSObject::getOwnPropertyDescriptor(exec
, propertyName
, descriptor
);
111 bool RuntimeArray::getOwnPropertySlot(ExecState
*exec
, unsigned index
, PropertySlot
& slot
)
113 if (index
< getLength()) {
114 slot
.setCustomIndex(this, index
, indexGetter
);
118 return JSObject::getOwnPropertySlot(exec
, index
, slot
);
121 void RuntimeArray::put(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, PutPropertySlot
& slot
)
123 if (propertyName
== exec
->propertyNames().length
) {
124 throwError(exec
, RangeError
);
129 unsigned index
= propertyName
.toArrayIndex(&ok
);
131 getConcreteArray()->setValueAt(exec
, index
, value
);
135 JSObject::put(exec
, propertyName
, value
, slot
);
138 void RuntimeArray::put(ExecState
* exec
, unsigned index
, JSValue value
)
140 if (index
>= getLength()) {
141 throwError(exec
, RangeError
);
145 getConcreteArray()->setValueAt(exec
, index
, value
);
148 bool RuntimeArray::deleteProperty(ExecState
*, const Identifier
&)
153 bool RuntimeArray::deleteProperty(ExecState
*, unsigned)