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 "JSDOMBinding.h"
32
33 using namespace WebCore;
34
35 namespace JSC {
36
37 const ClassInfo RuntimeArray::s_info = { "RuntimeArray", &JSArray::info, 0, 0 };
38
RuntimeArray(ExecState * exec,Bindings::Array * array)39 RuntimeArray::RuntimeArray(ExecState* exec, Bindings::Array* array)
40 // FIXME: deprecatedGetDOMStructure uses the prototype off of the wrong global object
41 // We need to pass in the right global object for "array".
42 : JSObject(deprecatedGetDOMStructure<RuntimeArray>(exec))
43 , _array(array)
44 {
45 }
46
lengthGetter(ExecState * exec,const Identifier &,const PropertySlot & slot)47 JSValue RuntimeArray::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
48 {
49 RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slot.slotBase()));
50 return jsNumber(exec, thisObj->getLength());
51 }
52
indexGetter(ExecState * exec,const Identifier &,const PropertySlot & slot)53 JSValue RuntimeArray::indexGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
54 {
55 RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slot.slotBase()));
56 return thisObj->getConcreteArray()->valueAt(exec, slot.index());
57 }
58
getOwnPropertySlot(ExecState * exec,const Identifier & propertyName,PropertySlot & slot)59 bool RuntimeArray::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
60 {
61 if (propertyName == exec->propertyNames().length) {
62 slot.setCustom(this, lengthGetter);
63 return true;
64 }
65
66 bool ok;
67 unsigned index = propertyName.toArrayIndex(&ok);
68 if (ok) {
69 if (index < getLength()) {
70 slot.setCustomIndex(this, index, indexGetter);
71 return true;
72 }
73 }
74
75 return JSObject::getOwnPropertySlot(exec, propertyName, slot);
76 }
77
getOwnPropertySlot(ExecState * exec,unsigned index,PropertySlot & slot)78 bool RuntimeArray::getOwnPropertySlot(ExecState *exec, unsigned index, PropertySlot& slot)
79 {
80 if (index < getLength()) {
81 slot.setCustomIndex(this, index, indexGetter);
82 return true;
83 }
84
85 return JSObject::getOwnPropertySlot(exec, index, slot);
86 }
87
put(ExecState * exec,const Identifier & propertyName,JSValue value,PutPropertySlot & slot)88 void RuntimeArray::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
89 {
90 if (propertyName == exec->propertyNames().length) {
91 throwError(exec, RangeError);
92 return;
93 }
94
95 bool ok;
96 unsigned index = propertyName.toArrayIndex(&ok);
97 if (ok) {
98 getConcreteArray()->setValueAt(exec, index, value);
99 return;
100 }
101
102 JSObject::put(exec, propertyName, value, slot);
103 }
104
put(ExecState * exec,unsigned index,JSValue value)105 void RuntimeArray::put(ExecState* exec, unsigned index, JSValue value)
106 {
107 if (index >= getLength()) {
108 throwError(exec, RangeError);
109 return;
110 }
111
112 getConcreteArray()->setValueAt(exec, index, value);
113 }
114
deleteProperty(ExecState *,const Identifier &)115 bool RuntimeArray::deleteProperty(ExecState*, const Identifier&)
116 {
117 return false;
118 }
119
deleteProperty(ExecState *,unsigned)120 bool RuntimeArray::deleteProperty(ExecState*, unsigned)
121 {
122 return false;
123 }
124
125 }
126