tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / JSCell.cpp
1 /*
2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4  *  Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public License
17  *  along with this library; see the file COPYING.LIB.  If not, write to
18  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #include "config.h"
24 #include "JSCell.h"
25
26 #include "JSFunction.h"
27 #include "JSString.h"
28 #include "JSObject.h"
29 #include "NumberObject.h"
30 #include <wtf/MathExtras.h>
31
32 namespace JSC {
33
34 bool JSCell::getString(ExecState* exec, UString&stringValue) const
35 {
36     if (!isString())
37         return false;
38     stringValue = static_cast<const JSString*>(this)->value(exec);
39     return true;
40 }
41
42 UString JSCell::getString(ExecState* exec) const
43 {
44     return isString() ? static_cast<const JSString*>(this)->value(exec) : UString();
45 }
46
47 JSObject* JSCell::getObject()
48 {
49     return isObject() ? asObject(this) : 0;
50 }
51
52 const JSObject* JSCell::getObject() const
53 {
54     return isObject() ? static_cast<const JSObject*>(this) : 0;
55 }
56
57 CallType JSCell::getCallData(JSCell*, CallData&)
58 {
59     return CallTypeNone;
60 }
61
62 ConstructType JSCell::getConstructData(JSCell*, ConstructData&)
63 {
64     return ConstructTypeNone;
65 }
66
67 bool JSCell::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& identifier, PropertySlot& slot)
68 {
69     // This is not a general purpose implementation of getOwnPropertySlot.
70     // It should only be called by JSValue::get.
71     // It calls getPropertySlot, not getOwnPropertySlot.
72     JSObject* object = cell->toObject(exec, exec->lexicalGlobalObject());
73     slot.setBase(object);
74     if (!object->getPropertySlot(exec, identifier, slot))
75         slot.setUndefined();
76     return true;
77 }
78
79 bool JSCell::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned identifier, PropertySlot& slot)
80 {
81     // This is not a general purpose implementation of getOwnPropertySlot.
82     // It should only be called by JSValue::get.
83     // It calls getPropertySlot, not getOwnPropertySlot.
84     JSObject* object = cell->toObject(exec, exec->lexicalGlobalObject());
85     slot.setBase(object);
86     if (!object->getPropertySlot(exec, identifier, slot))
87         slot.setUndefined();
88     return true;
89 }
90
91 void JSCell::put(JSCell* cell, ExecState* exec, const Identifier& identifier, JSValue value, PutPropertySlot& slot)
92 {
93     JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
94     thisObject->methodTable()->put(thisObject, exec, identifier, value, slot);
95 }
96
97 void JSCell::putByIndex(JSCell* cell, ExecState* exec, unsigned identifier, JSValue value)
98 {
99     JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
100     thisObject->methodTable()->putByIndex(thisObject, exec, identifier, value);
101 }
102
103 bool JSCell::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& identifier)
104 {
105     JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
106     return thisObject->methodTable()->deleteProperty(thisObject, exec, identifier);
107 }
108
109 bool JSCell::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned identifier)
110 {
111     JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
112     return thisObject->methodTable()->deletePropertyByIndex(thisObject, exec, identifier);
113 }
114
115 JSObject* JSCell::toThisObject(JSCell* cell, ExecState* exec)
116 {
117     return cell->toObject(exec, exec->lexicalGlobalObject());
118 }
119
120 JSValue JSCell::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
121 {
122     if (isString())
123         return static_cast<const JSString*>(this)->toPrimitive(exec, preferredType);
124     return static_cast<const JSObject*>(this)->toPrimitive(exec, preferredType);
125 }
126
127 bool JSCell::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value) const
128 {
129     if (isString())
130         return static_cast<const JSString*>(this)->getPrimitiveNumber(exec, number, value);
131     return static_cast<const JSObject*>(this)->getPrimitiveNumber(exec, number, value);
132 }
133
134 double JSCell::toNumber(ExecState* exec) const
135
136     if (isString())
137         return static_cast<const JSString*>(this)->toNumber(exec);
138     return static_cast<const JSObject*>(this)->toNumber(exec);
139 }
140
141 UString JSCell::toString(ExecState* exec) const
142 {
143     if (isString())
144         return static_cast<const JSString*>(this)->toString(exec);
145     return static_cast<const JSObject*>(this)->toString(exec);
146 }
147
148 JSObject* JSCell::toObject(ExecState* exec, JSGlobalObject* globalObject) const
149 {
150     if (isString())
151         return static_cast<const JSString*>(this)->toObject(exec, globalObject);
152     ASSERT(isObject());
153     return static_cast<JSObject*>(const_cast<JSCell*>(this));
154 }
155
156 void slowValidateCell(JSCell* cell)
157 {
158     ASSERT_GC_OBJECT_LOOKS_VALID(cell);
159 }
160
161 void JSCell::defineGetter(JSObject*, ExecState*, const Identifier&, JSObject*, unsigned)
162 {
163     ASSERT_NOT_REACHED();
164 }
165
166 void JSCell::defineSetter(JSObject*, ExecState*, const Identifier&, JSObject*, unsigned)
167 {
168     ASSERT_NOT_REACHED();
169 }
170
171 JSValue JSCell::defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType)
172 {
173     ASSERT_NOT_REACHED();
174     return jsUndefined();
175 }
176
177 void JSCell::getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
178 {
179     ASSERT_NOT_REACHED();
180 }
181
182 UString JSCell::className(const JSObject*)
183 {
184     ASSERT_NOT_REACHED();
185     return UString();
186 }
187
188 void JSCell::getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
189 {
190     ASSERT_NOT_REACHED();
191 }
192
193 bool JSCell::hasInstance(JSObject*, ExecState*, JSValue, JSValue)
194 {
195     ASSERT_NOT_REACHED();
196     return false;
197 }
198
199 void JSCell::putWithAttributes(JSObject*, ExecState*, const Identifier&, JSValue, unsigned)
200 {
201     ASSERT_NOT_REACHED();
202 }
203
204 bool JSCell::defineOwnProperty(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&, bool)
205 {
206     ASSERT_NOT_REACHED();
207     return false;
208 }
209
210 bool JSCell::getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&)
211 {
212     ASSERT_NOT_REACHED();
213     return false;
214 }
215
216 } // namespace JSC