1 | /* |
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected]) |
3 | * Copyright (C) 2001 Peter Kelly ([email protected]) |
4 | * Copyright (C) 2003-2019 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 | #pragma once |
24 | |
25 | #include "JSExportMacros.h" |
26 | #include "PureNaN.h" |
27 | #include <functional> |
28 | #include <math.h> |
29 | #include <stddef.h> |
30 | #include <stdint.h> |
31 | #include <wtf/Assertions.h> |
32 | #include <wtf/Forward.h> |
33 | #include <wtf/HashMap.h> |
34 | #include <wtf/HashTraits.h> |
35 | #include <wtf/MathExtras.h> |
36 | #include <wtf/MediaTime.h> |
37 | #include <wtf/StdLibExtras.h> |
38 | #include <wtf/TriState.h> |
39 | |
40 | namespace JSC { |
41 | |
42 | class AssemblyHelpers; |
43 | class JSBigInt; |
44 | class CallFrame; |
45 | class JSCell; |
46 | class JSValueSource; |
47 | class VM; |
48 | class JSGlobalObject; |
49 | class JSObject; |
50 | class JSString; |
51 | class Identifier; |
52 | class PropertyName; |
53 | class PropertySlot; |
54 | class PutPropertySlot; |
55 | class Structure; |
56 | #if ENABLE(DFG_JIT) |
57 | namespace DFG { |
58 | class JITCompiler; |
59 | class OSRExitCompiler; |
60 | class SpeculativeJIT; |
61 | } |
62 | #endif |
63 | #if ENABLE(C_LOOP) |
64 | namespace LLInt { |
65 | class CLoop; |
66 | } |
67 | #endif |
68 | |
69 | struct ClassInfo; |
70 | struct DumpContext; |
71 | struct Instruction; |
72 | struct MethodTable; |
73 | enum class Unknown { }; |
74 | |
75 | template <class T, typename Traits> class WriteBarrierBase; |
76 | template<class T> |
77 | using WriteBarrierTraitsSelect = typename std::conditional<std::is_same<T, Unknown>::value, |
78 | DumbValueTraits<T>, DumbPtrTraits<T> |
79 | >::type; |
80 | |
81 | enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString }; |
82 | enum ECMAMode { StrictMode, NotStrictMode }; |
83 | |
84 | enum class CallType : unsigned; |
85 | struct CallData; |
86 | enum class ConstructType : unsigned; |
87 | struct ConstructData; |
88 | |
89 | typedef int64_t EncodedJSValue; |
90 | |
91 | union EncodedValueDescriptor { |
92 | int64_t asInt64; |
93 | #if USE(JSVALUE32_64) |
94 | double asDouble; |
95 | #elif USE(JSVALUE64) |
96 | JSCell* ptr; |
97 | #endif |
98 | |
99 | #if CPU(BIG_ENDIAN) |
100 | struct { |
101 | int32_t tag; |
102 | int32_t payload; |
103 | } asBits; |
104 | #else |
105 | struct { |
106 | int32_t payload; |
107 | int32_t tag; |
108 | } asBits; |
109 | #endif |
110 | }; |
111 | |
112 | #define TagOffset (offsetof(EncodedValueDescriptor, asBits.tag)) |
113 | #define PayloadOffset (offsetof(EncodedValueDescriptor, asBits.payload)) |
114 | |
115 | #if USE(JSVALUE64) |
116 | #define CellPayloadOffset 0 |
117 | #else |
118 | #define CellPayloadOffset PayloadOffset |
119 | #endif |
120 | |
121 | enum WhichValueWord { |
122 | TagWord, |
123 | PayloadWord |
124 | }; |
125 | |
126 | int64_t tryConvertToInt52(double); |
127 | bool isInt52(double); |
128 | |
129 | enum class SourceCodeRepresentation : uint8_t { |
130 | Other, |
131 | Integer, |
132 | Double, |
133 | LinkTimeConstant, |
134 | }; |
135 | |
136 | class JSValue { |
137 | friend struct EncodedJSValueHashTraits; |
138 | friend struct EncodedJSValueWithRepresentationHashTraits; |
139 | friend class AssemblyHelpers; |
140 | friend class JIT; |
141 | friend class JITSlowPathCall; |
142 | friend class JITStubs; |
143 | friend class JITStubCall; |
144 | friend class JSInterfaceJIT; |
145 | friend class JSValueSource; |
146 | friend class SpecializedThunkJIT; |
147 | #if ENABLE(DFG_JIT) |
148 | friend class DFG::JITCompiler; |
149 | friend class DFG::OSRExitCompiler; |
150 | friend class DFG::SpeculativeJIT; |
151 | #endif |
152 | #if ENABLE(C_LOOP) |
153 | friend class LLInt::CLoop; |
154 | #endif |
155 | |
156 | public: |
157 | #if USE(JSVALUE32_64) |
158 | enum { Int32Tag = 0xffffffff }; |
159 | enum { BooleanTag = 0xfffffffe }; |
160 | enum { NullTag = 0xfffffffd }; |
161 | enum { UndefinedTag = 0xfffffffc }; |
162 | enum { CellTag = 0xfffffffb }; |
163 | enum { EmptyValueTag = 0xfffffffa }; |
164 | enum { DeletedValueTag = 0xfffffff9 }; |
165 | |
166 | enum { LowestTag = DeletedValueTag }; |
167 | |
168 | #endif |
169 | |
170 | static EncodedJSValue encode(JSValue); |
171 | static JSValue decode(EncodedJSValue); |
172 | |
173 | enum JSNullTag { JSNull }; |
174 | enum JSUndefinedTag { JSUndefined }; |
175 | enum JSTrueTag { JSTrue }; |
176 | enum JSFalseTag { JSFalse }; |
177 | enum JSCellTag { JSCellType }; |
178 | enum EncodeAsDoubleTag { EncodeAsDouble }; |
179 | |
180 | JSValue(); |
181 | JSValue(JSNullTag); |
182 | JSValue(JSUndefinedTag); |
183 | JSValue(JSTrueTag); |
184 | JSValue(JSFalseTag); |
185 | JSValue(JSCell* ptr); |
186 | JSValue(const JSCell* ptr); |
187 | |
188 | // Numbers |
189 | JSValue(EncodeAsDoubleTag, double); |
190 | explicit JSValue(double); |
191 | explicit JSValue(char); |
192 | explicit JSValue(unsigned char); |
193 | explicit JSValue(short); |
194 | explicit JSValue(unsigned short); |
195 | explicit JSValue(int); |
196 | explicit JSValue(unsigned); |
197 | explicit JSValue(long); |
198 | explicit JSValue(unsigned long); |
199 | explicit JSValue(long long); |
200 | explicit JSValue(unsigned long long); |
201 | |
202 | explicit operator bool() const; |
203 | bool operator==(const JSValue& other) const; |
204 | bool operator!=(const JSValue& other) const; |
205 | |
206 | bool isInt32() const; |
207 | bool isUInt32() const; |
208 | bool isDouble() const; |
209 | bool isTrue() const; |
210 | bool isFalse() const; |
211 | |
212 | int32_t asInt32() const; |
213 | uint32_t asUInt32() const; |
214 | int64_t asAnyInt() const; |
215 | uint32_t asUInt32AsAnyInt() const; |
216 | int32_t asInt32AsAnyInt() const; |
217 | double asDouble() const; |
218 | bool asBoolean() const; |
219 | double asNumber() const; |
220 | |
221 | int32_t asInt32ForArithmetic() const; // Boolean becomes an int, but otherwise like asInt32(). |
222 | |
223 | // Querying the type. |
224 | bool isEmpty() const; |
225 | bool isFunction(VM&) const; |
226 | bool isCallable(VM&, CallType&, CallData&) const; |
227 | bool isConstructor(VM&) const; |
228 | bool isConstructor(VM&, ConstructType&, ConstructData&) const; |
229 | bool isUndefined() const; |
230 | bool isNull() const; |
231 | bool isUndefinedOrNull() const; |
232 | bool isBoolean() const; |
233 | bool isAnyInt() const; |
234 | bool isUInt32AsAnyInt() const; |
235 | bool isInt32AsAnyInt() const; |
236 | bool isNumber() const; |
237 | bool isString() const; |
238 | bool isBigInt() const; |
239 | bool isSymbol() const; |
240 | bool isPrimitive() const; |
241 | bool isGetterSetter() const; |
242 | bool isCustomGetterSetter() const; |
243 | bool isObject() const; |
244 | bool inherits(VM&, const ClassInfo*) const; |
245 | template<typename Target> bool inherits(VM&) const; |
246 | const ClassInfo* classInfoOrNull(VM&) const; |
247 | |
248 | // Extracting the value. |
249 | bool getString(JSGlobalObject*, WTF::String&) const; |
250 | WTF::String getString(JSGlobalObject*) const; // null string if not a string |
251 | JSObject* getObject() const; // 0 if not an object |
252 | |
253 | // Extracting integer values. |
254 | bool getUInt32(uint32_t&) const; |
255 | |
256 | // Basic conversions. |
257 | JSValue toPrimitive(JSGlobalObject*, PreferredPrimitiveType = NoPreference) const; |
258 | bool getPrimitiveNumber(JSGlobalObject*, double& number, JSValue&); |
259 | |
260 | bool toBoolean(JSGlobalObject*) const; |
261 | TriState pureToBoolean() const; |
262 | |
263 | // toNumber conversion is expected to be side effect free if an exception has |
264 | // been set in the CallFrame already. |
265 | double toNumber(JSGlobalObject*) const; |
266 | |
267 | Variant<JSBigInt*, double> toNumeric(JSGlobalObject*) const; |
268 | Variant<JSBigInt*, int32_t> toBigIntOrInt32(JSGlobalObject*) const; |
269 | |
270 | // toNumber conversion if it can be done without side effects. |
271 | Optional<double> toNumberFromPrimitive() const; |
272 | |
273 | JSString* toString(JSGlobalObject*) const; // On exception, this returns the empty string. |
274 | JSString* toStringOrNull(JSGlobalObject*) const; // On exception, this returns null, to make exception checks faster. |
275 | Identifier toPropertyKey(JSGlobalObject*) const; |
276 | WTF::String toWTFString(JSGlobalObject*) const; |
277 | JSObject* toObject(JSGlobalObject*) const; |
278 | |
279 | // Integer conversions. |
280 | JS_EXPORT_PRIVATE double toInteger(JSGlobalObject*) const; |
281 | JS_EXPORT_PRIVATE double toIntegerPreserveNaN(JSGlobalObject*) const; |
282 | int32_t toInt32(JSGlobalObject*) const; |
283 | uint32_t toUInt32(JSGlobalObject*) const; |
284 | uint32_t toIndex(JSGlobalObject*, const char* errorName) const; |
285 | double toLength(JSGlobalObject*) const; |
286 | |
287 | // Floating point conversions (this is a convenience function for WebCore; |
288 | // single precision float is not a representation used in JS or JSC). |
289 | float toFloat(JSGlobalObject* globalObject) const { return static_cast<float>(toNumber(globalObject)); } |
290 | |
291 | // Object operations, with the toObject operation included. |
292 | JSValue get(JSGlobalObject*, PropertyName) const; |
293 | JSValue get(JSGlobalObject*, PropertyName, PropertySlot&) const; |
294 | JSValue get(JSGlobalObject*, unsigned propertyName) const; |
295 | JSValue get(JSGlobalObject*, unsigned propertyName, PropertySlot&) const; |
296 | JSValue get(JSGlobalObject*, uint64_t propertyName) const; |
297 | |
298 | bool getPropertySlot(JSGlobalObject*, PropertyName, PropertySlot&) const; |
299 | template<typename CallbackWhenNoException> typename std::result_of<CallbackWhenNoException(bool, PropertySlot&)>::type getPropertySlot(JSGlobalObject*, PropertyName, CallbackWhenNoException) const; |
300 | template<typename CallbackWhenNoException> typename std::result_of<CallbackWhenNoException(bool, PropertySlot&)>::type getPropertySlot(JSGlobalObject*, PropertyName, PropertySlot&, CallbackWhenNoException) const; |
301 | |
302 | bool getOwnPropertySlot(JSGlobalObject*, PropertyName, PropertySlot&) const; |
303 | |
304 | bool put(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&); |
305 | bool putInline(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&); |
306 | JS_EXPORT_PRIVATE bool putToPrimitive(JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&); |
307 | JS_EXPORT_PRIVATE bool putToPrimitiveByIndex(JSGlobalObject*, unsigned propertyName, JSValue, bool shouldThrow); |
308 | bool putByIndex(JSGlobalObject*, unsigned propertyName, JSValue, bool shouldThrow); |
309 | |
310 | JSValue toThis(JSGlobalObject*, ECMAMode) const; |
311 | |
312 | static bool equal(JSGlobalObject*, JSValue v1, JSValue v2); |
313 | static bool equalSlowCase(JSGlobalObject*, JSValue v1, JSValue v2); |
314 | static bool equalSlowCaseInline(JSGlobalObject*, JSValue v1, JSValue v2); |
315 | static bool strictEqual(JSGlobalObject*, JSValue v1, JSValue v2); |
316 | static bool strictEqualSlowCase(JSGlobalObject*, JSValue v1, JSValue v2); |
317 | static bool strictEqualSlowCaseInline(JSGlobalObject*, JSValue v1, JSValue v2); |
318 | static TriState pureStrictEqual(JSValue v1, JSValue v2); |
319 | |
320 | bool isCell() const; |
321 | JSCell* asCell() const; |
322 | JS_EXPORT_PRIVATE bool isValidCallee(); |
323 | |
324 | Structure* structureOrNull() const; |
325 | JSValue structureOrUndefined() const; |
326 | |
327 | JS_EXPORT_PRIVATE void dump(PrintStream&) const; |
328 | void dumpInContext(PrintStream&, DumpContext*) const; |
329 | void dumpInContextAssumingStructure(PrintStream&, DumpContext*, Structure*) const; |
330 | void dumpForBacktrace(PrintStream&) const; |
331 | |
332 | JS_EXPORT_PRIVATE JSObject* synthesizePrototype(JSGlobalObject*) const; |
333 | bool requireObjectCoercible(JSGlobalObject*) const; |
334 | |
335 | // Constants used for Int52. Int52 isn't part of JSValue right now, but JSValues may be |
336 | // converted to Int52s and back again. |
337 | static constexpr const unsigned numberOfInt52Bits = 52; |
338 | static constexpr const int64_t notInt52 = static_cast<int64_t>(1) << numberOfInt52Bits; |
339 | static constexpr const unsigned int52ShiftAmount = 12; |
340 | |
341 | static ptrdiff_t offsetOfPayload() { return OBJECT_OFFSETOF(JSValue, u.asBits.payload); } |
342 | static ptrdiff_t offsetOfTag() { return OBJECT_OFFSETOF(JSValue, u.asBits.tag); } |
343 | |
344 | #if USE(JSVALUE32_64) |
345 | /* |
346 | * On 32-bit platforms USE(JSVALUE32_64) should be defined, and we use a NaN-encoded |
347 | * form for immediates. |
348 | * |
349 | * The encoding makes use of unused NaN space in the IEEE754 representation. Any value |
350 | * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values |
351 | * can encode a 51-bit payload. Hardware produced and C-library payloads typically |
352 | * have a payload of zero. We assume that non-zero payloads are available to encode |
353 | * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are |
354 | * all set represents a NaN with a non-zero payload, we can use this space in the NaN |
355 | * ranges to encode other values (however there are also other ranges of NaN space that |
356 | * could have been selected). |
357 | * |
358 | * For JSValues that do not contain a double value, the high 32 bits contain the tag |
359 | * values listed in the enums below, which all correspond to NaN-space. In the case of |
360 | * cell, integer and bool values the lower 32 bits (the 'payload') contain the pointer |
361 | * integer or boolean value; in the case of all other tags the payload is 0. |
362 | */ |
363 | uint32_t tag() const; |
364 | int32_t payload() const; |
365 | |
366 | // This should only be used by the LLInt C Loop interpreter and OSRExit code who needs |
367 | // synthesize JSValue from its "register"s holding tag and payload values. |
368 | explicit JSValue(int32_t tag, int32_t payload); |
369 | |
370 | #elif USE(JSVALUE64) |
371 | /* |
372 | * On 64-bit platforms USE(JSVALUE64) should be defined, and we use a NaN-encoded |
373 | * form for immediates. |
374 | * |
375 | * The encoding makes use of unused NaN space in the IEEE754 representation. Any value |
376 | * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values |
377 | * can encode a 51-bit payload. Hardware produced and C-library payloads typically |
378 | * have a payload of zero. We assume that non-zero payloads are available to encode |
379 | * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are |
380 | * all set represents a NaN with a non-zero payload, we can use this space in the NaN |
381 | * ranges to encode other values (however there are also other ranges of NaN space that |
382 | * could have been selected). |
383 | * |
384 | * This range of NaN space is represented by 64-bit numbers begining with the 15-bit |
385 | * hex patterns 0xFFFC and 0xFFFE - we rely on the fact that no valid double-precision |
386 | * numbers will fall in these ranges. |
387 | * |
388 | * The top 15-bits denote the type of the encoded JSValue: |
389 | * |
390 | * Pointer { 0000:PPPP:PPPP:PPPP |
391 | * / 0002:****:****:**** |
392 | * Double { ... |
393 | * \ FFFC:****:****:**** |
394 | * Integer { FFFE:0000:IIII:IIII |
395 | * |
396 | * The scheme we have implemented encodes double precision values by performing a |
397 | * 64-bit integer addition of the value 2^49 to the number. After this manipulation |
398 | * no encoded double-precision value will begin with the pattern 0x0000 or 0xFFFE. |
399 | * Values must be decoded by reversing this operation before subsequent floating point |
400 | * operations may be peformed. |
401 | * |
402 | * 32-bit signed integers are marked with the 16-bit tag 0xFFFE. |
403 | * |
404 | * The tag 0x0000 denotes a pointer, or another form of tagged immediate. Boolean, |
405 | * null and undefined values are represented by specific, invalid pointer values: |
406 | * |
407 | * False: 0x06 |
408 | * True: 0x07 |
409 | * Undefined: 0x0a |
410 | * Null: 0x02 |
411 | * |
412 | * These values have the following properties: |
413 | * - Bit 1 (OtherTag) is set for all four values, allowing real pointers to be |
414 | * quickly distinguished from all immediate values, including these invalid pointers. |
415 | * - With bit 3 is masked out (UndefinedTag) Undefined and Null share the |
416 | * same value, allowing null & undefined to be quickly detected. |
417 | * |
418 | * No valid JSValue will have the bit pattern 0x0, this is used to represent array |
419 | * holes, and as a C++ 'no value' result (e.g. JSValue() has an internal value of 0). |
420 | */ |
421 | |
422 | // This value is 2^49, used to encode doubles such that the encoded value will begin |
423 | // with a 15-bit pattern within the range 0x0002..0xFFFC. |
424 | static constexpr size_t DoubleEncodeOffsetBit = 49; |
425 | static constexpr int64_t DoubleEncodeOffset = 1ll << DoubleEncodeOffsetBit; |
426 | // If all bits in the mask are set, this indicates an integer number, |
427 | // if any but not all are set this value is a double precision number. |
428 | static constexpr int64_t NumberTag = 0xfffe000000000000ll; |
429 | |
430 | // All non-numeric (bool, null, undefined) immediates have bit 2 set. |
431 | static constexpr int32_t OtherTag = 0x2; |
432 | static constexpr int32_t BoolTag = 0x4; |
433 | static constexpr int32_t UndefinedTag = 0x8; |
434 | // Combined integer value for non-numeric immediates. |
435 | static constexpr int32_t ValueFalse = OtherTag | BoolTag | false; |
436 | static constexpr int32_t ValueTrue = OtherTag | BoolTag | true; |
437 | static constexpr int32_t ValueUndefined = OtherTag | UndefinedTag; |
438 | static constexpr int32_t ValueNull = OtherTag; |
439 | |
440 | static constexpr int64_t MiscTag = OtherTag | BoolTag | UndefinedTag; |
441 | |
442 | // NotCellMask is used to check for all types of immediate values (either number or 'other'). |
443 | static constexpr int64_t NotCellMask = NumberTag | OtherTag; |
444 | |
445 | // These special values are never visible to JavaScript code; Empty is used to represent |
446 | // Array holes, and for uninitialized JSValues. Deleted is used in hash table code. |
447 | // These values would map to cell types in the JSValue encoding, but not valid GC cell |
448 | // pointer should have either of these values (Empty is null, deleted is at an invalid |
449 | // alignment for a GC cell, and in the zero page). |
450 | static constexpr int32_t ValueEmpty = 0x0; |
451 | static constexpr int32_t ValueDeleted = 0x4; |
452 | |
453 | static constexpr int64_t WasmTag = OtherTag | 0x1; |
454 | static constexpr int64_t WasmMask = NumberTag | 0x7; |
455 | // We tag Wasm non-JSCell pointers with a 3 at the bottom. We can test if a 64-bit JSValue pattern |
456 | // is a Wasm callee by masking the upper 16 bits and the lower 3 bits, and seeing if |
457 | // the resulting value is 3. The full test is: x & WasmMask == WasmTag |
458 | // This works because the lower 3 bits of the non-number immediate values are as follows: |
459 | // undefined: 0b010 |
460 | // null: 0b010 |
461 | // true: 0b111 |
462 | // false: 0b110 |
463 | // The test rejects all of these because none have just the value 3 in their lower 3 bits. |
464 | // The test rejects all numbers because they have non-zero upper 16 bits. |
465 | // The test also rejects normal cells because they won't have the number 3 as |
466 | // their lower 3 bits. Note, this bit pattern also allows the normal JSValue isCell(), etc, |
467 | // predicates to work on a Wasm::Callee because the various tests will fail if you |
468 | // bit casted a boxed Wasm::Callee* to a JSValue. isCell() would fail since it sees |
469 | // OtherTag. The other tests also trivially fail, since it won't be a number, |
470 | // and it won't be equal to null, undefined, true, or false. The isBoolean() predicate |
471 | // will fail because we won't have BoolTag set. |
472 | #endif |
473 | |
474 | private: |
475 | template <class T> JSValue(WriteBarrierBase<T, WriteBarrierTraitsSelect<T>>); |
476 | |
477 | enum HashTableDeletedValueTag { HashTableDeletedValue }; |
478 | JSValue(HashTableDeletedValueTag); |
479 | |
480 | inline const JSValue asValue() const { return *this; } |
481 | JS_EXPORT_PRIVATE double toNumberSlowCase(JSGlobalObject*) const; |
482 | JS_EXPORT_PRIVATE JSString* toStringSlowCase(JSGlobalObject*, bool returnEmptyStringOnError) const; |
483 | JS_EXPORT_PRIVATE WTF::String toWTFStringSlowCase(JSGlobalObject*) const; |
484 | JS_EXPORT_PRIVATE JSObject* toObjectSlowCase(JSGlobalObject*) const; |
485 | JS_EXPORT_PRIVATE JSValue toThisSlowCase(JSGlobalObject*, ECMAMode) const; |
486 | |
487 | EncodedValueDescriptor u; |
488 | }; |
489 | |
490 | typedef IntHash<EncodedJSValue> EncodedJSValueHash; |
491 | |
492 | #if USE(JSVALUE32_64) |
493 | struct EncodedJSValueHashTraits : HashTraits<EncodedJSValue> { |
494 | static constexpr bool emptyValueIsZero = false; |
495 | static EncodedJSValue emptyValue() { return JSValue::encode(JSValue()); } |
496 | static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); } |
497 | static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); } |
498 | }; |
499 | #else |
500 | struct EncodedJSValueHashTraits : HashTraits<EncodedJSValue> { |
501 | static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); } |
502 | static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); } |
503 | }; |
504 | #endif |
505 | |
506 | typedef std::pair<EncodedJSValue, SourceCodeRepresentation> EncodedJSValueWithRepresentation; |
507 | |
508 | struct EncodedJSValueWithRepresentationHashTraits : HashTraits<EncodedJSValueWithRepresentation> { |
509 | static constexpr bool emptyValueIsZero = false; |
510 | static EncodedJSValueWithRepresentation emptyValue() { return std::make_pair(JSValue::encode(JSValue()), SourceCodeRepresentation::Other); } |
511 | static void constructDeletedValue(EncodedJSValueWithRepresentation& slot) { slot = std::make_pair(JSValue::encode(JSValue(JSValue::HashTableDeletedValue)), SourceCodeRepresentation::Other); } |
512 | static bool isDeletedValue(EncodedJSValueWithRepresentation value) { return value == std::make_pair(JSValue::encode(JSValue(JSValue::HashTableDeletedValue)), SourceCodeRepresentation::Other); } |
513 | }; |
514 | |
515 | struct EncodedJSValueWithRepresentationHash { |
516 | static unsigned hash(const EncodedJSValueWithRepresentation& value) |
517 | { |
518 | return WTF::pairIntHash(EncodedJSValueHash::hash(value.first), IntHash<SourceCodeRepresentation>::hash(value.second)); |
519 | } |
520 | static bool equal(const EncodedJSValueWithRepresentation& a, const EncodedJSValueWithRepresentation& b) |
521 | { |
522 | return a == b; |
523 | } |
524 | static constexpr bool safeToCompareToEmptyOrDeleted = true; |
525 | }; |
526 | |
527 | // Stand-alone helper functions. |
528 | inline JSValue jsNull() |
529 | { |
530 | return JSValue(JSValue::JSNull); |
531 | } |
532 | |
533 | inline JSValue jsUndefined() |
534 | { |
535 | return JSValue(JSValue::JSUndefined); |
536 | } |
537 | |
538 | inline JSValue jsTDZValue() |
539 | { |
540 | return JSValue(); |
541 | } |
542 | |
543 | inline JSValue jsBoolean(bool b) |
544 | { |
545 | return b ? JSValue(JSValue::JSTrue) : JSValue(JSValue::JSFalse); |
546 | } |
547 | |
548 | ALWAYS_INLINE JSValue jsDoubleNumber(double d) |
549 | { |
550 | ASSERT(JSValue(JSValue::EncodeAsDouble, d).isNumber()); |
551 | return JSValue(JSValue::EncodeAsDouble, d); |
552 | } |
553 | |
554 | ALWAYS_INLINE JSValue jsNumber(double d) |
555 | { |
556 | ASSERT(JSValue(d).isNumber()); |
557 | ASSERT(!isImpureNaN(d)); |
558 | return JSValue(d); |
559 | } |
560 | |
561 | ALWAYS_INLINE JSValue jsNumber(const MediaTime& t) |
562 | { |
563 | return jsNumber(t.toDouble()); |
564 | } |
565 | |
566 | ALWAYS_INLINE JSValue jsNumber(char i) |
567 | { |
568 | return JSValue(i); |
569 | } |
570 | |
571 | ALWAYS_INLINE JSValue jsNumber(unsigned char i) |
572 | { |
573 | return JSValue(i); |
574 | } |
575 | |
576 | ALWAYS_INLINE JSValue jsNumber(short i) |
577 | { |
578 | return JSValue(i); |
579 | } |
580 | |
581 | ALWAYS_INLINE JSValue jsNumber(unsigned short i) |
582 | { |
583 | return JSValue(i); |
584 | } |
585 | |
586 | ALWAYS_INLINE JSValue jsNumber(int i) |
587 | { |
588 | return JSValue(i); |
589 | } |
590 | |
591 | ALWAYS_INLINE JSValue jsNumber(unsigned i) |
592 | { |
593 | return JSValue(i); |
594 | } |
595 | |
596 | ALWAYS_INLINE JSValue jsNumber(long i) |
597 | { |
598 | return JSValue(i); |
599 | } |
600 | |
601 | ALWAYS_INLINE JSValue jsNumber(unsigned long i) |
602 | { |
603 | return JSValue(i); |
604 | } |
605 | |
606 | ALWAYS_INLINE JSValue jsNumber(long long i) |
607 | { |
608 | return JSValue(i); |
609 | } |
610 | |
611 | ALWAYS_INLINE JSValue jsNumber(unsigned long long i) |
612 | { |
613 | return JSValue(i); |
614 | } |
615 | |
616 | ALWAYS_INLINE EncodedJSValue encodedJSUndefined() |
617 | { |
618 | return JSValue::encode(jsUndefined()); |
619 | } |
620 | |
621 | ALWAYS_INLINE EncodedJSValue encodedJSValue() |
622 | { |
623 | return JSValue::encode(JSValue()); |
624 | } |
625 | |
626 | inline bool operator==(const JSValue a, const JSCell* b) { return a == JSValue(b); } |
627 | inline bool operator==(const JSCell* a, const JSValue b) { return JSValue(a) == b; } |
628 | |
629 | inline bool operator!=(const JSValue a, const JSCell* b) { return a != JSValue(b); } |
630 | inline bool operator!=(const JSCell* a, const JSValue b) { return JSValue(a) != b; } |
631 | |
632 | |
633 | bool isThisValueAltered(const PutPropertySlot&, JSObject* baseObject); |
634 | |
635 | // See section 7.2.9: https://tc39.github.io/ecma262/#sec-samevalue |
636 | bool sameValue(JSGlobalObject*, JSValue a, JSValue b); |
637 | |
638 | } // namespace JSC |
639 | |