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