| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* JS::Value implementation. */ |
| 8 |
|
| 9 |
#ifndef js_Value_h |
| 10 |
#define js_Value_h |
| 11 |
|
| 12 |
#include "mozilla/Attributes.h" |
| 13 |
#include "mozilla/Casting.h" |
| 14 |
#include "mozilla/FloatingPoint.h" |
| 15 |
#include "mozilla/Likely.h" |
| 16 |
#include "mozilla/Maybe.h" |
| 17 |
|
| 18 |
#include <limits> /* for std::numeric_limits */ |
| 19 |
#include <type_traits> |
| 20 |
|
| 21 |
#include "jstypes.h" |
| 22 |
|
| 23 |
#include "js/HeapAPI.h" |
| 24 |
#include "js/RootingAPI.h" |
| 25 |
#include "js/TypeDecls.h" |
| 26 |
|
| 27 |
namespace JS { |
| 28 |
class JS_PUBLIC_API Value; |
| 29 |
} |
| 30 |
|
| 31 |
// [SMDOC] JS::Value Boxing Formats |
| 32 |
// |
| 33 |
// JS::Value is a 64-bit value, on all architectures. It is conceptually a |
| 34 |
// discriminated union of all the types of values that can be represented in SM: |
| 35 |
// - Object Pointers |
| 36 |
// - 64 bit IEEE 754 floats |
| 37 |
// - 32-bit integer values |
| 38 |
// - and quite a few more (see JSValueType) |
| 39 |
// |
| 40 |
// The ECMAScript standard specifies that ECMAScript numbers are IEEE 64-bit |
| 41 |
// floating-point values. A JS::Value can represent any JavaScript number |
| 42 |
// value directly, without referring to additional storage, or represent an |
| 43 |
// object, string, or other ECMAScript value, and remember which type it is. |
| 44 |
// |
| 45 |
// This may seem surprising: how can a 64-bit type hold all the 64-bit IEEE |
| 46 |
// values, and still distinguish them from objects, strings, and so on, |
| 47 |
// which have 64-bit addresses ? |
| 48 |
// |
| 49 |
// This is possible for two reasons: |
| 50 |
// |
| 51 |
// - First, ECMAScript implementations aren't required to distinguish all |
| 52 |
// the values the IEEE 64-bit format can represent. |
| 53 |
// |
| 54 |
// The IEEE 754 format for floating point numbers specifies that every |
| 55 |
// floating-point value whose 11-bit exponent field is all ones, and whose |
| 56 |
// 52-bit fraction field is non-zero, has the value NaN. EMCAScript requires |
| 57 |
// only one NaN value. This means we can use one IEEE NaN to represent |
| 58 |
// ECMAScript's NaN, and use all the other 2^52-2 NaN bitstrings to |
| 59 |
// represent the other ECMAScript values. |
| 60 |
// |
| 61 |
// - Second, on the 64 bit architectures we suppport, only the |
| 62 |
// lower 48 bits of an address are currently significant. The upper sixteen |
| 63 |
// bits are required to be the sign-extension of bit 48. Furthermore, user |
| 64 |
// code always runs in "positive addresses": those in which bit 48 is zero. So |
| 65 |
// we only actually need 47 bits to store all possible object or string |
| 66 |
// addresses, even on 64-bit platforms. |
| 67 |
// |
| 68 |
// Our memory initialization system ensures that all pointers we will store in |
| 69 |
// objects use only 47 bits. See js::gc::MapAlignedPagesRandom. |
| 70 |
// |
| 71 |
// The introduction of 5-level page tables, supporting 57-bit virtual |
| 72 |
// addresses, is a potential complication. For now, large addresses are |
| 73 |
// opt-in, and we simply don't use them. |
| 74 |
// |
| 75 |
// With a 52-bit fraction field, and 47 bits needed for the 'payload', we |
| 76 |
// have up to five bits left to store a 'tag' value, to indicate which |
| 77 |
// branch of our discriminated union is live. (In practice, one of those |
| 78 |
// bits is used up to simplify NaN representation; see micro-optimization 5 |
| 79 |
// below.) |
| 80 |
// |
| 81 |
// Thus, we define JS::Value representations in terms of the IEEE 64-bit |
| 82 |
// floating-point format: |
| 83 |
// |
| 84 |
// - Any bitstring that IEEE calls a number or an infinity represents that |
| 85 |
// ECMAScript number. |
| 86 |
// |
| 87 |
// - Any bitstring that IEEE calls a NaN represents either an ECMAScript NaN |
| 88 |
// or a non-number ECMAScript value, as determined by a tag field stored |
| 89 |
// towards the most significant end of the fraction field (exactly where |
| 90 |
// depends on the address size). If the tag field indicates that this |
| 91 |
// JS::Value is an object, the fraction field's least significant end |
| 92 |
// holds the address of a JSObject; if a string, the address of a |
| 93 |
// JSString; and so on. |
| 94 |
// |
| 95 |
// To enforce this invariant, anywhere that may provide a numerical value |
| 96 |
// which may have a non-canonical NaN value (NaN, but not the one we've chosen |
| 97 |
// for ECMAScript) we must convert that to the canonical NaN. See |
| 98 |
// JS::CanonicalizeNaN. |
| 99 |
// |
| 100 |
// We have two boxing modes defined: NUNBOX32 and PUNBOX64.The first is |
| 101 |
// "NaN unboxed boxing" (or Nunboxing), as non-Number payload are stored |
| 102 |
// unaltered in the lower bits. The second is "Packed NaN boxing" (or |
| 103 |
// punboxing), which is 'logically like nunboxing, but with all the unused bits |
| 104 |
// sucked out' [1], as we rely on unused bits of the payload to pack the |
| 105 |
// payload in the lower bits using Nunboxing. |
| 106 |
// |
| 107 |
// - In NUNBOX32 the tag is stored in the least-significant bits of the high |
| 108 |
// word of the NaN. Since it's used on 32-bit systems, this has the nice |
| 109 |
// property that boxed values are simply stored in the low-word of the 8-byte |
| 110 |
// NaN. |
| 111 |
// |
| 112 |
// - In PUNBOX64, since we need to store more pointer bits (47, see above), the |
| 113 |
// tag is stored in the 5 most significant bits of the fraction adjacent to |
| 114 |
// the exponent. |
| 115 |
// |
| 116 |
// Tag values are carefully ordered to support a set of micro-optimizations. In |
| 117 |
// particular: |
| 118 |
// |
| 119 |
// 1. Object is the highest tag, to simplify isPrimitive checks. (See |
| 120 |
// ValueUpperExclPrimitiveTag) |
| 121 |
// 2. Numbers (Double and Int32) are the lowest tags, to simplify isNumber |
| 122 |
// checks. (See ValueUpperInclNumberTag) |
| 123 |
// 3. Non-GC tags are ordered before GC-tags, to simplify isGCThing checks. (See |
| 124 |
// ValueLowerInclGCThingTag) |
| 125 |
// 4. The tags for Object and Null differ by a single flipped bit, to simplify |
| 126 |
// toObjectOrNull. (See ValueObjectOrNullBit) |
| 127 |
// 5. In PUNBOX64, the most significant bit of every non-Double tag is always |
| 128 |
// set. This is to simplify isDouble checks. Note that the highest bitstring |
| 129 |
// that corresponds to a non-NaN double is -Infinity: |
| 130 |
// 0xfff0_0000_0000_0000 |
| 131 |
// But the canonical hardware NaN (produced by, for example, 0/0) is: |
| 132 |
// 0x?ff8_0000_0000_0000 |
| 133 |
// on all platforms with JIT support*. (The most significant bit is the sign |
| 134 |
// bit; it is 1 on x86, but 0 on ARM.) The most significant bit of the |
| 135 |
// fraction field is set, which corresponds to the most significant of the 5 |
| 136 |
// tag bits. Because we only use tags that have the high bit set, any Value |
| 137 |
// represented by a bitstring less than or equal to 0xfff8_..._0000 is a |
| 138 |
// Double. (If we wanted to use all five bits, we could define 0x10 as |
| 139 |
// JSVAL_TYPE_NAN, and mask off the most significant bit of the tag for |
| 140 |
// IsDouble checks. This is not yet necessary, because we still have room |
| 141 |
// left to allocate new tags.) |
| 142 |
// |
| 143 |
// * But see JS_NONCANONICAL_HARDWARE_NAN below. |
| 144 |
// |
| 145 |
// [1]: |
| 146 |
// https://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations#969f63bbe4eb912778c9da85feb0f5763e7a7862 |
| 147 |
|
| 148 |
/* JS::Value can store a full int32_t. */ |
| 149 |
#define JSVAL_INT_BITS 32 |
| 150 |
#define JSVAL_INT_MIN ((int32_t)0x80000000) |
| 151 |
#define JSVAL_INT_MAX ((int32_t)0x7fffffff) |
| 152 |
|
| 153 |
#if defined(JS_NUNBOX32) |
| 154 |
# define JSVAL_TAG_SHIFT 32 |
| 155 |
#elif defined(JS_PUNBOX64) |
| 156 |
# define JSVAL_TAG_SHIFT 47 |
| 157 |
#endif |
| 158 |
|
| 159 |
// Use enums so that printing a JS::Value in the debugger shows nice |
| 160 |
// symbolic type tags. |
| 161 |
|
| 162 |
enum JSValueType : uint8_t { |
| 163 |
JSVAL_TYPE_DOUBLE = 0x00, |
| 164 |
JSVAL_TYPE_INT32 = 0x01, |
| 165 |
JSVAL_TYPE_BOOLEAN = 0x02, |
| 166 |
JSVAL_TYPE_UNDEFINED = 0x03, |
| 167 |
JSVAL_TYPE_NULL = 0x04, |
| 168 |
JSVAL_TYPE_MAGIC = 0x05, |
| 169 |
JSVAL_TYPE_STRING = 0x06, |
| 170 |
JSVAL_TYPE_SYMBOL = 0x07, |
| 171 |
JSVAL_TYPE_PRIVATE_GCTHING = 0x08, |
| 172 |
JSVAL_TYPE_BIGINT = 0x09, |
| 173 |
JSVAL_TYPE_OBJECT = 0x0c, |
| 174 |
|
| 175 |
// This type never appears in a Value; it's only an out-of-band value. |
| 176 |
JSVAL_TYPE_UNKNOWN = 0x20 |
| 177 |
}; |
| 178 |
|
| 179 |
namespace JS { |
| 180 |
enum class ValueType : uint8_t { |
| 181 |
Double = JSVAL_TYPE_DOUBLE, |
| 182 |
Int32 = JSVAL_TYPE_INT32, |
| 183 |
Boolean = JSVAL_TYPE_BOOLEAN, |
| 184 |
Undefined = JSVAL_TYPE_UNDEFINED, |
| 185 |
Null = JSVAL_TYPE_NULL, |
| 186 |
Magic = JSVAL_TYPE_MAGIC, |
| 187 |
String = JSVAL_TYPE_STRING, |
| 188 |
Symbol = JSVAL_TYPE_SYMBOL, |
| 189 |
PrivateGCThing = JSVAL_TYPE_PRIVATE_GCTHING, |
| 190 |
BigInt = JSVAL_TYPE_BIGINT, |
| 191 |
Object = JSVAL_TYPE_OBJECT, |
| 192 |
}; |
| 193 |
} // namespace JS |
| 194 |
|
| 195 |
static_assert(sizeof(JSValueType) == 1, |
| 196 |
"compiler typed enum support is apparently buggy"); |
| 197 |
|
| 198 |
#if defined(JS_NUNBOX32) |
| 199 |
|
| 200 |
enum JSValueTag : uint32_t { |
| 201 |
JSVAL_TAG_CLEAR = 0xFFFFFF80, |
| 202 |
JSVAL_TAG_INT32 = JSVAL_TAG_CLEAR | JSVAL_TYPE_INT32, |
| 203 |
JSVAL_TAG_UNDEFINED = JSVAL_TAG_CLEAR | JSVAL_TYPE_UNDEFINED, |
| 204 |
JSVAL_TAG_NULL = JSVAL_TAG_CLEAR | JSVAL_TYPE_NULL, |
| 205 |
JSVAL_TAG_BOOLEAN = JSVAL_TAG_CLEAR | JSVAL_TYPE_BOOLEAN, |
| 206 |
JSVAL_TAG_MAGIC = JSVAL_TAG_CLEAR | JSVAL_TYPE_MAGIC, |
| 207 |
JSVAL_TAG_STRING = JSVAL_TAG_CLEAR | JSVAL_TYPE_STRING, |
| 208 |
JSVAL_TAG_SYMBOL = JSVAL_TAG_CLEAR | JSVAL_TYPE_SYMBOL, |
| 209 |
JSVAL_TAG_PRIVATE_GCTHING = JSVAL_TAG_CLEAR | JSVAL_TYPE_PRIVATE_GCTHING, |
| 210 |
JSVAL_TAG_BIGINT = JSVAL_TAG_CLEAR | JSVAL_TYPE_BIGINT, |
| 211 |
JSVAL_TAG_OBJECT = JSVAL_TAG_CLEAR | JSVAL_TYPE_OBJECT |
| 212 |
}; |
| 213 |
|
| 214 |
static_assert(sizeof(JSValueTag) == sizeof(uint32_t), |
| 215 |
"compiler typed enum support is apparently buggy"); |
| 216 |
|
| 217 |
#elif defined(JS_PUNBOX64) |
| 218 |
|
| 219 |
enum JSValueTag : uint32_t { |
| 220 |
JSVAL_TAG_MAX_DOUBLE = 0x1FFF0, |
| 221 |
JSVAL_TAG_INT32 = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_INT32, |
| 222 |
JSVAL_TAG_UNDEFINED = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_UNDEFINED, |
| 223 |
JSVAL_TAG_NULL = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_NULL, |
| 224 |
JSVAL_TAG_BOOLEAN = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_BOOLEAN, |
| 225 |
JSVAL_TAG_MAGIC = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_MAGIC, |
| 226 |
JSVAL_TAG_STRING = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_STRING, |
| 227 |
JSVAL_TAG_SYMBOL = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_SYMBOL, |
| 228 |
JSVAL_TAG_PRIVATE_GCTHING = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_PRIVATE_GCTHING, |
| 229 |
JSVAL_TAG_BIGINT = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_BIGINT, |
| 230 |
JSVAL_TAG_OBJECT = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_OBJECT |
| 231 |
}; |
| 232 |
|
| 233 |
static_assert(sizeof(JSValueTag) == sizeof(uint32_t), |
| 234 |
"compiler typed enum support is apparently buggy"); |
| 235 |
|
| 236 |
enum JSValueShiftedTag : uint64_t { |
| 237 |
// See Bug 584653 for why we include 0xFFFFFFFF. |
| 238 |
JSVAL_SHIFTED_TAG_MAX_DOUBLE = |
| 239 |
((uint64_t(JSVAL_TAG_MAX_DOUBLE) << JSVAL_TAG_SHIFT) | 0xFFFFFFFF), |
| 240 |
JSVAL_SHIFTED_TAG_INT32 = (uint64_t(JSVAL_TAG_INT32) << JSVAL_TAG_SHIFT), |
| 241 |
JSVAL_SHIFTED_TAG_UNDEFINED = |
| 242 |
(uint64_t(JSVAL_TAG_UNDEFINED) << JSVAL_TAG_SHIFT), |
| 243 |
JSVAL_SHIFTED_TAG_NULL = (uint64_t(JSVAL_TAG_NULL) << JSVAL_TAG_SHIFT), |
| 244 |
JSVAL_SHIFTED_TAG_BOOLEAN = (uint64_t(JSVAL_TAG_BOOLEAN) << JSVAL_TAG_SHIFT), |
| 245 |
JSVAL_SHIFTED_TAG_MAGIC = (uint64_t(JSVAL_TAG_MAGIC) << JSVAL_TAG_SHIFT), |
| 246 |
JSVAL_SHIFTED_TAG_STRING = (uint64_t(JSVAL_TAG_STRING) << JSVAL_TAG_SHIFT), |
| 247 |
JSVAL_SHIFTED_TAG_SYMBOL = (uint64_t(JSVAL_TAG_SYMBOL) << JSVAL_TAG_SHIFT), |
| 248 |
JSVAL_SHIFTED_TAG_PRIVATE_GCTHING = |
| 249 |
(uint64_t(JSVAL_TAG_PRIVATE_GCTHING) << JSVAL_TAG_SHIFT), |
| 250 |
JSVAL_SHIFTED_TAG_BIGINT = (uint64_t(JSVAL_TAG_BIGINT) << JSVAL_TAG_SHIFT), |
| 251 |
JSVAL_SHIFTED_TAG_OBJECT = (uint64_t(JSVAL_TAG_OBJECT) << JSVAL_TAG_SHIFT) |
| 252 |
}; |
| 253 |
|
| 254 |
static_assert(sizeof(JSValueShiftedTag) == sizeof(uint64_t), |
| 255 |
"compiler typed enum support is apparently buggy"); |
| 256 |
|
| 257 |
#endif |
| 258 |
|
| 259 |
namespace JS { |
| 260 |
namespace detail { |
| 261 |
|
| 262 |
#if defined(JS_NUNBOX32) |
| 263 |
|
| 264 |
constexpr JSValueTag ValueTypeToTag(JSValueType type) { |
| 265 |
return static_cast<JSValueTag>(JSVAL_TAG_CLEAR | |
| 266 |
std::underlying_type_t<JSValueType>(type)); |
| 267 |
} |
| 268 |
|
| 269 |
constexpr bool ValueIsDouble(uint64_t bits) { |
| 270 |
return uint32_t(bits >> JSVAL_TAG_SHIFT) <= uint32_t(JSVAL_TAG_CLEAR); |
| 271 |
} |
| 272 |
|
| 273 |
constexpr JSValueTag ValueUpperExclPrimitiveTag = JSVAL_TAG_OBJECT; |
| 274 |
constexpr JSValueTag ValueUpperInclNumberTag = JSVAL_TAG_INT32; |
| 275 |
constexpr JSValueTag ValueLowerInclGCThingTag = JSVAL_TAG_STRING; |
| 276 |
|
| 277 |
#elif defined(JS_PUNBOX64) |
| 278 |
|
| 279 |
constexpr JSValueTag ValueTypeToTag(JSValueType type) { |
| 280 |
return static_cast<JSValueTag>(JSVAL_TAG_MAX_DOUBLE | |
| 281 |
std::underlying_type_t<JSValueType>(type)); |
| 282 |
} |
| 283 |
|
| 284 |
constexpr bool ValueIsDouble(uint64_t bits) { |
| 285 |
return bits <= JSVAL_SHIFTED_TAG_MAX_DOUBLE; |
| 286 |
} |
| 287 |
|
| 288 |
constexpr uint64_t ValueTagMask = 0xFFFF'8000'0000'0000; |
| 289 |
|
| 290 |
// This should only be used in toGCThing. See the 'Spectre mitigations' comment. |
| 291 |
constexpr uint64_t ValueGCThingPayloadMask = 0x0000'7FFF'FFFF'FFFF; |
| 292 |
|
| 293 |
// Mask used to combine an unbox operation with getting the chunk base. |
| 294 |
constexpr uint64_t ValueGCThingPayloadChunkMask = |
| 295 |
ValueGCThingPayloadMask & ~js::gc::ChunkMask; |
| 296 |
|
| 297 |
constexpr uint64_t ValueTypeToShiftedTag(JSValueType type) { |
| 298 |
return static_cast<uint64_t>(ValueTypeToTag(type)) << JSVAL_TAG_SHIFT; |
| 299 |
} |
| 300 |
# define JSVAL_TYPE_TO_SHIFTED_TAG(type) \ |
| 301 |
(JS::detail::ValueTypeToShiftedTag(type)) |
| 302 |
|
| 303 |
constexpr JSValueTag ValueUpperExclPrimitiveTag = JSVAL_TAG_OBJECT; |
| 304 |
constexpr JSValueTag ValueUpperInclNumberTag = JSVAL_TAG_INT32; |
| 305 |
constexpr JSValueTag ValueLowerInclGCThingTag = JSVAL_TAG_STRING; |
| 306 |
|
| 307 |
constexpr uint64_t ValueUpperExclShiftedPrimitiveTag = JSVAL_SHIFTED_TAG_OBJECT; |
| 308 |
constexpr uint64_t ValueUpperExclShiftedNumberTag = JSVAL_SHIFTED_TAG_BOOLEAN; |
| 309 |
constexpr uint64_t ValueLowerInclShiftedGCThingTag = JSVAL_SHIFTED_TAG_STRING; |
| 310 |
|
| 311 |
// JSVAL_TYPE_OBJECT and JSVAL_TYPE_NULL differ by one bit. We can use this to |
| 312 |
// implement toObjectOrNull more efficiently. |
| 313 |
constexpr uint64_t ValueObjectOrNullBit = 0x8ULL << JSVAL_TAG_SHIFT; |
| 314 |
static_assert( |
| 315 |
(JSVAL_SHIFTED_TAG_NULL ^ JSVAL_SHIFTED_TAG_OBJECT) == ValueObjectOrNullBit, |
| 316 |
"ValueObjectOrNullBit must be consistent with object and null tags"); |
| 317 |
|
| 318 |
constexpr uint64_t IsValidUserModePointer(uint64_t bits) { |
| 319 |
// All 64-bit platforms that we support actually have a 48-bit address space |
| 320 |
// for user-mode pointers, with the top 16 bits all set to zero. |
| 321 |
return (bits & 0xFFFF'0000'0000'0000) == 0; |
| 322 |
} |
| 323 |
|
| 324 |
#endif /* JS_PUNBOX64 */ |
| 325 |
|
| 326 |
} // namespace detail |
| 327 |
} // namespace JS |
| 328 |
|
| 329 |
#define JSVAL_TYPE_TO_TAG(type) (JS::detail::ValueTypeToTag(type)) |
| 330 |
|
| 331 |
enum JSWhyMagic { |
| 332 |
/** a hole in a native object's elements */ |
| 333 |
JS_ELEMENTS_HOLE, |
| 334 |
|
| 335 |
/** there is not a pending iterator value */ |
| 336 |
JS_NO_ITER_VALUE, |
| 337 |
|
| 338 |
/** exception value thrown when closing a generator */ |
| 339 |
JS_GENERATOR_CLOSING, |
| 340 |
|
| 341 |
/** used in debug builds to catch tracing errors */ |
| 342 |
JS_ARG_POISON, |
| 343 |
|
| 344 |
/** an empty subnode in the AST serializer */ |
| 345 |
JS_SERIALIZE_NO_NODE, |
| 346 |
|
| 347 |
/** magic value passed to natives to indicate construction */ |
| 348 |
JS_IS_CONSTRUCTING, |
| 349 |
|
| 350 |
/** see class js::HashableValue */ |
| 351 |
JS_HASH_KEY_EMPTY, |
| 352 |
|
| 353 |
/** error while running Ion code */ |
| 354 |
JS_ION_ERROR, |
| 355 |
|
| 356 |
/** missing recover instruction result */ |
| 357 |
JS_ION_BAILOUT, |
| 358 |
|
| 359 |
/** optimized out slot */ |
| 360 |
JS_OPTIMIZED_OUT, |
| 361 |
|
| 362 |
/** uninitialized lexical bindings that produce ReferenceError on touch. */ |
| 363 |
JS_UNINITIALIZED_LEXICAL, |
| 364 |
|
| 365 |
/** arguments object can't be created because environment is dead. */ |
| 366 |
JS_MISSING_ARGUMENTS, |
| 367 |
|
| 368 |
/** exception value thrown when interrupting irregexp */ |
| 369 |
JS_INTERRUPT_REGEXP, |
| 370 |
|
| 371 |
/** for local use */ |
| 372 |
JS_GENERIC_MAGIC, |
| 373 |
|
| 374 |
/** |
| 375 |
* When an error object is created without the error cause argument, we set |
| 376 |
* the error's cause slot to this magic value. |
| 377 |
*/ |
| 378 |
JS_ERROR_WITHOUT_CAUSE, |
| 379 |
|
| 380 |
JS_WHY_MAGIC_COUNT |
| 381 |
}; |
| 382 |
|
| 383 |
namespace js { |
| 384 |
class JS_PUBLIC_API GenericPrinter; |
| 385 |
class JSONPrinter; |
| 386 |
|
| 387 |
static inline JS::Value PoisonedObjectValue(uintptr_t poison); |
| 388 |
|
| 389 |
} // namespace js |
| 390 |
|
| 391 |
namespace JS { |
| 392 |
|
| 393 |
namespace detail { |
| 394 |
|
| 395 |
// IEEE-754 bit pattern for double-precision positive infinity. |
| 396 |
constexpr int InfinitySignBit = 0; |
| 397 |
constexpr uint64_t InfinityBits = |
| 398 |
mozilla::InfinityBits<double, detail::InfinitySignBit>::value; |
| 399 |
|
| 400 |
// This is a quiet NaN on IEEE-754[2008] compatible platforms, including X86, |
| 401 |
// ARM, SPARC, RISC-V and modern MIPS. |
| 402 |
// |
| 403 |
// Note: The default sign bit for a hardware synthesized NaN differs between X86 |
| 404 |
// and ARM. Both values are considered compatible values on both |
| 405 |
// platforms. |
| 406 |
constexpr int CanonicalizedNaNSignBit = 0; |
| 407 |
constexpr uint64_t CanonicalizedNaNSignificand = 0x8000000000000; |
| 408 |
|
| 409 |
#if defined(__sparc__) |
| 410 |
// Some architectures (not to name names) generate NaNs with bit patterns that |
| 411 |
// are incompatible with JS::Value's bit pattern restrictions. Instead we must |
| 412 |
// canonicalize all hardware values before storing in JS::Value. |
| 413 |
# define JS_NONCANONICAL_HARDWARE_NAN |
| 414 |
#endif |
| 415 |
|
| 416 |
#if defined(__mips__) && !defined(__mips_nan_2008) |
| 417 |
// These builds may run on hardware that has differing polarity of the signaling |
| 418 |
// NaN bit. While the kernel may handle the trap for us, it is a performance |
| 419 |
// issue so instead we compute the NaN to use on startup. The runtime value must |
| 420 |
// still meet `ValueIsDouble` requirements which are checked on startup. |
| 421 |
|
| 422 |
// In particular, we expect one of the following values on MIPS: |
| 423 |
// - 0x7FF7FFFFFFFFFFFF Legacy |
| 424 |
// - 0x7FF8000000000000 IEEE-754[2008] |
| 425 |
# define JS_RUNTIME_CANONICAL_NAN |
| 426 |
#endif |
| 427 |
|
| 428 |
#if defined(JS_RUNTIME_CANONICAL_NAN) |
| 429 |
extern JS_PUBLIC_API uint64_t CanonicalizedNaNBits; |
| 430 |
#else |
| 431 |
constexpr uint64_t CanonicalizedNaNBits = |
| 432 |
mozilla::SpecificNaNBits<double, detail::CanonicalizedNaNSignBit, |
| 433 |
detail::CanonicalizedNaNSignificand>::value; |
| 434 |
#endif |
| 435 |
} // namespace detail |
| 436 |
|
| 437 |
// Return a quiet NaN that is compatible with JS::Value restrictions. |
| 438 |
static MOZ_ALWAYS_INLINE double GenericNaN() { |
| 439 |
#if !defined(JS_RUNTIME_CANONICAL_NAN) |
| 440 |
static_assert(detail::ValueIsDouble(detail::CanonicalizedNaNBits), |
| 441 |
"Canonical NaN must be compatible with JS::Value"); |
| 442 |
#endif |
| 443 |
|
| 444 |
return mozilla::BitwiseCast<double>(detail::CanonicalizedNaNBits); |
| 445 |
} |
| 446 |
|
| 447 |
// Return the infinity the engine uses |
| 448 |
static MOZ_ALWAYS_INLINE double Infinity() { |
| 449 |
return mozilla::BitwiseCast<double>(detail::InfinityBits); |
| 450 |
} |
| 451 |
|
| 452 |
// Convert an arbitrary double to one compatible with JS::Value representation |
| 453 |
// by replacing any NaN value with a canonical one. |
| 454 |
static MOZ_ALWAYS_INLINE double CanonicalizeNaN(double d) { |
| 455 |
if (MOZ_UNLIKELY(std::isnan(d))) { |
| 456 |
return GenericNaN(); |
| 457 |
} |
| 458 |
return d; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* [SMDOC] JS::Value type |
| 463 |
* |
| 464 |
* JS::Value is the interface for a single JavaScript Engine value. A few |
| 465 |
* general notes on JS::Value: |
| 466 |
* |
| 467 |
* - JS::Value has setX() and isX() members for X in |
| 468 |
* |
| 469 |
* { Int32, Double, String, Symbol, BigInt, Boolean, Undefined, Null, |
| 470 |
* Object, Magic } |
| 471 |
* |
| 472 |
* JS::Value also contains toX() for each of the non-singleton types. |
| 473 |
* |
| 474 |
* - Magic is a singleton type whose payload contains either a JSWhyMagic |
| 475 |
* "reason" for the magic value or a uint32_t value. By providing JSWhyMagic |
| 476 |
* values when creating and checking for magic values, it is possible to |
| 477 |
* assert, at runtime, that only magic values with the expected reason flow |
| 478 |
* through a particular value. For example, if cx->exception has a magic |
| 479 |
* value, the reason must be JS_GENERATOR_CLOSING. |
| 480 |
* |
| 481 |
* - The JS::Value operations are preferred. The JSVAL_* operations remain for |
| 482 |
* compatibility; they may be removed at some point. These operations mostly |
| 483 |
* provide similar functionality. But there are a few key differences. One |
| 484 |
* is that JS::Value gives null a separate type. |
| 485 |
* Also, to help prevent mistakenly boxing a nullable JSObject* as an object, |
| 486 |
* Value::setObject takes a JSObject&. (Conversely, Value::toObject returns a |
| 487 |
* JSObject&.) A convenience member Value::setObjectOrNull is provided. |
| 488 |
* |
| 489 |
* - Note that JS::Value is 8 bytes on 32 and 64-bit architectures. Thus, on |
| 490 |
* 32-bit user code should avoid copying jsval/JS::Value as much as possible, |
| 491 |
* preferring to pass by const Value&. |
| 492 |
* |
| 493 |
* Spectre mitigations |
| 494 |
* =================== |
| 495 |
* To mitigate Spectre attacks, we do the following: |
| 496 |
* |
| 497 |
* - On 64-bit platforms, when unboxing a Value, we XOR the bits with the |
| 498 |
* expected type tag (instead of masking the payload bits). This guarantees |
| 499 |
* that toString, toObject, toSymbol will return an invalid pointer (because |
| 500 |
* some high bits will be set) when called on a Value with a different type |
| 501 |
* tag. |
| 502 |
* |
| 503 |
* - On 32-bit platforms,when unboxing an object/string/symbol Value, we use a |
| 504 |
* conditional move (not speculated) to zero the payload register if the type |
| 505 |
* doesn't match. |
| 506 |
*/ |
| 507 |
class alignas(8) Value { |
| 508 |
private: |
| 509 |
uint64_t asBits_; |
| 510 |
|
| 511 |
public: |
| 512 |
constexpr Value() : asBits_(bitsFromTagAndPayload(JSVAL_TAG_UNDEFINED, 0)) {} |
| 513 |
|
| 514 |
private: |
| 515 |
explicit constexpr Value(uint64_t asBits) : asBits_(asBits) {} |
| 516 |
|
| 517 |
static uint64_t bitsFromDouble(double d) { |
| 518 |
#if defined(JS_NONCANONICAL_HARDWARE_NAN) |
| 519 |
d = CanonicalizeNaN(d); |
| 520 |
#endif |
| 521 |
return mozilla::BitwiseCast<uint64_t>(d); |
| 522 |
} |
| 523 |
|
| 524 |
static_assert(sizeof(JSValueType) == 1, |
| 525 |
"type bits must fit in a single byte"); |
| 526 |
static_assert(sizeof(JSValueTag) == 4, |
| 527 |
"32-bit Value's tag_ must have size 4 to complement the " |
| 528 |
"payload union's size 4"); |
| 529 |
static_assert(sizeof(JSWhyMagic) <= 4, |
| 530 |
"32-bit Value's JSWhyMagic payload field must not inflate " |
| 531 |
"the payload beyond 4 bytes"); |
| 532 |
|
| 533 |
public: |
| 534 |
#if defined(JS_NUNBOX32) |
| 535 |
using PayloadType = uint32_t; |
| 536 |
#elif defined(JS_PUNBOX64) |
| 537 |
using PayloadType = uint64_t; |
| 538 |
#endif |
| 539 |
|
| 540 |
static constexpr uint64_t bitsFromTagAndPayload(JSValueTag tag, |
| 541 |
PayloadType payload) { |
| 542 |
return (uint64_t(tag) << JSVAL_TAG_SHIFT) | payload; |
| 543 |
} |
| 544 |
|
| 545 |
static constexpr Value fromTagAndPayload(JSValueTag tag, |
| 546 |
PayloadType payload) { |
| 547 |
return fromRawBits(bitsFromTagAndPayload(tag, payload)); |
| 548 |
} |
| 549 |
|
| 550 |
static constexpr Value fromRawBits(uint64_t asBits) { return Value(asBits); } |
| 551 |
|
| 552 |
static constexpr Value fromInt32(int32_t i) { |
| 553 |
return fromTagAndPayload(JSVAL_TAG_INT32, uint32_t(i)); |
| 554 |
} |
| 555 |
|
| 556 |
static Value fromDouble(double d) { return fromRawBits(bitsFromDouble(d)); } |
| 557 |
|
| 558 |
/** |
| 559 |
* Returns false if creating a NumberValue containing the given type would |
| 560 |
* be lossy, true otherwise. |
| 561 |
*/ |
| 562 |
template <typename T> |
| 563 |
static bool isNumberRepresentable(const T t) { |
| 564 |
return T(double(t)) == t; |
| 565 |
} |
| 566 |
|
| 567 |
/*** Mutators ***/ |
| 568 |
|
| 569 |
void setNull() { |
| 570 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_NULL, 0); |
| 571 |
MOZ_ASSERT(isNull()); |
| 572 |
} |
| 573 |
|
| 574 |
void setUndefined() { |
| 575 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_UNDEFINED, 0); |
| 576 |
MOZ_ASSERT(isUndefined()); |
| 577 |
} |
| 578 |
|
| 579 |
void setInt32(int32_t i) { |
| 580 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_INT32, uint32_t(i)); |
| 581 |
MOZ_ASSERT(toInt32() == i); |
| 582 |
} |
| 583 |
|
| 584 |
void setDouble(double d) { |
| 585 |
asBits_ = bitsFromDouble(d); |
| 586 |
MOZ_ASSERT(isDouble()); |
| 587 |
} |
| 588 |
|
| 589 |
void setString(JSString* str) { |
| 590 |
MOZ_ASSERT(js::gc::IsCellPointerValid(str)); |
| 591 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_STRING, PayloadType(str)); |
| 592 |
MOZ_ASSERT(toString() == str); |
| 593 |
} |
| 594 |
|
| 595 |
void setSymbol(JS::Symbol* sym) { |
| 596 |
MOZ_ASSERT(js::gc::IsCellPointerValid(sym)); |
| 597 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_SYMBOL, PayloadType(sym)); |
| 598 |
MOZ_ASSERT(toSymbol() == sym); |
| 599 |
} |
| 600 |
|
| 601 |
void setBigInt(JS::BigInt* bi) { |
| 602 |
MOZ_ASSERT(js::gc::IsCellPointerValid(bi)); |
| 603 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_BIGINT, PayloadType(bi)); |
| 604 |
MOZ_ASSERT(toBigInt() == bi); |
| 605 |
} |
| 606 |
|
| 607 |
void setObject(JSObject& obj) { |
| 608 |
MOZ_ASSERT(js::gc::IsCellPointerValid(&obj)); |
| 609 |
setObjectNoCheck(&obj); |
| 610 |
MOZ_ASSERT(&toObject() == &obj); |
| 611 |
} |
| 612 |
|
| 613 |
void changeGCThingPayload(js::gc::Cell* cell) { |
| 614 |
MOZ_ASSERT(js::gc::IsCellPointerValid(cell)); |
| 615 |
#ifdef DEBUG |
| 616 |
assertTraceKindMatches(cell); |
| 617 |
#endif |
| 618 |
asBits_ = bitsFromTagAndPayload(toTag(), PayloadType(cell)); |
| 619 |
MOZ_ASSERT(toGCThing() == cell); |
| 620 |
} |
| 621 |
|
| 622 |
private: |
| 623 |
#ifdef DEBUG |
| 624 |
void assertTraceKindMatches(js::gc::Cell* cell) const; |
| 625 |
#endif |
| 626 |
|
| 627 |
void setObjectNoCheck(JSObject* obj) { |
| 628 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_OBJECT, PayloadType(obj)); |
| 629 |
} |
| 630 |
|
| 631 |
friend inline Value js::PoisonedObjectValue(uintptr_t poison); |
| 632 |
|
| 633 |
public: |
| 634 |
void setBoolean(bool b) { |
| 635 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_BOOLEAN, uint32_t(b)); |
| 636 |
MOZ_ASSERT(toBoolean() == b); |
| 637 |
} |
| 638 |
|
| 639 |
void setMagic(JSWhyMagic why) { |
| 640 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_MAGIC, uint32_t(why)); |
| 641 |
MOZ_ASSERT(whyMagic() == why); |
| 642 |
} |
| 643 |
|
| 644 |
void setMagicUint32(uint32_t payload) { |
| 645 |
MOZ_ASSERT(payload >= JS_WHY_MAGIC_COUNT, |
| 646 |
"This should only be used for non-standard magic values"); |
| 647 |
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_MAGIC, payload); |
| 648 |
MOZ_ASSERT(magicUint32() == payload); |
| 649 |
} |
| 650 |
|
| 651 |
void setNumber(float f) { |
| 652 |
int32_t i; |
| 653 |
if (mozilla::NumberIsInt32(f, &i)) { |
| 654 |
setInt32(i); |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
setDouble(double(f)); |
| 659 |
} |
| 660 |
|
| 661 |
void setNumber(double d) { |
| 662 |
int32_t i; |
| 663 |
if (mozilla::NumberIsInt32(d, &i)) { |
| 664 |
setInt32(i); |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
setDouble(d); |
| 669 |
} |
| 670 |
|
| 671 |
template <typename T> |
| 672 |
void setNumber(const T t) { |
| 673 |
static_assert(std::is_integral<T>::value, "must be integral type"); |
| 674 |
MOZ_ASSERT(isNumberRepresentable(t), "value creation would be lossy"); |
| 675 |
|
| 676 |
if constexpr (std::numeric_limits<T>::is_signed) { |
| 677 |
if constexpr (sizeof(t) <= sizeof(int32_t)) { |
| 678 |
setInt32(int32_t(t)); |
| 679 |
} else { |
| 680 |
if (JSVAL_INT_MIN <= t && t <= JSVAL_INT_MAX) { |
| 681 |
setInt32(int32_t(t)); |
| 682 |
} else { |
| 683 |
setDouble(double(t)); |
| 684 |
} |
| 685 |
} |
| 686 |
} else { |
| 687 |
if constexpr (sizeof(t) <= sizeof(uint16_t)) { |
| 688 |
setInt32(int32_t(t)); |
| 689 |
} else { |
| 690 |
if (t <= JSVAL_INT_MAX) { |
| 691 |
setInt32(int32_t(t)); |
| 692 |
} else { |
| 693 |
setDouble(double(t)); |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
void setObjectOrNull(JSObject* arg) { |
| 700 |
if (arg) { |
| 701 |
setObject(*arg); |
| 702 |
} else { |
| 703 |
setNull(); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
void swap(Value& rhs) { |
| 708 |
uint64_t tmp = rhs.asBits_; |
| 709 |
rhs.asBits_ = asBits_; |
| 710 |
asBits_ = tmp; |
| 711 |
} |
| 712 |
|
| 713 |
private: |
| 714 |
JSValueTag toTag() const { return JSValueTag(asBits_ >> JSVAL_TAG_SHIFT); } |
| 715 |
|
| 716 |
template <typename T, JSValueTag Tag> |
| 717 |
T* unboxGCPointer() const { |
| 718 |
MOZ_ASSERT((asBits_ & js::gc::CellAlignMask) == 0, |
| 719 |
"GC pointer is not aligned. Is this memory corruption?"); |
| 720 |
#if defined(JS_NUNBOX32) |
| 721 |
uintptr_t payload = uint32_t(asBits_); |
| 722 |
return reinterpret_cast<T*>(payload); |
| 723 |
#elif defined(JS_PUNBOX64) |
| 724 |
// Note: the 'Spectre mitigations' comment at the top of this class |
| 725 |
// explains why we use XOR here. |
| 726 |
constexpr uint64_t shiftedTag = uint64_t(Tag) << JSVAL_TAG_SHIFT; |
| 727 |
return reinterpret_cast<T*>(uintptr_t(asBits_ ^ shiftedTag)); |
| 728 |
#endif |
| 729 |
} |
| 730 |
|
| 731 |
public: |
| 732 |
/*** JIT-only interfaces to interact with and create raw Values ***/ |
| 733 |
#if defined(JS_NUNBOX32) |
| 734 |
PayloadType toNunboxPayload() const { return uint32_t(asBits_); } |
| 735 |
|
| 736 |
JSValueTag toNunboxTag() const { return toTag(); } |
| 737 |
#elif defined(JS_PUNBOX64) |
| 738 |
const void* bitsAsPunboxPointer() const { |
| 739 |
return reinterpret_cast<void*>(asBits_); |
| 740 |
} |
| 741 |
#endif |
| 742 |
|
| 743 |
/*** Value type queries ***/ |
| 744 |
|
| 745 |
/* |
| 746 |
* N.B. GCC, in some but not all cases, chooses to emit signed comparison |
| 747 |
* of JSValueTag even though its underlying type has been forced to be |
| 748 |
* uint32_t. Thus, all comparisons should explicitly cast operands to |
| 749 |
* uint32_t. |
| 750 |
*/ |
| 751 |
|
| 752 |
bool isUndefined() const { |
| 753 |
#if defined(JS_NUNBOX32) |
| 754 |
return toTag() == JSVAL_TAG_UNDEFINED; |
| 755 |
#elif defined(JS_PUNBOX64) |
| 756 |
return asBits_ == JSVAL_SHIFTED_TAG_UNDEFINED; |
| 757 |
#endif |
| 758 |
} |
| 759 |
|
| 760 |
bool isNull() const { |
| 761 |
#if defined(JS_NUNBOX32) |
| 762 |
return toTag() == JSVAL_TAG_NULL; |
| 763 |
#elif defined(JS_PUNBOX64) |
| 764 |
return asBits_ == JSVAL_SHIFTED_TAG_NULL; |
| 765 |
#endif |
| 766 |
} |
| 767 |
|
| 768 |
bool isNullOrUndefined() const { return isNull() || isUndefined(); } |
| 769 |
|
| 770 |
bool isInt32() const { return toTag() == JSVAL_TAG_INT32; } |
| 771 |
|
| 772 |
bool isInt32(int32_t i32) const { |
| 773 |
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_INT32, uint32_t(i32)); |
| 774 |
} |
| 775 |
|
| 776 |
bool isDouble() const { return detail::ValueIsDouble(asBits_); } |
| 777 |
|
| 778 |
bool isNaN() const { return isDouble() && std::isnan(toDouble()); } |
| 779 |
|
| 780 |
bool isNumber() const { |
| 781 |
#if defined(JS_NUNBOX32) |
| 782 |
MOZ_ASSERT(toTag() != JSVAL_TAG_CLEAR); |
| 783 |
return uint32_t(toTag()) <= uint32_t(detail::ValueUpperInclNumberTag); |
| 784 |
#elif defined(JS_PUNBOX64) |
| 785 |
return asBits_ < detail::ValueUpperExclShiftedNumberTag; |
| 786 |
#endif |
| 787 |
} |
| 788 |
|
| 789 |
bool isString() const { return toTag() == JSVAL_TAG_STRING; } |
| 790 |
|
| 791 |
bool isSymbol() const { return toTag() == JSVAL_TAG_SYMBOL; } |
| 792 |
|
| 793 |
bool isBigInt() const { return toTag() == JSVAL_TAG_BIGINT; } |
| 794 |
|
| 795 |
bool isObject() const { |
| 796 |
#if defined(JS_NUNBOX32) |
| 797 |
return toTag() == JSVAL_TAG_OBJECT; |
| 798 |
#elif defined(JS_PUNBOX64) |
| 799 |
MOZ_ASSERT((asBits_ >> JSVAL_TAG_SHIFT) <= JSVAL_TAG_OBJECT); |
| 800 |
return asBits_ >= JSVAL_SHIFTED_TAG_OBJECT; |
| 801 |
#endif |
| 802 |
} |
| 803 |
|
| 804 |
bool isPrimitive() const { |
| 805 |
#if defined(JS_NUNBOX32) |
| 806 |
return uint32_t(toTag()) < uint32_t(detail::ValueUpperExclPrimitiveTag); |
| 807 |
#elif defined(JS_PUNBOX64) |
| 808 |
return asBits_ < detail::ValueUpperExclShiftedPrimitiveTag; |
| 809 |
#endif |
| 810 |
} |
| 811 |
|
| 812 |
bool isObjectOrNull() const { return isObject() || isNull(); } |
| 813 |
|
| 814 |
bool isNumeric() const { return isNumber() || isBigInt(); } |
| 815 |
|
| 816 |
bool isGCThing() const { |
| 817 |
#if defined(JS_NUNBOX32) |
| 818 |
/* gcc sometimes generates signed < without explicit casts. */ |
| 819 |
return uint32_t(toTag()) >= uint32_t(detail::ValueLowerInclGCThingTag); |
| 820 |
#elif defined(JS_PUNBOX64) |
| 821 |
return asBits_ >= detail::ValueLowerInclShiftedGCThingTag; |
| 822 |
#endif |
| 823 |
} |
| 824 |
|
| 825 |
bool isBoolean() const { return toTag() == JSVAL_TAG_BOOLEAN; } |
| 826 |
|
| 827 |
bool isTrue() const { |
| 828 |
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_BOOLEAN, uint32_t(true)); |
| 829 |
} |
| 830 |
|
| 831 |
bool isFalse() const { |
| 832 |
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_BOOLEAN, uint32_t(false)); |
| 833 |
} |
| 834 |
|
| 835 |
bool isMagic() const { return toTag() == JSVAL_TAG_MAGIC; } |
| 836 |
|
| 837 |
bool isMagic(JSWhyMagic why) const { |
| 838 |
if (!isMagic()) { |
| 839 |
return false; |
| 840 |
} |
| 841 |
MOZ_RELEASE_ASSERT(whyMagic() == why); |
| 842 |
return true; |
| 843 |
} |
| 844 |
|
| 845 |
// Like isMagic, but without the release assertion. |
| 846 |
// Note that in release builds this will return *false* for |
| 847 |
// non-matching magic values, because it is generally safer to |
| 848 |
// ignore an unexpected magic value than to misinterpret it. See bug |
| 849 |
// 2032226. |
| 850 |
bool isMagicNoReleaseCheck(JSWhyMagic why) const { |
| 851 |
MOZ_ASSERT_IF(isMagic(), whyMagic() == why); |
| 852 |
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_MAGIC, uint32_t(why)); |
| 853 |
} |
| 854 |
|
| 855 |
JS::TraceKind traceKind() const { |
| 856 |
MOZ_ASSERT(isGCThing()); |
| 857 |
static_assert((JSVAL_TAG_STRING & 0x03) == size_t(JS::TraceKind::String), |
| 858 |
"Value type tags must correspond with JS::TraceKinds."); |
| 859 |
static_assert((JSVAL_TAG_SYMBOL & 0x03) == size_t(JS::TraceKind::Symbol), |
| 860 |
"Value type tags must correspond with JS::TraceKinds."); |
| 861 |
static_assert((JSVAL_TAG_OBJECT & 0x03) == size_t(JS::TraceKind::Object), |
| 862 |
"Value type tags must correspond with JS::TraceKinds."); |
| 863 |
static_assert((JSVAL_TAG_BIGINT & 0x03) == size_t(JS::TraceKind::BigInt), |
| 864 |
"Value type tags must correspond with JS::TraceKinds."); |
| 865 |
if (MOZ_UNLIKELY(isPrivateGCThing())) { |
| 866 |
return JS::GCThingTraceKind(toGCThing()); |
| 867 |
} |
| 868 |
return JS::TraceKind(toTag() & 0x03); |
| 869 |
} |
| 870 |
|
| 871 |
JSWhyMagic whyMagic() const { |
| 872 |
MOZ_ASSERT(magicUint32() < JS_WHY_MAGIC_COUNT); |
| 873 |
return static_cast<JSWhyMagic>(magicUint32()); |
| 874 |
} |
| 875 |
|
| 876 |
uint32_t magicUint32() const { |
| 877 |
MOZ_ASSERT(isMagic()); |
| 878 |
return uint32_t(asBits_); |
| 879 |
} |
| 880 |
|
| 881 |
/*** Comparison ***/ |
| 882 |
|
| 883 |
bool operator==(const Value& rhs) const { return asBits_ == rhs.asBits_; } |
| 884 |
|
| 885 |
bool operator!=(const Value& rhs) const { return asBits_ != rhs.asBits_; } |
| 886 |
|
| 887 |
friend inline bool SameType(const Value& lhs, const Value& rhs); |
| 888 |
|
| 889 |
/*** Extract the value's typed payload ***/ |
| 890 |
|
| 891 |
int32_t toInt32() const { |
| 892 |
MOZ_ASSERT(isInt32()); |
| 893 |
return int32_t(asBits_); |
| 894 |
} |
| 895 |
|
| 896 |
double toDouble() const { |
| 897 |
MOZ_ASSERT(isDouble()); |
| 898 |
return mozilla::BitwiseCast<double>(asBits_); |
| 899 |
} |
| 900 |
|
| 901 |
double toNumber() const { |
| 902 |
MOZ_ASSERT(isNumber()); |
| 903 |
return isDouble() ? toDouble() : double(toInt32()); |
| 904 |
} |
| 905 |
|
| 906 |
JSString* toString() const { |
| 907 |
MOZ_ASSERT(isString()); |
| 908 |
return unboxGCPointer<JSString, JSVAL_TAG_STRING>(); |
| 909 |
} |
| 910 |
|
| 911 |
JS::Symbol* toSymbol() const { |
| 912 |
MOZ_ASSERT(isSymbol()); |
| 913 |
return unboxGCPointer<JS::Symbol, JSVAL_TAG_SYMBOL>(); |
| 914 |
} |
| 915 |
|
| 916 |
JS::BigInt* toBigInt() const { |
| 917 |
MOZ_ASSERT(isBigInt()); |
| 918 |
return unboxGCPointer<JS::BigInt, JSVAL_TAG_BIGINT>(); |
| 919 |
} |
| 920 |
|
| 921 |
JSObject& toObject() const { |
| 922 |
MOZ_ASSERT(isObject()); |
| 923 |
#if defined(JS_PUNBOX64) |
| 924 |
MOZ_ASSERT((asBits_ & detail::ValueGCThingPayloadMask) != 0); |
| 925 |
#endif |
| 926 |
return *unboxGCPointer<JSObject, JSVAL_TAG_OBJECT>(); |
| 927 |
} |
| 928 |
|
| 929 |
JSObject* toObjectOrNull() const { |
| 930 |
MOZ_ASSERT(isObjectOrNull()); |
| 931 |
#if defined(JS_NUNBOX32) |
| 932 |
return reinterpret_cast<JSObject*>(uintptr_t(asBits_)); |
| 933 |
#elif defined(JS_PUNBOX64) |
| 934 |
// Note: the 'Spectre mitigations' comment at the top of this class |
| 935 |
// explains why we use XOR here and in other to* methods. |
| 936 |
uint64_t ptrBits = |
| 937 |
(asBits_ ^ JSVAL_SHIFTED_TAG_OBJECT) & ~detail::ValueObjectOrNullBit; |
| 938 |
MOZ_ASSERT((ptrBits & 0x7) == 0); |
| 939 |
return reinterpret_cast<JSObject*>(ptrBits); |
| 940 |
#endif |
| 941 |
} |
| 942 |
|
| 943 |
js::gc::Cell* toGCThing() const { |
| 944 |
MOZ_ASSERT(isGCThing()); |
| 945 |
#if defined(JS_NUNBOX32) |
| 946 |
return reinterpret_cast<js::gc::Cell*>(uintptr_t(asBits_)); |
| 947 |
#elif defined(JS_PUNBOX64) |
| 948 |
uint64_t ptrBits = asBits_ & detail::ValueGCThingPayloadMask; |
| 949 |
MOZ_ASSERT((ptrBits & 0x7) == 0); |
| 950 |
return reinterpret_cast<js::gc::Cell*>(ptrBits); |
| 951 |
#endif |
| 952 |
} |
| 953 |
|
| 954 |
GCCellPtr toGCCellPtr() const { return GCCellPtr(toGCThing(), traceKind()); } |
| 955 |
|
| 956 |
bool toBoolean() const { |
| 957 |
MOZ_ASSERT(isBoolean()); |
| 958 |
#if defined(JS_NUNBOX32) |
| 959 |
return bool(toNunboxPayload()); |
| 960 |
#elif defined(JS_PUNBOX64) |
| 961 |
return bool(asBits_ & 0x1); |
| 962 |
#endif |
| 963 |
} |
| 964 |
|
| 965 |
constexpr uint64_t asRawBits() const { return asBits_; } |
| 966 |
|
| 967 |
JSValueType extractNonDoubleType() const { |
| 968 |
uint32_t type = toTag() & 0xF; |
| 969 |
MOZ_ASSERT(type > JSVAL_TYPE_DOUBLE); |
| 970 |
return JSValueType(type); |
| 971 |
} |
| 972 |
|
| 973 |
JS::ValueType type() const { |
| 974 |
if (isDouble()) { |
| 975 |
return JS::ValueType::Double; |
| 976 |
} |
| 977 |
|
| 978 |
JSValueType type = extractNonDoubleType(); |
| 979 |
MOZ_ASSERT(type <= JSVAL_TYPE_OBJECT); |
| 980 |
return JS::ValueType(type); |
| 981 |
} |
| 982 |
|
| 983 |
/* |
| 984 |
* Private API |
| 985 |
* |
| 986 |
* Private setters/getters allow the caller to read/write arbitrary |
| 987 |
* word-size pointers or uint32s. After storing to a value with |
| 988 |
* setPrivateX, it is the caller's responsibility to only read using |
| 989 |
* toPrivateX. Private values are given a type which ensures they |
| 990 |
* aren't marked by the GC. |
| 991 |
*/ |
| 992 |
|
| 993 |
void setPrivate(void* ptr) { |
| 994 |
#if defined(JS_PUNBOX64) |
| 995 |
MOZ_ASSERT(detail::IsValidUserModePointer(uintptr_t(ptr))); |
| 996 |
#endif |
| 997 |
asBits_ = uintptr_t(ptr); |
| 998 |
MOZ_ASSERT(isDouble()); |
| 999 |
} |
| 1000 |
|
| 1001 |
void* toPrivate() const { |
| 1002 |
MOZ_ASSERT(isDouble()); |
| 1003 |
#if defined(JS_PUNBOX64) |
| 1004 |
MOZ_ASSERT(detail::IsValidUserModePointer(asBits_)); |
| 1005 |
#endif |
| 1006 |
return reinterpret_cast<void*>(uintptr_t(asBits_)); |
| 1007 |
} |
| 1008 |
|
| 1009 |
void* toPrivateUnchecked() const { |
| 1010 |
return reinterpret_cast<void*>(uintptr_t(asBits_)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
void setPrivateUint32(uint32_t ui) { |
| 1014 |
MOZ_ASSERT(uint32_t(int32_t(ui)) == ui); |
| 1015 |
setInt32(int32_t(ui)); |
| 1016 |
} |
| 1017 |
|
| 1018 |
uint32_t toPrivateUint32() const { return uint32_t(toInt32()); } |
| 1019 |
|
| 1020 |
/* |
| 1021 |
* Private GC Thing API |
| 1022 |
* |
| 1023 |
* Non-JSObject, JSString, and JS::Symbol cells may be put into the 64-bit |
| 1024 |
* payload as private GC things. Such Values are considered isGCThing(), and |
| 1025 |
* as such, automatically marked. Their traceKind() is gotten via their |
| 1026 |
* cells. |
| 1027 |
*/ |
| 1028 |
|
| 1029 |
void setPrivateGCThing(js::gc::Cell* cell) { |
| 1030 |
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::String, |
| 1031 |
"Private GC thing Values must not be strings. Make a " |
| 1032 |
"StringValue instead."); |
| 1033 |
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::Symbol, |
| 1034 |
"Private GC thing Values must not be symbols. Make a " |
| 1035 |
"SymbolValue instead."); |
| 1036 |
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::BigInt, |
| 1037 |
"Private GC thing Values must not be BigInts. Make a " |
| 1038 |
"BigIntValue instead."); |
| 1039 |
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::Object, |
| 1040 |
"Private GC thing Values must not be objects. Make an " |
| 1041 |
"ObjectValue instead."); |
| 1042 |
|
| 1043 |
MOZ_ASSERT(js::gc::IsCellPointerValid(cell)); |
| 1044 |
#if defined(JS_PUNBOX64) |
| 1045 |
// VisualStudio cannot contain parenthesized C++ style cast and shift |
| 1046 |
// inside decltype in template parameter: |
| 1047 |
// AssertionConditionType<decltype((uintptr_t(x) >> 1))> |
| 1048 |
// It throws syntax error. |
| 1049 |
MOZ_ASSERT((((uintptr_t)cell) >> JSVAL_TAG_SHIFT) == 0); |
| 1050 |
#endif |
| 1051 |
asBits_ = |
| 1052 |
bitsFromTagAndPayload(JSVAL_TAG_PRIVATE_GCTHING, PayloadType(cell)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
bool isPrivateGCThing() const { return toTag() == JSVAL_TAG_PRIVATE_GCTHING; } |
| 1056 |
|
| 1057 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1058 |
void dump() const; |
| 1059 |
void dump(js::GenericPrinter& out) const; |
| 1060 |
void dump(js::JSONPrinter& json) const; |
| 1061 |
|
| 1062 |
void dumpFields(js::JSONPrinter& json) const; |
| 1063 |
void dumpStringContent(js::GenericPrinter& out) const; |
| 1064 |
#endif |
| 1065 |
} JS_HAZ_GC_POINTER MOZ_NON_PARAM; |
| 1066 |
|
| 1067 |
static_assert(sizeof(Value) == 8, |
| 1068 |
"Value size must leave three tag bits, be a binary power, and " |
| 1069 |
"is ubiquitously depended upon everywhere"); |
| 1070 |
|
| 1071 |
static MOZ_ALWAYS_INLINE void ExposeValueToActiveJS(const Value& v) { |
| 1072 |
#ifdef DEBUG |
| 1073 |
Value tmp = v; |
| 1074 |
MOZ_ASSERT(!js::gc::EdgeNeedsSweepUnbarrieredSlow(&tmp)); |
| 1075 |
#endif |
| 1076 |
if (v.isGCThing()) { |
| 1077 |
js::gc::ExposeGCThingToActiveJS(v.toGCCellPtr()); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
/************************************************************************/ |
| 1082 |
|
| 1083 |
static inline MOZ_MAY_CALL_AFTER_MUST_RETURN Value NullValue() { |
| 1084 |
Value v; |
| 1085 |
v.setNull(); |
| 1086 |
return v; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static constexpr Value UndefinedValue() { return Value(); } |
| 1090 |
|
| 1091 |
static constexpr Value Int32Value(int32_t i32) { return Value::fromInt32(i32); } |
| 1092 |
|
| 1093 |
static inline Value DoubleValue(double dbl) { |
| 1094 |
Value v; |
| 1095 |
v.setDouble(dbl); |
| 1096 |
return v; |
| 1097 |
} |
| 1098 |
|
| 1099 |
static inline Value CanonicalizedDoubleValue(double d) { |
| 1100 |
return Value::fromDouble(CanonicalizeNaN(d)); |
| 1101 |
} |
| 1102 |
|
| 1103 |
static inline Value NaNValue() { |
| 1104 |
return Value::fromRawBits(detail::CanonicalizedNaNBits); |
| 1105 |
} |
| 1106 |
|
| 1107 |
static inline Value InfinityValue() { |
| 1108 |
return Value::fromRawBits(detail::InfinityBits); |
| 1109 |
} |
| 1110 |
|
| 1111 |
static inline Value Float32Value(float f) { |
| 1112 |
Value v; |
| 1113 |
v.setDouble(f); |
| 1114 |
return v; |
| 1115 |
} |
| 1116 |
|
| 1117 |
static inline Value StringValue(JSString* str) { |
| 1118 |
Value v; |
| 1119 |
v.setString(str); |
| 1120 |
return v; |
| 1121 |
} |
| 1122 |
|
| 1123 |
static inline Value SymbolValue(JS::Symbol* sym) { |
| 1124 |
Value v; |
| 1125 |
v.setSymbol(sym); |
| 1126 |
return v; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static inline Value BigIntValue(JS::BigInt* bi) { |
| 1130 |
Value v; |
| 1131 |
v.setBigInt(bi); |
| 1132 |
return v; |
| 1133 |
} |
| 1134 |
|
| 1135 |
static inline Value BooleanValue(bool boo) { |
| 1136 |
Value v; |
| 1137 |
v.setBoolean(boo); |
| 1138 |
return v; |
| 1139 |
} |
| 1140 |
|
| 1141 |
static inline Value TrueValue() { |
| 1142 |
Value v; |
| 1143 |
v.setBoolean(true); |
| 1144 |
return v; |
| 1145 |
} |
| 1146 |
|
| 1147 |
static inline Value FalseValue() { |
| 1148 |
Value v; |
| 1149 |
v.setBoolean(false); |
| 1150 |
return v; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static inline Value ObjectValue(JSObject& obj) { |
| 1154 |
Value v; |
| 1155 |
v.setObject(obj); |
| 1156 |
return v; |
| 1157 |
} |
| 1158 |
|
| 1159 |
static inline Value MagicValue(JSWhyMagic why) { |
| 1160 |
Value v; |
| 1161 |
v.setMagic(why); |
| 1162 |
return v; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static inline Value MagicValueUint32(uint32_t payload) { |
| 1166 |
Value v; |
| 1167 |
v.setMagicUint32(payload); |
| 1168 |
return v; |
| 1169 |
} |
| 1170 |
|
| 1171 |
static constexpr Value NumberValue(uint32_t i) { |
| 1172 |
return i <= JSVAL_INT_MAX ? Int32Value(int32_t(i)) |
| 1173 |
: Value::fromDouble(double(i)); |
| 1174 |
} |
| 1175 |
|
| 1176 |
template <typename T> |
| 1177 |
static inline Value NumberValue(const T t) { |
| 1178 |
Value v; |
| 1179 |
v.setNumber(t); |
| 1180 |
return v; |
| 1181 |
} |
| 1182 |
|
| 1183 |
static inline Value ObjectOrNullValue(JSObject* obj) { |
| 1184 |
Value v; |
| 1185 |
v.setObjectOrNull(obj); |
| 1186 |
return v; |
| 1187 |
} |
| 1188 |
|
| 1189 |
static inline Value PrivateValue(void* ptr) { |
| 1190 |
Value v; |
| 1191 |
v.setPrivate(ptr); |
| 1192 |
return v; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static inline Value PrivateValue(uintptr_t ptr) { |
| 1196 |
return PrivateValue(reinterpret_cast<void*>(ptr)); |
| 1197 |
} |
| 1198 |
|
| 1199 |
static inline Value PrivateUint32Value(uint32_t ui) { |
| 1200 |
Value v; |
| 1201 |
v.setPrivateUint32(ui); |
| 1202 |
return v; |
| 1203 |
} |
| 1204 |
|
| 1205 |
static inline Value PrivateGCThingValue(js::gc::Cell* cell) { |
| 1206 |
Value v; |
| 1207 |
v.setPrivateGCThing(cell); |
| 1208 |
return v; |
| 1209 |
} |
| 1210 |
|
| 1211 |
inline bool SameType(const Value& lhs, const Value& rhs) { |
| 1212 |
#if defined(JS_NUNBOX32) |
| 1213 |
JSValueTag ltag = lhs.toTag(), rtag = rhs.toTag(); |
| 1214 |
return ltag == rtag || (ltag < JSVAL_TAG_CLEAR && rtag < JSVAL_TAG_CLEAR); |
| 1215 |
#elif defined(JS_PUNBOX64) |
| 1216 |
return (lhs.isDouble() && rhs.isDouble()) || |
| 1217 |
(((lhs.asBits_ ^ rhs.asBits_) & 0xFFFF800000000000ULL) == 0); |
| 1218 |
#endif |
| 1219 |
} |
| 1220 |
|
| 1221 |
} // namespace JS |
| 1222 |
|
| 1223 |
/************************************************************************/ |
| 1224 |
|
| 1225 |
namespace JS { |
| 1226 |
JS_PUBLIC_API void HeapValuePostWriteBarrier(Value* valuep, const Value& prev, |
| 1227 |
const Value& next); |
| 1228 |
JS_PUBLIC_API void HeapValueWriteBarriers(Value* valuep, const Value& prev, |
| 1229 |
const Value& next); |
| 1230 |
|
| 1231 |
template <> |
| 1232 |
struct GCPolicy<JS::Value> { |
| 1233 |
static void trace(JSTracer* trc, Value* v, const char* name) { |
| 1234 |
// This should only be called as part of root marking since that's the only |
| 1235 |
// time we should trace unbarriered GC thing pointers. This will assert if |
| 1236 |
// called at other times. |
| 1237 |
TraceRoot(trc, v, name); |
| 1238 |
} |
| 1239 |
static bool isTenured(const Value& thing) { |
| 1240 |
return !thing.isGCThing() || !IsInsideNursery(thing.toGCThing()); |
| 1241 |
} |
| 1242 |
static bool isValid(const Value& value) { |
| 1243 |
return !value.isGCThing() || js::gc::IsCellPointerValid(value.toGCThing()); |
| 1244 |
} |
| 1245 |
}; |
| 1246 |
|
| 1247 |
} // namespace JS |
| 1248 |
|
| 1249 |
namespace js { |
| 1250 |
|
| 1251 |
template <> |
| 1252 |
struct BarrierMethods<JS::Value> { |
| 1253 |
static gc::Cell* asGCThingOrNull(const JS::Value& v) { |
| 1254 |
return v.isGCThing() ? v.toGCThing() : nullptr; |
| 1255 |
} |
| 1256 |
static void writeBarriers(JS::Value* v, const JS::Value& prev, |
| 1257 |
const JS::Value& next) { |
| 1258 |
JS::HeapValueWriteBarriers(v, prev, next); |
| 1259 |
} |
| 1260 |
static void postWriteBarrier(JS::Value* v, const JS::Value& prev, |
| 1261 |
const JS::Value& next) { |
| 1262 |
JS::HeapValuePostWriteBarrier(v, prev, next); |
| 1263 |
} |
| 1264 |
static void exposeToJS(const JS::Value& v) { JS::ExposeValueToActiveJS(v); } |
| 1265 |
static void readBarrier(const JS::Value& v) { |
| 1266 |
if (v.isGCThing()) { |
| 1267 |
js::gc::IncrementalReadBarrier(v.toGCCellPtr()); |
| 1268 |
} |
| 1269 |
} |
| 1270 |
}; |
| 1271 |
|
| 1272 |
template <class Wrapper> |
| 1273 |
class MutableValueOperations; |
| 1274 |
|
| 1275 |
/** |
| 1276 |
* A class designed for CRTP use in implementing the non-mutating parts of the |
| 1277 |
* Value interface in Value-like classes. Wrapper must be a class inheriting |
| 1278 |
* ValueOperations<Wrapper> with a visible get() method returning a const |
| 1279 |
* reference to the Value abstracted by Wrapper. |
| 1280 |
*/ |
| 1281 |
template <class Wrapper> |
| 1282 |
class WrappedPtrOperations<JS::Value, Wrapper> { |
| 1283 |
const JS::Value& value() const { |
| 1284 |
return static_cast<const Wrapper*>(this)->get(); |
| 1285 |
} |
| 1286 |
|
| 1287 |
public: |
| 1288 |
bool isUndefined() const { return value().isUndefined(); } |
| 1289 |
bool isNull() const { return value().isNull(); } |
| 1290 |
bool isBoolean() const { return value().isBoolean(); } |
| 1291 |
bool isTrue() const { return value().isTrue(); } |
| 1292 |
bool isFalse() const { return value().isFalse(); } |
| 1293 |
bool isNumber() const { return value().isNumber(); } |
| 1294 |
bool isInt32() const { return value().isInt32(); } |
| 1295 |
bool isInt32(int32_t i32) const { return value().isInt32(i32); } |
| 1296 |
bool isDouble() const { return value().isDouble(); } |
| 1297 |
bool isString() const { return value().isString(); } |
| 1298 |
bool isSymbol() const { return value().isSymbol(); } |
| 1299 |
bool isBigInt() const { return value().isBigInt(); } |
| 1300 |
bool isObject() const { return value().isObject(); } |
| 1301 |
bool isMagic() const { return value().isMagic(); } |
| 1302 |
bool isMagic(JSWhyMagic why) const { return value().isMagic(why); } |
| 1303 |
bool isGCThing() const { return value().isGCThing(); } |
| 1304 |
bool isPrivateGCThing() const { return value().isPrivateGCThing(); } |
| 1305 |
bool isPrimitive() const { return value().isPrimitive(); } |
| 1306 |
|
| 1307 |
bool isNullOrUndefined() const { return value().isNullOrUndefined(); } |
| 1308 |
bool isObjectOrNull() const { return value().isObjectOrNull(); } |
| 1309 |
bool isNumeric() const { return value().isNumeric(); } |
| 1310 |
|
| 1311 |
bool toBoolean() const { return value().toBoolean(); } |
| 1312 |
double toNumber() const { return value().toNumber(); } |
| 1313 |
int32_t toInt32() const { return value().toInt32(); } |
| 1314 |
double toDouble() const { return value().toDouble(); } |
| 1315 |
JSString* toString() const { return value().toString(); } |
| 1316 |
JS::Symbol* toSymbol() const { return value().toSymbol(); } |
| 1317 |
JS::BigInt* toBigInt() const { return value().toBigInt(); } |
| 1318 |
JSObject& toObject() const { return value().toObject(); } |
| 1319 |
JSObject* toObjectOrNull() const { return value().toObjectOrNull(); } |
| 1320 |
JS::GCCellPtr toGCCellPtr() const { return value().toGCCellPtr(); } |
| 1321 |
gc::Cell* toGCThing() const { return value().toGCThing(); } |
| 1322 |
JS::TraceKind traceKind() const { return value().traceKind(); } |
| 1323 |
void* toPrivate() const { return value().toPrivate(); } |
| 1324 |
uint32_t toPrivateUint32() const { return value().toPrivateUint32(); } |
| 1325 |
|
| 1326 |
uint64_t asRawBits() const { return value().asRawBits(); } |
| 1327 |
JSValueType extractNonDoubleType() const { |
| 1328 |
return value().extractNonDoubleType(); |
| 1329 |
} |
| 1330 |
JS::ValueType type() const { return value().type(); } |
| 1331 |
|
| 1332 |
JSWhyMagic whyMagic() const { return value().whyMagic(); } |
| 1333 |
uint32_t magicUint32() const { return value().magicUint32(); } |
| 1334 |
}; |
| 1335 |
|
| 1336 |
/** |
| 1337 |
* A class designed for CRTP use in implementing all the mutating parts of the |
| 1338 |
* Value interface in Value-like classes. Wrapper must be a class inheriting |
| 1339 |
* MutableWrappedPtrOperations<Wrapper> with visible get() methods returning |
| 1340 |
* const and non-const references to the Value abstracted by Wrapper. |
| 1341 |
*/ |
| 1342 |
template <class Wrapper> |
| 1343 |
class MutableWrappedPtrOperations<JS::Value, Wrapper> |
| 1344 |
: public WrappedPtrOperations<JS::Value, Wrapper> { |
| 1345 |
protected: |
| 1346 |
void set(const JS::Value& v) { |
| 1347 |
// Call Wrapper::set to trigger any barriers. |
| 1348 |
static_cast<Wrapper*>(this)->set(v); |
| 1349 |
} |
| 1350 |
|
| 1351 |
public: |
| 1352 |
void setNull() { set(JS::NullValue()); } |
| 1353 |
void setUndefined() { set(JS::UndefinedValue()); } |
| 1354 |
void setInt32(int32_t i) { set(JS::Int32Value(i)); } |
| 1355 |
void setDouble(double d) { set(JS::DoubleValue(d)); } |
| 1356 |
void setNaN() { set(JS::NaNValue()); } |
| 1357 |
void setInfinity() { set(JS::InfinityValue()); } |
| 1358 |
void setBoolean(bool b) { set(JS::BooleanValue(b)); } |
| 1359 |
void setMagic(JSWhyMagic why) { set(JS::MagicValue(why)); } |
| 1360 |
template <typename T> |
| 1361 |
void setNumber(T t) { |
| 1362 |
set(JS::NumberValue(t)); |
| 1363 |
} |
| 1364 |
void setString(JSString* str) { set(JS::StringValue(str)); } |
| 1365 |
void setSymbol(JS::Symbol* sym) { set(JS::SymbolValue(sym)); } |
| 1366 |
void setBigInt(JS::BigInt* bi) { set(JS::BigIntValue(bi)); } |
| 1367 |
void setObject(JSObject& obj) { set(JS::ObjectValue(obj)); } |
| 1368 |
void setObjectOrNull(JSObject* arg) { set(JS::ObjectOrNullValue(arg)); } |
| 1369 |
void setPrivate(void* ptr) { set(JS::PrivateValue(ptr)); } |
| 1370 |
void setPrivateUint32(uint32_t ui) { set(JS::PrivateUint32Value(ui)); } |
| 1371 |
void setPrivateGCThing(js::gc::Cell* cell) { |
| 1372 |
set(JS::PrivateGCThingValue(cell)); |
| 1373 |
} |
| 1374 |
}; |
| 1375 |
|
| 1376 |
/* |
| 1377 |
* Augment the generic Heap<T> interface when T = Value with |
| 1378 |
* type-querying, value-extracting, and mutating operations. |
| 1379 |
*/ |
| 1380 |
template <typename Wrapper> |
| 1381 |
class HeapOperations<JS::Value, Wrapper> |
| 1382 |
: public MutableWrappedPtrOperations<JS::Value, Wrapper> {}; |
| 1383 |
|
| 1384 |
MOZ_HAVE_NORETURN MOZ_COLD MOZ_NEVER_INLINE void ReportBadValueTypeAndCrash( |
| 1385 |
const JS::Value& val); |
| 1386 |
|
| 1387 |
// If the Value is a GC pointer type, call |f| with the pointer cast to that |
| 1388 |
// type and return the result wrapped in a Maybe, otherwise return None(). |
| 1389 |
template <typename F> |
| 1390 |
auto MapGCThingTyped(const JS::Value& val, F&& f) { |
| 1391 |
switch (val.type()) { |
| 1392 |
case JS::ValueType::String: { |
| 1393 |
JSString* str = val.toString(); |
| 1394 |
MOZ_ASSERT(gc::IsCellPointerValid(str)); |
| 1395 |
return mozilla::Some(f(str)); |
| 1396 |
} |
| 1397 |
case JS::ValueType::Object: { |
| 1398 |
JSObject* obj = &val.toObject(); |
| 1399 |
MOZ_ASSERT(gc::IsCellPointerValid(obj)); |
| 1400 |
return mozilla::Some(f(obj)); |
| 1401 |
} |
| 1402 |
case JS::ValueType::Symbol: { |
| 1403 |
JS::Symbol* sym = val.toSymbol(); |
| 1404 |
MOZ_ASSERT(gc::IsCellPointerValid(sym)); |
| 1405 |
return mozilla::Some(f(sym)); |
| 1406 |
} |
| 1407 |
case JS::ValueType::BigInt: { |
| 1408 |
JS::BigInt* bi = val.toBigInt(); |
| 1409 |
MOZ_ASSERT(gc::IsCellPointerValid(bi)); |
| 1410 |
return mozilla::Some(f(bi)); |
| 1411 |
} |
| 1412 |
case JS::ValueType::PrivateGCThing: { |
| 1413 |
MOZ_ASSERT(gc::IsCellPointerValid(val.toGCThing())); |
| 1414 |
return mozilla::Some(MapGCThingTyped(val.toGCCellPtr(), std::move(f))); |
| 1415 |
} |
| 1416 |
case JS::ValueType::Double: |
| 1417 |
case JS::ValueType::Int32: |
| 1418 |
case JS::ValueType::Boolean: |
| 1419 |
case JS::ValueType::Undefined: |
| 1420 |
case JS::ValueType::Null: |
| 1421 |
case JS::ValueType::Magic: { |
| 1422 |
MOZ_ASSERT(!val.isGCThing()); |
| 1423 |
using ReturnType = decltype(f(static_cast<JSObject*>(nullptr))); |
| 1424 |
return mozilla::Maybe<ReturnType>(); |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
ReportBadValueTypeAndCrash(val); |
| 1429 |
} |
| 1430 |
|
| 1431 |
// If the Value is a GC pointer type, call |f| with the pointer cast to that |
| 1432 |
// type. Return whether this happened. |
| 1433 |
template <typename F> |
| 1434 |
bool ApplyGCThingTyped(const JS::Value& val, F&& f) { |
| 1435 |
return MapGCThingTyped(val, |
| 1436 |
[&f](auto t) { |
| 1437 |
f(t); |
| 1438 |
return true; |
| 1439 |
}) |
| 1440 |
.isSome(); |
| 1441 |
} |
| 1442 |
|
| 1443 |
static inline JS::Value PoisonedObjectValue(uintptr_t poison) { |
| 1444 |
JS::Value v; |
| 1445 |
v.setObjectNoCheck(reinterpret_cast<JSObject*>(poison)); |
| 1446 |
return v; |
| 1447 |
} |
| 1448 |
|
| 1449 |
} // namespace js |
| 1450 |
|
| 1451 |
#ifdef DEBUG |
| 1452 |
namespace JS { |
| 1453 |
|
| 1454 |
MOZ_ALWAYS_INLINE void AssertValueIsNotGray(const Value& value) { |
| 1455 |
if (value.isGCThing()) { |
| 1456 |
AssertCellIsNotGray(value.toGCThing()); |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
MOZ_ALWAYS_INLINE void AssertValueIsNotGray(const Heap<Value>& value) { |
| 1461 |
AssertValueIsNotGray(value.unbarrieredGet()); |
| 1462 |
} |
| 1463 |
|
| 1464 |
} // namespace JS |
| 1465 |
#endif |
| 1466 |
|
| 1467 |
/************************************************************************/ |
| 1468 |
|
| 1469 |
namespace JS { |
| 1470 |
|
| 1471 |
extern JS_PUBLIC_DATA const HandleValue NullHandleValue; |
| 1472 |
extern JS_PUBLIC_DATA const HandleValue UndefinedHandleValue; |
| 1473 |
extern JS_PUBLIC_DATA const HandleValue TrueHandleValue; |
| 1474 |
extern JS_PUBLIC_DATA const HandleValue FalseHandleValue; |
| 1475 |
extern JS_PUBLIC_DATA const Handle<mozilla::Maybe<Value>> NothingHandleValue; |
| 1476 |
|
| 1477 |
} // namespace JS |
| 1478 |
|
| 1479 |
#endif /* js_Value_h */ |