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 JSValue_h
|
---|
24 | #define JSValue_h
|
---|
25 |
|
---|
26 | #include "CallData.h"
|
---|
27 | #include "ConstructData.h"
|
---|
28 | #include "JSImmediate.h"
|
---|
29 | #include "ustring.h"
|
---|
30 | #include <stddef.h> // for size_t
|
---|
31 |
|
---|
32 | // The magic number 0x4000 is not important here, it is being subtracted back out (avoiding using zero since this
|
---|
33 | // can have unexpected effects in this type of macro, particularly where multiple-inheritance is involved).
|
---|
34 | #define OBJECT_OFFSET(class, member) (reinterpret_cast<ptrdiff_t>(&(reinterpret_cast<class*>(0x4000)->member)) - 0x4000)
|
---|
35 |
|
---|
36 | namespace JSC {
|
---|
37 |
|
---|
38 | class ExecState;
|
---|
39 | class Identifier;
|
---|
40 | class JSCell;
|
---|
41 | class JSObject;
|
---|
42 | class JSString;
|
---|
43 | class PropertySlot;
|
---|
44 | class PutPropertySlot;
|
---|
45 | class StructureID;
|
---|
46 | struct ClassInfo;
|
---|
47 | struct Instruction;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * JSValue is the base type for all primitives (Undefined, Null, Boolean,
|
---|
51 | * String, Number) and objects in ECMAScript.
|
---|
52 | *
|
---|
53 | * Note: you should never inherit from JSValue as it is for primitive types
|
---|
54 | * only (all of which are provided internally by KJS). Instead, inherit from
|
---|
55 | * JSObject.
|
---|
56 | */
|
---|
57 | class JSValue : Noncopyable {
|
---|
58 | friend class JSCell; // so it can derive from this class
|
---|
59 | friend class Heap; // so it can call asCell()
|
---|
60 | friend class Machine; // so it can call asCell()
|
---|
61 | private:
|
---|
62 | JSValue();
|
---|
63 | virtual ~JSValue();
|
---|
64 |
|
---|
65 | public:
|
---|
66 | // Querying the type.
|
---|
67 | bool isUndefined() const;
|
---|
68 | bool isNull() const;
|
---|
69 | bool isUndefinedOrNull() const;
|
---|
70 | bool isBoolean() const;
|
---|
71 | bool isNumber() const;
|
---|
72 | bool isString() const;
|
---|
73 | bool isGetterSetter() const;
|
---|
74 | bool isObject() const;
|
---|
75 | bool isObject(const ClassInfo*) const; // FIXME: Merge with inherits.
|
---|
76 |
|
---|
77 | // Extracting the value.
|
---|
78 | bool getBoolean(bool&) const;
|
---|
79 | bool getBoolean() const; // false if not a boolean
|
---|
80 | bool getNumber(double&) const;
|
---|
81 | double getNumber() const; // NaN if not a number
|
---|
82 | double uncheckedGetNumber() const;
|
---|
83 | bool getString(UString&) const;
|
---|
84 | UString getString() const; // null string if not a string
|
---|
85 | JSObject* getObject(); // NULL if not an object
|
---|
86 | const JSObject* getObject() const; // NULL if not an object
|
---|
87 |
|
---|
88 | CallType getCallData(CallData&);
|
---|
89 | ConstructType getConstructData(ConstructData&);
|
---|
90 |
|
---|
91 | // Extracting integer values.
|
---|
92 | bool getUInt32(uint32_t&) const;
|
---|
93 | bool getTruncatedInt32(int32_t&) const;
|
---|
94 | bool getTruncatedUInt32(uint32_t&) const;
|
---|
95 |
|
---|
96 | // Basic conversions.
|
---|
97 | enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
|
---|
98 | JSValue* toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
|
---|
99 | bool getPrimitiveNumber(ExecState*, double& number, JSValue*&);
|
---|
100 |
|
---|
101 | bool toBoolean(ExecState*) const;
|
---|
102 |
|
---|
103 | // toNumber conversion is expected to be side effect free if an exception has
|
---|
104 | // been set in the ExecState already.
|
---|
105 | double toNumber(ExecState*) const;
|
---|
106 | JSValue* toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
|
---|
107 | UString toString(ExecState*) const;
|
---|
108 | JSObject* toObject(ExecState*) const;
|
---|
109 |
|
---|
110 | // Integer conversions.
|
---|
111 | double toInteger(ExecState*) const;
|
---|
112 | double toIntegerPreserveNaN(ExecState*) const;
|
---|
113 | int32_t toInt32(ExecState*) const;
|
---|
114 | int32_t toInt32(ExecState*, bool& ok) const;
|
---|
115 | uint32_t toUInt32(ExecState*) const;
|
---|
116 | uint32_t toUInt32(ExecState*, bool& ok) const;
|
---|
117 |
|
---|
118 | // These are identical logic to above, and faster than jsNumber(number)->toInt32(exec)
|
---|
119 | static int32_t toInt32(double);
|
---|
120 | static uint32_t toUInt32(double);
|
---|
121 |
|
---|
122 | // Floating point conversions.
|
---|
123 | float toFloat(ExecState*) const;
|
---|
124 |
|
---|
125 | // Garbage collection.
|
---|
126 | void mark();
|
---|
127 | bool marked() const;
|
---|
128 |
|
---|
129 | static int32_t toInt32SlowCase(double, bool& ok);
|
---|
130 | static uint32_t toUInt32SlowCase(double, bool& ok);
|
---|
131 |
|
---|
132 | // Object operations, with the toObject operation included.
|
---|
133 | JSValue* get(ExecState*, const Identifier& propertyName) const;
|
---|
134 | JSValue* get(ExecState*, const Identifier& propertyName, PropertySlot&) const;
|
---|
135 | JSValue* get(ExecState*, unsigned propertyName) const;
|
---|
136 | JSValue* get(ExecState*, unsigned propertyName, PropertySlot&) const;
|
---|
137 | void put(ExecState*, const Identifier& propertyName, JSValue*, PutPropertySlot&);
|
---|
138 | void put(ExecState*, unsigned propertyName, JSValue*);
|
---|
139 | bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
140 | bool deleteProperty(ExecState*, unsigned propertyName);
|
---|
141 |
|
---|
142 | JSObject* toThisObject(ExecState*) const;
|
---|
143 | UString toThisString(ExecState*) const;
|
---|
144 | JSString* toThisJSString(ExecState*);
|
---|
145 |
|
---|
146 | JSValue* getJSNumber(); // 0 if this is not a JSNumber or number object
|
---|
147 |
|
---|
148 | private:
|
---|
149 | bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
150 | bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
151 | int32_t toInt32SlowCase(ExecState*, bool& ok) const;
|
---|
152 | uint32_t toUInt32SlowCase(ExecState*, bool& ok) const;
|
---|
153 |
|
---|
154 | // Implementation details.
|
---|
155 | JSCell* asCell();
|
---|
156 | const JSCell* asCell() const;
|
---|
157 | };
|
---|
158 |
|
---|
159 | inline JSValue::JSValue()
|
---|
160 | {
|
---|
161 | }
|
---|
162 |
|
---|
163 | inline JSValue::~JSValue()
|
---|
164 | {
|
---|
165 | }
|
---|
166 |
|
---|
167 | inline bool JSValue::isUndefined() const
|
---|
168 | {
|
---|
169 | return this == jsUndefined();
|
---|
170 | }
|
---|
171 |
|
---|
172 | inline bool JSValue::isNull() const
|
---|
173 | {
|
---|
174 | return this == jsNull();
|
---|
175 | }
|
---|
176 |
|
---|
177 | inline bool JSValue::isUndefinedOrNull() const
|
---|
178 | {
|
---|
179 | return JSImmediate::isUndefinedOrNull(this);
|
---|
180 | }
|
---|
181 |
|
---|
182 | inline bool JSValue::isBoolean() const
|
---|
183 | {
|
---|
184 | return JSImmediate::isBoolean(this);
|
---|
185 | }
|
---|
186 |
|
---|
187 | inline bool JSValue::getBoolean(bool& v) const
|
---|
188 | {
|
---|
189 | if (JSImmediate::isBoolean(this)) {
|
---|
190 | v = JSImmediate::toBoolean(this);
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 |
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 |
|
---|
197 | inline bool JSValue::getBoolean() const
|
---|
198 | {
|
---|
199 | return this == jsBoolean(true);
|
---|
200 | }
|
---|
201 |
|
---|
202 | ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const
|
---|
203 | {
|
---|
204 | int32_t i;
|
---|
205 | if (getTruncatedInt32(i))
|
---|
206 | return i;
|
---|
207 | bool ok;
|
---|
208 | return toInt32SlowCase(exec, ok);
|
---|
209 | }
|
---|
210 |
|
---|
211 | inline uint32_t JSValue::toUInt32(ExecState* exec) const
|
---|
212 | {
|
---|
213 | uint32_t i;
|
---|
214 | if (getTruncatedUInt32(i))
|
---|
215 | return i;
|
---|
216 | bool ok;
|
---|
217 | return toUInt32SlowCase(exec, ok);
|
---|
218 | }
|
---|
219 |
|
---|
220 | inline int32_t JSValue::toInt32(double val)
|
---|
221 | {
|
---|
222 | if (!(val >= -2147483648.0 && val < 2147483648.0)) {
|
---|
223 | bool ignored;
|
---|
224 | return toInt32SlowCase(val, ignored);
|
---|
225 | }
|
---|
226 | return static_cast<int32_t>(val);
|
---|
227 | }
|
---|
228 |
|
---|
229 | inline uint32_t JSValue::toUInt32(double val)
|
---|
230 | {
|
---|
231 | if (!(val >= 0.0 && val < 4294967296.0)) {
|
---|
232 | bool ignored;
|
---|
233 | return toUInt32SlowCase(val, ignored);
|
---|
234 | }
|
---|
235 | return static_cast<uint32_t>(val);
|
---|
236 | }
|
---|
237 |
|
---|
238 | inline int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
|
---|
239 | {
|
---|
240 | int32_t i;
|
---|
241 | if (getTruncatedInt32(i)) {
|
---|
242 | ok = true;
|
---|
243 | return i;
|
---|
244 | }
|
---|
245 | return toInt32SlowCase(exec, ok);
|
---|
246 | }
|
---|
247 |
|
---|
248 | inline uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
|
---|
249 | {
|
---|
250 | uint32_t i;
|
---|
251 | if (getTruncatedUInt32(i)) {
|
---|
252 | ok = true;
|
---|
253 | return i;
|
---|
254 | }
|
---|
255 | return toUInt32SlowCase(exec, ok);
|
---|
256 | }
|
---|
257 |
|
---|
258 | } // namespace JSC
|
---|
259 |
|
---|
260 | #endif // JSValue_h
|
---|