1 | /*
|
---|
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2004, 2005, 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 | #ifndef JSCell_h
|
---|
24 | #define JSCell_h
|
---|
25 |
|
---|
26 | #include "StructureID.h"
|
---|
27 | #include "JSValue.h"
|
---|
28 | #include "collector.h"
|
---|
29 |
|
---|
30 | namespace JSC {
|
---|
31 |
|
---|
32 | class JSCell : public JSValue {
|
---|
33 | friend class Heap;
|
---|
34 | friend class GetterSetter;
|
---|
35 | friend class JSObject;
|
---|
36 | friend class JSPropertyNameIterator;
|
---|
37 | friend class JSValue;
|
---|
38 | friend class JSNumberCell;
|
---|
39 | friend class JSString;
|
---|
40 | friend class Machine;
|
---|
41 | friend class CTI;
|
---|
42 | private:
|
---|
43 | explicit JSCell(StructureID*);
|
---|
44 | virtual ~JSCell();
|
---|
45 |
|
---|
46 | public:
|
---|
47 | // Querying the type.
|
---|
48 | bool isNumber() const;
|
---|
49 | bool isString() const;
|
---|
50 | bool isObject() const;
|
---|
51 | virtual bool isGetterSetter() const;
|
---|
52 | virtual bool isObject(const ClassInfo*) const;
|
---|
53 |
|
---|
54 | StructureID* structureID() const;
|
---|
55 |
|
---|
56 | // Extracting the value.
|
---|
57 | bool getNumber(double&) const;
|
---|
58 | double getNumber() const; // NaN if not a number
|
---|
59 | bool getString(UString&) const;
|
---|
60 | UString getString() const; // null string if not a string
|
---|
61 | JSObject* getObject(); // NULL if not an object
|
---|
62 | const JSObject* getObject() const; // NULL if not an object
|
---|
63 |
|
---|
64 | virtual CallType getCallData(CallData&);
|
---|
65 | virtual ConstructType getConstructData(ConstructData&);
|
---|
66 |
|
---|
67 | // Extracting integer values.
|
---|
68 | virtual bool getUInt32(uint32_t&) const;
|
---|
69 | virtual bool getTruncatedInt32(int32_t&) const;
|
---|
70 | virtual bool getTruncatedUInt32(uint32_t&) const;
|
---|
71 |
|
---|
72 | // Basic conversions.
|
---|
73 | virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType) const = 0;
|
---|
74 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*&) = 0;
|
---|
75 | virtual bool toBoolean(ExecState*) const = 0;
|
---|
76 | virtual double toNumber(ExecState*) const = 0;
|
---|
77 | virtual UString toString(ExecState*) const = 0;
|
---|
78 | virtual JSObject* toObject(ExecState*) const = 0;
|
---|
79 |
|
---|
80 | // Garbage collection.
|
---|
81 | void* operator new(size_t, ExecState*);
|
---|
82 | void* operator new(size_t, void* placementNewDestination) { return placementNewDestination; }
|
---|
83 | virtual void mark();
|
---|
84 | bool marked() const;
|
---|
85 |
|
---|
86 | // Object operations, with the toObject operation included.
|
---|
87 | virtual const ClassInfo* classInfo() const;
|
---|
88 | virtual void put(ExecState*, const Identifier& propertyName, JSValue*, PutPropertySlot&);
|
---|
89 | virtual void put(ExecState*, unsigned propertyName, JSValue*);
|
---|
90 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
91 | virtual bool deleteProperty(ExecState*, unsigned propertyName);
|
---|
92 |
|
---|
93 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
94 | virtual UString toThisString(ExecState*) const;
|
---|
95 | virtual JSString* toThisJSString(ExecState*);
|
---|
96 | virtual JSValue* getJSNumber();
|
---|
97 | void* vptr() { return *reinterpret_cast<void**>(this); }
|
---|
98 |
|
---|
99 | private:
|
---|
100 | // Base implementation, but for non-object classes implements getPropertySlot.
|
---|
101 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
102 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
103 |
|
---|
104 | StructureID* m_structureID;
|
---|
105 | };
|
---|
106 |
|
---|
107 | inline JSCell::JSCell(StructureID* structureID)
|
---|
108 | : m_structureID(structureID)
|
---|
109 | {
|
---|
110 | }
|
---|
111 |
|
---|
112 | inline JSCell::~JSCell()
|
---|
113 | {
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline bool JSCell::isNumber() const
|
---|
117 | {
|
---|
118 | return Heap::isNumber(const_cast<JSCell*>(this));
|
---|
119 | }
|
---|
120 |
|
---|
121 | inline bool JSCell::isObject() const
|
---|
122 | {
|
---|
123 | return m_structureID->typeInfo().type() == ObjectType;
|
---|
124 | }
|
---|
125 |
|
---|
126 | inline bool JSCell::isString() const
|
---|
127 | {
|
---|
128 | return m_structureID->typeInfo().type() == StringType;
|
---|
129 | }
|
---|
130 |
|
---|
131 | inline StructureID* JSCell::structureID() const
|
---|
132 | {
|
---|
133 | return m_structureID;
|
---|
134 | }
|
---|
135 |
|
---|
136 | inline bool JSCell::marked() const
|
---|
137 | {
|
---|
138 | return Heap::isCellMarked(this);
|
---|
139 | }
|
---|
140 |
|
---|
141 | inline void JSCell::mark()
|
---|
142 | {
|
---|
143 | return Heap::markCell(this);
|
---|
144 | }
|
---|
145 |
|
---|
146 | ALWAYS_INLINE JSCell* JSValue::asCell()
|
---|
147 | {
|
---|
148 | ASSERT(!JSImmediate::isImmediate(this));
|
---|
149 | return static_cast<JSCell*>(this);
|
---|
150 | }
|
---|
151 |
|
---|
152 | ALWAYS_INLINE const JSCell* JSValue::asCell() const
|
---|
153 | {
|
---|
154 | ASSERT(!JSImmediate::isImmediate(this));
|
---|
155 | return static_cast<const JSCell*>(this);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | // --- JSValue inlines ----------------------------
|
---|
160 |
|
---|
161 | inline bool JSValue::isNumber() const
|
---|
162 | {
|
---|
163 | return JSImmediate::isNumber(this) || (!JSImmediate::isImmediate(this) && asCell()->isNumber());
|
---|
164 | }
|
---|
165 |
|
---|
166 | inline bool JSValue::isString() const
|
---|
167 | {
|
---|
168 | return !JSImmediate::isImmediate(this) && asCell()->isString();
|
---|
169 | }
|
---|
170 |
|
---|
171 | inline bool JSValue::isGetterSetter() const
|
---|
172 | {
|
---|
173 | return !JSImmediate::isImmediate(this) && asCell()->isGetterSetter();
|
---|
174 | }
|
---|
175 |
|
---|
176 | inline bool JSValue::isObject() const
|
---|
177 | {
|
---|
178 | return !JSImmediate::isImmediate(this) && asCell()->isObject();
|
---|
179 | }
|
---|
180 |
|
---|
181 | inline bool JSValue::getNumber(double& v) const
|
---|
182 | {
|
---|
183 | if (JSImmediate::isImmediate(this)) {
|
---|
184 | v = JSImmediate::toDouble(this);
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 | return asCell()->getNumber(v);
|
---|
188 | }
|
---|
189 |
|
---|
190 | inline double JSValue::getNumber() const
|
---|
191 | {
|
---|
192 | return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->getNumber();
|
---|
193 | }
|
---|
194 |
|
---|
195 | inline bool JSValue::getString(UString& s) const
|
---|
196 | {
|
---|
197 | return !JSImmediate::isImmediate(this) && asCell()->getString(s);
|
---|
198 | }
|
---|
199 |
|
---|
200 | inline UString JSValue::getString() const
|
---|
201 | {
|
---|
202 | return JSImmediate::isImmediate(this) ? UString() : asCell()->getString();
|
---|
203 | }
|
---|
204 |
|
---|
205 | inline JSObject* JSValue::getObject()
|
---|
206 | {
|
---|
207 | return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
|
---|
208 | }
|
---|
209 |
|
---|
210 | inline const JSObject* JSValue::getObject() const
|
---|
211 | {
|
---|
212 | return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
|
---|
213 | }
|
---|
214 |
|
---|
215 | inline CallType JSValue::getCallData(CallData& callData)
|
---|
216 | {
|
---|
217 | return JSImmediate::isImmediate(this) ? CallTypeNone : asCell()->getCallData(callData);
|
---|
218 | }
|
---|
219 |
|
---|
220 | inline ConstructType JSValue::getConstructData(ConstructData& constructData)
|
---|
221 | {
|
---|
222 | return JSImmediate::isImmediate(this) ? ConstructTypeNone : asCell()->getConstructData(constructData);
|
---|
223 | }
|
---|
224 |
|
---|
225 | ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
|
---|
226 | {
|
---|
227 | return JSImmediate::isImmediate(this) ? JSImmediate::getUInt32(this, v) : asCell()->getUInt32(v);
|
---|
228 | }
|
---|
229 |
|
---|
230 | ALWAYS_INLINE bool JSValue::getTruncatedInt32(int32_t& v) const
|
---|
231 | {
|
---|
232 | return JSImmediate::isImmediate(this) ? JSImmediate::getTruncatedInt32(this, v) : asCell()->getTruncatedInt32(v);
|
---|
233 | }
|
---|
234 |
|
---|
235 | inline bool JSValue::getTruncatedUInt32(uint32_t& v) const
|
---|
236 | {
|
---|
237 | return JSImmediate::isImmediate(this) ? JSImmediate::getTruncatedUInt32(this, v) : asCell()->getTruncatedUInt32(v);
|
---|
238 | }
|
---|
239 |
|
---|
240 | inline void JSValue::mark()
|
---|
241 | {
|
---|
242 | ASSERT(!JSImmediate::isImmediate(this)); // callers should check !marked() before calling mark()
|
---|
243 | asCell()->mark();
|
---|
244 | }
|
---|
245 |
|
---|
246 | inline bool JSValue::marked() const
|
---|
247 | {
|
---|
248 | return JSImmediate::isImmediate(this) || asCell()->marked();
|
---|
249 | }
|
---|
250 |
|
---|
251 | inline JSValue* JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
|
---|
252 | {
|
---|
253 | return JSImmediate::isImmediate(this) ? const_cast<JSValue*>(this) : asCell()->toPrimitive(exec, preferredType);
|
---|
254 | }
|
---|
255 |
|
---|
256 | inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue*& value)
|
---|
257 | {
|
---|
258 | if (JSImmediate::isImmediate(this)) {
|
---|
259 | number = JSImmediate::toDouble(this);
|
---|
260 | value = this;
|
---|
261 | return true;
|
---|
262 | }
|
---|
263 | return asCell()->getPrimitiveNumber(exec, number, value);
|
---|
264 | }
|
---|
265 |
|
---|
266 | inline bool JSValue::toBoolean(ExecState* exec) const
|
---|
267 | {
|
---|
268 | return JSImmediate::isImmediate(this) ? JSImmediate::toBoolean(this) : asCell()->toBoolean(exec);
|
---|
269 | }
|
---|
270 |
|
---|
271 | ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
|
---|
272 | {
|
---|
273 | return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->toNumber(exec);
|
---|
274 | }
|
---|
275 |
|
---|
276 | inline UString JSValue::toString(ExecState* exec) const
|
---|
277 | {
|
---|
278 | return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : asCell()->toString(exec);
|
---|
279 | }
|
---|
280 |
|
---|
281 | inline JSObject* JSValue::toObject(ExecState* exec) const
|
---|
282 | {
|
---|
283 | return JSImmediate::isImmediate(this) ? JSImmediate::toObject(this, exec) : asCell()->toObject(exec);
|
---|
284 | }
|
---|
285 |
|
---|
286 | inline JSObject* JSValue::toThisObject(ExecState* exec) const
|
---|
287 | {
|
---|
288 | if (UNLIKELY(JSImmediate::isImmediate(this)))
|
---|
289 | return JSImmediate::toObject(this, exec);
|
---|
290 | return asCell()->toThisObject(exec);
|
---|
291 | }
|
---|
292 |
|
---|
293 | inline UString JSValue::toThisString(ExecState* exec) const
|
---|
294 | {
|
---|
295 | return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : asCell()->toThisString(exec);
|
---|
296 | }
|
---|
297 |
|
---|
298 | inline JSValue* JSValue::getJSNumber()
|
---|
299 | {
|
---|
300 |
|
---|
301 | return JSImmediate::isNumber(this) ? this : (JSImmediate::isImmediate(this) ? 0 : asCell()->getJSNumber());
|
---|
302 | }
|
---|
303 |
|
---|
304 | } // namespace JSC
|
---|
305 |
|
---|
306 | #endif // JSCell_h
|
---|