2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2013-02-02 02:24:37 +04:00
|
|
|
/* JS::Value implementation. */
|
2012-01-23 15:43:16 +04:00
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#ifndef js_Value_h
|
|
|
|
#define js_Value_h
|
2011-09-19 20:34:49 +04:00
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-10-28 19:47:31 +03:00
|
|
|
#include "mozilla/Casting.h"
|
2018-03-15 12:38:59 +03:00
|
|
|
#include "mozilla/Compiler.h"
|
2018-02-20 20:28:12 +03:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2013-02-02 04:15:49 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/Likely.h"
|
2019-02-09 11:26:27 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
#include <limits> /* for std::numeric_limits */
|
|
|
|
|
2014-05-21 17:47:10 +04:00
|
|
|
#include "js-config.h"
|
2013-10-11 23:11:48 +04:00
|
|
|
#include "jstypes.h"
|
|
|
|
|
2014-06-16 22:59:45 +04:00
|
|
|
#include "js/GCAPI.h"
|
2013-03-13 01:05:57 +04:00
|
|
|
#include "js/RootingAPI.h"
|
2012-01-23 15:43:16 +04:00
|
|
|
#include "js/Utility.h"
|
2011-09-19 20:34:49 +04:00
|
|
|
|
2018-03-28 06:20:43 +03:00
|
|
|
namespace JS {
|
2019-10-28 01:34:11 +03:00
|
|
|
class JS_PUBLIC_API Value;
|
2018-03-28 06:20:43 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
/* JS::Value can store a full int32_t. */
|
|
|
|
#define JSVAL_INT_BITS 32
|
|
|
|
#define JSVAL_INT_MIN ((int32_t)0x80000000)
|
|
|
|
#define JSVAL_INT_MAX ((int32_t)0x7fffffff)
|
|
|
|
|
2019-09-20 02:56:57 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
# define JSVAL_TAG_SHIFT 32
|
|
|
|
#elif defined(JS_PUNBOX64)
|
2010-07-22 05:57:01 +04:00
|
|
|
# define JSVAL_TAG_SHIFT 47
|
|
|
|
#endif
|
|
|
|
|
2016-10-18 10:46:01 +03:00
|
|
|
// Use enums so that printing a JS::Value in the debugger shows nice
|
2016-05-26 23:52:06 +03:00
|
|
|
// symbolic type tags.
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2018-03-15 12:38:59 +03:00
|
|
|
enum JSValueType : uint8_t {
|
2010-07-01 04:52:02 +04:00
|
|
|
JSVAL_TYPE_DOUBLE = 0x00,
|
|
|
|
JSVAL_TYPE_INT32 = 0x01,
|
2018-01-24 14:33:53 +03:00
|
|
|
JSVAL_TYPE_BOOLEAN = 0x02,
|
|
|
|
JSVAL_TYPE_UNDEFINED = 0x03,
|
|
|
|
JSVAL_TYPE_NULL = 0x04,
|
2016-12-26 18:40:21 +03:00
|
|
|
JSVAL_TYPE_MAGIC = 0x05,
|
|
|
|
JSVAL_TYPE_STRING = 0x06,
|
|
|
|
JSVAL_TYPE_SYMBOL = 0x07,
|
|
|
|
JSVAL_TYPE_PRIVATE_GCTHING = 0x08,
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
JSVAL_TYPE_BIGINT = 0x09,
|
2016-04-30 04:10:07 +03:00
|
|
|
JSVAL_TYPE_OBJECT = 0x0c,
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-02-26 11:48:01 +03:00
|
|
|
// This type never appears in a Value; it's only an out-of-band value.
|
|
|
|
JSVAL_TYPE_UNKNOWN = 0x20
|
2018-03-15 12:38:59 +03:00
|
|
|
};
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-02-21 23:36:44 +03:00
|
|
|
namespace JS {
|
|
|
|
enum class ValueType : uint8_t {
|
|
|
|
Double = JSVAL_TYPE_DOUBLE,
|
|
|
|
Int32 = JSVAL_TYPE_INT32,
|
|
|
|
Boolean = JSVAL_TYPE_BOOLEAN,
|
|
|
|
Undefined = JSVAL_TYPE_UNDEFINED,
|
|
|
|
Null = JSVAL_TYPE_NULL,
|
|
|
|
Magic = JSVAL_TYPE_MAGIC,
|
|
|
|
String = JSVAL_TYPE_STRING,
|
|
|
|
Symbol = JSVAL_TYPE_SYMBOL,
|
|
|
|
PrivateGCThing = JSVAL_TYPE_PRIVATE_GCTHING,
|
|
|
|
BigInt = JSVAL_TYPE_BIGINT,
|
|
|
|
Object = JSVAL_TYPE_OBJECT,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:33:20 +03:00
|
|
|
static_assert(sizeof(JSValueType) == 1,
|
|
|
|
"compiler typed enum support is apparently buggy");
|
2010-07-17 01:00:41 +04:00
|
|
|
|
2014-05-21 17:47:10 +04:00
|
|
|
#if defined(JS_NUNBOX32)
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
enum JSValueTag : uint32_t {
|
|
|
|
JSVAL_TAG_CLEAR = 0xFFFFFF80,
|
|
|
|
JSVAL_TAG_INT32 = JSVAL_TAG_CLEAR | JSVAL_TYPE_INT32,
|
|
|
|
JSVAL_TAG_UNDEFINED = JSVAL_TAG_CLEAR | JSVAL_TYPE_UNDEFINED,
|
|
|
|
JSVAL_TAG_NULL = JSVAL_TAG_CLEAR | JSVAL_TYPE_NULL,
|
|
|
|
JSVAL_TAG_BOOLEAN = JSVAL_TAG_CLEAR | JSVAL_TYPE_BOOLEAN,
|
|
|
|
JSVAL_TAG_MAGIC = JSVAL_TAG_CLEAR | JSVAL_TYPE_MAGIC,
|
|
|
|
JSVAL_TAG_STRING = JSVAL_TAG_CLEAR | JSVAL_TYPE_STRING,
|
|
|
|
JSVAL_TAG_SYMBOL = JSVAL_TAG_CLEAR | JSVAL_TYPE_SYMBOL,
|
|
|
|
JSVAL_TAG_PRIVATE_GCTHING = JSVAL_TAG_CLEAR | JSVAL_TYPE_PRIVATE_GCTHING,
|
|
|
|
JSVAL_TAG_BIGINT = JSVAL_TAG_CLEAR | JSVAL_TYPE_BIGINT,
|
|
|
|
JSVAL_TAG_OBJECT = JSVAL_TAG_CLEAR | JSVAL_TYPE_OBJECT
|
|
|
|
};
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2014-11-08 05:33:20 +03:00
|
|
|
static_assert(sizeof(JSValueTag) == sizeof(uint32_t),
|
|
|
|
"compiler typed enum support is apparently buggy");
|
2010-07-17 01:00:41 +04:00
|
|
|
|
2014-05-21 17:47:10 +04:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
enum JSValueTag : uint32_t {
|
2019-06-25 20:34:50 +03:00
|
|
|
JSVAL_TAG_MAX_DOUBLE = 0x1FFF0,
|
|
|
|
JSVAL_TAG_INT32 = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_INT32,
|
|
|
|
JSVAL_TAG_UNDEFINED = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_UNDEFINED,
|
|
|
|
JSVAL_TAG_NULL = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_NULL,
|
|
|
|
JSVAL_TAG_BOOLEAN = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_BOOLEAN,
|
|
|
|
JSVAL_TAG_MAGIC = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_MAGIC,
|
|
|
|
JSVAL_TAG_STRING = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_STRING,
|
|
|
|
JSVAL_TAG_SYMBOL = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_SYMBOL,
|
|
|
|
JSVAL_TAG_PRIVATE_GCTHING = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_PRIVATE_GCTHING,
|
|
|
|
JSVAL_TAG_BIGINT = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_BIGINT,
|
|
|
|
JSVAL_TAG_OBJECT = JSVAL_TAG_MAX_DOUBLE | JSVAL_TYPE_OBJECT
|
2019-06-12 20:51:06 +03:00
|
|
|
};
|
|
|
|
|
2014-11-08 05:33:20 +03:00
|
|
|
static_assert(sizeof(JSValueTag) == sizeof(uint32_t),
|
|
|
|
"compiler typed enum support is apparently buggy");
|
2010-07-22 05:57:01 +04:00
|
|
|
|
2018-03-15 12:38:59 +03:00
|
|
|
enum JSValueShiftedTag : uint64_t {
|
2020-05-22 01:23:26 +03:00
|
|
|
// See Bug 584653 for why we include 0xFFFFFFFF.
|
2019-06-25 20:34:50 +03:00
|
|
|
JSVAL_SHIFTED_TAG_MAX_DOUBLE =
|
2020-05-22 01:23:26 +03:00
|
|
|
((uint64_t(JSVAL_TAG_MAX_DOUBLE) << JSVAL_TAG_SHIFT) | 0xFFFFFFFF),
|
|
|
|
JSVAL_SHIFTED_TAG_INT32 = (uint64_t(JSVAL_TAG_INT32) << JSVAL_TAG_SHIFT),
|
2016-04-30 04:10:07 +03:00
|
|
|
JSVAL_SHIFTED_TAG_UNDEFINED =
|
2020-05-22 01:23:26 +03:00
|
|
|
(uint64_t(JSVAL_TAG_UNDEFINED) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_NULL = (uint64_t(JSVAL_TAG_NULL) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_BOOLEAN = (uint64_t(JSVAL_TAG_BOOLEAN) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_MAGIC = (uint64_t(JSVAL_TAG_MAGIC) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_STRING = (uint64_t(JSVAL_TAG_STRING) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_SYMBOL = (uint64_t(JSVAL_TAG_SYMBOL) << JSVAL_TAG_SHIFT),
|
2016-12-27 15:31:07 +03:00
|
|
|
JSVAL_SHIFTED_TAG_PRIVATE_GCTHING =
|
2020-05-22 01:23:26 +03:00
|
|
|
(uint64_t(JSVAL_TAG_PRIVATE_GCTHING) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_BIGINT = (uint64_t(JSVAL_TAG_BIGINT) << JSVAL_TAG_SHIFT),
|
|
|
|
JSVAL_SHIFTED_TAG_OBJECT = (uint64_t(JSVAL_TAG_OBJECT) << JSVAL_TAG_SHIFT)
|
2018-03-15 12:38:59 +03:00
|
|
|
};
|
2010-07-22 05:57:01 +04:00
|
|
|
|
2014-11-08 05:33:20 +03:00
|
|
|
static_assert(sizeof(JSValueShiftedTag) == sizeof(uint64_t),
|
|
|
|
"compiler typed enum support is apparently buggy");
|
2010-07-17 01:00:41 +04:00
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
#endif
|
2019-06-12 20:47:42 +03:00
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
namespace JS {
|
|
|
|
namespace detail {
|
|
|
|
|
2014-05-21 17:47:10 +04:00
|
|
|
#if defined(JS_NUNBOX32)
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
constexpr JSValueTag ValueTypeToTag(JSValueType type) {
|
|
|
|
return static_cast<JSValueTag>(JSVAL_TAG_CLEAR | type);
|
|
|
|
}
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
constexpr bool ValueIsDouble(uint64_t bits) {
|
|
|
|
return uint32_t(bits >> JSVAL_TAG_SHIFT) <= uint32_t(JSVAL_TAG_CLEAR);
|
|
|
|
}
|
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
constexpr JSValueTag ValueUpperExclPrimitiveTag = JSVAL_TAG_OBJECT;
|
|
|
|
constexpr JSValueTag ValueUpperInclNumberTag = JSVAL_TAG_INT32;
|
|
|
|
constexpr JSValueTag ValueLowerInclGCThingTag = JSVAL_TAG_STRING;
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2014-05-21 17:47:10 +04:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-06-25 20:34:50 +03:00
|
|
|
constexpr JSValueTag ValueTypeToTag(JSValueType type) {
|
|
|
|
return static_cast<JSValueTag>(JSVAL_TAG_MAX_DOUBLE | type);
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
constexpr bool ValueIsDouble(uint64_t bits) {
|
|
|
|
return bits <= JSVAL_SHIFTED_TAG_MAX_DOUBLE;
|
|
|
|
}
|
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
constexpr uint64_t ValueTagMask = 0xFFFF'8000'0000'0000;
|
2018-01-24 14:33:53 +03:00
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
// This should only be used in toGCThing. See the 'Spectre mitigations' comment.
|
|
|
|
constexpr uint64_t ValueGCThingPayloadMask = 0x0000'7FFF'FFFF'FFFF;
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
constexpr uint64_t ValueTypeToShiftedTag(JSValueType type) {
|
|
|
|
return static_cast<uint64_t>(ValueTypeToTag(type)) << JSVAL_TAG_SHIFT;
|
|
|
|
}
|
|
|
|
# define JSVAL_TYPE_TO_SHIFTED_TAG(type) \
|
|
|
|
(JS::detail::ValueTypeToShiftedTag(type))
|
2019-06-12 20:51:06 +03:00
|
|
|
|
2019-06-25 20:34:50 +03:00
|
|
|
constexpr JSValueTag ValueUpperExclPrimitiveTag = JSVAL_TAG_OBJECT;
|
|
|
|
constexpr JSValueTag ValueUpperInclNumberTag = JSVAL_TAG_INT32;
|
|
|
|
constexpr JSValueTag ValueLowerInclGCThingTag = JSVAL_TAG_STRING;
|
2019-06-12 20:49:52 +03:00
|
|
|
|
2019-06-25 20:34:50 +03:00
|
|
|
constexpr uint64_t ValueUpperExclShiftedPrimitiveTag = JSVAL_SHIFTED_TAG_OBJECT;
|
|
|
|
constexpr uint64_t ValueUpperExclShiftedNumberTag = JSVAL_SHIFTED_TAG_BOOLEAN;
|
|
|
|
constexpr uint64_t ValueLowerInclShiftedGCThingTag = JSVAL_SHIFTED_TAG_STRING;
|
2019-06-12 20:49:52 +03:00
|
|
|
|
2019-06-25 20:34:50 +03:00
|
|
|
// JSVAL_TYPE_OBJECT and JSVAL_TYPE_NULL differ by one bit. We can use this to
|
|
|
|
// implement toObjectOrNull more efficiently.
|
|
|
|
constexpr uint64_t ValueObjectOrNullBit = 0x8ULL << JSVAL_TAG_SHIFT;
|
|
|
|
static_assert(
|
|
|
|
(JSVAL_SHIFTED_TAG_NULL ^ JSVAL_SHIFTED_TAG_OBJECT) == ValueObjectOrNullBit,
|
|
|
|
"ValueObjectOrNullBit must be consistent with object and null tags");
|
2018-01-24 14:33:53 +03:00
|
|
|
|
2019-07-25 02:19:57 +03:00
|
|
|
constexpr uint64_t IsValidUserModePointer(uint64_t bits) {
|
|
|
|
// All 64-bit platforms that we support actually have a 48-bit address space
|
|
|
|
// for user-mode pointers, with the top 16 bits all set to zero.
|
|
|
|
return (bits & 0xFFFF'0000'0000'0000) == 0;
|
|
|
|
}
|
2019-07-16 22:10:30 +03:00
|
|
|
|
2014-05-21 17:47:10 +04:00
|
|
|
#endif /* JS_PUNBOX64 */
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2019-06-12 20:49:52 +03:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
#define JSVAL_TYPE_TO_TAG(type) (JS::detail::ValueTypeToTag(type))
|
|
|
|
|
2018-03-15 12:38:59 +03:00
|
|
|
enum JSWhyMagic {
|
2015-10-17 20:27:16 +03:00
|
|
|
/** a hole in a native object's elements */
|
|
|
|
JS_ELEMENTS_HOLE,
|
|
|
|
|
|
|
|
/** there is not a pending iterator value */
|
|
|
|
JS_NO_ITER_VALUE,
|
|
|
|
|
|
|
|
/** exception value thrown when closing a generator */
|
|
|
|
JS_GENERATOR_CLOSING,
|
|
|
|
|
|
|
|
/** used in debug builds to catch tracing errors */
|
|
|
|
JS_ARG_POISON,
|
|
|
|
|
|
|
|
/** an empty subnode in the AST serializer */
|
|
|
|
JS_SERIALIZE_NO_NODE,
|
|
|
|
|
|
|
|
/** optimized-away 'arguments' value */
|
|
|
|
JS_OPTIMIZED_ARGUMENTS,
|
|
|
|
|
|
|
|
/** magic value passed to natives to indicate construction */
|
|
|
|
JS_IS_CONSTRUCTING,
|
|
|
|
|
|
|
|
/** see class js::HashableValue */
|
|
|
|
JS_HASH_KEY_EMPTY,
|
|
|
|
|
|
|
|
/** error while running Ion code */
|
|
|
|
JS_ION_ERROR,
|
|
|
|
|
|
|
|
/** missing recover instruction result */
|
|
|
|
JS_ION_BAILOUT,
|
|
|
|
|
|
|
|
/** optimized out slot */
|
|
|
|
JS_OPTIMIZED_OUT,
|
|
|
|
|
|
|
|
/** uninitialized lexical bindings that produce ReferenceError on touch. */
|
|
|
|
JS_UNINITIALIZED_LEXICAL,
|
|
|
|
|
2018-01-30 20:57:40 +03:00
|
|
|
/** standard constructors are not created for off-thread parsing. */
|
|
|
|
JS_OFF_THREAD_CONSTRUCTOR,
|
|
|
|
|
2018-08-02 10:11:57 +03:00
|
|
|
/** used in jit::TrySkipAwait */
|
|
|
|
JS_CANNOT_SKIP_AWAIT,
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/** for local use */
|
|
|
|
JS_GENERIC_MAGIC,
|
|
|
|
|
2019-10-31 01:35:28 +03:00
|
|
|
/**
|
|
|
|
* Write records queued up in WritableStreamDefaultController.[[queue]] in the
|
|
|
|
* spec are either "close" (a String) or Record { [[chunk]]: chunk }, where
|
|
|
|
* chunk is an arbitrary user-provided (and therefore non-magic) value.
|
|
|
|
* Represent "close" the String as this magic value; represent Record records
|
|
|
|
* as the |chunk| value within each of them.
|
|
|
|
*/
|
|
|
|
JS_WRITABLESTREAM_CLOSE_RECORD,
|
|
|
|
|
2020-06-24 21:40:45 +03:00
|
|
|
/**
|
|
|
|
* The ReadableStream pipe-to operation concludes with a "finalize" operation
|
|
|
|
* that accepts an optional |error| argument. In certain cases that optional
|
|
|
|
* |error| must be stored in a handler function, for use after a promise has
|
|
|
|
* settled. We represent the argument not being provided, in those cases,
|
|
|
|
* using this magic value.
|
|
|
|
*/
|
|
|
|
JS_READABLESTREAM_PIPETO_FINALIZE_WITHOUT_ERROR,
|
|
|
|
|
2014-11-01 02:46:26 +03:00
|
|
|
JS_WHY_MAGIC_COUNT
|
2018-03-15 12:38:59 +03:00
|
|
|
};
|
2010-07-01 04:52:02 +04:00
|
|
|
|
2017-09-15 13:04:40 +03:00
|
|
|
namespace js {
|
|
|
|
static inline JS::Value PoisonedObjectValue(uintptr_t poison);
|
|
|
|
} // namespace js
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
namespace JS {
|
|
|
|
|
2016-10-28 19:47:30 +03:00
|
|
|
namespace detail {
|
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
// IEEE-754 bit pattern for double-precision positive infinity.
|
|
|
|
constexpr int InfinitySignBit = 0;
|
|
|
|
constexpr uint64_t InfinityBits =
|
|
|
|
mozilla::InfinityBits<double, detail::InfinitySignBit>::value;
|
|
|
|
|
|
|
|
// This is a quiet NaN on IEEE-754[2008] compatible platforms, including X86,
|
|
|
|
// ARM, SPARC and modern MIPS.
|
|
|
|
//
|
|
|
|
// Note: The default sign bit for a hardware sythesized NaN differs between X86
|
|
|
|
// and ARM. Both values are considered compatible values on both
|
|
|
|
// platforms.
|
2016-10-28 19:47:30 +03:00
|
|
|
constexpr int CanonicalizedNaNSignBit = 0;
|
2019-06-12 20:49:52 +03:00
|
|
|
constexpr uint64_t CanonicalizedNaNSignificand = 0x8000000000000;
|
2016-10-28 19:47:30 +03:00
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
#if defined(__sparc__)
|
|
|
|
// Some architectures (not to name names) generate NaNs with bit patterns that
|
|
|
|
// are incompatible with JS::Value's bit pattern restrictions. Instead we must
|
|
|
|
// canonicalize all hardware values before storing in JS::Value.
|
|
|
|
# define JS_NONCANONICAL_HARDWARE_NAN
|
|
|
|
#endif
|
|
|
|
|
2020-05-27 15:30:26 +03:00
|
|
|
#if defined(__mips__) && !defined(__mips_nan_2008)
|
|
|
|
// These builds may run on hardware that has differing polarity of the signaling
|
|
|
|
// NaN bit. While the kernel may handle the trap for us, it is a performance
|
|
|
|
// issue so instead we compute the NaN to use on startup. The runtime value must
|
|
|
|
// still meet `ValueIsDouble` requirements which are checked on startup.
|
|
|
|
|
|
|
|
// In particular, we expect one of the following values on MIPS:
|
|
|
|
// - 0x7FF7FFFFFFFFFFFF Legacy
|
|
|
|
// - 0x7FF8000000000000 IEEE-754[2008]
|
|
|
|
# define JS_RUNTIME_CANONICAL_NAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(JS_RUNTIME_CANONICAL_NAN)
|
|
|
|
extern uint64_t CanonicalizedNaNBits;
|
|
|
|
#else
|
2016-10-28 19:47:30 +03:00
|
|
|
constexpr uint64_t CanonicalizedNaNBits =
|
|
|
|
mozilla::SpecificNaNBits<double, detail::CanonicalizedNaNSignBit,
|
|
|
|
detail::CanonicalizedNaNSignificand>::value;
|
2020-05-27 15:30:26 +03:00
|
|
|
#endif
|
2016-10-28 19:47:30 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
// Return a quiet NaN that is compatible with JS::Value restrictions.
|
2013-09-20 05:42:56 +04:00
|
|
|
static MOZ_ALWAYS_INLINE double GenericNaN() {
|
2020-05-27 15:30:26 +03:00
|
|
|
#if !defined(JS_RUNTIME_CANONICAL_NAN)
|
2020-05-22 01:23:26 +03:00
|
|
|
static_assert(detail::ValueIsDouble(detail::CanonicalizedNaNBits),
|
|
|
|
"Canonical NaN must be compatible with JS::Value");
|
2020-05-27 15:30:26 +03:00
|
|
|
#endif
|
2020-05-22 01:23:26 +03:00
|
|
|
|
|
|
|
return mozilla::BitwiseCast<double>(detail::CanonicalizedNaNBits);
|
2013-09-20 05:42:56 +04:00
|
|
|
}
|
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
// Convert an arbitrary double to one compatible with JS::Value representation
|
|
|
|
// by replacing any NaN value with a canonical one.
|
|
|
|
static MOZ_ALWAYS_INLINE double CanonicalizeNaN(double d) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (MOZ_UNLIKELY(mozilla::IsNaN(d))) {
|
2013-10-17 12:16:17 +04:00
|
|
|
return GenericNaN();
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2013-10-17 12:16:17 +04:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2018-06-26 19:02:59 +03:00
|
|
|
* [SMDOC] JS::Value type
|
|
|
|
*
|
2013-02-02 04:15:49 +04:00
|
|
|
* JS::Value is the interface for a single JavaScript Engine value. A few
|
|
|
|
* general notes on JS::Value:
|
|
|
|
*
|
|
|
|
* - JS::Value has setX() and isX() members for X in
|
|
|
|
*
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
* { Int32, Double, String, Symbol, BigInt, Boolean, Undefined, Null,
|
|
|
|
* Object, Magic }
|
2013-02-02 04:15:49 +04:00
|
|
|
*
|
|
|
|
* JS::Value also contains toX() for each of the non-singleton types.
|
|
|
|
*
|
2014-03-06 00:49:36 +04:00
|
|
|
* - Magic is a singleton type whose payload contains either a JSWhyMagic
|
|
|
|
* "reason" for the magic value or a uint32_t value. By providing JSWhyMagic
|
|
|
|
* values when creating and checking for magic values, it is possible to
|
|
|
|
* assert, at runtime, that only magic values with the expected reason flow
|
|
|
|
* through a particular value. For example, if cx->exception has a magic
|
|
|
|
* value, the reason must be JS_GENERATOR_CLOSING.
|
2013-02-02 04:15:49 +04:00
|
|
|
*
|
|
|
|
* - The JS::Value operations are preferred. The JSVAL_* operations remain for
|
|
|
|
* compatibility; they may be removed at some point. These operations mostly
|
|
|
|
* provide similar functionality. But there are a few key differences. One
|
2014-04-28 07:27:54 +04:00
|
|
|
* is that JS::Value gives null a separate type.
|
2013-02-02 04:15:49 +04:00
|
|
|
* Also, to help prevent mistakenly boxing a nullable JSObject* as an object,
|
2014-01-15 00:41:22 +04:00
|
|
|
* Value::setObject takes a JSObject&. (Conversely, Value::toObject returns a
|
2013-02-02 04:15:49 +04:00
|
|
|
* JSObject&.) A convenience member Value::setObjectOrNull is provided.
|
|
|
|
*
|
|
|
|
* - Note that JS::Value is 8 bytes on 32 and 64-bit architectures. Thus, on
|
|
|
|
* 32-bit user code should avoid copying jsval/JS::Value as much as possible,
|
2015-03-29 01:22:11 +03:00
|
|
|
* preferring to pass by const Value&.
|
2018-01-24 14:33:53 +03:00
|
|
|
*
|
|
|
|
* Spectre mitigations
|
|
|
|
* ===================
|
|
|
|
* To mitigate Spectre attacks, we do the following:
|
|
|
|
*
|
|
|
|
* - On 64-bit platforms, when unboxing a Value, we XOR the bits with the
|
|
|
|
* expected type tag (instead of masking the payload bits). This guarantees
|
|
|
|
* that toString, toObject, toSymbol will return an invalid pointer (because
|
|
|
|
* some high bits will be set) when called on a Value with a different type
|
|
|
|
* tag.
|
2018-02-02 16:39:24 +03:00
|
|
|
*
|
|
|
|
* - On 32-bit platforms,when unboxing an object/string/symbol Value, we use a
|
|
|
|
* conditional move (not speculated) to zero the payload register if the type
|
|
|
|
* doesn't match.
|
2013-02-02 04:15:49 +04:00
|
|
|
*/
|
2019-09-20 02:56:55 +03:00
|
|
|
class alignas(8) Value {
|
2018-03-28 06:20:43 +03:00
|
|
|
private:
|
|
|
|
uint64_t asBits_;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-28 06:20:43 +03:00
|
|
|
public:
|
2018-04-26 05:40:09 +03:00
|
|
|
constexpr Value() : asBits_(bitsFromTagAndPayload(JSVAL_TAG_UNDEFINED, 0)) {}
|
2018-03-28 06:20:43 +03:00
|
|
|
Value(const Value& v) = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-28 06:20:43 +03:00
|
|
|
private:
|
|
|
|
explicit constexpr Value(uint64_t asBits) : asBits_(asBits) {}
|
2019-06-11 18:16:40 +03:00
|
|
|
|
|
|
|
static uint64_t bitsFromDouble(double d) {
|
|
|
|
#if defined(JS_NONCANONICAL_HARDWARE_NAN)
|
|
|
|
d = CanonicalizeNaN(d);
|
|
|
|
#endif
|
|
|
|
return mozilla::BitwiseCast<uint64_t>(d);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-28 06:20:43 +03:00
|
|
|
static_assert(sizeof(JSValueType) == 1,
|
|
|
|
"type bits must fit in a single byte");
|
|
|
|
static_assert(sizeof(JSValueTag) == 4,
|
|
|
|
"32-bit Value's tag_ must have size 4 to complement the "
|
|
|
|
"payload union's size 4");
|
|
|
|
static_assert(sizeof(JSWhyMagic) <= 4,
|
|
|
|
"32-bit Value's JSWhyMagic payload field must not inflate "
|
|
|
|
"the payload beyond 4 bytes");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
public:
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
using PayloadType = uint32_t;
|
|
|
|
#elif defined(JS_PUNBOX64)
|
|
|
|
using PayloadType = uint64_t;
|
|
|
|
#endif
|
|
|
|
|
2018-03-28 06:20:43 +03:00
|
|
|
static constexpr uint64_t bitsFromTagAndPayload(JSValueTag tag,
|
|
|
|
PayloadType payload) {
|
2019-09-20 02:56:57 +03:00
|
|
|
return (uint64_t(tag) << JSVAL_TAG_SHIFT) | payload;
|
2018-03-28 06:20:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr Value fromTagAndPayload(JSValueTag tag,
|
|
|
|
PayloadType payload) {
|
|
|
|
return fromRawBits(bitsFromTagAndPayload(tag, payload));
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr Value fromRawBits(uint64_t asBits) { return Value(asBits); }
|
|
|
|
|
|
|
|
static constexpr Value fromInt32(int32_t i) {
|
|
|
|
return fromTagAndPayload(JSVAL_TAG_INT32, uint32_t(i));
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:16:40 +03:00
|
|
|
static Value fromDouble(double d) { return fromRawBits(bitsFromDouble(d)); }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-10-13 19:42:39 +03:00
|
|
|
/**
|
|
|
|
* Returns false if creating a NumberValue containing the given type would
|
|
|
|
* be lossy, true otherwise.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static bool isNumberRepresentable(const T t) {
|
|
|
|
return T(double(t)) == t;
|
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
/*** Mutators ***/
|
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
void setNull() {
|
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_NULL, 0);
|
|
|
|
MOZ_ASSERT(isNull());
|
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
void setUndefined() {
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_UNDEFINED, 0);
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(isUndefined());
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setInt32(int32_t i) {
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_INT32, uint32_t(i));
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(toInt32() == i);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setDouble(double d) {
|
2019-06-11 18:16:40 +03:00
|
|
|
asBits_ = bitsFromDouble(d);
|
2016-10-28 19:47:31 +03:00
|
|
|
MOZ_ASSERT(isDouble());
|
2016-10-18 10:46:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void setString(JSString* str) {
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(js::gc::IsCellPointerValid(str));
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_STRING, PayloadType(str));
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(toString() == str);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void setSymbol(JS::Symbol* sym) {
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(js::gc::IsCellPointerValid(sym));
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_SYMBOL, PayloadType(sym));
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(toSymbol() == sym);
|
2014-06-23 19:55:51 +04:00
|
|
|
}
|
|
|
|
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
void setBigInt(JS::BigInt* bi) {
|
|
|
|
MOZ_ASSERT(js::gc::IsCellPointerValid(bi));
|
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_BIGINT, PayloadType(bi));
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(toBigInt() == bi);
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void setObject(JSObject& obj) {
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(js::gc::IsCellPointerValid(&obj));
|
2016-10-18 10:45:59 +03:00
|
|
|
setObjectNoCheck(&obj);
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(&toObject() == &obj);
|
2016-10-18 10:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setObjectNoCheck(JSObject* obj) {
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_OBJECT, PayloadType(obj));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2017-09-15 13:04:40 +03:00
|
|
|
friend inline Value js::PoisonedObjectValue(uintptr_t poison);
|
2016-10-18 10:45:59 +03:00
|
|
|
|
|
|
|
public:
|
2013-02-02 04:15:49 +04:00
|
|
|
void setBoolean(bool b) {
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_BOOLEAN, uint32_t(b));
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(toBoolean() == b);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setMagic(JSWhyMagic why) {
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_MAGIC, uint32_t(why));
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(whyMagic() == why);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 00:49:36 +04:00
|
|
|
void setMagicUint32(uint32_t payload) {
|
2019-09-20 02:56:59 +03:00
|
|
|
MOZ_ASSERT(payload >= JS_WHY_MAGIC_COUNT,
|
|
|
|
"This should only be used for non-standard magic values");
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ = bitsFromTagAndPayload(JSVAL_TAG_MAGIC, payload);
|
2019-06-12 20:51:06 +03:00
|
|
|
MOZ_ASSERT(magicUint32() == payload);
|
2014-03-06 00:49:36 +04:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:21:46 +03:00
|
|
|
void setNumber(uint32_t ui) {
|
2013-02-02 04:15:49 +04:00
|
|
|
if (ui > JSVAL_INT_MAX) {
|
|
|
|
setDouble((double)ui);
|
2019-11-19 10:21:46 +03:00
|
|
|
return;
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
2019-11-19 10:21:46 +03:00
|
|
|
|
|
|
|
setInt32((int32_t)ui);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2019-11-19 10:21:46 +03:00
|
|
|
void setNumber(double d) {
|
2013-02-02 04:15:49 +04:00
|
|
|
int32_t i;
|
2014-02-27 19:23:11 +04:00
|
|
|
if (mozilla::NumberIsInt32(d, &i)) {
|
2013-02-02 04:15:49 +04:00
|
|
|
setInt32(i);
|
2019-11-19 10:21:46 +03:00
|
|
|
return;
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
setDouble(d);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void setObjectOrNull(JSObject* arg) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (arg) {
|
2018-03-28 06:20:43 +03:00
|
|
|
setObject(*arg);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2018-03-28 06:20:43 +03:00
|
|
|
setNull();
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void swap(Value& rhs) {
|
2018-03-28 06:20:43 +03:00
|
|
|
uint64_t tmp = rhs.asBits_;
|
|
|
|
rhs.asBits_ = asBits_;
|
|
|
|
asBits_ = tmp;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
private:
|
2019-09-20 02:56:57 +03:00
|
|
|
JSValueTag toTag() const { return JSValueTag(asBits_ >> JSVAL_TAG_SHIFT); }
|
2019-09-20 00:12:06 +03:00
|
|
|
|
2019-09-20 02:56:53 +03:00
|
|
|
template <typename T, JSValueTag Tag>
|
|
|
|
T* unboxGCPointer() const {
|
|
|
|
MOZ_ASSERT((asBits_ & js::gc::CellAlignMask) == 0,
|
|
|
|
"GC pointer is not aligned. Is this memory corruption?");
|
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
uintptr_t payload = uint32_t(asBits_);
|
|
|
|
return reinterpret_cast<T*>(payload);
|
|
|
|
#elif defined(JS_PUNBOX64)
|
|
|
|
// Note: the 'Spectre mitigations' comment at the top of this class
|
|
|
|
// explains why we use XOR here.
|
|
|
|
constexpr uint64_t shiftedTag = uint64_t(Tag) << JSVAL_TAG_SHIFT;
|
|
|
|
return reinterpret_cast<T*>(uintptr_t(asBits_ ^ shiftedTag));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
public:
|
2016-10-18 10:46:01 +03:00
|
|
|
/*** JIT-only interfaces to interact with and create raw Values ***/
|
|
|
|
#if defined(JS_NUNBOX32)
|
2019-09-20 02:56:57 +03:00
|
|
|
PayloadType toNunboxPayload() const { return uint32_t(asBits_); }
|
2016-10-18 10:46:01 +03:00
|
|
|
|
2019-09-20 02:56:57 +03:00
|
|
|
JSValueTag toNunboxTag() const { return toTag(); }
|
2016-10-18 10:46:01 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
|
|
|
const void* bitsAsPunboxPointer() const {
|
2018-03-28 06:20:43 +03:00
|
|
|
return reinterpret_cast<void*>(asBits_);
|
2016-10-18 10:46:01 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
/*** Value type queries ***/
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
/*
|
|
|
|
* N.B. GCC, in some but not all cases, chooses to emit signed comparison
|
|
|
|
* of JSValueTag even though its underlying type has been forced to be
|
|
|
|
* uint32_t. Thus, all comparisons should explicitly cast operands to
|
|
|
|
* uint32_t.
|
|
|
|
*/
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
bool isUndefined() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
return toTag() == JSVAL_TAG_UNDEFINED;
|
|
|
|
#elif defined(JS_PUNBOX64)
|
2018-03-28 06:20:43 +03:00
|
|
|
return asBits_ == JSVAL_SHIFTED_TAG_UNDEFINED;
|
2016-10-18 10:45:59 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isNull() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
return toTag() == JSVAL_TAG_NULL;
|
|
|
|
#elif defined(JS_PUNBOX64)
|
2018-03-28 06:20:43 +03:00
|
|
|
return asBits_ == JSVAL_SHIFTED_TAG_NULL;
|
2016-10-18 10:45:59 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isNullOrUndefined() const { return isNull() || isUndefined(); }
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
bool isInt32() const { return toTag() == JSVAL_TAG_INT32; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
bool isInt32(int32_t i32) const {
|
2018-03-28 06:20:43 +03:00
|
|
|
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_INT32, uint32_t(i32));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2020-05-22 01:23:26 +03:00
|
|
|
bool isDouble() const { return detail::ValueIsDouble(asBits_); }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
bool isNumber() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
MOZ_ASSERT(toTag() != JSVAL_TAG_CLEAR);
|
2019-06-12 20:49:52 +03:00
|
|
|
return uint32_t(toTag()) <= uint32_t(detail::ValueUpperInclNumberTag);
|
2016-10-18 10:45:59 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-25 20:34:50 +03:00
|
|
|
return asBits_ < detail::ValueUpperExclShiftedNumberTag;
|
2016-10-18 10:45:59 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
bool isString() const { return toTag() == JSVAL_TAG_STRING; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
bool isSymbol() const { return toTag() == JSVAL_TAG_SYMBOL; }
|
2014-06-23 19:55:51 +04:00
|
|
|
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
bool isBigInt() const { return toTag() == JSVAL_TAG_BIGINT; }
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
bool isObject() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
return toTag() == JSVAL_TAG_OBJECT;
|
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-25 20:34:50 +03:00
|
|
|
MOZ_ASSERT((asBits_ >> JSVAL_TAG_SHIFT) <= JSVAL_TAG_OBJECT);
|
|
|
|
return asBits_ >= JSVAL_SHIFTED_TAG_OBJECT;
|
2016-10-18 10:45:59 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isPrimitive() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
2019-06-12 20:49:52 +03:00
|
|
|
return uint32_t(toTag()) < uint32_t(detail::ValueUpperExclPrimitiveTag);
|
2016-10-18 10:45:59 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-25 20:34:50 +03:00
|
|
|
return asBits_ < detail::ValueUpperExclShiftedPrimitiveTag;
|
2016-10-18 10:45:59 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2016-12-26 18:40:21 +03:00
|
|
|
bool isObjectOrNull() const { return isObject() || isNull(); }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2019-07-19 14:41:19 +03:00
|
|
|
bool isNumeric() const { return isNumber() || isBigInt(); }
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
bool isGCThing() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
/* gcc sometimes generates signed < without explicit casts. */
|
2019-06-12 20:49:52 +03:00
|
|
|
return uint32_t(toTag()) >= uint32_t(detail::ValueLowerInclGCThingTag);
|
2016-10-18 10:45:59 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-25 20:34:50 +03:00
|
|
|
return asBits_ >= detail::ValueLowerInclShiftedGCThingTag;
|
2016-10-18 10:45:59 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
bool isBoolean() const { return toTag() == JSVAL_TAG_BOOLEAN; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
bool isTrue() const {
|
2018-03-28 06:20:43 +03:00
|
|
|
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_BOOLEAN, uint32_t(true));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isFalse() const {
|
2018-03-28 06:20:43 +03:00
|
|
|
return asBits_ == bitsFromTagAndPayload(JSVAL_TAG_BOOLEAN, uint32_t(false));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
bool isMagic() const { return toTag() == JSVAL_TAG_MAGIC; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
bool isMagic(JSWhyMagic why) const {
|
2019-03-06 23:23:43 +03:00
|
|
|
if (!isMagic()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-20 02:56:59 +03:00
|
|
|
MOZ_RELEASE_ASSERT(whyMagic() == why);
|
2019-03-06 23:23:43 +03:00
|
|
|
return true;
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 20:40:24 +03:00
|
|
|
JS::TraceKind traceKind() const {
|
2016-12-26 18:40:21 +03:00
|
|
|
MOZ_ASSERT(isGCThing());
|
2016-10-18 10:46:00 +03:00
|
|
|
static_assert((JSVAL_TAG_STRING & 0x03) == size_t(JS::TraceKind::String),
|
|
|
|
"Value type tags must correspond with JS::TraceKinds.");
|
|
|
|
static_assert((JSVAL_TAG_SYMBOL & 0x03) == size_t(JS::TraceKind::Symbol),
|
|
|
|
"Value type tags must correspond with JS::TraceKinds.");
|
|
|
|
static_assert((JSVAL_TAG_OBJECT & 0x03) == size_t(JS::TraceKind::Object),
|
|
|
|
"Value type tags must correspond with JS::TraceKinds.");
|
2020-01-02 16:03:20 +03:00
|
|
|
static_assert((JSVAL_TAG_BIGINT & 0x03) == size_t(JS::TraceKind::BigInt),
|
|
|
|
"Value type tags must correspond with JS::TraceKinds.");
|
2018-09-06 13:11:07 +03:00
|
|
|
if (MOZ_UNLIKELY(isPrivateGCThing())) {
|
2016-10-18 10:46:00 +03:00
|
|
|
return JS::GCThingTraceKind(toGCThing());
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2016-10-18 10:46:00 +03:00
|
|
|
return JS::TraceKind(toTag() & 0x03);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
JSWhyMagic whyMagic() const {
|
2019-09-20 02:56:59 +03:00
|
|
|
MOZ_ASSERT(magicUint32() < JS_WHY_MAGIC_COUNT);
|
|
|
|
return static_cast<JSWhyMagic>(magicUint32());
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 00:49:36 +04:00
|
|
|
uint32_t magicUint32() const {
|
|
|
|
MOZ_ASSERT(isMagic());
|
2019-09-20 02:56:59 +03:00
|
|
|
return uint32_t(asBits_);
|
2014-03-06 00:49:36 +04:00
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
/*** Comparison ***/
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool operator==(const Value& rhs) const { return asBits_ == rhs.asBits_; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool operator!=(const Value& rhs) const { return asBits_ != rhs.asBits_; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
friend inline bool SameType(const Value& lhs, const Value& rhs);
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
/*** Extract the value's typed payload ***/
|
|
|
|
|
|
|
|
int32_t toInt32() const {
|
|
|
|
MOZ_ASSERT(isInt32());
|
2018-03-28 06:20:43 +03:00
|
|
|
return int32_t(asBits_);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double toDouble() const {
|
|
|
|
MOZ_ASSERT(isDouble());
|
2019-06-12 20:51:06 +03:00
|
|
|
return mozilla::BitwiseCast<double>(asBits_);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double toNumber() const {
|
|
|
|
MOZ_ASSERT(isNumber());
|
|
|
|
return isDouble() ? toDouble() : double(toInt32());
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSString* toString() const {
|
2013-02-02 04:15:49 +04:00
|
|
|
MOZ_ASSERT(isString());
|
2019-09-20 02:56:53 +03:00
|
|
|
return unboxGCPointer<JSString, JSVAL_TAG_STRING>();
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS::Symbol* toSymbol() const {
|
2014-06-23 19:55:51 +04:00
|
|
|
MOZ_ASSERT(isSymbol());
|
2019-09-20 02:56:53 +03:00
|
|
|
return unboxGCPointer<JS::Symbol, JSVAL_TAG_SYMBOL>();
|
2014-06-23 19:55:51 +04:00
|
|
|
}
|
|
|
|
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
JS::BigInt* toBigInt() const {
|
|
|
|
MOZ_ASSERT(isBigInt());
|
2019-09-20 02:56:53 +03:00
|
|
|
return unboxGCPointer<JS::BigInt, JSVAL_TAG_BIGINT>();
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject& toObject() const {
|
2013-02-02 04:15:49 +04:00
|
|
|
MOZ_ASSERT(isObject());
|
2019-09-20 02:56:53 +03:00
|
|
|
#if defined(JS_PUNBOX64)
|
|
|
|
MOZ_ASSERT((asBits_ & detail::ValueGCThingPayloadMask) != 0);
|
2016-10-18 10:46:00 +03:00
|
|
|
#endif
|
2019-09-20 02:56:53 +03:00
|
|
|
return *unboxGCPointer<JSObject, JSVAL_TAG_OBJECT>();
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* toObjectOrNull() const {
|
2013-02-02 04:15:49 +04:00
|
|
|
MOZ_ASSERT(isObjectOrNull());
|
2016-10-18 10:46:00 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
2019-09-20 02:56:57 +03:00
|
|
|
return reinterpret_cast<JSObject*>(uintptr_t(asBits_));
|
2016-10-18 10:46:00 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-25 20:34:50 +03:00
|
|
|
// Note: the 'Spectre mitigations' comment at the top of this class
|
|
|
|
// explains why we use XOR here and in other to* methods.
|
|
|
|
uint64_t ptrBits =
|
|
|
|
(asBits_ ^ JSVAL_SHIFTED_TAG_OBJECT) & ~detail::ValueObjectOrNullBit;
|
2016-10-18 10:46:00 +03:00
|
|
|
MOZ_ASSERT((ptrBits & 0x7) == 0);
|
|
|
|
return reinterpret_cast<JSObject*>(ptrBits);
|
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
js::gc::Cell* toGCThing() const {
|
2013-02-02 04:15:49 +04:00
|
|
|
MOZ_ASSERT(isGCThing());
|
2016-10-18 10:46:00 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
2019-09-20 02:56:57 +03:00
|
|
|
return reinterpret_cast<js::gc::Cell*>(uintptr_t(asBits_));
|
2016-10-18 10:46:00 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-12 20:49:52 +03:00
|
|
|
uint64_t ptrBits = asBits_ & detail::ValueGCThingPayloadMask;
|
2016-10-18 10:46:00 +03:00
|
|
|
MOZ_ASSERT((ptrBits & 0x7) == 0);
|
|
|
|
return reinterpret_cast<js::gc::Cell*>(ptrBits);
|
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 20:40:24 +03:00
|
|
|
GCCellPtr toGCCellPtr() const { return GCCellPtr(toGCThing(), traceKind()); }
|
2014-12-05 20:38:34 +03:00
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
bool toBoolean() const {
|
|
|
|
MOZ_ASSERT(isBoolean());
|
2016-10-18 10:46:00 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
2019-09-20 02:56:57 +03:00
|
|
|
return bool(toNunboxPayload());
|
2016-10-18 10:46:00 +03:00
|
|
|
#elif defined(JS_PUNBOX64)
|
2019-06-12 20:51:06 +03:00
|
|
|
return bool(asBits_ & 0x1);
|
2016-10-18 10:46:00 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t payloadAsRawUint32() const {
|
|
|
|
MOZ_ASSERT(!isDouble());
|
2019-09-20 02:56:59 +03:00
|
|
|
return uint32_t(asBits_);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2020-04-17 20:52:57 +03:00
|
|
|
constexpr uint64_t asRawBits() const { return asBits_; }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
JSValueType extractNonDoubleType() const {
|
2016-10-18 10:46:00 +03:00
|
|
|
uint32_t type = toTag() & 0xF;
|
|
|
|
MOZ_ASSERT(type > JSVAL_TYPE_DOUBLE);
|
|
|
|
return JSValueType(type);
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2019-02-21 23:36:44 +03:00
|
|
|
JS::ValueType type() const {
|
|
|
|
if (isDouble()) {
|
|
|
|
return JS::ValueType::Double;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValueType type = extractNonDoubleType();
|
|
|
|
MOZ_ASSERT(type <= JSVAL_TYPE_OBJECT);
|
|
|
|
return JS::ValueType(type);
|
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
/*
|
|
|
|
* Private API
|
|
|
|
*
|
2019-07-16 22:10:30 +03:00
|
|
|
* Private setters/getters allow the caller to read/write arbitrary
|
|
|
|
* word-size pointers or uint32s. After storing to a value with
|
|
|
|
* setPrivateX, it is the caller's responsibility to only read using
|
|
|
|
* toPrivateX. Private values are given a type which ensures they
|
|
|
|
* aren't marked by the GC.
|
2013-02-02 04:15:49 +04:00
|
|
|
*/
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void setPrivate(void* ptr) {
|
2019-09-20 02:56:57 +03:00
|
|
|
#if defined(JS_PUNBOX64)
|
2019-07-25 02:19:57 +03:00
|
|
|
MOZ_ASSERT(detail::IsValidUserModePointer(uintptr_t(ptr)));
|
2019-09-20 02:52:10 +03:00
|
|
|
#endif
|
2019-09-20 02:56:57 +03:00
|
|
|
asBits_ = uintptr_t(ptr);
|
2016-10-18 10:45:59 +03:00
|
|
|
MOZ_ASSERT(isDouble());
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void* toPrivate() const {
|
2016-10-18 10:45:59 +03:00
|
|
|
MOZ_ASSERT(isDouble());
|
2019-09-20 02:56:57 +03:00
|
|
|
#if defined(JS_PUNBOX64)
|
2019-07-25 02:19:57 +03:00
|
|
|
MOZ_ASSERT(detail::IsValidUserModePointer(asBits_));
|
2016-10-18 10:46:00 +03:00
|
|
|
#endif
|
2019-09-20 02:56:57 +03:00
|
|
|
return reinterpret_cast<void*>(uintptr_t(asBits_));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setPrivateUint32(uint32_t ui) {
|
2013-02-02 06:41:35 +04:00
|
|
|
MOZ_ASSERT(uint32_t(int32_t(ui)) == ui);
|
|
|
|
setInt32(int32_t(ui));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:17:12 +03:00
|
|
|
uint32_t toPrivateUint32() const { return uint32_t(toInt32()); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-04-30 04:10:07 +03:00
|
|
|
/*
|
|
|
|
* Private GC Thing API
|
|
|
|
*
|
|
|
|
* Non-JSObject, JSString, and JS::Symbol cells may be put into the 64-bit
|
2016-12-26 18:40:21 +03:00
|
|
|
* payload as private GC things. Such Values are considered isGCThing(), and
|
|
|
|
* as such, automatically marked. Their traceKind() is gotten via their
|
|
|
|
* cells.
|
2016-04-30 04:10:07 +03:00
|
|
|
*/
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-04-30 04:10:07 +03:00
|
|
|
void setPrivateGCThing(js::gc::Cell* cell) {
|
2016-10-18 10:45:59 +03:00
|
|
|
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::String,
|
|
|
|
"Private GC thing Values must not be strings. Make a "
|
|
|
|
"StringValue instead.");
|
|
|
|
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::Symbol,
|
|
|
|
"Private GC thing Values must not be symbols. Make a "
|
|
|
|
"SymbolValue instead.");
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::BigInt,
|
|
|
|
"Private GC thing Values must not be BigInts. Make a "
|
|
|
|
"BigIntValue instead.");
|
2016-10-18 10:45:59 +03:00
|
|
|
MOZ_ASSERT(JS::GCThingTraceKind(cell) != JS::TraceKind::Object,
|
|
|
|
"Private GC thing Values must not be objects. Make an "
|
|
|
|
"ObjectValue instead.");
|
|
|
|
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(js::gc::IsCellPointerValid(cell));
|
2016-10-18 10:45:59 +03:00
|
|
|
#if defined(JS_PUNBOX64)
|
|
|
|
// VisualStudio cannot contain parenthesized C++ style cast and shift
|
|
|
|
// inside decltype in template parameter:
|
|
|
|
// AssertionConditionType<decltype((uintptr_t(x) >> 1))>
|
|
|
|
// It throws syntax error.
|
|
|
|
MOZ_ASSERT((((uintptr_t)cell) >> JSVAL_TAG_SHIFT) == 0);
|
|
|
|
#endif
|
2018-03-28 06:20:43 +03:00
|
|
|
asBits_ =
|
|
|
|
bitsFromTagAndPayload(JSVAL_TAG_PRIVATE_GCTHING, PayloadType(cell));
|
2016-04-30 04:10:07 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:45:59 +03:00
|
|
|
bool isPrivateGCThing() const { return toTag() == JSVAL_TAG_PRIVATE_GCTHING; }
|
2018-07-21 04:36:20 +03:00
|
|
|
} JS_HAZ_GC_POINTER MOZ_NON_PARAM;
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2018-03-27 22:26:10 +03:00
|
|
|
static_assert(sizeof(Value) == 8,
|
|
|
|
"Value size must leave three tag bits, be a binary power, and "
|
|
|
|
"is ubiquitously depended upon everywhere");
|
2016-10-18 10:46:01 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline bool IsOptimizedPlaceholderMagicValue(const Value& v) {
|
2014-04-24 12:59:36 +04:00
|
|
|
if (v.isMagic()) {
|
|
|
|
MOZ_ASSERT(v.whyMagic() == JS_OPTIMIZED_ARGUMENTS ||
|
|
|
|
v.whyMagic() == JS_OPTIMIZED_OUT);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static MOZ_ALWAYS_INLINE void ExposeValueToActiveJS(const Value& v) {
|
2017-04-26 13:18:39 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
Value tmp = v;
|
|
|
|
MOZ_ASSERT(!js::gc::EdgeNeedsSweepUnbarrieredSlow(&tmp));
|
|
|
|
#endif
|
2018-09-06 13:11:07 +03:00
|
|
|
if (v.isGCThing()) {
|
2014-05-20 01:58:05 +04:00
|
|
|
js::gc::ExposeGCThingToActiveJS(GCCellPtr(v));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2014-06-16 22:59:45 +04:00
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
/************************************************************************/
|
|
|
|
|
2017-02-28 22:41:25 +03:00
|
|
|
static inline MOZ_MAY_CALL_AFTER_MUST_RETURN Value NullValue() {
|
2013-02-02 04:15:49 +04:00
|
|
|
Value v;
|
|
|
|
v.setNull();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
static constexpr Value UndefinedValue() { return Value(); }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
static constexpr Value Int32Value(int32_t i32) { return Value::fromInt32(i32); }
|
2013-02-02 04:15:49 +04:00
|
|
|
|
|
|
|
static inline Value DoubleValue(double dbl) {
|
|
|
|
Value v;
|
|
|
|
v.setDouble(dbl);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:51:52 +03:00
|
|
|
static inline Value CanonicalizedDoubleValue(double d) {
|
2019-06-03 18:59:17 +03:00
|
|
|
return Value::fromDouble(CanonicalizeNaN(d));
|
2015-07-17 12:51:52 +03:00
|
|
|
}
|
|
|
|
|
2020-05-27 15:30:26 +03:00
|
|
|
static inline Value NaNValue() {
|
2019-06-25 20:34:50 +03:00
|
|
|
return Value::fromRawBits(detail::CanonicalizedNaNBits);
|
2019-06-03 18:59:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value InfinityValue() {
|
2019-06-25 20:34:50 +03:00
|
|
|
return Value::fromRawBits(detail::InfinityBits);
|
2013-09-20 05:42:56 +04:00
|
|
|
}
|
|
|
|
|
2013-07-19 02:13:15 +04:00
|
|
|
static inline Value Float32Value(float f) {
|
|
|
|
Value v;
|
|
|
|
v.setDouble(f);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static inline Value StringValue(JSString* str) {
|
2013-02-02 04:15:49 +04:00
|
|
|
Value v;
|
|
|
|
v.setString(str);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static inline Value SymbolValue(JS::Symbol* sym) {
|
2014-06-23 19:55:51 +04:00
|
|
|
Value v;
|
|
|
|
v.setSymbol(sym);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
static inline Value BigIntValue(JS::BigInt* bi) {
|
|
|
|
Value v;
|
|
|
|
v.setBigInt(bi);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
static inline Value BooleanValue(bool boo) {
|
|
|
|
Value v;
|
|
|
|
v.setBoolean(boo);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-11-16 06:46:39 +04:00
|
|
|
static inline Value TrueValue() {
|
|
|
|
Value v;
|
|
|
|
v.setBoolean(true);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value FalseValue() {
|
|
|
|
Value v;
|
|
|
|
v.setBoolean(false);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static inline Value ObjectValue(JSObject& obj) {
|
2013-02-02 04:15:49 +04:00
|
|
|
Value v;
|
|
|
|
v.setObject(obj);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value MagicValue(JSWhyMagic why) {
|
|
|
|
Value v;
|
|
|
|
v.setMagic(why);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-03-06 00:49:36 +04:00
|
|
|
static inline Value MagicValueUint32(uint32_t payload) {
|
|
|
|
Value v;
|
|
|
|
v.setMagicUint32(payload);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
static inline Value NumberValue(float f) {
|
|
|
|
Value v;
|
|
|
|
v.setNumber(f);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value NumberValue(double dbl) {
|
|
|
|
Value v;
|
|
|
|
v.setNumber(dbl);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value NumberValue(int8_t i) { return Int32Value(i); }
|
|
|
|
|
|
|
|
static inline Value NumberValue(uint8_t i) { return Int32Value(i); }
|
|
|
|
|
|
|
|
static inline Value NumberValue(int16_t i) { return Int32Value(i); }
|
|
|
|
|
|
|
|
static inline Value NumberValue(uint16_t i) { return Int32Value(i); }
|
|
|
|
|
|
|
|
static inline Value NumberValue(int32_t i) { return Int32Value(i); }
|
|
|
|
|
2019-06-12 20:51:06 +03:00
|
|
|
static constexpr Value NumberValue(uint32_t i) {
|
2015-07-17 12:51:54 +03:00
|
|
|
return i <= JSVAL_INT_MAX ? Int32Value(int32_t(i))
|
2016-10-28 19:47:31 +03:00
|
|
|
: Value::fromDouble(double(i));
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <bool Signed>
|
|
|
|
class MakeNumberValue {
|
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
static inline Value create(const T t) {
|
|
|
|
Value v;
|
2018-09-06 13:11:07 +03:00
|
|
|
if (JSVAL_INT_MIN <= t && t <= JSVAL_INT_MAX) {
|
2013-02-02 04:15:49 +04:00
|
|
|
v.setInt32(int32_t(t));
|
2018-09-06 13:11:07 +03:00
|
|
|
} else {
|
2013-02-02 04:15:49 +04:00
|
|
|
v.setDouble(double(t));
|
|
|
|
}
|
|
|
|
return v;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class MakeNumberValue<false> {
|
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
static inline Value create(const T t) {
|
|
|
|
Value v;
|
2018-09-06 13:11:07 +03:00
|
|
|
if (t <= JSVAL_INT_MAX) {
|
2013-02-02 04:15:49 +04:00
|
|
|
v.setInt32(int32_t(t));
|
2018-09-06 13:11:07 +03:00
|
|
|
} else {
|
2013-02-02 04:15:49 +04:00
|
|
|
v.setDouble(double(t));
|
|
|
|
}
|
|
|
|
return v;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline Value NumberValue(const T t) {
|
2015-10-13 19:42:39 +03:00
|
|
|
MOZ_ASSERT(Value::isNumberRepresentable(t), "value creation would be lossy");
|
2013-02-02 04:15:49 +04:00
|
|
|
return detail::MakeNumberValue<std::numeric_limits<T>::is_signed>::create(t);
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static inline Value ObjectOrNullValue(JSObject* obj) {
|
2013-02-02 04:15:49 +04:00
|
|
|
Value v;
|
|
|
|
v.setObjectOrNull(obj);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static inline Value PrivateValue(void* ptr) {
|
2013-02-02 04:15:49 +04:00
|
|
|
Value v;
|
|
|
|
v.setPrivate(ptr);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value PrivateUint32Value(uint32_t ui) {
|
|
|
|
Value v;
|
|
|
|
v.setPrivateUint32(ui);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-04-30 04:10:07 +03:00
|
|
|
static inline Value PrivateGCThingValue(js::gc::Cell* cell) {
|
|
|
|
Value v;
|
|
|
|
v.setPrivateGCThing(cell);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline bool SameType(const Value& lhs, const Value& rhs) {
|
2016-10-18 10:46:00 +03:00
|
|
|
#if defined(JS_NUNBOX32)
|
|
|
|
JSValueTag ltag = lhs.toTag(), rtag = rhs.toTag();
|
|
|
|
return ltag == rtag || (ltag < JSVAL_TAG_CLEAR && rtag < JSVAL_TAG_CLEAR);
|
|
|
|
#elif defined(JS_PUNBOX64)
|
|
|
|
return (lhs.isDouble() && rhs.isDouble()) ||
|
2018-03-28 06:20:43 +03:00
|
|
|
(((lhs.asBits_ ^ rhs.asBits_) & 0xFFFF800000000000ULL) == 0);
|
2016-10-18 10:46:00 +03:00
|
|
|
#endif
|
2013-02-02 04:15:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
2013-05-27 15:51:25 +04:00
|
|
|
namespace JS {
|
2019-09-20 13:09:19 +03:00
|
|
|
JS_PUBLIC_API void HeapValuePostWriteBarrier(Value* valuep, const Value& prev,
|
|
|
|
const Value& next);
|
2019-03-27 19:26:09 +03:00
|
|
|
JS_PUBLIC_API void HeapValueWriteBarriers(Value* valuep, const Value& prev,
|
|
|
|
const Value& next);
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
template <>
|
|
|
|
struct GCPolicy<JS::Value> {
|
2016-04-26 19:18:48 +03:00
|
|
|
static void trace(JSTracer* trc, Value* v, const char* name) {
|
2019-10-09 13:30:02 +03:00
|
|
|
// It's not safe to trace unbarriered pointers except as part of root
|
|
|
|
// marking.
|
|
|
|
UnsafeTraceRoot(trc, v, name);
|
2016-01-26 23:53:35 +03:00
|
|
|
}
|
2016-04-04 21:50:12 +03:00
|
|
|
static bool isTenured(const Value& thing) {
|
|
|
|
return !thing.isGCThing() || !IsInsideNursery(thing.toGCThing());
|
|
|
|
}
|
2017-09-19 14:31:31 +03:00
|
|
|
static bool isValid(const Value& value) {
|
|
|
|
return !value.isGCThing() || js::gc::IsCellPointerValid(value.toGCThing());
|
|
|
|
}
|
2013-02-02 04:15:49 +04:00
|
|
|
};
|
|
|
|
|
2016-04-26 19:18:48 +03:00
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
template <>
|
|
|
|
struct BarrierMethods<JS::Value> {
|
2015-03-29 01:22:11 +03:00
|
|
|
static gc::Cell* asGCThingOrNull(const JS::Value& v) {
|
2016-12-26 18:40:21 +03:00
|
|
|
return v.isGCThing() ? v.toGCThing() : nullptr;
|
2014-08-01 01:43:45 +04:00
|
|
|
}
|
2019-09-20 13:09:19 +03:00
|
|
|
static void postWriteBarrier(JS::Value* v, const JS::Value& prev,
|
|
|
|
const JS::Value& next) {
|
|
|
|
JS::HeapValuePostWriteBarrier(v, prev, next);
|
2014-05-01 20:26:12 +04:00
|
|
|
}
|
2016-10-18 10:45:57 +03:00
|
|
|
static void exposeToJS(const JS::Value& v) { JS::ExposeValueToActiveJS(v); }
|
2013-02-02 04:15:49 +04:00
|
|
|
};
|
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <class Wrapper>
|
|
|
|
class MutableValueOperations;
|
2013-02-02 04:15:49 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2013-02-02 04:15:49 +04:00
|
|
|
* A class designed for CRTP use in implementing the non-mutating parts of the
|
2017-01-10 13:12:14 +03:00
|
|
|
* Value interface in Value-like classes. Wrapper must be a class inheriting
|
|
|
|
* ValueOperations<Wrapper> with a visible get() method returning a const
|
|
|
|
* reference to the Value abstracted by Wrapper.
|
2013-02-02 04:15:49 +04:00
|
|
|
*/
|
2017-01-10 13:12:14 +03:00
|
|
|
template <class Wrapper>
|
|
|
|
class WrappedPtrOperations<JS::Value, Wrapper> {
|
|
|
|
const JS::Value& value() const {
|
|
|
|
return static_cast<const Wrapper*>(this)->get();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
public:
|
2015-08-05 12:38:00 +03:00
|
|
|
bool isUndefined() const { return value().isUndefined(); }
|
|
|
|
bool isNull() const { return value().isNull(); }
|
|
|
|
bool isBoolean() const { return value().isBoolean(); }
|
|
|
|
bool isTrue() const { return value().isTrue(); }
|
|
|
|
bool isFalse() const { return value().isFalse(); }
|
|
|
|
bool isNumber() const { return value().isNumber(); }
|
|
|
|
bool isInt32() const { return value().isInt32(); }
|
|
|
|
bool isInt32(int32_t i32) const { return value().isInt32(i32); }
|
|
|
|
bool isDouble() const { return value().isDouble(); }
|
|
|
|
bool isString() const { return value().isString(); }
|
|
|
|
bool isSymbol() const { return value().isSymbol(); }
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
bool isBigInt() const { return value().isBigInt(); }
|
2015-08-05 12:38:00 +03:00
|
|
|
bool isObject() const { return value().isObject(); }
|
|
|
|
bool isMagic() const { return value().isMagic(); }
|
|
|
|
bool isMagic(JSWhyMagic why) const { return value().isMagic(why); }
|
|
|
|
bool isGCThing() const { return value().isGCThing(); }
|
2016-12-26 18:40:21 +03:00
|
|
|
bool isPrimitive() const { return value().isPrimitive(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-05 12:38:00 +03:00
|
|
|
bool isNullOrUndefined() const { return value().isNullOrUndefined(); }
|
|
|
|
bool isObjectOrNull() const { return value().isObjectOrNull(); }
|
2019-07-19 14:41:19 +03:00
|
|
|
bool isNumeric() const { return value().isNumeric(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-05 12:38:00 +03:00
|
|
|
bool toBoolean() const { return value().toBoolean(); }
|
|
|
|
double toNumber() const { return value().toNumber(); }
|
|
|
|
int32_t toInt32() const { return value().toInt32(); }
|
|
|
|
double toDouble() const { return value().toDouble(); }
|
|
|
|
JSString* toString() const { return value().toString(); }
|
|
|
|
JS::Symbol* toSymbol() const { return value().toSymbol(); }
|
Bug 1366287 - Part 1.0: Define a new BigInt primitive type, with a GDB prettyprinter, Rust binding support, and a new out-of-line TraceKind. (Disabled by default, implemented only incompletely, currently passing --enable-bigint will disable JITs, will be flipped on Eventually once every sub-aspect is in place, Don't Have A Cow, Man.) r=jwalden, r=Ms2ger, r=sfink
--HG--
extra : rebase_source : aa13bd94bc6157ff8134894e3ba2e7a2b53e28d9
2018-05-24 21:26:09 +03:00
|
|
|
JS::BigInt* toBigInt() const { return value().toBigInt(); }
|
2015-08-05 12:38:00 +03:00
|
|
|
JSObject& toObject() const { return value().toObject(); }
|
|
|
|
JSObject* toObjectOrNull() const { return value().toObjectOrNull(); }
|
|
|
|
gc::Cell* toGCThing() const { return value().toGCThing(); }
|
|
|
|
JS::TraceKind traceKind() const { return value().traceKind(); }
|
2016-05-23 23:17:12 +03:00
|
|
|
void* toPrivate() const { return value().toPrivate(); }
|
|
|
|
uint32_t toPrivateUint32() const { return value().toPrivateUint32(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-05-23 23:17:12 +03:00
|
|
|
uint64_t asRawBits() const { return value().asRawBits(); }
|
2015-08-05 12:38:00 +03:00
|
|
|
JSValueType extractNonDoubleType() const {
|
|
|
|
return value().extractNonDoubleType();
|
|
|
|
}
|
2019-02-21 23:36:44 +03:00
|
|
|
JS::ValueType type() const { return value().type(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-05 12:38:00 +03:00
|
|
|
JSWhyMagic whyMagic() const { return value().whyMagic(); }
|
|
|
|
uint32_t magicUint32() const { return value().magicUint32(); }
|
2013-02-02 04:15:49 +04:00
|
|
|
};
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2013-11-20 02:53:36 +04:00
|
|
|
* A class designed for CRTP use in implementing all the mutating parts of the
|
2017-01-10 13:12:14 +03:00
|
|
|
* Value interface in Value-like classes. Wrapper must be a class inheriting
|
|
|
|
* MutableWrappedPtrOperations<Wrapper> with visible get() methods returning
|
|
|
|
* const and non-const references to the Value abstracted by Wrapper.
|
2013-02-02 04:15:49 +04:00
|
|
|
*/
|
2017-01-10 13:12:14 +03:00
|
|
|
template <class Wrapper>
|
|
|
|
class MutableWrappedPtrOperations<JS::Value, Wrapper>
|
|
|
|
: public WrappedPtrOperations<JS::Value, Wrapper> {
|
2019-11-20 02:40:02 +03:00
|
|
|
protected:
|
|
|
|
void set(const JS::Value& v) {
|
|
|
|
// Call Wrapper::set to trigger any barriers.
|
|
|
|
static_cast<Wrapper*>(this)->set(v);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
public:
|
2019-11-20 02:40:02 +03:00
|
|
|
void setNull() { set(JS::NullValue()); }
|
|
|
|
void setUndefined() { set(JS::UndefinedValue()); }
|
|
|
|
void setInt32(int32_t i) { set(JS::Int32Value(i)); }
|
|
|
|
void setDouble(double d) { set(JS::DoubleValue(d)); }
|
2020-05-22 01:23:26 +03:00
|
|
|
void setNaN() { set(JS::NaNValue()); }
|
|
|
|
void setInfinity() { set(JS::InfinityValue()); }
|
2019-11-20 02:40:02 +03:00
|
|
|
void setBoolean(bool b) { set(JS::BooleanValue(b)); }
|
|
|
|
void setMagic(JSWhyMagic why) { set(JS::MagicValue(why)); }
|
|
|
|
void setNumber(uint32_t ui) { set(JS::NumberValue(ui)); }
|
|
|
|
void setNumber(double d) { set(JS::NumberValue(d)); }
|
|
|
|
void setString(JSString* str) { set(JS::StringValue(str)); }
|
|
|
|
void setSymbol(JS::Symbol* sym) { set(JS::SymbolValue(sym)); }
|
|
|
|
void setBigInt(JS::BigInt* bi) { set(JS::BigIntValue(bi)); }
|
|
|
|
void setObject(JSObject& obj) { set(JS::ObjectValue(obj)); }
|
|
|
|
void setObjectOrNull(JSObject* arg) { set(JS::ObjectOrNullValue(arg)); }
|
|
|
|
void setPrivate(void* ptr) { set(JS::PrivateValue(ptr)); }
|
|
|
|
void setPrivateUint32(uint32_t ui) { set(JS::PrivateUint32Value(ui)); }
|
2016-04-30 04:10:07 +03:00
|
|
|
void setPrivateGCThing(js::gc::Cell* cell) {
|
2019-11-20 02:40:02 +03:00
|
|
|
set(JS::PrivateGCThingValue(cell));
|
2016-04-30 04:10:07 +03:00
|
|
|
}
|
2013-06-18 14:00:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Augment the generic Heap<T> interface when T = Value with
|
|
|
|
* type-querying, value-extracting, and mutating operations.
|
2013-05-27 15:51:25 +04:00
|
|
|
*/
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename Wrapper>
|
|
|
|
class HeapBase<JS::Value, Wrapper>
|
2019-11-20 02:40:02 +03:00
|
|
|
: public MutableWrappedPtrOperations<JS::Value, Wrapper> {
|
2013-06-18 14:00:37 +04:00
|
|
|
public:
|
2019-11-20 02:40:02 +03:00
|
|
|
void setMagic(JSWhyMagic why) { this->set(JS::MagicValueUint32(why)); }
|
2013-11-12 15:21:01 +04:00
|
|
|
|
2019-11-19 10:21:46 +03:00
|
|
|
void setNumber(uint32_t ui) {
|
2014-02-27 19:23:11 +04:00
|
|
|
if (ui > JSVAL_INT_MAX) {
|
2019-11-20 02:40:02 +03:00
|
|
|
this->setDouble((double)ui);
|
2019-11-19 10:21:46 +03:00
|
|
|
return;
|
2013-11-12 15:21:01 +04:00
|
|
|
}
|
2019-11-19 10:21:46 +03:00
|
|
|
|
2019-11-20 02:40:02 +03:00
|
|
|
this->setInt32((int32_t)ui);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-11-12 15:21:01 +04:00
|
|
|
|
2019-11-19 10:21:46 +03:00
|
|
|
void setNumber(double d) {
|
2013-11-12 15:21:01 +04:00
|
|
|
int32_t i;
|
2014-02-27 19:23:11 +04:00
|
|
|
if (mozilla::NumberIsInt32(d, &i)) {
|
2019-11-20 02:40:02 +03:00
|
|
|
this->setInt32(i);
|
2019-11-19 10:21:46 +03:00
|
|
|
return;
|
2013-05-27 15:51:25 +04:00
|
|
|
}
|
2019-11-19 10:21:46 +03:00
|
|
|
|
2019-11-20 02:40:02 +03:00
|
|
|
this->setDouble(d);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-05-27 15:51:25 +04:00
|
|
|
};
|
|
|
|
|
2019-02-09 11:26:27 +03:00
|
|
|
// If the Value is a GC pointer type, call |f| with the pointer cast to that
|
|
|
|
// type and return the result wrapped in a Maybe, otherwise return None().
|
|
|
|
template <typename F>
|
|
|
|
auto MapGCThingTyped(const JS::Value& val, F&& f) {
|
2019-02-21 23:36:44 +03:00
|
|
|
switch (val.type()) {
|
|
|
|
case JS::ValueType::String: {
|
|
|
|
JSString* str = val.toString();
|
|
|
|
MOZ_ASSERT(gc::IsCellPointerValid(str));
|
|
|
|
return mozilla::Some(f(str));
|
|
|
|
}
|
|
|
|
case JS::ValueType::Object: {
|
|
|
|
JSObject* obj = &val.toObject();
|
|
|
|
MOZ_ASSERT(gc::IsCellPointerValid(obj));
|
|
|
|
return mozilla::Some(f(obj));
|
|
|
|
}
|
|
|
|
case JS::ValueType::Symbol: {
|
|
|
|
JS::Symbol* sym = val.toSymbol();
|
|
|
|
MOZ_ASSERT(gc::IsCellPointerValid(sym));
|
|
|
|
return mozilla::Some(f(sym));
|
|
|
|
}
|
|
|
|
case JS::ValueType::BigInt: {
|
|
|
|
JS::BigInt* bi = val.toBigInt();
|
|
|
|
MOZ_ASSERT(gc::IsCellPointerValid(bi));
|
|
|
|
return mozilla::Some(f(bi));
|
|
|
|
}
|
|
|
|
case JS::ValueType::PrivateGCThing: {
|
|
|
|
MOZ_ASSERT(gc::IsCellPointerValid(val.toGCThing()));
|
|
|
|
return mozilla::Some(MapGCThingTyped(val.toGCCellPtr(), std::move(f)));
|
|
|
|
}
|
|
|
|
case JS::ValueType::Double:
|
|
|
|
case JS::ValueType::Int32:
|
|
|
|
case JS::ValueType::Boolean:
|
|
|
|
case JS::ValueType::Undefined:
|
|
|
|
case JS::ValueType::Null:
|
|
|
|
case JS::ValueType::Magic: {
|
|
|
|
MOZ_ASSERT(!val.isGCThing());
|
|
|
|
using ReturnType = decltype(f(static_cast<JSObject*>(nullptr)));
|
|
|
|
return mozilla::Maybe<ReturnType>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("no missing return");
|
2015-04-23 20:42:31 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 11:26:27 +03:00
|
|
|
// If the Value is a GC pointer type, call |f| with the pointer cast to that
|
|
|
|
// type. Return whether this happened.
|
|
|
|
template <typename F>
|
|
|
|
bool ApplyGCThingTyped(const JS::Value& val, F&& f) {
|
|
|
|
return MapGCThingTyped(val,
|
|
|
|
[&f](auto t) {
|
|
|
|
f(t);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.isSome();
|
|
|
|
}
|
2015-04-23 20:42:31 +03:00
|
|
|
|
2017-09-15 13:04:40 +03:00
|
|
|
static inline JS::Value PoisonedObjectValue(uintptr_t poison) {
|
|
|
|
JS::Value v;
|
|
|
|
v.setObjectNoCheck(reinterpret_cast<JSObject*>(poison));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
} // namespace js
|
|
|
|
|
2017-03-02 13:22:47 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
namespace JS {
|
|
|
|
|
2018-12-07 00:28:10 +03:00
|
|
|
MOZ_ALWAYS_INLINE void AssertValueIsNotGray(const Value& value) {
|
|
|
|
if (value.isGCThing()) {
|
|
|
|
AssertCellIsNotGray(value.toGCThing());
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2017-03-02 13:22:47 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:28:10 +03:00
|
|
|
MOZ_ALWAYS_INLINE void AssertValueIsNotGray(const Heap<Value>& value) {
|
|
|
|
AssertValueIsNotGray(value.unbarrieredGet());
|
2017-03-02 13:22:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
#endif
|
|
|
|
|
2013-02-02 04:15:49 +04:00
|
|
|
/************************************************************************/
|
|
|
|
|
2013-08-20 10:45:26 +04:00
|
|
|
namespace JS {
|
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_PUBLIC_DATA const HandleValue NullHandleValue;
|
|
|
|
extern JS_PUBLIC_DATA const HandleValue UndefinedHandleValue;
|
|
|
|
extern JS_PUBLIC_DATA const HandleValue TrueHandleValue;
|
|
|
|
extern JS_PUBLIC_DATA const HandleValue FalseHandleValue;
|
2020-04-21 09:10:42 +03:00
|
|
|
extern JS_PUBLIC_DATA const Handle<mozilla::Maybe<Value>> NothingHandleValue;
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace JS
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#endif /* js_Value_h */
|