]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSValue.h
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.
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.
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.
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.
23 #include <stddef.h> // for size_t
30 #include "ConstructData.h"
39 class PutPropertySlot
;
45 enum PreferredPrimitiveType
{ NoPreference
, PreferNumber
, PreferString
};
48 class JSValueEncodedAsPointer
;
51 friend class JSImmediate
;
53 static JSValuePtr
makeImmediate(intptr_t value
)
55 return JSValuePtr(reinterpret_cast<JSCell
*>(value
));
58 intptr_t immediateValue()
60 return reinterpret_cast<intptr_t>(m_ptr
);
69 JSValuePtr(JSCell
* ptr
)
74 JSValuePtr(const JSCell
* ptr
)
75 : m_ptr(const_cast<JSCell
*>(ptr
))
84 bool operator==(const JSValuePtr other
) const
86 return m_ptr
== other
.m_ptr
;
89 bool operator!=(const JSValuePtr other
) const
91 return m_ptr
!= other
.m_ptr
;
94 static JSValueEncodedAsPointer
* encode(JSValuePtr value
)
96 return reinterpret_cast<JSValueEncodedAsPointer
*>(value
.m_ptr
);
99 static JSValuePtr
decode(JSValueEncodedAsPointer
* ptr
)
101 return JSValuePtr(reinterpret_cast<JSCell
*>(ptr
));
104 // Querying the type.
105 bool isUndefined() 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;
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
124 CallType
getCallData(CallData
&);
125 ConstructType
getConstructData(ConstructData
&);
127 // Extracting integer values.
128 bool getUInt32(uint32_t&) const;
129 bool getTruncatedInt32(int32_t&) const;
130 bool getTruncatedUInt32(uint32_t&) const;
132 // Basic conversions.
133 JSValuePtr
toPrimitive(ExecState
*, PreferredPrimitiveType
= NoPreference
) const;
134 bool getPrimitiveNumber(ExecState
*, double& number
, JSValuePtr
&);
136 bool toBoolean(ExecState
*) const;
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;
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
);
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
);
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
)); }
171 // Garbage collection.
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
);
183 bool needsThisConversion() const;
184 JSObject
* toThisObject(ExecState
*) const;
185 UString
toThisString(ExecState
*) const;
186 JSString
* toThisJSString(ExecState
*);
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
);
195 JSValuePtr
getJSNumber(); // noValue() if this is not a JSNumber or number object
198 JSCell
* asCell() const;
201 inline const JSValuePtr
asValue() const { return *this; }
203 bool isDoubleNumber() const;
204 double getDoubleNumber() const;
209 inline JSValuePtr
noValue()
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
; }
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
; }