]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSCell.cpp
JavaScriptCore-1218.35.tar.gz
[apple/javascriptcore.git] / 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 "Operations.h"
31 #include <wtf/MathExtras.h>
32
33 namespace JSC {
34
35 ASSERT_HAS_TRIVIAL_DESTRUCTOR(JSCell);
36
37 void JSCell::destroy(JSCell* cell)
38 {
39 cell->JSCell::~JSCell();
40 }
41
42 void JSCell::copyBackingStore(JSCell*, CopyVisitor&)
43 {
44 }
45
46 bool JSCell::getString(ExecState* exec, String& stringValue) const
47 {
48 if (!isString())
49 return false;
50 stringValue = static_cast<const JSString*>(this)->value(exec);
51 return true;
52 }
53
54 String JSCell::getString(ExecState* exec) const
55 {
56 return isString() ? static_cast<const JSString*>(this)->value(exec) : String();
57 }
58
59 JSObject* JSCell::getObject()
60 {
61 return isObject() ? asObject(this) : 0;
62 }
63
64 const JSObject* JSCell::getObject() const
65 {
66 return isObject() ? static_cast<const JSObject*>(this) : 0;
67 }
68
69 CallType JSCell::getCallData(JSCell*, CallData& callData)
70 {
71 callData.js.functionExecutable = 0;
72 callData.js.scope = 0;
73 callData.native.function = 0;
74 return CallTypeNone;
75 }
76
77 ConstructType JSCell::getConstructData(JSCell*, ConstructData& constructData)
78 {
79 constructData.js.functionExecutable = 0;
80 constructData.js.scope = 0;
81 constructData.native.function = 0;
82 return ConstructTypeNone;
83 }
84
85 bool JSCell::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName identifier, PropertySlot& slot)
86 {
87 // This is not a general purpose implementation of getOwnPropertySlot.
88 // It should only be called by JSValue::get.
89 // It calls getPropertySlot, not getOwnPropertySlot.
90 JSObject* object = cell->toObject(exec, exec->lexicalGlobalObject());
91 slot.setBase(object);
92 if (!object->getPropertySlot(exec, identifier, slot))
93 slot.setUndefined();
94 return true;
95 }
96
97 bool JSCell::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned identifier, PropertySlot& slot)
98 {
99 // This is not a general purpose implementation of getOwnPropertySlot.
100 // It should only be called by JSValue::get.
101 // It calls getPropertySlot, not getOwnPropertySlot.
102 JSObject* object = cell->toObject(exec, exec->lexicalGlobalObject());
103 slot.setBase(object);
104 if (!object->getPropertySlot(exec, identifier, slot))
105 slot.setUndefined();
106 return true;
107 }
108
109 void JSCell::put(JSCell* cell, ExecState* exec, PropertyName identifier, JSValue value, PutPropertySlot& slot)
110 {
111 if (cell->isString()) {
112 JSValue(cell).putToPrimitive(exec, identifier, value, slot);
113 return;
114 }
115 JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
116 thisObject->methodTable()->put(thisObject, exec, identifier, value, slot);
117 }
118
119 void JSCell::putByIndex(JSCell* cell, ExecState* exec, unsigned identifier, JSValue value, bool shouldThrow)
120 {
121 if (cell->isString()) {
122 PutPropertySlot slot(shouldThrow);
123 JSValue(cell).putToPrimitive(exec, Identifier::from(exec, identifier), value, slot);
124 return;
125 }
126 JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
127 thisObject->methodTable()->putByIndex(thisObject, exec, identifier, value, shouldThrow);
128 }
129
130 bool JSCell::deleteProperty(JSCell* cell, ExecState* exec, PropertyName identifier)
131 {
132 JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
133 return thisObject->methodTable()->deleteProperty(thisObject, exec, identifier);
134 }
135
136 bool JSCell::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned identifier)
137 {
138 JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
139 return thisObject->methodTable()->deletePropertyByIndex(thisObject, exec, identifier);
140 }
141
142 JSObject* JSCell::toThisObject(JSCell* cell, ExecState* exec)
143 {
144 return cell->toObject(exec, exec->lexicalGlobalObject());
145 }
146
147 JSValue JSCell::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
148 {
149 if (isString())
150 return static_cast<const JSString*>(this)->toPrimitive(exec, preferredType);
151 return static_cast<const JSObject*>(this)->toPrimitive(exec, preferredType);
152 }
153
154 bool JSCell::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value) const
155 {
156 if (isString())
157 return static_cast<const JSString*>(this)->getPrimitiveNumber(exec, number, value);
158 return static_cast<const JSObject*>(this)->getPrimitiveNumber(exec, number, value);
159 }
160
161 double JSCell::toNumber(ExecState* exec) const
162 {
163 if (isString())
164 return static_cast<const JSString*>(this)->toNumber(exec);
165 return static_cast<const JSObject*>(this)->toNumber(exec);
166 }
167
168 JSObject* JSCell::toObject(ExecState* exec, JSGlobalObject* globalObject) const
169 {
170 if (isString())
171 return static_cast<const JSString*>(this)->toObject(exec, globalObject);
172 ASSERT(isObject());
173 return jsCast<JSObject*>(const_cast<JSCell*>(this));
174 }
175
176 void slowValidateCell(JSCell* cell)
177 {
178 ASSERT_GC_OBJECT_LOOKS_VALID(cell);
179 }
180
181 JSValue JSCell::defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType)
182 {
183 RELEASE_ASSERT_NOT_REACHED();
184 return jsUndefined();
185 }
186
187 void JSCell::getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
188 {
189 RELEASE_ASSERT_NOT_REACHED();
190 }
191
192 void JSCell::getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
193 {
194 RELEASE_ASSERT_NOT_REACHED();
195 }
196
197 String JSCell::className(const JSObject*)
198 {
199 RELEASE_ASSERT_NOT_REACHED();
200 return String();
201 }
202
203 const char* JSCell::className()
204 {
205 return classInfo()->className;
206 }
207
208 void JSCell::getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
209 {
210 RELEASE_ASSERT_NOT_REACHED();
211 }
212
213 bool JSCell::customHasInstance(JSObject*, ExecState*, JSValue)
214 {
215 RELEASE_ASSERT_NOT_REACHED();
216 return false;
217 }
218
219 void JSCell::putDirectVirtual(JSObject*, ExecState*, PropertyName, JSValue, unsigned)
220 {
221 RELEASE_ASSERT_NOT_REACHED();
222 }
223
224 bool JSCell::defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool)
225 {
226 RELEASE_ASSERT_NOT_REACHED();
227 return false;
228 }
229
230 bool JSCell::getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&)
231 {
232 RELEASE_ASSERT_NOT_REACHED();
233 return false;
234 }
235
236 } // namespace JSC