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, 2009, 2012 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.
27 #include <stddef.h> // for size_t
29 #include <wtf/Assertions.h>
30 #include <wtf/Forward.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/HashTraits.h>
33 #include <wtf/MathExtras.h>
34 #include <wtf/StdLibExtras.h>
35 #include <wtf/TriState.h>
39 // This is used a lot throughout JavaScriptCore for everything from value boxing to marking
40 // values as being missing, so it is useful to have it abbreviated.
41 #define QNaN (std::numeric_limits<double>::quiet_NaN())
51 class PutPropertySlot
;
54 class AssemblyHelpers
;
56 class JITCodeGenerator
;
58 class OSRExitCompiler
;
62 #if ENABLE(LLINT_C_LOOP)
72 template <class T
> class WriteBarrierBase
;
74 enum PreferredPrimitiveType
{ NoPreference
, PreferNumber
, PreferString
};
77 typedef int64_t EncodedJSValue
;
79 union EncodedValueDescriptor
{
100 // This implements ToInt32, defined in ECMA-262 9.5.
101 JS_EXPORT_PRIVATE
int32_t toInt32(double);
103 // This implements ToUInt32, defined in ECMA-262 9.6.
104 inline uint32_t toUInt32(double number
)
106 // As commented in the spec, the operation of ToInt32 and ToUint32 only differ
107 // in how the result is interpreted; see NOTEs in sections 9.5 and 9.6.
108 return toInt32(number
);
112 friend struct EncodedJSValueHashTraits
;
114 friend class JITStubs
;
115 friend class JITStubCall
;
116 friend class JSInterfaceJIT
;
117 friend class SpecializedThunkJIT
;
119 friend class DFG::AssemblyHelpers
;
120 friend class DFG::JITCompiler
;
121 friend class DFG::JITCodeGenerator
;
122 friend class DFG::JSValueSource
;
123 friend class DFG::OSRExitCompiler
;
124 friend class DFG::SpeculativeJIT
;
126 #if ENABLE(LLINT_C_LOOP)
127 friend class LLInt::CLoop
;
131 #if USE(JSVALUE32_64)
132 enum { Int32Tag
= 0xffffffff };
133 enum { BooleanTag
= 0xfffffffe };
134 enum { NullTag
= 0xfffffffd };
135 enum { UndefinedTag
= 0xfffffffc };
136 enum { CellTag
= 0xfffffffb };
137 enum { EmptyValueTag
= 0xfffffffa };
138 enum { DeletedValueTag
= 0xfffffff9 };
140 enum { LowestTag
= DeletedValueTag
};
143 static EncodedJSValue
encode(JSValue
);
144 static JSValue
decode(EncodedJSValue
);
146 enum JSNullTag
{ JSNull
};
147 enum JSUndefinedTag
{ JSUndefined
};
148 enum JSTrueTag
{ JSTrue
};
149 enum JSFalseTag
{ JSFalse
};
150 enum EncodeAsDoubleTag
{ EncodeAsDouble
};
154 JSValue(JSUndefinedTag
);
157 JSValue(JSCell
* ptr
);
158 JSValue(const JSCell
* ptr
);
161 JSValue(EncodeAsDoubleTag
, double);
162 explicit JSValue(double);
163 explicit JSValue(char);
164 explicit JSValue(unsigned char);
165 explicit JSValue(short);
166 explicit JSValue(unsigned short);
167 explicit JSValue(int);
168 explicit JSValue(unsigned);
169 explicit JSValue(long);
170 explicit JSValue(unsigned long);
171 explicit JSValue(long long);
172 explicit JSValue(unsigned long long);
174 operator bool() const;
175 bool operator==(const JSValue
& other
) const;
176 bool operator!=(const JSValue
& other
) const;
178 bool isInt32() const;
179 bool isUInt32() const;
180 bool isDouble() const;
182 bool isFalse() const;
184 int32_t asInt32() const;
185 uint32_t asUInt32() const;
186 double asDouble() const;
187 bool asBoolean() const;
188 double asNumber() const;
190 // Querying the type.
191 bool isEmpty() const;
192 bool isFunction() const;
193 bool isUndefined() const;
195 bool isUndefinedOrNull() const;
196 bool isBoolean() const;
197 bool isNumber() const;
198 bool isString() const;
199 bool isPrimitive() const;
200 bool isGetterSetter() const;
201 bool isObject() const;
202 bool inherits(const ClassInfo
*) const;
204 // Extracting the value.
205 bool getString(ExecState
*, WTF::String
&) const;
206 WTF::String
getString(ExecState
*) const; // null string if not a string
207 JSObject
* getObject() const; // 0 if not an object
209 // Extracting integer values.
210 bool getUInt32(uint32_t&) const;
212 // Basic conversions.
213 JSValue
toPrimitive(ExecState
*, PreferredPrimitiveType
= NoPreference
) const;
214 bool getPrimitiveNumber(ExecState
*, double& number
, JSValue
&);
216 bool toBoolean(ExecState
*) const;
217 TriState
pureToBoolean() const;
219 // toNumber conversion is expected to be side effect free if an exception has
220 // been set in the ExecState already.
221 double toNumber(ExecState
*) const;
222 JSString
* toString(ExecState
*) const;
223 WTF::String
toWTFString(ExecState
*) const;
224 WTF::String
toWTFStringInline(ExecState
*) const;
225 JSObject
* toObject(ExecState
*) const;
226 JSObject
* toObject(ExecState
*, JSGlobalObject
*) const;
228 // Integer conversions.
229 JS_EXPORT_PRIVATE
double toInteger(ExecState
*) const;
230 double toIntegerPreserveNaN(ExecState
*) const;
231 int32_t toInt32(ExecState
*) const;
232 uint32_t toUInt32(ExecState
*) const;
234 // Floating point conversions (this is a convenience method for webcore;
235 // signle precision float is not a representation used in JS or JSC).
236 float toFloat(ExecState
* exec
) const { return static_cast<float>(toNumber(exec
)); }
238 // Object operations, with the toObject operation included.
239 JSValue
get(ExecState
*, PropertyName
) const;
240 JSValue
get(ExecState
*, PropertyName
, PropertySlot
&) const;
241 JSValue
get(ExecState
*, unsigned propertyName
) const;
242 JSValue
get(ExecState
*, unsigned propertyName
, PropertySlot
&) const;
243 void put(ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
244 void putToPrimitive(ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
245 void putToPrimitiveByIndex(ExecState
*, unsigned propertyName
, JSValue
, bool shouldThrow
);
246 void putByIndex(ExecState
*, unsigned propertyName
, JSValue
, bool shouldThrow
);
248 JSObject
* toThisObject(ExecState
*) const;
250 static bool equal(ExecState
*, JSValue v1
, JSValue v2
);
251 static bool equalSlowCase(ExecState
*, JSValue v1
, JSValue v2
);
252 static bool equalSlowCaseInline(ExecState
*, JSValue v1
, JSValue v2
);
253 static bool strictEqual(ExecState
*, JSValue v1
, JSValue v2
);
254 static bool strictEqualSlowCase(ExecState
*, JSValue v1
, JSValue v2
);
255 static bool strictEqualSlowCaseInline(ExecState
*, JSValue v1
, JSValue v2
);
258 JSCell
* asCell() const;
259 JS_EXPORT_PRIVATE
bool isValidCallee();
261 JSValue
structureOrUndefined() const;
263 JS_EXPORT_PRIVATE
void dump(PrintStream
&) const;
265 JS_EXPORT_PRIVATE JSObject
* synthesizePrototype(ExecState
*) const;
268 template <class T
> JSValue(WriteBarrierBase
<T
>);
270 enum HashTableDeletedValueTag
{ HashTableDeletedValue
};
271 JSValue(HashTableDeletedValueTag
);
273 inline const JSValue
asValue() const { return *this; }
274 JS_EXPORT_PRIVATE
double toNumberSlowCase(ExecState
*) const;
275 JS_EXPORT_PRIVATE JSString
* toStringSlowCase(ExecState
*) const;
276 JS_EXPORT_PRIVATE
WTF::String
toWTFStringSlowCase(ExecState
*) const;
277 JS_EXPORT_PRIVATE JSObject
* toObjectSlowCase(ExecState
*, JSGlobalObject
*) const;
278 JS_EXPORT_PRIVATE JSObject
* toThisObjectSlowCase(ExecState
*) const;
280 #if USE(JSVALUE32_64)
282 * On 32-bit platforms USE(JSVALUE32_64) should be defined, and we use a NaN-encoded
283 * form for immediates.
285 * The encoding makes use of unused NaN space in the IEEE754 representation. Any value
286 * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values
287 * can encode a 51-bit payload. Hardware produced and C-library payloads typically
288 * have a payload of zero. We assume that non-zero payloads are available to encode
289 * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are
290 * all set represents a NaN with a non-zero payload, we can use this space in the NaN
291 * ranges to encode other values (however there are also other ranges of NaN space that
292 * could have been selected).
294 * For JSValues that do not contain a double value, the high 32 bits contain the tag
295 * values listed in the enums below, which all correspond to NaN-space. In the case of
296 * cell, integer and bool values the lower 32 bits (the 'payload') contain the pointer
297 * integer or boolean value; in the case of all other tags the payload is 0.
299 uint32_t tag() const;
300 int32_t payload() const;
302 #if ENABLE(LLINT_C_LOOP)
303 // This should only be used by the LLInt C Loop interpreter who needs
304 // synthesize JSValue from its "register"s holding tag and payload
306 explicit JSValue(int32_t tag
, int32_t payload
);
311 * On 64-bit platforms USE(JSVALUE64) should be defined, and we use a NaN-encoded
312 * form for immediates.
314 * The encoding makes use of unused NaN space in the IEEE754 representation. Any value
315 * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values
316 * can encode a 51-bit payload. Hardware produced and C-library payloads typically
317 * have a payload of zero. We assume that non-zero payloads are available to encode
318 * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are
319 * all set represents a NaN with a non-zero payload, we can use this space in the NaN
320 * ranges to encode other values (however there are also other ranges of NaN space that
321 * could have been selected).
323 * This range of NaN space is represented by 64-bit numbers begining with the 16-bit
324 * hex patterns 0xFFFE and 0xFFFF - we rely on the fact that no valid double-precision
325 * numbers will begin fall in these ranges.
327 * The top 16-bits denote the type of the encoded JSValue:
329 * Pointer { 0000:PPPP:PPPP:PPPP
330 * / 0001:****:****:****
332 * \ FFFE:****:****:****
333 * Integer { FFFF:0000:IIII:IIII
335 * The scheme we have implemented encodes double precision values by performing a
336 * 64-bit integer addition of the value 2^48 to the number. After this manipulation
337 * no encoded double-precision value will begin with the pattern 0x0000 or 0xFFFF.
338 * Values must be decoded by reversing this operation before subsequent floating point
339 * operations my be peformed.
341 * 32-bit signed integers are marked with the 16-bit tag 0xFFFF.
343 * The tag 0x0000 denotes a pointer, or another form of tagged immediate. Boolean,
344 * null and undefined values are represented by specific, invalid pointer values:
351 * These values have the following properties:
352 * - Bit 1 (TagBitTypeOther) is set for all four values, allowing real pointers to be
353 * quickly distinguished from all immediate values, including these invalid pointers.
354 * - With bit 3 is masked out (TagBitUndefined) Undefined and Null share the
355 * same value, allowing null & undefined to be quickly detected.
357 * No valid JSValue will have the bit pattern 0x0, this is used to represent array
358 * holes, and as a C++ 'no value' result (e.g. JSValue() has an internal value of 0).
361 // These values are #defines since using static const integers here is a ~1% regression!
363 // This value is 2^48, used to encode doubles such that the encoded value will begin
364 // with a 16-bit pattern within the range 0x0001..0xFFFE.
365 #define DoubleEncodeOffset 0x1000000000000ll
366 // If all bits in the mask are set, this indicates an integer number,
367 // if any but not all are set this value is a double precision number.
368 #define TagTypeNumber 0xffff000000000000ll
370 // All non-numeric (bool, null, undefined) immediates have bit 2 set.
371 #define TagBitTypeOther 0x2ll
372 #define TagBitBool 0x4ll
373 #define TagBitUndefined 0x8ll
374 // Combined integer value for non-numeric immediates.
375 #define ValueFalse (TagBitTypeOther | TagBitBool | false)
376 #define ValueTrue (TagBitTypeOther | TagBitBool | true)
377 #define ValueUndefined (TagBitTypeOther | TagBitUndefined)
378 #define ValueNull (TagBitTypeOther)
380 // TagMask is used to check for all types of immediate values (either number or 'other').
381 #define TagMask (TagTypeNumber | TagBitTypeOther)
383 // These special values are never visible to JavaScript code; Empty is used to represent
384 // Array holes, and for uninitialized JSValues. Deleted is used in hash table code.
385 // These values would map to cell types in the JSValue encoding, but not valid GC cell
386 // pointer should have either of these values (Empty is null, deleted is at an invalid
387 // alignment for a GC cell, and in the zero page).
388 #define ValueEmpty 0x0ll
389 #define ValueDeleted 0x4ll
392 EncodedValueDescriptor u
;
395 typedef IntHash
<EncodedJSValue
> EncodedJSValueHash
;
397 #if USE(JSVALUE32_64)
398 struct EncodedJSValueHashTraits
: HashTraits
<EncodedJSValue
> {
399 static const bool emptyValueIsZero
= false;
400 static EncodedJSValue
emptyValue() { return JSValue::encode(JSValue()); }
401 static void constructDeletedValue(EncodedJSValue
& slot
) { slot
= JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
402 static bool isDeletedValue(EncodedJSValue value
) { return value
== JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
405 struct EncodedJSValueHashTraits
: HashTraits
<EncodedJSValue
> {
406 static void constructDeletedValue(EncodedJSValue
& slot
) { slot
= JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
407 static bool isDeletedValue(EncodedJSValue value
) { return value
== JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
411 typedef HashMap
<EncodedJSValue
, unsigned, EncodedJSValueHash
, EncodedJSValueHashTraits
> JSValueMap
;
413 // Stand-alone helper functions.
414 inline JSValue
jsNull()
416 return JSValue(JSValue::JSNull
);
419 inline JSValue
jsUndefined()
421 return JSValue(JSValue::JSUndefined
);
424 inline JSValue
jsBoolean(bool b
)
426 return b
? JSValue(JSValue::JSTrue
) : JSValue(JSValue::JSFalse
);
429 ALWAYS_INLINE JSValue
jsDoubleNumber(double d
)
431 ASSERT(JSValue(JSValue::EncodeAsDouble
, d
).isNumber());
432 return JSValue(JSValue::EncodeAsDouble
, d
);
435 ALWAYS_INLINE JSValue
jsNumber(double d
)
437 ASSERT(JSValue(d
).isNumber());
441 ALWAYS_INLINE JSValue
jsNumber(char i
)
446 ALWAYS_INLINE JSValue
jsNumber(unsigned char i
)
451 ALWAYS_INLINE JSValue
jsNumber(short i
)
456 ALWAYS_INLINE JSValue
jsNumber(unsigned short i
)
461 ALWAYS_INLINE JSValue
jsNumber(int i
)
466 ALWAYS_INLINE JSValue
jsNumber(unsigned i
)
471 ALWAYS_INLINE JSValue
jsNumber(long i
)
476 ALWAYS_INLINE JSValue
jsNumber(unsigned long i
)
481 ALWAYS_INLINE JSValue
jsNumber(long long i
)
486 ALWAYS_INLINE JSValue
jsNumber(unsigned long long i
)
491 inline bool operator==(const JSValue a
, const JSCell
* b
) { return a
== JSValue(b
); }
492 inline bool operator==(const JSCell
* a
, const JSValue b
) { return JSValue(a
) == b
; }
494 inline bool operator!=(const JSValue a
, const JSCell
* b
) { return a
!= JSValue(b
); }
495 inline bool operator!=(const JSCell
* a
, const JSValue b
) { return JSValue(a
) != b
; }
499 #endif // JSCJSValue_h