#ifndef INCLUDE_V8_INTERNAL_H_
#define INCLUDE_V8_INTERNAL_H_
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <atomic>
#include <compare>
#include <concepts>
#include <iterator>
#include <limits>
#include <memory>
#include <optional>
#include <type_traits>
#include "v8config.h"
namespace v8 {
class Array;
class Context;
class Data;
class Isolate;
namespace internal {
class Heap;
class LocalHeap;
class Isolate;
class IsolateGroup;
class LocalIsolate;
typedef uintptr_t Address;
static constexpr Address kNullAddress = 0;
constexpr int KB = 1024;
constexpr int MB = KB * 1024;
constexpr int GB = MB * 1024;
#ifdef V8_TARGET_ARCH_X64
constexpr size_t TB = size_t{GB} * 1024;
#endif
* Configuration of tagging scheme.
*/
const int kApiSystemPointerSize = sizeof(void*);
const int kApiDoubleSize = sizeof(double);
const int kApiInt32Size = sizeof(int32_t);
const int kApiInt64Size = sizeof(int64_t);
const int kApiSizetSize = sizeof(size_t);
const int kHeapObjectTag = 1;
const int kWeakHeapObjectTag = 3;
const int kHeapObjectTagSize = 2;
const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
const intptr_t kHeapObjectReferenceTagMask = 1 << (kHeapObjectTagSize - 1);
const int kForwardingTag = 0;
const int kForwardingTagSize = 2;
const intptr_t kForwardingTagMask = (1 << kForwardingTagSize) - 1;
const int kSmiTag = 0;
const int kSmiTagSize = 1;
const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
template <size_t tagged_ptr_size>
struct SmiTagging;
constexpr intptr_t kIntptrAllBitsSet = intptr_t{-1};
constexpr uintptr_t kUintptrAllBitsSet =
static_cast<uintptr_t>(kIntptrAllBitsSet);
template <>
struct SmiTagging<4> {
enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
static constexpr intptr_t kSmiMinValue =
static_cast<intptr_t>(kUintptrAllBitsSet << (kSmiValueSize - 1));
static constexpr intptr_t kSmiMaxValue = -(kSmiMinValue + 1);
V8_INLINE static constexpr int SmiToInt(Address value) {
int shift_bits = kSmiTagSize + kSmiShiftSize;
return static_cast<int32_t>(static_cast<uint32_t>(value)) >> shift_bits;
}
template <class T, typename std::enable_if_t<std::is_integral_v<T> &&
std::is_signed_v<T>>* = nullptr>
V8_INLINE static constexpr bool IsValidSmi(T value) {
return (static_cast<uintptr_t>(value) -
static_cast<uintptr_t>(kSmiMinValue)) <=
(static_cast<uintptr_t>(kSmiMaxValue) -
static_cast<uintptr_t>(kSmiMinValue));
}
template <class T,
typename std::enable_if_t<std::is_integral_v<T> &&
std::is_unsigned_v<T>>* = nullptr>
V8_INLINE static constexpr bool IsValidSmi(T value) {
static_assert(kSmiMaxValue <= std::numeric_limits<uintptr_t>::max());
return value <= static_cast<uintptr_t>(kSmiMaxValue);
}
V8_INLINE static constexpr bool IsValidSmi(int64_t value) {
return (static_cast<uint64_t>(value) -
static_cast<uint64_t>(kSmiMinValue)) <=
(static_cast<uint64_t>(kSmiMaxValue) -
static_cast<uint64_t>(kSmiMinValue));
}
V8_INLINE static constexpr bool IsValidSmi(uint64_t value) {
static_assert(kSmiMaxValue <= std::numeric_limits<uint64_t>::max());
return value <= static_cast<uint64_t>(kSmiMaxValue);
}
};
template <>
struct SmiTagging<8> {
enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
static constexpr intptr_t kSmiMinValue =
static_cast<intptr_t>(kUintptrAllBitsSet << (kSmiValueSize - 1));
static constexpr intptr_t kSmiMaxValue = -(kSmiMinValue + 1);
V8_INLINE static constexpr int SmiToInt(Address value) {
int shift_bits = kSmiTagSize + kSmiShiftSize;
return static_cast<int>(static_cast<intptr_t>(value) >> shift_bits);
}
template <class T, typename std::enable_if_t<std::is_integral_v<T> &&
std::is_signed_v<T>>* = nullptr>
V8_INLINE static constexpr bool IsValidSmi(T value) {
return std::numeric_limits<int32_t>::min() <= value &&
value <= std::numeric_limits<int32_t>::max();
}
template <class T,
typename std::enable_if_t<std::is_integral_v<T> &&
std::is_unsigned_v<T>>* = nullptr>
V8_INLINE static constexpr bool IsValidSmi(T value) {
return value <= std::numeric_limits<int32_t>::max();
}
};
#ifdef V8_COMPRESS_POINTERS
constexpr size_t kPtrComprCageReservationSize = size_t{1} << 32;
constexpr size_t kPtrComprCageBaseAlignment = size_t{1} << 32;
static_assert(
kApiSystemPointerSize == kApiInt64Size,
"Pointer compression can be enabled only for 64-bit architectures");
const int kApiTaggedSize = kApiInt32Size;
#else
const int kApiTaggedSize = kApiSystemPointerSize;
#endif
constexpr bool PointerCompressionIsEnabled() {
return kApiTaggedSize != kApiSystemPointerSize;
}
#ifdef V8_31BIT_SMIS_ON_64BIT_ARCH
using PlatformSmiTagging = SmiTagging<kApiInt32Size>;
#else
using PlatformSmiTagging = SmiTagging<kApiTaggedSize>;
#endif
const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
const int kSmiMinValue = static_cast<int>(PlatformSmiTagging::kSmiMinValue);
const int kSmiMaxValue = static_cast<int>(PlatformSmiTagging::kSmiMaxValue);
constexpr bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
constexpr bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
constexpr bool Is64() { return kApiSystemPointerSize == sizeof(int64_t); }
V8_INLINE static constexpr Address IntToSmi(int value) {
return (static_cast<Address>(value) << (kSmiTagSize + kSmiShiftSize)) |
kSmiTag;
}
* Sandbox related types, constants, and functions.
*/
constexpr bool SandboxIsEnabled() {
#ifdef V8_ENABLE_SANDBOX
return true;
#else
return false;
#endif
}
using SandboxedPointer_t = Address;
#ifdef V8_ENABLE_SANDBOX
#if defined(V8_TARGET_OS_ANDROID) || defined(V8_TARGET_OS_OHOS)
constexpr size_t kSandboxSizeLog2 = 37;
#elif defined(V8_TARGET_OS_IOS)
constexpr size_t kSandboxSizeLog2 = 34;
#else
constexpr size_t kSandboxSizeLog2 = 40;
#endif
constexpr size_t kSandboxSize = 1ULL << kSandboxSizeLog2;
constexpr size_t kSandboxAlignment = kPtrComprCageBaseAlignment;
constexpr uint64_t kSandboxedPointerShift = 64 - kSandboxSizeLog2;
constexpr size_t kSandboxGuardRegionSize = 32ULL * GB + 4ULL * GB;
static_assert((kSandboxGuardRegionSize % kSandboxAlignment) == 0,
"The size of the guard regions around the sandbox must be a "
"multiple of its required alignment.");
constexpr size_t kSandboxMinimumReservationSize = 8ULL * GB;
static_assert(kSandboxMinimumReservationSize > kPtrComprCageReservationSize,
"The minimum reservation size for a sandbox must be larger than "
"the pointer compression cage contained within it.");
constexpr size_t kMaxSafeBufferSizeForSandbox = 32ULL * GB - 1;
static_assert(kMaxSafeBufferSizeForSandbox <= kSandboxGuardRegionSize,
"The maximum allowed buffer size must not be larger than the "
"sandbox's guard regions");
constexpr size_t kBoundedSizeShift = 29;
static_assert(1ULL << (64 - kBoundedSizeShift) ==
kMaxSafeBufferSizeForSandbox + 1,
"The maximum size of a BoundedSize must be synchronized with the "
"kMaxSafeBufferSizeForSandbox");
#endif
#ifdef V8_COMPRESS_POINTERS
#if defined(V8_TARGET_OS_ANDROID) || defined(V8_TARGET_OS_OHOS)
constexpr size_t kExternalPointerTableReservationSize = 256 * MB;
constexpr uint32_t kExternalPointerIndexShift = 7;
#else
constexpr size_t kExternalPointerTableReservationSize = 512 * MB;
constexpr uint32_t kExternalPointerIndexShift = 6;
#endif
constexpr int kExternalPointerTableEntrySize = 8;
constexpr int kExternalPointerTableEntrySizeLog2 = 3;
constexpr size_t kMaxExternalPointers =
kExternalPointerTableReservationSize / kExternalPointerTableEntrySize;
static_assert((1 << (32 - kExternalPointerIndexShift)) == kMaxExternalPointers,
"kExternalPointerTableReservationSize and "
"kExternalPointerIndexShift don't match");
#else
constexpr size_t kMaxExternalPointers = 0;
#endif
constexpr uint64_t kExternalPointerMarkBit = 1ULL << 48;
constexpr uint64_t kExternalPointerTagShift = 49;
constexpr uint64_t kExternalPointerTagMask = 0x00fe000000000000ULL;
constexpr uint64_t kExternalPointerShiftedTagMask =
kExternalPointerTagMask >> kExternalPointerTagShift;
static_assert(kExternalPointerShiftedTagMask << kExternalPointerTagShift ==
kExternalPointerTagMask);
constexpr uint64_t kExternalPointerTagAndMarkbitMask = 0x00ff000000000000ULL;
constexpr uint64_t kExternalPointerPayloadMask = 0xff00ffffffffffffULL;
using ExternalPointerHandle = uint32_t;
#ifdef V8_ENABLE_SANDBOX
using ExternalPointer_t = ExternalPointerHandle;
#else
using ExternalPointer_t = Address;
#endif
constexpr ExternalPointer_t kNullExternalPointer = 0;
constexpr ExternalPointerHandle kNullExternalPointerHandle = 0;
using CppHeapPointerHandle = uint32_t;
#ifdef V8_COMPRESS_POINTERS
using CppHeapPointer_t = CppHeapPointerHandle;
#else
using CppHeapPointer_t = Address;
#endif
constexpr CppHeapPointer_t kNullCppHeapPointer = 0;
constexpr CppHeapPointerHandle kNullCppHeapPointerHandle = 0;
constexpr uint64_t kCppHeapPointerMarkBit = 1ULL;
constexpr uint64_t kCppHeapPointerTagShift = 1;
constexpr uint64_t kCppHeapPointerPayloadShift = 16;
#ifdef V8_COMPRESS_POINTERS
constexpr size_t kCppHeapPointerTableReservationSize =
kExternalPointerTableReservationSize;
constexpr uint32_t kCppHeapPointerIndexShift = kExternalPointerIndexShift;
constexpr int kCppHeapPointerTableEntrySize = 8;
constexpr int kCppHeapPointerTableEntrySizeLog2 = 3;
constexpr size_t kMaxCppHeapPointers =
kCppHeapPointerTableReservationSize / kCppHeapPointerTableEntrySize;
static_assert((1 << (32 - kCppHeapPointerIndexShift)) == kMaxCppHeapPointers,
"kCppHeapPointerTableReservationSize and "
"kCppHeapPointerIndexShift don't match");
#else
constexpr size_t kMaxCppHeapPointers = 0;
#endif
#define V8_EMBEDDER_DATA_TAG_COUNT 15
#define V8_EXTERNAL_POINTER_TAG_COUNT 40
// B E
// C D
template <typename Tag>
struct TagRange {
static_assert(std::is_enum_v<Tag> &&
std::is_same_v<std::underlying_type_t<Tag>, uint16_t>,
"Tag parameter must be an enum with base type uint16_t");
constexpr TagRange(Tag first, Tag last) : first(first), last(last) {}
constexpr TagRange(Tag tag)
: first(tag), last(tag) {}
constexpr TagRange() : TagRange(static_cast<Tag>(0)) {}
constexpr bool IsEmpty() const { return first == 0 && last == 0; }
constexpr size_t Size() const {
if (IsEmpty()) {
return 0;
} else {
return last - first + 1;
}
}
constexpr bool Contains(Tag tag) const {
return static_cast<uint32_t>(tag) - first <=
static_cast<uint32_t>(last) - first;
}
constexpr bool Contains(TagRange tag_range) const {
return tag_range.first >= first && tag_range.last <= last;
}
constexpr bool operator==(const TagRange other) const {
return first == other.first && last == other.last;
}
constexpr size_t hash_value() const {
static_assert(std::is_same_v<std::underlying_type_t<Tag>, uint16_t>);
return (static_cast<size_t>(first) << 16) | last;
}
const Tag first;
const Tag last;
};
enum ExternalPointerTag : uint16_t {
kFirstExternalPointerTag = 0,
kExternalPointerNullTag = 0,
kFirstSharedExternalPointerTag,
kWaiterQueueNodeTag = kFirstSharedExternalPointerTag,
kExternalStringResourceTag,
kExternalStringResourceDataTag,
kLastSharedExternalPointerTag = kExternalStringResourceDataTag,
kNativeContextMicrotaskQueueTag,
kFirstEmbedderDataTag,
kLastEmbedderDataTag = kFirstEmbedderDataTag + V8_EMBEDDER_DATA_TAG_COUNT - 1,
kFirstExternalTypeTag,
kLastExternalTypeTag =
kFirstExternalTypeTag + V8_EXTERNAL_POINTER_TAG_COUNT - 1,
kFastApiExternalTypeTag = kLastExternalTypeTag,
kFirstMaybeReadOnlyExternalPointerTag,
kFunctionTemplateInfoCallbackTag = kFirstMaybeReadOnlyExternalPointerTag,
kAccessorInfoGetterTag,
kAccessorInfoSetterTag,
kFirstInterceptorInfoExternalPointerTag,
kApiNamedPropertyQueryCallbackTag = kFirstInterceptorInfoExternalPointerTag,
kApiNamedPropertyGetterCallbackTag,
kApiNamedPropertySetterCallbackTag,
kApiNamedPropertyDescriptorCallbackTag,
kApiNamedPropertyDefinerCallbackTag,
kApiNamedPropertyDeleterCallbackTag,
kApiNamedPropertyEnumeratorCallbackTag,
kApiIndexedPropertyQueryCallbackTag,
kApiIndexedPropertyGetterCallbackTag,
kApiIndexedPropertySetterCallbackTag,
kApiIndexedPropertyDescriptorCallbackTag,
kApiIndexedPropertyDefinerCallbackTag,
kApiIndexedPropertyDeleterCallbackTag,
kApiIndexedPropertyEnumeratorCallbackTag,
kLastInterceptorInfoExternalPointerTag =
kApiIndexedPropertyEnumeratorCallbackTag,
kLastMaybeReadOnlyExternalPointerTag = kLastInterceptorInfoExternalPointerTag,
kWasmStackMemoryTag,
kFirstForeignExternalPointerTag,
kGenericForeignTag = kFirstForeignExternalPointerTag,
kApiAccessCheckCallbackTag,
kApiAbortScriptExecutionCallbackTag,
kSyntheticModuleTag,
kMicrotaskCallbackTag,
kMicrotaskCallbackDataTag,
kMessageListenerTag,
kWaiterQueueForeignTag,
kFirstManagedResourceTag,
kFirstManagedExternalPointerTag = kFirstManagedResourceTag,
kGenericManagedTag = kFirstManagedExternalPointerTag,
kWasmWasmStreamingTag,
kWasmFuncDataTag,
kWasmManagedDataTag,
kWasmNativeModuleTag,
kCFunctionWithSignatureTag,
kIcuBreakIteratorTag,
kIcuListFormatterTag,
kIcuLocaleTag,
kIcuSimpleDateFormatTag,
kIcuDateIntervalFormatTag,
kIcuRelativeDateTimeFormatterTag,
kIcuLocalizedNumberFormatterTag,
kIcuPluralRulesTag,
kIcuCollatorTag,
kIcuBreakIteratorWithTextTag,
kTemporalDurationTag,
kTemporalInstantTag,
kTemporalPlainDateTag,
kTemporalPlainTimeTag,
kTemporalPlainDateTimeTag,
kTemporalPlainYearMonthTag,
kTemporalPlainMonthDayTag,
kTemporalZonedDateTimeTag,
kDisplayNamesInternalTag,
kD8WorkerTag,
kD8ModuleEmbedderDataTag,
kLastForeignExternalPointerTag = kD8ModuleEmbedderDataTag,
kLastManagedExternalPointerTag = kLastForeignExternalPointerTag,
kArrayBufferExtensionTag,
kLastManagedResourceTag = kArrayBufferExtensionTag,
kExternalPointerZappedEntryTag = 0x7d,
kExternalPointerEvacuationEntryTag = 0x7e,
kExternalPointerFreeEntryTag = 0x7f,
kLastExternalPointerTag = 0x7f,
};
using ExternalPointerTagRange = TagRange<ExternalPointerTag>;
constexpr ExternalPointerTagRange kAnyExternalPointerTagRange(
kFirstExternalPointerTag, kLastExternalPointerTag);
constexpr ExternalPointerTagRange kAnySharedExternalPointerTagRange(
kFirstSharedExternalPointerTag, kLastSharedExternalPointerTag);
constexpr ExternalPointerTagRange kAnyForeignExternalPointerTagRange(
kFirstForeignExternalPointerTag, kLastForeignExternalPointerTag);
constexpr ExternalPointerTagRange kAnyInterceptorInfoExternalPointerTagRange(
kFirstInterceptorInfoExternalPointerTag,
kLastInterceptorInfoExternalPointerTag);
constexpr ExternalPointerTagRange kAnyManagedExternalPointerTagRange(
kFirstManagedExternalPointerTag, kLastManagedExternalPointerTag);
constexpr ExternalPointerTagRange kAnyMaybeReadOnlyExternalPointerTagRange(
kFirstMaybeReadOnlyExternalPointerTag,
kLastMaybeReadOnlyExternalPointerTag);
constexpr ExternalPointerTagRange kAnyManagedResourceExternalPointerTag(
kFirstManagedResourceTag, kLastManagedResourceTag);
V8_INLINE static constexpr bool IsSharedExternalPointerType(
ExternalPointerTagRange tag_range) {
return kAnySharedExternalPointerTagRange.Contains(tag_range);
}
V8_INLINE static constexpr bool IsMaybeReadOnlyExternalPointerType(
ExternalPointerTagRange tag_range) {
return kAnyMaybeReadOnlyExternalPointerTagRange.Contains(tag_range);
}
V8_INLINE static constexpr bool IsManagedExternalPointerType(
ExternalPointerTagRange tag_range) {
return kAnyManagedResourceExternalPointerTag.Contains(tag_range);
}
V8_INLINE static constexpr bool ExternalPointerCanBeEmpty(
ExternalPointerTagRange tag_range) {
return tag_range.Contains(kArrayBufferExtensionTag) ||
(tag_range.first <= kLastEmbedderDataTag &&
kFirstEmbedderDataTag <= tag_range.last) ||
kAnyInterceptorInfoExternalPointerTagRange.Contains(tag_range);
}
using IndirectPointerHandle = uint32_t;
constexpr IndirectPointerHandle kNullIndirectPointerHandle = 0;
using TrustedPointerHandle = IndirectPointerHandle;
constexpr size_t kTrustedPointerTableReservationSize = 64 * MB;
constexpr uint32_t kTrustedPointerHandleShift = 9;
constexpr TrustedPointerHandle kNullTrustedPointerHandle =
kNullIndirectPointerHandle;
constexpr int kTrustedPointerTableEntrySize = 8;
constexpr int kTrustedPointerTableEntrySizeLog2 = 3;
constexpr size_t kMaxTrustedPointers =
kTrustedPointerTableReservationSize / kTrustedPointerTableEntrySize;
static_assert((1 << (32 - kTrustedPointerHandleShift)) == kMaxTrustedPointers,
"kTrustedPointerTableReservationSize and "
"kTrustedPointerHandleShift don't match");
using CodePointerHandle = IndirectPointerHandle;
constexpr size_t kCodePointerTableReservationSize = 128 * MB;
constexpr uint32_t kCodePointerHandleShift = 9;
constexpr CodePointerHandle kNullCodePointerHandle = kNullIndirectPointerHandle;
constexpr uint32_t kCodePointerHandleMarker = 0x1;
static_assert(kCodePointerHandleShift > 0);
static_assert(kTrustedPointerHandleShift > 0);
constexpr int kCodePointerTableEntrySize = 16;
constexpr int kCodePointerTableEntrySizeLog2 = 4;
constexpr size_t kMaxCodePointers =
kCodePointerTableReservationSize / kCodePointerTableEntrySize;
static_assert(
(1 << (32 - kCodePointerHandleShift)) == kMaxCodePointers,
"kCodePointerTableReservationSize and kCodePointerHandleShift don't match");
constexpr int kCodePointerTableEntryEntrypointOffset = 0;
constexpr int kCodePointerTableEntryCodeObjectOffset = 8;
constexpr bool kRuntimeGeneratedCodeObjectsLiveInTrustedSpace = true;
constexpr bool kBuiltinCodeObjectsLiveInTrustedSpace = false;
constexpr bool kAllCodeObjectsLiveInTrustedSpace =
kRuntimeGeneratedCodeObjectsLiveInTrustedSpace &&
kBuiltinCodeObjectsLiveInTrustedSpace;
V8_DEPRECATE_SOON(
"Use GetCurrentIsolate() instead, which is guaranteed to return the same "
"isolate since https://crrev.com/c/6458560.")
V8_EXPORT internal::Isolate* IsolateFromNeverReadOnlySpaceObject(Address obj);
V8_EXPORT bool ShouldThrowOnError(internal::Isolate* isolate);
struct HandleScopeData final {
static constexpr uint32_t kSizeInBytes =
2 * kApiSystemPointerSize + 2 * kApiInt32Size;
Address* next;
Address* limit;
int level;
int sealed_level;
void Initialize() {
next = limit = nullptr;
sealed_level = level = 0;
}
};
static_assert(HandleScopeData::kSizeInBytes == sizeof(HandleScopeData));
* This class exports constants and functionality from within v8 that
* is necessary to implement inline functions in the v8 api. Don't
* depend on functions and constants defined here.
*/
class Internals {
#ifdef V8_MAP_PACKING
V8_INLINE static constexpr Address UnpackMapWord(Address mapword) {
return mapword ^ kMapWordXorMask;
}
#endif
public:
static const int kHeapObjectMapOffset = 0;
static const int kMapInstanceTypeOffset = 1 * kApiTaggedSize + kApiInt32Size;
static const int kStringResourceOffset =
1 * kApiTaggedSize + 2 * kApiInt32Size;
static const int kOddballKindOffset = 4 * kApiTaggedSize + kApiDoubleSize;
static const int kJSObjectHeaderSize = 3 * kApiTaggedSize;
#ifdef V8_COMPRESS_POINTERS
static const int kJSAPIObjectWithEmbedderSlotsHeaderSize =
kJSObjectHeaderSize + kApiInt32Size;
#else
static const int kJSAPIObjectWithEmbedderSlotsHeaderSize =
kJSObjectHeaderSize + kApiTaggedSize;
#endif
static const int kFixedArrayHeaderSize = 2 * kApiTaggedSize;
static const int kEmbedderDataArrayHeaderSize = 2 * kApiTaggedSize;
static const int kEmbedderDataSlotSize = kApiSystemPointerSize;
#ifdef V8_ENABLE_SANDBOX
static const int kEmbedderDataSlotExternalPointerOffset = kApiTaggedSize;
#else
static const int kEmbedderDataSlotExternalPointerOffset = 0;
#endif
static const int kNativeContextEmbedderDataOffset = 6 * kApiTaggedSize;
static const int kStringRepresentationAndEncodingMask = 0x0f;
static const int kStringEncodingMask = 0x8;
static const int kExternalTwoByteRepresentationTag = 0x02;
static const int kExternalOneByteRepresentationTag = 0x0a;
static const int kCallbackInfoDataOffset = 1 * kApiTaggedSize;
static const uint32_t kNumIsolateDataSlots = 4;
static const int kStackGuardSize = 8 * kApiSystemPointerSize;
static const int kNumberOfBooleanFlags = 6;
static const int kErrorMessageParamSize = 1;
static const int kTablesAlignmentPaddingSize = 1;
static const int kRegExpStaticResultOffsetsVectorSize = kApiSystemPointerSize;
static const int kBuiltinTier0EntryTableSize = 7 * kApiSystemPointerSize;
static const int kBuiltinTier0TableSize = 7 * kApiSystemPointerSize;
static const int kLinearAllocationAreaSize = 3 * kApiSystemPointerSize;
static const int kThreadLocalTopSize = 29 * kApiSystemPointerSize;
static const int kHandleScopeDataSize =
2 * kApiSystemPointerSize + 2 * kApiInt32Size;
static const int kExternalPointerTableBasePointerOffset = 0;
static const int kSegmentedTableSegmentPoolSize = 4;
static const int kExternalPointerTableSize =
4 * kApiSystemPointerSize +
kSegmentedTableSegmentPoolSize * sizeof(uint32_t);
static const int kTrustedPointerTableSize =
4 * kApiSystemPointerSize +
kSegmentedTableSegmentPoolSize * sizeof(uint32_t);
static const int kTrustedPointerTableBasePointerOffset = 0;
static const int kIsolateCageBaseOffset = 0;
static const int kIsolateStackGuardOffset =
kIsolateCageBaseOffset + kApiSystemPointerSize;
static const int kVariousBooleanFlagsOffset =
kIsolateStackGuardOffset + kStackGuardSize;
static const int kErrorMessageParamOffset =
kVariousBooleanFlagsOffset + kNumberOfBooleanFlags;
static const int kBuiltinTier0EntryTableOffset =
kErrorMessageParamOffset + kErrorMessageParamSize +
kTablesAlignmentPaddingSize + kRegExpStaticResultOffsetsVectorSize;
static const int kBuiltinTier0TableOffset =
kBuiltinTier0EntryTableOffset + kBuiltinTier0EntryTableSize;
static const int kNewAllocationInfoOffset =
kBuiltinTier0TableOffset + kBuiltinTier0TableSize;
static const int kOldAllocationInfoOffset =
kNewAllocationInfoOffset + kLinearAllocationAreaSize;
static const int kLastYoungAllocationOffset =
kOldAllocationInfoOffset + kApiSystemPointerSize;
static const int kFastCCallAlignmentPaddingSize =
kApiSystemPointerSize == 8 ? 5 * kApiSystemPointerSize
: 1 * kApiSystemPointerSize;
static const int kIsolateFastCCallCallerPcOffset =
kLastYoungAllocationOffset + kLinearAllocationAreaSize +
kFastCCallAlignmentPaddingSize;
static const int kIsolateFastCCallCallerFpOffset =
kIsolateFastCCallCallerPcOffset + kApiSystemPointerSize;
static const int kIsolateFastApiCallTargetOffset =
kIsolateFastCCallCallerFpOffset + kApiSystemPointerSize;
static const int kIsolateLongTaskStatsCounterOffset =
kIsolateFastApiCallTargetOffset + kApiSystemPointerSize;
static const int kIsolateThreadLocalTopOffset =
kIsolateLongTaskStatsCounterOffset + kApiSizetSize;
static const int kIsolateHandleScopeDataOffset =
kIsolateThreadLocalTopOffset + kThreadLocalTopSize;
static const int kIsolateEmbedderDataOffset =
kIsolateHandleScopeDataOffset + kHandleScopeDataSize;
#ifdef V8_COMPRESS_POINTERS
static const int kIsolateExternalPointerTableOffset =
kIsolateEmbedderDataOffset + kNumIsolateDataSlots * kApiSystemPointerSize;
static const int kIsolateSharedExternalPointerTableAddressOffset =
kIsolateExternalPointerTableOffset + kExternalPointerTableSize;
static const int kIsolateCppHeapPointerTableOffset =
kIsolateSharedExternalPointerTableAddressOffset + kApiSystemPointerSize;
#ifdef V8_ENABLE_SANDBOX
static const int kIsolateTrustedCageBaseOffset =
kIsolateCppHeapPointerTableOffset + kExternalPointerTableSize;
static const int kIsolateTrustedPointerTableOffset =
kIsolateTrustedCageBaseOffset + kApiSystemPointerSize;
static const int kIsolateSharedTrustedPointerTableAddressOffset =
kIsolateTrustedPointerTableOffset + kTrustedPointerTableSize;
static const int kIsolateTrustedPointerPublishingScopeOffset =
kIsolateSharedTrustedPointerTableAddressOffset + kApiSystemPointerSize;
static const int kIsolateCodePointerTableBaseAddressOffset =
kIsolateTrustedPointerPublishingScopeOffset + kApiSystemPointerSize;
static const int kIsolateApiCallbackThunkArgumentOffset =
kIsolateCodePointerTableBaseAddressOffset + kApiSystemPointerSize;
#else
static const int kIsolateApiCallbackThunkArgumentOffset =
kIsolateCppHeapPointerTableOffset + kExternalPointerTableSize;
#endif
#else
static const int kIsolateApiCallbackThunkArgumentOffset =
kIsolateEmbedderDataOffset + kNumIsolateDataSlots * kApiSystemPointerSize;
#endif
static const int kJSDispatchTableOffset =
kIsolateApiCallbackThunkArgumentOffset + kApiSystemPointerSize;
static const int kIsolateRegexpExecVectorArgumentOffset =
kJSDispatchTableOffset + kApiSystemPointerSize;
static const int kContinuationPreservedEmbedderDataOffset =
kIsolateRegexpExecVectorArgumentOffset + kApiSystemPointerSize;
static const int kIsolateRootsOffset =
kContinuationPreservedEmbedderDataOffset + kApiSystemPointerSize;
static const int kDisallowGarbageCollectionAlign = alignof(uint32_t);
static const int kDisallowGarbageCollectionSize = sizeof(uint32_t);
#if V8_STATIC_ROOTS_BOOL
#define EXPORTED_STATIC_ROOTS_PTR_LIST(V) \
V(UndefinedValue, 0x11) \
V(NullValue, 0x2d) \
V(TrueValue, 0x71) \
V(FalseValue, 0x55) \
V(EmptyString, 0x49) \
\
\
V(TheHoleValue, kBuildDependentTheHoleValue)
using Tagged_t = uint32_t;
struct StaticReadOnlyRoot {
#ifdef V8_ENABLE_WEBASSEMBLY
static constexpr Tagged_t kBuildDependentTheHoleValue = 0x2fffd;
#else
static constexpr Tagged_t kBuildDependentTheHoleValue = 0xfffd;
#endif
#define DEF_ROOT(name, value) static constexpr Tagged_t k##name = value;
EXPORTED_STATIC_ROOTS_PTR_LIST(DEF_ROOT)
#undef DEF_ROOT
static constexpr Tagged_t kStringMapLowerBound = 0;
static constexpr Tagged_t kStringMapUpperBound = 0x425;
#define PLUSONE(...) +1
static constexpr size_t kNumberOfExportedStaticRoots =
2 + EXPORTED_STATIC_ROOTS_PTR_LIST(PLUSONE);
#undef PLUSONE
};
#endif
static const int kUndefinedValueRootIndex = 0;
static const int kTheHoleValueRootIndex = 1;
static const int kNullValueRootIndex = 2;
static const int kTrueValueRootIndex = 3;
static const int kFalseValueRootIndex = 4;
static const int kEmptyStringRootIndex = 5;
static const int kNodeClassIdOffset = 1 * kApiSystemPointerSize;
static const int kNodeFlagsOffset = 1 * kApiSystemPointerSize + 3;
static const int kNodeStateMask = 0x3;
static const int kNodeStateIsWeakValue = 2;
static const int kFirstNonstringType = 0x80;
static const int kOddballType = 0x83;
static const int kForeignType = 0xcc;
static const int kJSSpecialApiObjectType = 0x410;
static const int kJSObjectType = 0x421;
static const int kFirstJSApiObjectType = 0x422;
static const int kLastJSApiObjectType = 0x80A;
static const int kFirstEmbedderJSApiObjectType = 0;
static const int kLastEmbedderJSApiObjectType =
kLastJSApiObjectType - kFirstJSApiObjectType;
static const int kUndefinedOddballKind = 4;
static const int kNullOddballKind = 3;
static const int kDontThrow = 0;
static const int kThrowOnError = 1;
static const int kInferShouldThrowMode = 2;
static constexpr size_t kExternalAllocationSoftLimit = 64 * 1024 * 1024;
#ifdef V8_MAP_PACKING
static const uintptr_t kMapWordMetadataMask = 0xffffULL << 48;
static const uintptr_t kMapWordSignature = 0b10;
static const int kMapWordXorMask = 0b11;
#endif
V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
#ifdef V8_ENABLE_CHECKS
CheckInitializedImpl(isolate);
#endif
}
V8_INLINE static constexpr bool HasHeapObjectTag(Address value) {
return (value & kHeapObjectTagMask) == static_cast<Address>(kHeapObjectTag);
}
V8_INLINE static constexpr int SmiValue(Address value) {
return PlatformSmiTagging::SmiToInt(value);
}
V8_INLINE static constexpr Address AddressToSmi(Address value) {
return (value << (kSmiTagSize + PlatformSmiTagging::kSmiShiftSize)) |
kSmiTag;
}
V8_INLINE static constexpr Address IntToSmi(int value) {
return AddressToSmi(static_cast<Address>(value));
}
template <typename T,
typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
V8_INLINE static constexpr Address IntegralToSmi(T value) {
return AddressToSmi(static_cast<Address>(value));
}
template <typename T,
typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
V8_INLINE static constexpr bool IsValidSmi(T value) {
return PlatformSmiTagging::IsValidSmi(value);
}
template <typename T,
typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
static constexpr std::optional<Address> TryIntegralToSmi(T value) {
if (V8_LIKELY(PlatformSmiTagging::IsValidSmi(value))) {
return {AddressToSmi(static_cast<Address>(value))};
}
return {};
}
#if V8_STATIC_ROOTS_BOOL
V8_INLINE static bool is_identical(Address obj, Tagged_t constant) {
return static_cast<Tagged_t>(obj) == constant;
}
V8_INLINE static bool CheckInstanceMapRange(Address obj, Tagged_t first_map,
Tagged_t last_map) {
auto map = ReadRawField<Tagged_t>(obj, kHeapObjectMapOffset);
#ifdef V8_MAP_PACKING
map = UnpackMapWord(map);
#endif
return map >= first_map && map <= last_map;
}
#endif
V8_INLINE static int GetInstanceType(Address obj) {
Address map = ReadTaggedPointerField(obj, kHeapObjectMapOffset);
#ifdef V8_MAP_PACKING
map = UnpackMapWord(map);
#endif
return ReadRawField<uint16_t>(map, kMapInstanceTypeOffset);
}
V8_INLINE static Address LoadMap(Address obj) {
if (!HasHeapObjectTag(obj)) return kNullAddress;
Address map = ReadTaggedPointerField(obj, kHeapObjectMapOffset);
#ifdef V8_MAP_PACKING
map = UnpackMapWord(map);
#endif
return map;
}
V8_INLINE static int GetOddballKind(Address obj) {
return SmiValue(ReadTaggedSignedField(obj, kOddballKindOffset));
}
V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
int representation = (instance_type & kStringRepresentationAndEncodingMask);
return representation == kExternalTwoByteRepresentationTag;
}
V8_INLINE static constexpr bool CanHaveInternalField(int instance_type) {
static_assert(kJSObjectType + 1 == kFirstJSApiObjectType);
static_assert(kJSObjectType < kLastJSApiObjectType);
static_assert(kFirstJSApiObjectType < kLastJSApiObjectType);
return instance_type == kJSSpecialApiObjectType ||
(static_cast<unsigned>(static_cast<unsigned>(instance_type) -
static_cast<unsigned>(kJSObjectType)) <=
static_cast<unsigned>(kLastJSApiObjectType - kJSObjectType));
}
V8_INLINE static uint8_t GetNodeFlag(Address* obj, int shift) {
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
return *addr & static_cast<uint8_t>(1U << shift);
}
V8_INLINE static void UpdateNodeFlag(Address* obj, bool value, int shift) {
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
uint8_t mask = static_cast<uint8_t>(1U << shift);
*addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
}
V8_INLINE static uint8_t GetNodeState(Address* obj) {
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
return *addr & kNodeStateMask;
}
V8_INLINE static void UpdateNodeState(Address* obj, uint8_t value) {
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
*addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
}
V8_INLINE static void SetEmbedderData(v8::Isolate* isolate, uint32_t slot,
void* data) {
Address addr = reinterpret_cast<Address>(isolate) +
kIsolateEmbedderDataOffset + slot * kApiSystemPointerSize;
*reinterpret_cast<void**>(addr) = data;
}
V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
uint32_t slot) {
Address addr = reinterpret_cast<Address>(isolate) +
kIsolateEmbedderDataOffset + slot * kApiSystemPointerSize;
return *reinterpret_cast<void* const*>(addr);
}
V8_INLINE static HandleScopeData* GetHandleScopeData(v8::Isolate* isolate) {
Address addr =
reinterpret_cast<Address>(isolate) + kIsolateHandleScopeDataOffset;
return reinterpret_cast<HandleScopeData*>(addr);
}
V8_INLINE static void IncrementLongTasksStatsCounter(v8::Isolate* isolate) {
Address addr =
reinterpret_cast<Address>(isolate) + kIsolateLongTaskStatsCounterOffset;
++(*reinterpret_cast<size_t*>(addr));
}
V8_INLINE static Address* GetRootSlot(v8::Isolate* isolate, int index) {
Address addr = reinterpret_cast<Address>(isolate) + kIsolateRootsOffset +
index * kApiSystemPointerSize;
return reinterpret_cast<Address*>(addr);
}
V8_INLINE static Address GetRoot(v8::Isolate* isolate, int index) {
#if V8_STATIC_ROOTS_BOOL
Address base = *reinterpret_cast<Address*>(
reinterpret_cast<uintptr_t>(isolate) + kIsolateCageBaseOffset);
switch (index) {
#define DECOMPRESS_ROOT(name, ...) \
case k##name##RootIndex: \
return base + StaticReadOnlyRoot::k##name;
EXPORTED_STATIC_ROOTS_PTR_LIST(DECOMPRESS_ROOT)
#undef DECOMPRESS_ROOT
#undef EXPORTED_STATIC_ROOTS_PTR_LIST
default:
break;
}
#endif
return *GetRootSlot(isolate, index);
}
#ifdef V8_ENABLE_SANDBOX
V8_INLINE static Address* GetExternalPointerTableBase(v8::Isolate* isolate) {
Address addr = reinterpret_cast<Address>(isolate) +
kIsolateExternalPointerTableOffset +
kExternalPointerTableBasePointerOffset;
return *reinterpret_cast<Address**>(addr);
}
V8_INLINE static Address* GetSharedExternalPointerTableBase(
v8::Isolate* isolate) {
Address addr = reinterpret_cast<Address>(isolate) +
kIsolateSharedExternalPointerTableAddressOffset;
addr = *reinterpret_cast<Address*>(addr);
addr += kExternalPointerTableBasePointerOffset;
return *reinterpret_cast<Address**>(addr);
}
#endif
template <typename T>
V8_INLINE static T ReadRawField(Address heap_object_ptr, int offset) {
Address addr = heap_object_ptr + offset - kHeapObjectTag;
#ifdef V8_COMPRESS_POINTERS
if constexpr (sizeof(T) > kApiTaggedSize) {
T r;
memcpy(&r, reinterpret_cast<void*>(addr), sizeof(T));
return r;
}
#endif
return *reinterpret_cast<const T*>(addr);
}
V8_INLINE static Address ReadTaggedPointerField(Address heap_object_ptr,
int offset) {
#ifdef V8_COMPRESS_POINTERS
uint32_t value = ReadRawField<uint32_t>(heap_object_ptr, offset);
Address base = GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr);
return base + static_cast<Address>(static_cast<uintptr_t>(value));
#else
return ReadRawField<Address>(heap_object_ptr, offset);
#endif
}
V8_INLINE static Address ReadTaggedSignedField(Address heap_object_ptr,
int offset) {
#ifdef V8_COMPRESS_POINTERS
uint32_t value = ReadRawField<uint32_t>(heap_object_ptr, offset);
return static_cast<Address>(static_cast<uintptr_t>(value));
#else
return ReadRawField<Address>(heap_object_ptr, offset);
#endif
}
V8_EXPORT static v8::Isolate* GetCurrentIsolate();
V8_INLINE static v8::Isolate* GetCurrentIsolateForSandbox() {
#ifdef V8_ENABLE_SANDBOX
return GetCurrentIsolate();
#else
return nullptr;
#endif
}
template <ExternalPointerTagRange tag_range>
V8_INLINE static Address ReadExternalPointerField(v8::Isolate* isolate,
Address heap_object_ptr,
int offset) {
#ifdef V8_ENABLE_SANDBOX
static_assert(!tag_range.IsEmpty());
Address* table = IsSharedExternalPointerType(tag_range)
? GetSharedExternalPointerTableBase(isolate)
: GetExternalPointerTableBase(isolate);
internal::ExternalPointerHandle handle =
ReadRawField<ExternalPointerHandle>(heap_object_ptr, offset);
uint32_t index = handle >> kExternalPointerIndexShift;
std::atomic<Address>* ptr =
reinterpret_cast<std::atomic<Address>*>(&table[index]);
Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
ExternalPointerTag actual_tag = static_cast<ExternalPointerTag>(
(entry & kExternalPointerTagMask) >> kExternalPointerTagShift);
if (V8_LIKELY(tag_range.Contains(actual_tag))) {
return entry & kExternalPointerPayloadMask;
} else {
return 0;
}
return entry;
#else
return ReadRawField<Address>(heap_object_ptr, offset);
#endif
}
V8_INLINE static Address ReadExternalPointerField(
v8::Isolate* isolate, Address heap_object_ptr, int offset,
ExternalPointerTagRange tag_range) {
#ifdef V8_ENABLE_SANDBOX
Address* table = IsSharedExternalPointerType(tag_range)
? GetSharedExternalPointerTableBase(isolate)
: GetExternalPointerTableBase(isolate);
internal::ExternalPointerHandle handle =
ReadRawField<ExternalPointerHandle>(heap_object_ptr, offset);
uint32_t index = handle >> kExternalPointerIndexShift;
std::atomic<Address>* ptr =
reinterpret_cast<std::atomic<Address>*>(&table[index]);
Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
ExternalPointerTag actual_tag = static_cast<ExternalPointerTag>(
(entry & kExternalPointerTagMask) >> kExternalPointerTagShift);
if (V8_LIKELY(tag_range.Contains(actual_tag))) {
return entry & kExternalPointerPayloadMask;
} else {
return 0;
}
return entry;
#else
return ReadRawField<Address>(heap_object_ptr, offset);
#endif
}
#ifdef V8_COMPRESS_POINTERS
V8_INLINE static Address GetPtrComprCageBaseFromOnHeapAddress(Address addr) {
return addr & -static_cast<intptr_t>(kPtrComprCageBaseAlignment);
}
V8_INLINE static uint32_t CompressTagged(Address value) {
return static_cast<uint32_t>(value);
}
V8_INLINE static Address DecompressTaggedField(Address heap_object_ptr,
uint32_t value) {
Address base = GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr);
return base + static_cast<Address>(static_cast<uintptr_t>(value));
}
#endif
};
template <bool PerformCheck>
struct CastCheck {
template <class T>
static void Perform(T* data);
};
template <>
template <class T>
void CastCheck<true>::Perform(T* data) {
T::Cast(data);
}
template <>
template <class T>
void CastCheck<false>::Perform(T* data) {}
template <class T>
V8_INLINE void PerformCastCheck(T* data) {
CastCheck<std::is_base_of_v<Data, T> &&
!std::is_same_v<Data, std::remove_cv_t<T>>>::Perform(data);
}
class BackingStoreBase {};
constexpr int kGarbageCollectionReasonMaxValue = 30;
class V8_EXPORT StrongRootAllocatorBase {
public:
Heap* heap() const { return heap_; }
constexpr bool operator==(const StrongRootAllocatorBase&) const = default;
protected:
explicit StrongRootAllocatorBase(Heap* heap) : heap_(heap) {}
explicit StrongRootAllocatorBase(LocalHeap* heap);
explicit StrongRootAllocatorBase(Isolate* isolate);
explicit StrongRootAllocatorBase(v8::Isolate* isolate);
explicit StrongRootAllocatorBase(LocalIsolate* isolate);
Address* allocate_impl(size_t n);
void deallocate_impl(Address* p, size_t n) noexcept;
private:
Heap* heap_;
};
template <typename T>
class StrongRootAllocator : private std::allocator<T> {
public:
using value_type = T;
template <typename HeapOrIsolateT>
explicit StrongRootAllocator(HeapOrIsolateT*) {}
template <typename U>
StrongRootAllocator(const StrongRootAllocator<U>& other) noexcept {}
using std::allocator<T>::allocate;
using std::allocator<T>::deallocate;
};
template <typename Iterator>
concept HasIteratorConcept = requires { typename Iterator::iterator_concept; };
template <typename Iterator>
concept HasIteratorCategory =
requires { typename Iterator::iterator_category; };
template <typename Iterator>
struct MaybeDefineIteratorConcept {};
template <HasIteratorConcept Iterator>
struct MaybeDefineIteratorConcept<Iterator> {
using iterator_concept = typename Iterator::iterator_concept;
};
template <typename Iterator>
requires(HasIteratorCategory<Iterator> && !HasIteratorConcept<Iterator>)
struct MaybeDefineIteratorConcept<Iterator> {
using iterator_concept =
typename std::iterator_traits<Iterator>::iterator_concept;
};
template <typename Iterator, typename ElementType = void>
class WrappedIterator : public MaybeDefineIteratorConcept<Iterator> {
public:
static_assert(
std::is_void_v<ElementType> ||
(std::is_convertible_v<typename std::iterator_traits<Iterator>::pointer,
std::add_pointer_t<ElementType>> &&
std::is_convertible_v<typename std::iterator_traits<Iterator>::reference,
std::add_lvalue_reference_t<ElementType>>));
using difference_type =
typename std::iterator_traits<Iterator>::difference_type;
using value_type =
std::conditional_t<std::is_void_v<ElementType>,
typename std::iterator_traits<Iterator>::value_type,
ElementType>;
using pointer =
std::conditional_t<std::is_void_v<ElementType>,
typename std::iterator_traits<Iterator>::pointer,
std::add_pointer_t<ElementType>>;
using reference =
std::conditional_t<std::is_void_v<ElementType>,
typename std::iterator_traits<Iterator>::reference,
std::add_lvalue_reference_t<ElementType>>;
using iterator_category =
typename std::iterator_traits<Iterator>::iterator_category;
constexpr WrappedIterator() noexcept = default;
constexpr explicit WrappedIterator(Iterator it) noexcept : it_(it) {}
template <typename OtherIterator, typename OtherElementType>
requires std::is_convertible_v<OtherIterator, Iterator>
constexpr WrappedIterator(
const WrappedIterator<OtherIterator, OtherElementType>& other) noexcept
: it_(other.base()) {}
[[nodiscard]] constexpr reference operator*() const noexcept { return *it_; }
[[nodiscard]] constexpr pointer operator->() const noexcept {
if constexpr (std::is_pointer_v<Iterator>) {
return it_;
} else {
return it_.operator->();
}
}
template <typename OtherIterator, typename OtherElementType>
[[nodiscard]] constexpr bool operator==(
const WrappedIterator<OtherIterator, OtherElementType>& other)
const noexcept {
return it_ == other.base();
}
template <typename OtherIterator, typename OtherElementType>
[[nodiscard]] constexpr auto operator<=>(
const WrappedIterator<OtherIterator, OtherElementType>& other)
const noexcept {
if constexpr (std::three_way_comparable_with<Iterator, OtherIterator>) {
return it_ <=> other.base();
} else if constexpr (std::totally_ordered_with<Iterator, OtherIterator>) {
if (it_ < other.base()) {
return std::strong_ordering::less;
}
return (it_ > other.base()) ? std::strong_ordering::greater
: std::strong_ordering::equal;
} else {
if (it_ < other.base()) {
return std::partial_ordering::less;
}
if (other.base() < it_) {
return std::partial_ordering::greater;
}
return (it_ == other.base()) ? std::partial_ordering::equivalent
: std::partial_ordering::unordered;
}
}
constexpr WrappedIterator& operator++() noexcept {
++it_;
return *this;
}
constexpr WrappedIterator operator++(int) noexcept {
WrappedIterator result(*this);
++(*this);
return result;
}
constexpr WrappedIterator& operator--() noexcept {
--it_;
return *this;
}
constexpr WrappedIterator operator--(int) noexcept {
WrappedIterator result(*this);
--(*this);
return result;
}
[[nodiscard]] constexpr WrappedIterator operator+(
difference_type n) const noexcept {
WrappedIterator result(*this);
result += n;
return result;
}
[[nodiscard]] friend constexpr WrappedIterator operator+(
difference_type n, const WrappedIterator& x) noexcept {
return x + n;
}
constexpr WrappedIterator& operator+=(difference_type n) noexcept {
it_ += n;
return *this;
}
[[nodiscard]] constexpr WrappedIterator operator-(
difference_type n) const noexcept {
return *this + -n;
}
constexpr WrappedIterator& operator-=(difference_type n) noexcept {
return *this += -n;
}
template <typename OtherIterator, typename OtherElementType>
[[nodiscard]] constexpr auto operator-(
const WrappedIterator<OtherIterator, OtherElementType>& other)
const noexcept {
return it_ - other.base();
}
[[nodiscard]] constexpr reference operator[](
difference_type n) const noexcept {
return it_[n];
}
[[nodiscard]] constexpr const Iterator& base() const noexcept { return it_; }
private:
Iterator it_;
};
class ValueHelper final {
public:
#ifdef V8_ENABLE_DIRECT_HANDLE
static constexpr Address kTaggedNullAddress = 1;
using InternalRepresentationType = internal::Address;
static constexpr InternalRepresentationType kEmpty = kTaggedNullAddress;
#else
using InternalRepresentationType = internal::Address*;
static constexpr InternalRepresentationType kEmpty = nullptr;
#endif
template <typename T>
V8_INLINE static bool IsEmpty(T* value) {
return ValueAsRepr(value) == kEmpty;
}
template <template <typename T, typename... Ms> typename H, typename T,
typename... Ms>
V8_INLINE static T* HandleAsValue(const H<T, Ms...>& handle) {
return handle.template value<T>();
}
#ifdef V8_ENABLE_DIRECT_HANDLE
template <typename T>
V8_INLINE static Address ValueAsAddress(const T* value) {
return reinterpret_cast<Address>(value);
}
template <typename T, bool check_null = true, typename S>
V8_INLINE static T* SlotAsValue(S* slot) {
if (check_null && slot == nullptr) {
return reinterpret_cast<T*>(kTaggedNullAddress);
}
return *reinterpret_cast<T**>(slot);
}
template <typename T>
V8_INLINE static InternalRepresentationType ValueAsRepr(const T* value) {
return reinterpret_cast<InternalRepresentationType>(value);
}
template <typename T>
V8_INLINE static T* ReprAsValue(InternalRepresentationType repr) {
return reinterpret_cast<T*>(repr);
}
#else
template <typename T>
V8_INLINE static Address ValueAsAddress(const T* value) {
return *reinterpret_cast<const Address*>(value);
}
template <typename T, bool check_null = true, typename S>
V8_INLINE static T* SlotAsValue(S* slot) {
return reinterpret_cast<T*>(slot);
}
template <typename T>
V8_INLINE static InternalRepresentationType ValueAsRepr(const T* value) {
return const_cast<InternalRepresentationType>(
reinterpret_cast<const Address*>(value));
}
template <typename T>
V8_INLINE static T* ReprAsValue(InternalRepresentationType repr) {
return reinterpret_cast<T*>(repr);
}
#endif
};
* Helper functions about handles.
*/
class HandleHelper final {
public:
* Checks whether two handles are equal.
* They are equal iff they are both empty or they are both non-empty and the
* objects to which they refer are physically equal.
*
* If both handles refer to JS objects, this is the same as strict equality.
* For primitives, such as numbers or strings, a `false` return value does not
* indicate that the values aren't equal in the JavaScript sense.
* Use `Value::StrictEquals()` to check primitives for equality.
*/
template <typename T1, typename T2>
V8_INLINE static bool EqualHandles(const T1& lhs, const T2& rhs) {
if (lhs.IsEmpty()) return rhs.IsEmpty();
if (rhs.IsEmpty()) return false;
return lhs.ptr() == rhs.ptr();
}
};
V8_EXPORT void VerifyHandleIsNonEmpty(bool is_empty);
void PrintFunctionCallbackInfo(void* function_callback_info);
void PrintPropertyCallbackInfo(void* property_callback_info);
}
}
#endif