tizen beta release
[profile/ivi/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(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
70 {
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));
75
76     if (mode == IncludeDontEnumProperties)
77         propertyNames.add(exec->propertyNames().length);
78
79     JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
80 }
81
82 bool RuntimeArray::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
83 {
84     RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
85     if (propertyName == exec->propertyNames().length) {
86         slot.setCacheableCustom(thisObject, thisObject->lengthGetter);
87         return true;
88     }
89     
90     bool ok;
91     unsigned index = propertyName.toArrayIndex(ok);
92     if (ok) {
93         if (index < thisObject->getLength()) {
94             slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
95             return true;
96         }
97     }
98     
99     return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
100 }
101
102 bool RuntimeArray::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
103 {
104     RuntimeArray* thisObject = jsCast<RuntimeArray*>(object);
105     if (propertyName == exec->propertyNames().length) {
106         PropertySlot slot;
107         slot.setCustom(thisObject, lengthGetter);
108         descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
109         return true;
110     }
111     
112     bool ok;
113     unsigned index = propertyName.toArrayIndex(ok);
114     if (ok) {
115         if (index < thisObject->getLength()) {
116             PropertySlot slot;
117             slot.setCustomIndex(thisObject, index, indexGetter);
118             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | DontEnum);
119             return true;
120         }
121     }
122     
123     return JSObject::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
124 }
125
126 bool RuntimeArray::getOwnPropertySlotByIndex(JSCell* cell, ExecState *exec, unsigned index, PropertySlot& slot)
127 {
128     RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
129     if (index < thisObject->getLength()) {
130         slot.setCustomIndex(thisObject, index, thisObject->indexGetter);
131         return true;
132     }
133     
134     return JSObject::getOwnPropertySlotByIndex(thisObject, exec, index, slot);
135 }
136
137 void RuntimeArray::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
138 {
139     RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
140     if (propertyName == exec->propertyNames().length) {
141         throwError(exec, createRangeError(exec, "Range error"));
142         return;
143     }
144     
145     bool ok;
146     unsigned index = propertyName.toArrayIndex(ok);
147     if (ok) {
148         thisObject->getConcreteArray()->setValueAt(exec, index, value);
149         return;
150     }
151     
152     JSObject::put(thisObject, exec, propertyName, value, slot);
153 }
154
155 void RuntimeArray::putByIndex(JSCell* cell, ExecState* exec, unsigned index, JSValue value)
156 {
157     RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell);
158     if (index >= thisObject->getLength()) {
159         throwError(exec, createRangeError(exec, "Range error"));
160         return;
161     }
162     
163     thisObject->getConcreteArray()->setValueAt(exec, index, value);
164 }
165
166 bool RuntimeArray::deleteProperty(JSCell*, ExecState*, const Identifier&)
167 {
168     return false;
169 }
170
171 bool RuntimeArray::deletePropertyByIndex(JSCell*, ExecState*, unsigned)
172 {
173     return false;
174 }
175
176 }