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 | private:
|
---|
60 | JSValue();
|
---|
61 | virtual ~JSValue();
|
---|
62 |
|
---|
63 | public:
|
---|
64 | // Querying the type.
|
---|
65 | bool isUndefined() const;
|
---|
66 | bool isNull() const;
|
---|
67 | bool isUndefinedOrNull() const;
|
---|
68 | bool isBoolean() const;
|
---|
69 | bool isNumber() const;
|
---|
70 | bool isString() const;
|
---|
71 | bool isGetterSetter() const;
|
---|
72 | bool isObject() const;
|
---|
73 | bool isObject(const ClassInfo*) const; // FIXME: Merge with inherits.
|
---|
74 |
|
---|
75 | // Extracting the value.
|
---|
76 | bool getBoolean(bool&) const;
|
---|
77 | bool getBoolean() const; // false if not a boolean
|
---|
78 | bool getNumber(double&) const;
|
---|
79 | double getNumber() const; // NaN if not a number
|
---|
80 | double uncheckedGetNumber() const;
|
---|
81 | bool getString(UString&) const;
|
---|
82 | UString getString() const; // null string if not a string
|
---|
83 | JSObject* getObject(); // NULL if not an object
|
---|
84 | const JSObject* getObject() const; // NULL if not an object
|
---|
85 |
|
---|
86 | CallType getCallData(CallData&);
|
---|
87 | ConstructType getConstructData(ConstructData&);
|
---|
88 |
|
---|
89 | // Extracting integer values.
|
---|
90 | bool getUInt32(uint32_t&) const;
|
---|
91 | bool getTruncatedInt32(int32_t&) const;
|
---|
92 | bool getTruncatedUInt32(uint32_t&) const;
|
---|
93 |
|
---|
94 | // Basic conversions.
|
---|
95 | enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
|
---|
96 | JSValue* toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
|
---|
97 | bool getPrimitiveNumber(ExecState*, double& number, JSValue*&);
|
---|
98 |
|
---|
99 | bool toBoolean(ExecState*) const;
|
---|
100 |
|
---|
101 | // toNumber conversion is expected to be side effect free if an exception has
|
---|
102 | // been set in the ExecState already.
|
---|
103 | double toNumber(ExecState*) const;
|
---|
104 | JSValue* toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
|
---|
105 | UString toString(ExecState*) const;
|
---|
106 | JSObject* toObject(ExecState*) const;
|
---|
107 |
|
---|
108 | // Integer conversions.
|
---|
109 | double toInteger(ExecState*) const;
|
---|
110 | double toIntegerPreserveNaN(ExecState*) const;
|
---|
111 | int32_t toInt32(ExecState*) const;
|
---|
112 | int32_t toInt32(ExecState*, bool& ok) const;
|
---|
113 | uint32_t toUInt32(ExecState*) const;
|
---|
114 | uint32_t toUInt32(ExecState*, bool& ok) const;
|
---|
115 |
|
---|
116 | // These are identical logic to above, and faster than jsNumber(number)->toInt32(exec)
|
---|
117 | static int32_t toInt32(double);
|
---|
118 | static uint32_t toUInt32(double);
|
---|
119 |
|
---|
120 | // Floating point conversions.
|
---|
121 | float toFloat(ExecState*) const;
|
---|
122 |
|
---|
123 | // Garbage collection.
|
---|
124 | void mark();
|
---|
125 | bool marked() const;
|
---|
126 |
|
---|
127 | static int32_t toInt32SlowCase(double, bool& ok);
|
---|
128 | static uint32_t toUInt32SlowCase(double, bool& ok);
|
---|
129 |
|
---|
130 | // Object operations, with the toObject operation included.
|
---|
131 | JSValue* get(ExecState*, const Identifier& propertyName) const;
|
---|
132 | JSValue* get(ExecState*, const Identifier& propertyName, PropertySlot&) const;
|
---|
133 | JSValue* get(ExecState*, unsigned propertyName) const;
|
---|
134 | JSValue* get(ExecState*, unsigned propertyName, PropertySlot&) const;
|
---|
135 | void put(ExecState*, const Identifier& propertyName, JSValue*, PutPropertySlot&);
|
---|
136 | void put(ExecState*, unsigned propertyName, JSValue*);
|
---|
137 | bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
138 | bool deleteProperty(ExecState*, unsigned propertyName);
|
---|
139 |
|
---|
140 | JSObject* toThisObject(ExecState*) const;
|
---|
141 | UString toThisString(ExecState*) const;
|
---|
142 | JSString* toThisJSString(ExecState*);
|
---|
143 |
|
---|
144 | JSValue* getJSNumber(); // 0 if this is not a JSNumber or number object
|
---|
145 |
|
---|
146 | JSCell* asCell();
|
---|
147 | const JSCell* asCell() const;
|
---|
148 |
|
---|
149 | private:
|
---|
150 | bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
151 | bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
152 | int32_t toInt32SlowCase(ExecState*, bool& ok) const;
|
---|
153 | uint32_t toUInt32SlowCase(ExecState*, bool& ok) const;
|
---|
154 | };
|
---|
155 |
|
---|
156 | inline JSValue::JSValue()
|
---|
157 | {
|
---|
158 | }
|
---|
159 |
|
---|
160 | inline JSValue::~JSValue()
|
---|
161 | {
|
---|
162 | }
|
---|
163 |
|
---|
164 | inline bool JSValue::isUndefined() const
|
---|
165 | {
|
---|
166 | return this == jsUndefined();
|
---|
167 | }
|
---|
168 |
|
---|
169 | inline bool JSValue::isNull() const
|
---|
170 | {
|
---|
171 | return this == jsNull();
|
---|
172 | }
|
---|
173 |
|
---|
174 | inline bool JSValue::isUndefinedOrNull() const
|
---|
175 | {
|
---|
176 | return JSImmediate::isUndefinedOrNull(this);
|
---|
177 | }
|
---|
178 |
|
---|
179 | inline bool JSValue::isBoolean() const
|
---|
180 | {
|
---|
181 | return JSImmediate::isBoolean(this);
|
---|
182 | }
|
---|
183 |
|
---|
184 | inline bool JSValue::getBoolean(bool& v) const
|
---|
185 | {
|
---|
186 | if (JSImmediate::isBoolean(this)) {
|
---|
187 | v = JSImmediate::toBoolean(this);
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return false;
|
---|
192 | }
|
---|
193 |
|
---|
194 | inline bool JSValue::getBoolean() const
|
---|
195 | {
|
---|
196 | return this == jsBoolean(true);
|
---|
197 | }
|
---|
198 |
|
---|
199 | ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const
|
---|
200 | {
|
---|
201 | int32_t i;
|
---|
202 | if (getTruncatedInt32(i))
|
---|
203 | return i;
|
---|
204 | bool ok;
|
---|
205 | return toInt32SlowCase(exec, ok);
|
---|
206 | }
|
---|
207 |
|
---|
208 | inline uint32_t JSValue::toUInt32(ExecState* exec) const
|
---|
209 | {
|
---|
210 | uint32_t i;
|
---|
211 | if (getTruncatedUInt32(i))
|
---|
212 | return i;
|
---|
213 | bool ok;
|
---|
214 | return toUInt32SlowCase(exec, ok);
|
---|
215 | }
|
---|
216 |
|
---|
217 | inline int32_t JSValue::toInt32(double val)
|
---|
218 | {
|
---|
219 | if (!(val >= -2147483648.0 && val < 2147483648.0)) {
|
---|
220 | bool ignored;
|
---|
221 | return toInt32SlowCase(val, ignored);
|
---|
222 | }
|
---|
223 | return static_cast<int32_t>(val);
|
---|
224 | }
|
---|
225 |
|
---|
226 | inline uint32_t JSValue::toUInt32(double val)
|
---|
227 | {
|
---|
228 | if (!(val >= 0.0 && val < 4294967296.0)) {
|
---|
229 | bool ignored;
|
---|
230 | return toUInt32SlowCase(val, ignored);
|
---|
231 | }
|
---|
232 | return static_cast<uint32_t>(val);
|
---|
233 | }
|
---|
234 |
|
---|
235 | inline int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
|
---|
236 | {
|
---|
237 | int32_t i;
|
---|
238 | if (getTruncatedInt32(i)) {
|
---|
239 | ok = true;
|
---|
240 | return i;
|
---|
241 | }
|
---|
242 | return toInt32SlowCase(exec, ok);
|
---|
243 | }
|
---|
244 |
|
---|
245 | inline uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
|
---|
246 | {
|
---|
247 | uint32_t i;
|
---|
248 | if (getTruncatedUInt32(i)) {
|
---|
249 | ok = true;
|
---|
250 | return i;
|
---|
251 | }
|
---|
252 | return toUInt32SlowCase(exec, ok);
|
---|
253 | }
|
---|
254 |
|
---|
255 | } // namespace JSC
|
---|
256 |
|
---|
257 | #endif // JSValue_h
|
---|