]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSCell.cpp
JavaScriptCore-1097.3.tar.gz
[apple/javascriptcore.git] / runtime / JSCell.cpp
CommitLineData
b37bf2e1
A
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"
9dae56ea 24#include "JSCell.h"
b37bf2e1 25
9dae56ea
A
26#include "JSFunction.h"
27#include "JSString.h"
28#include "JSObject.h"
6fe7ccc8 29#include "NumberObject.h"
b37bf2e1
A
30#include <wtf/MathExtras.h>
31
9dae56ea 32namespace JSC {
b37bf2e1 33
6fe7ccc8
A
34ASSERT_HAS_TRIVIAL_DESTRUCTOR(JSCell);
35
36void JSCell::destroy(JSCell* cell)
b37bf2e1 37{
6fe7ccc8 38 cell->JSCell::~JSCell();
b37bf2e1
A
39}
40
f9bf01c6 41bool JSCell::getString(ExecState* exec, UString&stringValue) const
b37bf2e1 42{
9dae56ea
A
43 if (!isString())
44 return false;
f9bf01c6 45 stringValue = static_cast<const JSString*>(this)->value(exec);
9dae56ea 46 return true;
b37bf2e1
A
47}
48
f9bf01c6 49UString JSCell::getString(ExecState* exec) const
b37bf2e1 50{
f9bf01c6 51 return isString() ? static_cast<const JSString*>(this)->value(exec) : UString();
b37bf2e1
A
52}
53
9dae56ea 54JSObject* JSCell::getObject()
b37bf2e1 55{
f9bf01c6 56 return isObject() ? asObject(this) : 0;
b37bf2e1
A
57}
58
9dae56ea 59const JSObject* JSCell::getObject() const
b37bf2e1 60{
9dae56ea 61 return isObject() ? static_cast<const JSObject*>(this) : 0;
b37bf2e1
A
62}
63
6fe7ccc8 64CallType JSCell::getCallData(JSCell*, CallData&)
b37bf2e1 65{
9dae56ea 66 return CallTypeNone;
b37bf2e1
A
67}
68
6fe7ccc8 69ConstructType JSCell::getConstructData(JSCell*, ConstructData&)
b37bf2e1 70{
9dae56ea 71 return ConstructTypeNone;
b37bf2e1
A
72}
73
6fe7ccc8 74bool JSCell::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& identifier, PropertySlot& slot)
b37bf2e1 75{
9dae56ea
A
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.
6fe7ccc8 79 JSObject* object = cell->toObject(exec, exec->lexicalGlobalObject());
9dae56ea
A
80 slot.setBase(object);
81 if (!object->getPropertySlot(exec, identifier, slot))
82 slot.setUndefined();
83 return true;
b37bf2e1
A
84}
85
6fe7ccc8 86bool JSCell::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned identifier, PropertySlot& slot)
b37bf2e1 87{
9dae56ea
A
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.
6fe7ccc8 91 JSObject* object = cell->toObject(exec, exec->lexicalGlobalObject());
9dae56ea
A
92 slot.setBase(object);
93 if (!object->getPropertySlot(exec, identifier, slot))
94 slot.setUndefined();
b37bf2e1
A
95 return true;
96}
97
6fe7ccc8 98void JSCell::put(JSCell* cell, ExecState* exec, const Identifier& identifier, JSValue value, PutPropertySlot& slot)
b37bf2e1 99{
6fe7ccc8
A
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);
b37bf2e1
A
106}
107
6fe7ccc8 108void JSCell::putByIndex(JSCell* cell, ExecState* exec, unsigned identifier, JSValue value, bool shouldThrow)
b37bf2e1 109{
6fe7ccc8
A
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);
b37bf2e1
A
117}
118
6fe7ccc8 119bool JSCell::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& identifier)
b37bf2e1 120{
6fe7ccc8
A
121 JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
122 return thisObject->methodTable()->deleteProperty(thisObject, exec, identifier);
b37bf2e1
A
123}
124
6fe7ccc8 125bool JSCell::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned identifier)
b37bf2e1 126{
6fe7ccc8
A
127 JSObject* thisObject = cell->toObject(exec, exec->lexicalGlobalObject());
128 return thisObject->methodTable()->deletePropertyByIndex(thisObject, exec, identifier);
b37bf2e1
A
129}
130
6fe7ccc8 131JSObject* JSCell::toThisObject(JSCell* cell, ExecState* exec)
b37bf2e1 132{
6fe7ccc8 133 return cell->toObject(exec, exec->lexicalGlobalObject());
b37bf2e1
A
134}
135
6fe7ccc8 136JSValue JSCell::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
b37bf2e1 137{
6fe7ccc8
A
138 if (isString())
139 return static_cast<const JSString*>(this)->toPrimitive(exec, preferredType);
140 return static_cast<const JSObject*>(this)->toPrimitive(exec, preferredType);
9dae56ea
A
141}
142
6fe7ccc8 143bool JSCell::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value) const
9dae56ea 144{
6fe7ccc8
A
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
150double 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
157JSObject* 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));
b37bf2e1
A
163}
164
6fe7ccc8
A
165void slowValidateCell(JSCell* cell)
166{
167 ASSERT_GC_OBJECT_LOOKS_VALID(cell);
168}
169
170JSValue JSCell::defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType)
f9bf01c6
A
171{
172 ASSERT_NOT_REACHED();
6fe7ccc8 173 return jsUndefined();
f9bf01c6
A
174}
175
6fe7ccc8 176void JSCell::getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
f9bf01c6
A
177{
178 ASSERT_NOT_REACHED();
f9bf01c6
A
179}
180
6fe7ccc8 181UString JSCell::className(const JSObject*)
f9bf01c6
A
182{
183 ASSERT_NOT_REACHED();
6fe7ccc8 184 return UString();
f9bf01c6
A
185}
186
6fe7ccc8 187void JSCell::getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode)
f9bf01c6
A
188{
189 ASSERT_NOT_REACHED();
f9bf01c6
A
190}
191
6fe7ccc8 192bool JSCell::hasInstance(JSObject*, ExecState*, JSValue, JSValue)
f9bf01c6
A
193{
194 ASSERT_NOT_REACHED();
6fe7ccc8 195 return false;
f9bf01c6
A
196}
197
6fe7ccc8 198void JSCell::putDirectVirtual(JSObject*, ExecState*, const Identifier&, JSValue, unsigned)
f9bf01c6
A
199{
200 ASSERT_NOT_REACHED();
f9bf01c6
A
201}
202
6fe7ccc8 203bool JSCell::defineOwnProperty(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&, bool)
14957cd0 204{
6fe7ccc8 205 ASSERT_NOT_REACHED();
14957cd0 206 return false;
14957cd0
A
207}
208
6fe7ccc8 209bool JSCell::getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&)
14957cd0 210{
6fe7ccc8
A
211 ASSERT_NOT_REACHED();
212 return false;
14957cd0
A
213}
214
9dae56ea 215} // namespace JSC