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.
28 #include <stddef.h> // for size_t
30 #include <wtf/Assertions.h>
31 #include <wtf/Forward.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/HashTraits.h>
34 #include <wtf/MathExtras.h>
35 #include <wtf/StdLibExtras.h>
36 #include <wtf/TriState.h>
40 class AssemblyHelpers
;
50 class PutPropertySlot
;
54 class OSRExitCompiler
;
69 template <class T
> class WriteBarrierBase
;
71 enum PreferredPrimitiveType
{ NoPreference
, PreferNumber
, PreferString
};
72 enum ECMAMode
{ StrictMode
, NotStrictMode
};
74 typedef int64_t EncodedJSValue
;
76 union EncodedValueDescriptor
{
97 #define TagOffset (OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag))
98 #define PayloadOffset (OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload))
101 #define CellPayloadOffset 0
103 #define CellPayloadOffset PayloadOffset
106 enum WhichValueWord
{
111 // This implements ToInt32, defined in ECMA-262 9.5.
112 JS_EXPORT_PRIVATE
int32_t toInt32(double);
114 // This implements ToUInt32, defined in ECMA-262 9.6.
115 inline uint32_t toUInt32(double number
)
117 // As commented in the spec, the operation of ToInt32 and ToUint32 only differ
118 // in how the result is interpreted; see NOTEs in sections 9.5 and 9.6.
119 return toInt32(number
);
122 int64_t tryConvertToInt52(double);
123 bool isInt52(double);
126 friend struct EncodedJSValueHashTraits
;
127 friend class AssemblyHelpers
;
129 friend class JITSlowPathCall
;
130 friend class JITStubs
;
131 friend class JITStubCall
;
132 friend class JSInterfaceJIT
;
133 friend class JSValueSource
;
134 friend class SpecializedThunkJIT
;
136 friend class DFG::JITCompiler
;
137 friend class DFG::OSRExitCompiler
;
138 friend class DFG::SpeculativeJIT
;
141 friend class LLInt::CLoop
;
145 #if USE(JSVALUE32_64)
146 enum { Int32Tag
= 0xffffffff };
147 enum { BooleanTag
= 0xfffffffe };
148 enum { NullTag
= 0xfffffffd };
149 enum { UndefinedTag
= 0xfffffffc };
150 enum { CellTag
= 0xfffffffb };
151 enum { EmptyValueTag
= 0xfffffffa };
152 enum { DeletedValueTag
= 0xfffffff9 };
154 enum { LowestTag
= DeletedValueTag
};
157 static EncodedJSValue
encode(JSValue
);
158 static JSValue
decode(EncodedJSValue
);
160 enum JSNullTag
{ JSNull
};
161 enum JSUndefinedTag
{ JSUndefined
};
162 enum JSTrueTag
{ JSTrue
};
163 enum JSFalseTag
{ JSFalse
};
164 enum EncodeAsDoubleTag
{ EncodeAsDouble
};
168 JSValue(JSUndefinedTag
);
171 JSValue(JSCell
* ptr
);
172 JSValue(const JSCell
* ptr
);
175 JSValue(EncodeAsDoubleTag
, double);
176 explicit JSValue(double);
177 explicit JSValue(char);
178 explicit JSValue(unsigned char);
179 explicit JSValue(short);
180 explicit JSValue(unsigned short);
181 explicit JSValue(int);
182 explicit JSValue(unsigned);
183 explicit JSValue(long);
184 explicit JSValue(unsigned long);
185 explicit JSValue(long long);
186 explicit JSValue(unsigned long long);
188 typedef void* (JSValue::*UnspecifiedBoolType
);
189 operator UnspecifiedBoolType
*() const;
190 bool operator==(const JSValue
& other
) const;
191 bool operator!=(const JSValue
& other
) const;
193 bool isInt32() const;
194 bool isUInt32() const;
195 bool isDouble() const;
197 bool isFalse() const;
199 int32_t asInt32() const;
200 uint32_t asUInt32() const;
201 int64_t asMachineInt() const;
202 double asDouble() const;
203 bool asBoolean() const;
204 double asNumber() const;
206 int32_t asInt32ForArithmetic() const; // Boolean becomes an int, but otherwise like asInt32().
208 // Querying the type.
209 bool isEmpty() const;
210 bool isFunction() const;
211 bool isUndefined() const;
213 bool isUndefinedOrNull() const;
214 bool isBoolean() const;
215 bool isMachineInt() const;
216 bool isNumber() const;
217 bool isString() const;
218 bool isPrimitive() const;
219 bool isGetterSetter() const;
220 bool isCustomGetterSetter() const;
221 bool isObject() const;
222 bool inherits(const ClassInfo
*) const;
224 // Extracting the value.
225 bool getString(ExecState
*, WTF::String
&) const;
226 WTF::String
getString(ExecState
*) const; // null string if not a string
227 JSObject
* getObject() const; // 0 if not an object
229 // Extracting integer values.
230 bool getUInt32(uint32_t&) const;
232 // Basic conversions.
233 JSValue
toPrimitive(ExecState
*, PreferredPrimitiveType
= NoPreference
) const;
234 bool getPrimitiveNumber(ExecState
*, double& number
, JSValue
&);
236 bool toBoolean(ExecState
*) const;
237 TriState
pureToBoolean() const;
239 // toNumber conversion is expected to be side effect free if an exception has
240 // been set in the ExecState already.
241 double toNumber(ExecState
*) const;
242 JSString
* toString(ExecState
*) const;
243 WTF::String
toWTFString(ExecState
*) const;
244 WTF::String
toWTFStringInline(ExecState
*) const;
245 JSObject
* toObject(ExecState
*) const;
246 JSObject
* toObject(ExecState
*, JSGlobalObject
*) const;
248 // Integer conversions.
249 JS_EXPORT_PRIVATE
double toInteger(ExecState
*) const;
250 double toIntegerPreserveNaN(ExecState
*) const;
251 int32_t toInt32(ExecState
*) const;
252 uint32_t toUInt32(ExecState
*) const;
254 // Floating point conversions (this is a convenience method for webcore;
255 // signle precision float is not a representation used in JS or JSC).
256 float toFloat(ExecState
* exec
) const { return static_cast<float>(toNumber(exec
)); }
258 // Object operations, with the toObject operation included.
259 JSValue
get(ExecState
*, PropertyName
) const;
260 JSValue
get(ExecState
*, PropertyName
, PropertySlot
&) const;
261 JSValue
get(ExecState
*, unsigned propertyName
) const;
262 JSValue
get(ExecState
*, unsigned propertyName
, PropertySlot
&) const;
263 void put(ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
264 void putToPrimitive(ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
265 void putToPrimitiveByIndex(ExecState
*, unsigned propertyName
, JSValue
, bool shouldThrow
);
266 void putByIndex(ExecState
*, unsigned propertyName
, JSValue
, bool shouldThrow
);
268 JSValue
toThis(ExecState
*, ECMAMode
) const;
270 static bool equal(ExecState
*, JSValue v1
, JSValue v2
);
271 static bool equalSlowCase(ExecState
*, JSValue v1
, JSValue v2
);
272 static bool equalSlowCaseInline(ExecState
*, JSValue v1
, JSValue v2
);
273 static bool strictEqual(ExecState
*, JSValue v1
, JSValue v2
);
274 static bool strictEqualSlowCase(ExecState
*, JSValue v1
, JSValue v2
);
275 static bool strictEqualSlowCaseInline(ExecState
*, JSValue v1
, JSValue v2
);
276 static TriState
pureStrictEqual(JSValue v1
, JSValue v2
);
279 JSCell
* asCell() const;
280 JS_EXPORT_PRIVATE
bool isValidCallee();
282 JSValue
structureOrUndefined() const;
284 JS_EXPORT_PRIVATE
void dump(PrintStream
&) const;
285 void dumpInContext(PrintStream
&, DumpContext
*) const;
286 void dumpForBacktrace(PrintStream
&) const;
288 JS_EXPORT_PRIVATE JSObject
* synthesizePrototype(ExecState
*) const;
290 // Constants used for Int52. Int52 isn't part of JSValue right now, but JSValues may be
291 // converted to Int52s and back again.
292 static const unsigned numberOfInt52Bits
= 52;
293 static const int64_t notInt52
= static_cast<int64_t>(1) << numberOfInt52Bits
;
294 static const unsigned int52ShiftAmount
= 12;
296 static ptrdiff_t offsetOfPayload() { return OBJECT_OFFSETOF(JSValue
, u
.asBits
.payload
); }
297 static ptrdiff_t offsetOfTag() { return OBJECT_OFFSETOF(JSValue
, u
.asBits
.tag
); }
299 #if USE(JSVALUE32_64)
301 * On 32-bit platforms USE(JSVALUE32_64) should be defined, and we use a NaN-encoded
302 * form for immediates.
304 * The encoding makes use of unused NaN space in the IEEE754 representation. Any value
305 * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values
306 * can encode a 51-bit payload. Hardware produced and C-library payloads typically
307 * have a payload of zero. We assume that non-zero payloads are available to encode
308 * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are
309 * all set represents a NaN with a non-zero payload, we can use this space in the NaN
310 * ranges to encode other values (however there are also other ranges of NaN space that
311 * could have been selected).
313 * For JSValues that do not contain a double value, the high 32 bits contain the tag
314 * values listed in the enums below, which all correspond to NaN-space. In the case of
315 * cell, integer and bool values the lower 32 bits (the 'payload') contain the pointer
316 * integer or boolean value; in the case of all other tags the payload is 0.
318 uint32_t tag() const;
319 int32_t payload() const;
322 // This should only be used by the LLInt C Loop interpreter who needs
323 // synthesize JSValue from its "register"s holding tag and payload
325 explicit JSValue(int32_t tag
, int32_t payload
);
330 * On 64-bit platforms USE(JSVALUE64) should be defined, and we use a NaN-encoded
331 * form for immediates.
333 * The encoding makes use of unused NaN space in the IEEE754 representation. Any value
334 * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values
335 * can encode a 51-bit payload. Hardware produced and C-library payloads typically
336 * have a payload of zero. We assume that non-zero payloads are available to encode
337 * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are
338 * all set represents a NaN with a non-zero payload, we can use this space in the NaN
339 * ranges to encode other values (however there are also other ranges of NaN space that
340 * could have been selected).
342 * This range of NaN space is represented by 64-bit numbers begining with the 16-bit
343 * hex patterns 0xFFFE and 0xFFFF - we rely on the fact that no valid double-precision
344 * numbers will fall in these ranges.
346 * The top 16-bits denote the type of the encoded JSValue:
348 * Pointer { 0000:PPPP:PPPP:PPPP
349 * / 0001:****:****:****
351 * \ FFFE:****:****:****
352 * Integer { FFFF:0000:IIII:IIII
354 * The scheme we have implemented encodes double precision values by performing a
355 * 64-bit integer addition of the value 2^48 to the number. After this manipulation
356 * no encoded double-precision value will begin with the pattern 0x0000 or 0xFFFF.
357 * Values must be decoded by reversing this operation before subsequent floating point
358 * operations may be peformed.
360 * 32-bit signed integers are marked with the 16-bit tag 0xFFFF.
362 * The tag 0x0000 denotes a pointer, or another form of tagged immediate. Boolean,
363 * null and undefined values are represented by specific, invalid pointer values:
370 * These values have the following properties:
371 * - Bit 1 (TagBitTypeOther) is set for all four values, allowing real pointers to be
372 * quickly distinguished from all immediate values, including these invalid pointers.
373 * - With bit 3 is masked out (TagBitUndefined) Undefined and Null share the
374 * same value, allowing null & undefined to be quickly detected.
376 * No valid JSValue will have the bit pattern 0x0, this is used to represent array
377 * holes, and as a C++ 'no value' result (e.g. JSValue() has an internal value of 0).
380 // These values are #defines since using static const integers here is a ~1% regression!
382 // This value is 2^48, used to encode doubles such that the encoded value will begin
383 // with a 16-bit pattern within the range 0x0001..0xFFFE.
384 #define DoubleEncodeOffset 0x1000000000000ll
385 // If all bits in the mask are set, this indicates an integer number,
386 // if any but not all are set this value is a double precision number.
387 #define TagTypeNumber 0xffff000000000000ll
389 // All non-numeric (bool, null, undefined) immediates have bit 2 set.
390 #define TagBitTypeOther 0x2ll
391 #define TagBitBool 0x4ll
392 #define TagBitUndefined 0x8ll
393 // Combined integer value for non-numeric immediates.
394 #define ValueFalse (TagBitTypeOther | TagBitBool | false)
395 #define ValueTrue (TagBitTypeOther | TagBitBool | true)
396 #define ValueUndefined (TagBitTypeOther | TagBitUndefined)
397 #define ValueNull (TagBitTypeOther)
399 // TagMask is used to check for all types of immediate values (either number or 'other').
400 #define TagMask (TagTypeNumber | TagBitTypeOther)
402 // These special values are never visible to JavaScript code; Empty is used to represent
403 // Array holes, and for uninitialized JSValues. Deleted is used in hash table code.
404 // These values would map to cell types in the JSValue encoding, but not valid GC cell
405 // pointer should have either of these values (Empty is null, deleted is at an invalid
406 // alignment for a GC cell, and in the zero page).
407 #define ValueEmpty 0x0ll
408 #define ValueDeleted 0x4ll
412 template <class T
> JSValue(WriteBarrierBase
<T
>);
414 enum HashTableDeletedValueTag
{ HashTableDeletedValue
};
415 JSValue(HashTableDeletedValueTag
);
417 inline const JSValue
asValue() const { return *this; }
418 JS_EXPORT_PRIVATE
double toNumberSlowCase(ExecState
*) const;
419 JS_EXPORT_PRIVATE JSString
* toStringSlowCase(ExecState
*) const;
420 JS_EXPORT_PRIVATE
WTF::String
toWTFStringSlowCase(ExecState
*) const;
421 JS_EXPORT_PRIVATE JSObject
* toObjectSlowCase(ExecState
*, JSGlobalObject
*) const;
422 JS_EXPORT_PRIVATE JSValue
toThisSlowCase(ExecState
*, ECMAMode
) const;
424 EncodedValueDescriptor u
;
427 typedef IntHash
<EncodedJSValue
> EncodedJSValueHash
;
429 #if USE(JSVALUE32_64)
430 struct EncodedJSValueHashTraits
: HashTraits
<EncodedJSValue
> {
431 static const bool emptyValueIsZero
= false;
432 static EncodedJSValue
emptyValue() { return JSValue::encode(JSValue()); }
433 static void constructDeletedValue(EncodedJSValue
& slot
) { slot
= JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
434 static bool isDeletedValue(EncodedJSValue value
) { return value
== JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
437 struct EncodedJSValueHashTraits
: HashTraits
<EncodedJSValue
> {
438 static void constructDeletedValue(EncodedJSValue
& slot
) { slot
= JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
439 static bool isDeletedValue(EncodedJSValue value
) { return value
== JSValue::encode(JSValue(JSValue::HashTableDeletedValue
)); }
443 typedef HashMap
<EncodedJSValue
, unsigned, EncodedJSValueHash
, EncodedJSValueHashTraits
> JSValueMap
;
445 // Stand-alone helper functions.
446 inline JSValue
jsNull()
448 return JSValue(JSValue::JSNull
);
451 inline JSValue
jsUndefined()
453 return JSValue(JSValue::JSUndefined
);
456 inline JSValue
jsBoolean(bool b
)
458 return b
? JSValue(JSValue::JSTrue
) : JSValue(JSValue::JSFalse
);
461 ALWAYS_INLINE JSValue
jsDoubleNumber(double d
)
463 ASSERT(JSValue(JSValue::EncodeAsDouble
, d
).isNumber());
464 return JSValue(JSValue::EncodeAsDouble
, d
);
467 ALWAYS_INLINE JSValue
jsNumber(double d
)
469 ASSERT(JSValue(d
).isNumber());
473 ALWAYS_INLINE JSValue
jsNumber(char i
)
478 ALWAYS_INLINE JSValue
jsNumber(unsigned char i
)
483 ALWAYS_INLINE JSValue
jsNumber(short i
)
488 ALWAYS_INLINE JSValue
jsNumber(unsigned short i
)
493 ALWAYS_INLINE JSValue
jsNumber(int i
)
498 ALWAYS_INLINE JSValue
jsNumber(unsigned i
)
503 ALWAYS_INLINE JSValue
jsNumber(long i
)
508 ALWAYS_INLINE JSValue
jsNumber(unsigned long i
)
513 ALWAYS_INLINE JSValue
jsNumber(long long i
)
518 ALWAYS_INLINE JSValue
jsNumber(unsigned long long i
)
523 inline bool operator==(const JSValue a
, const JSCell
* b
) { return a
== JSValue(b
); }
524 inline bool operator==(const JSCell
* a
, const JSValue b
) { return JSValue(a
) == b
; }
526 inline bool operator!=(const JSValue a
, const JSCell
* b
) { return a
!= JSValue(b
); }
527 inline bool operator!=(const JSCell
* a
, const JSValue b
) { return JSValue(a
) != b
; }
531 #endif // JSCJSValue_h