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 | #include <stddef.h> // for size_t
|
---|
24 | #include <stdint.h>
|
---|
25 |
|
---|
26 | #ifndef JSValue_h
|
---|
27 | #define JSValue_h
|
---|
28 |
|
---|
29 | #include "CallData.h"
|
---|
30 | #include "ConstructData.h"
|
---|
31 |
|
---|
32 | namespace JSC {
|
---|
33 |
|
---|
34 | class Identifier;
|
---|
35 | class JSCell;
|
---|
36 | class JSImmediate;
|
---|
37 | class JSObject;
|
---|
38 | class JSString;
|
---|
39 | class PropertySlot;
|
---|
40 | class PutPropertySlot;
|
---|
41 | class UString;
|
---|
42 |
|
---|
43 | struct ClassInfo;
|
---|
44 | struct Instruction;
|
---|
45 |
|
---|
46 | enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
|
---|
47 |
|
---|
48 | typedef void* EncodedJSValuePtr;
|
---|
49 |
|
---|
50 | class JSValuePtr {
|
---|
51 | friend class JSImmediate;
|
---|
52 |
|
---|
53 | static JSValuePtr makeImmediate(intptr_t value)
|
---|
54 | {
|
---|
55 | return JSValuePtr(reinterpret_cast<JSCell*>(value));
|
---|
56 | }
|
---|
57 |
|
---|
58 | intptr_t immediateValue()
|
---|
59 | {
|
---|
60 | return reinterpret_cast<intptr_t>(m_ptr);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public:
|
---|
64 | enum ImpossibleValueTag { ImpossibleValue };
|
---|
65 | enum JSNullTag { JSNull };
|
---|
66 | enum JSUndefinedTag { JSUndefined };
|
---|
67 | enum JSTrueTag { JSTrue };
|
---|
68 | enum JSFalseTag { JSFalse };
|
---|
69 |
|
---|
70 | static EncodedJSValuePtr encode(JSValuePtr value);
|
---|
71 | static JSValuePtr decode(EncodedJSValuePtr ptr);
|
---|
72 |
|
---|
73 | JSValuePtr();
|
---|
74 | JSValuePtr(ImpossibleValueTag);
|
---|
75 | JSValuePtr(JSNullTag);
|
---|
76 | JSValuePtr(JSUndefinedTag);
|
---|
77 | JSValuePtr(JSTrueTag);
|
---|
78 | JSValuePtr(JSFalseTag);
|
---|
79 | JSValuePtr(JSCell* ptr);
|
---|
80 | JSValuePtr(const JSCell* ptr);
|
---|
81 |
|
---|
82 | operator bool() const;
|
---|
83 | bool operator==(const JSValuePtr other) const;
|
---|
84 | bool operator!=(const JSValuePtr other) const;
|
---|
85 |
|
---|
86 | // Querying the type.
|
---|
87 | bool isUndefined() const;
|
---|
88 | bool isNull() const;
|
---|
89 | bool isUndefinedOrNull() const;
|
---|
90 | bool isBoolean() const;
|
---|
91 | bool isNumber() const;
|
---|
92 | bool isString() const;
|
---|
93 | bool isGetterSetter() const;
|
---|
94 | bool isObject() const;
|
---|
95 | bool isObject(const ClassInfo*) const;
|
---|
96 |
|
---|
97 | // Extracting the value.
|
---|
98 | bool getBoolean(bool&) const;
|
---|
99 | bool getBoolean() const; // false if not a boolean
|
---|
100 | bool getNumber(double&) const;
|
---|
101 | double uncheckedGetNumber() const;
|
---|
102 | bool getString(UString&) const;
|
---|
103 | UString getString() const; // null string if not a string
|
---|
104 | JSObject* getObject() const; // 0 if not an object
|
---|
105 |
|
---|
106 | CallType getCallData(CallData&);
|
---|
107 | ConstructType getConstructData(ConstructData&);
|
---|
108 |
|
---|
109 | // Extracting integer values.
|
---|
110 | bool getUInt32(uint32_t&) const;
|
---|
111 | bool getTruncatedInt32(int32_t&) const;
|
---|
112 | bool getTruncatedUInt32(uint32_t&) const;
|
---|
113 |
|
---|
114 | // Basic conversions.
|
---|
115 | JSValuePtr toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
|
---|
116 | bool getPrimitiveNumber(ExecState*, double& number, JSValuePtr&);
|
---|
117 |
|
---|
118 | bool toBoolean(ExecState*) const;
|
---|
119 |
|
---|
120 | // toNumber conversion is expected to be side effect free if an exception has
|
---|
121 | // been set in the ExecState already.
|
---|
122 | double toNumber(ExecState*) const;
|
---|
123 | JSValuePtr toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
|
---|
124 | UString toString(ExecState*) const;
|
---|
125 | JSObject* toObject(ExecState*) const;
|
---|
126 |
|
---|
127 | // Integer conversions.
|
---|
128 | // 'x.numberToInt32(output)' is equivalent to 'x.isNumber() && x.toInt32(output)'
|
---|
129 | double toInteger(ExecState*) const;
|
---|
130 | double toIntegerPreserveNaN(ExecState*) const;
|
---|
131 | int32_t toInt32(ExecState*) const;
|
---|
132 | int32_t toInt32(ExecState*, bool& ok) const;
|
---|
133 | bool numberToInt32(int32_t& arg);
|
---|
134 | uint32_t toUInt32(ExecState*) const;
|
---|
135 | uint32_t toUInt32(ExecState*, bool& ok) const;
|
---|
136 | bool numberToUInt32(uint32_t& arg);
|
---|
137 |
|
---|
138 | // Fast integer operations; these values return results where the value is trivially available
|
---|
139 | // in a convenient form, for use in optimizations. No assumptions should be made based on the
|
---|
140 | // results of these operations, for example !isInt32Fast() does not necessarily indicate the
|
---|
141 | // result of getNumber will not be 0.
|
---|
142 | bool isInt32Fast() const;
|
---|
143 | int32_t getInt32Fast() const;
|
---|
144 | bool isUInt32Fast() const;
|
---|
145 | uint32_t getUInt32Fast() const;
|
---|
146 | static JSValuePtr makeInt32Fast(int32_t);
|
---|
147 | static bool areBothInt32Fast(JSValuePtr, JSValuePtr);
|
---|
148 |
|
---|
149 | // Floating point conversions (this is a convenience method for webcore;
|
---|
150 | // signle precision float is not a representation used in JS or JSC).
|
---|
151 | float toFloat(ExecState* exec) const { return static_cast<float>(toNumber(exec)); }
|
---|
152 |
|
---|
153 | // Garbage collection.
|
---|
154 | void mark();
|
---|
155 | bool marked() const;
|
---|
156 |
|
---|
157 | // Object operations, with the toObject operation included.
|
---|
158 | JSValuePtr get(ExecState*, const Identifier& propertyName) const;
|
---|
159 | JSValuePtr get(ExecState*, const Identifier& propertyName, PropertySlot&) const;
|
---|
160 | JSValuePtr get(ExecState*, unsigned propertyName) const;
|
---|
161 | JSValuePtr get(ExecState*, unsigned propertyName, PropertySlot&) const;
|
---|
162 | void put(ExecState*, const Identifier& propertyName, JSValuePtr, PutPropertySlot&);
|
---|
163 | void put(ExecState*, unsigned propertyName, JSValuePtr);
|
---|
164 |
|
---|
165 | bool needsThisConversion() const;
|
---|
166 | JSObject* toThisObject(ExecState*) const;
|
---|
167 | UString toThisString(ExecState*) const;
|
---|
168 | JSString* toThisJSString(ExecState*);
|
---|
169 |
|
---|
170 | static bool equal(ExecState* exec, JSValuePtr v1, JSValuePtr v2);
|
---|
171 | static bool equalSlowCase(ExecState* exec, JSValuePtr v1, JSValuePtr v2);
|
---|
172 | static bool equalSlowCaseInline(ExecState* exec, JSValuePtr v1, JSValuePtr v2);
|
---|
173 | static bool strictEqual(JSValuePtr v1, JSValuePtr v2);
|
---|
174 | static bool strictEqualSlowCase(JSValuePtr v1, JSValuePtr v2);
|
---|
175 | static bool strictEqualSlowCaseInline(JSValuePtr v1, JSValuePtr v2);
|
---|
176 |
|
---|
177 | JSValuePtr getJSNumber(); // noValue() if this is not a JSNumber or number object
|
---|
178 |
|
---|
179 | bool isCell() const;
|
---|
180 | JSCell* asCell() const;
|
---|
181 |
|
---|
182 | private:
|
---|
183 | inline const JSValuePtr asValue() const { return *this; }
|
---|
184 |
|
---|
185 | bool isDoubleNumber() const;
|
---|
186 | double getDoubleNumber() const;
|
---|
187 |
|
---|
188 | JSCell* m_ptr;
|
---|
189 | };
|
---|
190 |
|
---|
191 | // Stand-alone helper functions.
|
---|
192 | inline JSValuePtr noValue()
|
---|
193 | {
|
---|
194 | return JSValuePtr();
|
---|
195 | }
|
---|
196 |
|
---|
197 | inline JSValuePtr jsImpossibleValue()
|
---|
198 | {
|
---|
199 | return JSValuePtr(JSValuePtr::ImpossibleValue);
|
---|
200 | }
|
---|
201 |
|
---|
202 | inline JSValuePtr jsNull()
|
---|
203 | {
|
---|
204 | return JSValuePtr(JSValuePtr::JSNull);
|
---|
205 | }
|
---|
206 |
|
---|
207 | inline JSValuePtr jsUndefined()
|
---|
208 | {
|
---|
209 | return JSValuePtr(JSValuePtr::JSUndefined);
|
---|
210 | }
|
---|
211 |
|
---|
212 | inline JSValuePtr jsBoolean(bool b)
|
---|
213 | {
|
---|
214 | return b ? JSValuePtr(JSValuePtr::JSTrue) : JSValuePtr(JSValuePtr::JSFalse);
|
---|
215 | }
|
---|
216 |
|
---|
217 | inline bool operator==(const JSValuePtr a, const JSCell* b) { return a == JSValuePtr(b); }
|
---|
218 | inline bool operator==(const JSCell* a, const JSValuePtr b) { return JSValuePtr(a) == b; }
|
---|
219 |
|
---|
220 | inline bool operator!=(const JSValuePtr a, const JSCell* b) { return a != JSValuePtr(b); }
|
---|
221 | inline bool operator!=(const JSCell* a, const JSValuePtr b) { return JSValuePtr(a) != b; }
|
---|
222 |
|
---|
223 | // JSValuePtr member functions.
|
---|
224 | inline EncodedJSValuePtr JSValuePtr::encode(JSValuePtr value)
|
---|
225 | {
|
---|
226 | return reinterpret_cast<EncodedJSValuePtr>(value.m_ptr);
|
---|
227 | }
|
---|
228 |
|
---|
229 | inline JSValuePtr JSValuePtr::decode(EncodedJSValuePtr ptr)
|
---|
230 | {
|
---|
231 | return JSValuePtr(reinterpret_cast<JSCell*>(ptr));
|
---|
232 | }
|
---|
233 |
|
---|
234 | inline JSValuePtr::JSValuePtr()
|
---|
235 | : m_ptr(0)
|
---|
236 | {
|
---|
237 | }
|
---|
238 |
|
---|
239 | inline JSValuePtr::JSValuePtr(JSCell* ptr)
|
---|
240 | : m_ptr(ptr)
|
---|
241 | {
|
---|
242 | }
|
---|
243 |
|
---|
244 | inline JSValuePtr::JSValuePtr(const JSCell* ptr)
|
---|
245 | : m_ptr(const_cast<JSCell*>(ptr))
|
---|
246 | {
|
---|
247 | }
|
---|
248 |
|
---|
249 | inline JSValuePtr::operator bool() const
|
---|
250 | {
|
---|
251 | return m_ptr;
|
---|
252 | }
|
---|
253 |
|
---|
254 | inline bool JSValuePtr::operator==(const JSValuePtr other) const
|
---|
255 | {
|
---|
256 | return m_ptr == other.m_ptr;
|
---|
257 | }
|
---|
258 |
|
---|
259 | inline bool JSValuePtr::operator!=(const JSValuePtr other) const
|
---|
260 | {
|
---|
261 | return m_ptr != other.m_ptr;
|
---|
262 | }
|
---|
263 |
|
---|
264 | inline bool JSValuePtr::isUndefined() const
|
---|
265 | {
|
---|
266 | return asValue() == jsUndefined();
|
---|
267 | }
|
---|
268 |
|
---|
269 | inline bool JSValuePtr::isNull() const
|
---|
270 | {
|
---|
271 | return asValue() == jsNull();
|
---|
272 | }
|
---|
273 |
|
---|
274 | } // namespace JSC
|
---|
275 |
|
---|
276 | #endif // JSValue_h
|
---|