]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSValue.h
JavaScriptCore-521.tar.gz
[apple/javascriptcore.git] / runtime / JSValue.h
1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
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 JSObject;
37 class JSString;
38 class PropertySlot;
39 class PutPropertySlot;
40 class UString;
41
42 struct ClassInfo;
43 struct Instruction;
44
45 enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
46
47 class JSImmediate;
48 class JSValueEncodedAsPointer;
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 JSValuePtr()
65 : m_ptr(0)
66 {
67 }
68
69 JSValuePtr(JSCell* ptr)
70 : m_ptr(ptr)
71 {
72 }
73
74 JSValuePtr(const JSCell* ptr)
75 : m_ptr(const_cast<JSCell*>(ptr))
76 {
77 }
78
79 operator bool() const
80 {
81 return m_ptr;
82 }
83
84 bool operator==(const JSValuePtr other) const
85 {
86 return m_ptr == other.m_ptr;
87 }
88
89 bool operator!=(const JSValuePtr other) const
90 {
91 return m_ptr != other.m_ptr;
92 }
93
94 static JSValueEncodedAsPointer* encode(JSValuePtr value)
95 {
96 return reinterpret_cast<JSValueEncodedAsPointer*>(value.m_ptr);
97 }
98
99 static JSValuePtr decode(JSValueEncodedAsPointer* ptr)
100 {
101 return JSValuePtr(reinterpret_cast<JSCell*>(ptr));
102 }
103
104 // Querying the type.
105 bool isUndefined() const;
106 bool isNull() const;
107 bool isUndefinedOrNull() const;
108 bool isBoolean() const;
109 bool isNumber() const;
110 bool isString() const;
111 bool isGetterSetter() const;
112 bool isObject() const;
113 bool isObject(const ClassInfo*) const;
114
115 // Extracting the value.
116 bool getBoolean(bool&) const;
117 bool getBoolean() const; // false if not a boolean
118 bool getNumber(double&) const;
119 double uncheckedGetNumber() const;
120 bool getString(UString&) const;
121 UString getString() const; // null string if not a string
122 JSObject* getObject() const; // 0 if not an object
123
124 CallType getCallData(CallData&);
125 ConstructType getConstructData(ConstructData&);
126
127 // Extracting integer values.
128 bool getUInt32(uint32_t&) const;
129 bool getTruncatedInt32(int32_t&) const;
130 bool getTruncatedUInt32(uint32_t&) const;
131
132 // Basic conversions.
133 JSValuePtr toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
134 bool getPrimitiveNumber(ExecState*, double& number, JSValuePtr&);
135
136 bool toBoolean(ExecState*) const;
137
138 // toNumber conversion is expected to be side effect free if an exception has
139 // been set in the ExecState already.
140 double toNumber(ExecState*) const;
141 JSValuePtr toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
142 UString toString(ExecState*) const;
143 JSObject* toObject(ExecState*) const;
144
145 // Integer conversions.
146 // 'x.numberToInt32(output)' is equivalent to 'x.isNumber() && x.toInt32(output)'
147 double toInteger(ExecState*) const;
148 double toIntegerPreserveNaN(ExecState*) const;
149 int32_t toInt32(ExecState*) const;
150 int32_t toInt32(ExecState*, bool& ok) const;
151 bool numberToInt32(int32_t& arg);
152 uint32_t toUInt32(ExecState*) const;
153 uint32_t toUInt32(ExecState*, bool& ok) const;
154 bool numberToUInt32(uint32_t& arg);
155
156 // Fast integer operations; these values return results where the value is trivially available
157 // in a convenient form, for use in optimizations. No assumptions should be made based on the
158 // results of these operations, for example !isInt32Fast() does not necessarily indicate the
159 // result of getNumber will not be 0.
160 bool isInt32Fast() const;
161 int32_t getInt32Fast() const;
162 bool isUInt32Fast() const;
163 uint32_t getUInt32Fast() const;
164 static JSValuePtr makeInt32Fast(int32_t);
165 static bool areBothInt32Fast(JSValuePtr, JSValuePtr);
166
167 // Floating point conversions (this is a convenience method for webcore;
168 // signle precision float is not a representation used in JS or JSC).
169 float toFloat(ExecState* exec) const { return static_cast<float>(toNumber(exec)); }
170
171 // Garbage collection.
172 void mark();
173 bool marked() const;
174
175 // Object operations, with the toObject operation included.
176 JSValuePtr get(ExecState*, const Identifier& propertyName) const;
177 JSValuePtr get(ExecState*, const Identifier& propertyName, PropertySlot&) const;
178 JSValuePtr get(ExecState*, unsigned propertyName) const;
179 JSValuePtr get(ExecState*, unsigned propertyName, PropertySlot&) const;
180 void put(ExecState*, const Identifier& propertyName, JSValuePtr, PutPropertySlot&);
181 void put(ExecState*, unsigned propertyName, JSValuePtr);
182
183 bool needsThisConversion() const;
184 JSObject* toThisObject(ExecState*) const;
185 UString toThisString(ExecState*) const;
186 JSString* toThisJSString(ExecState*);
187
188 static bool equal(ExecState* exec, JSValuePtr v1, JSValuePtr v2);
189 static bool equalSlowCase(ExecState* exec, JSValuePtr v1, JSValuePtr v2);
190 static bool equalSlowCaseInline(ExecState* exec, JSValuePtr v1, JSValuePtr v2);
191 static bool strictEqual(JSValuePtr v1, JSValuePtr v2);
192 static bool strictEqualSlowCase(JSValuePtr v1, JSValuePtr v2);
193 static bool strictEqualSlowCaseInline(JSValuePtr v1, JSValuePtr v2);
194
195 JSValuePtr getJSNumber(); // noValue() if this is not a JSNumber or number object
196
197 bool isCell() const;
198 JSCell* asCell() const;
199
200 private:
201 inline const JSValuePtr asValue() const { return *this; }
202
203 bool isDoubleNumber() const;
204 double getDoubleNumber() const;
205
206 JSCell* m_ptr;
207 };
208
209 inline JSValuePtr noValue()
210 {
211 return JSValuePtr();
212 }
213
214 inline bool operator==(const JSValuePtr a, const JSCell* b) { return a == JSValuePtr(b); }
215 inline bool operator==(const JSCell* a, const JSValuePtr b) { return JSValuePtr(a) == b; }
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 } // namespace JSC
221
222 #endif // JSValue_h