vendor: add V8 shared artifacts

This commit is contained in:
abdussamedulutas 2026-07-28 18:44:46 +03:00
parent d3d1643516
commit d51f1053cd
145 changed files with 37359 additions and 0 deletions

BIN
vendor/artifacts/v8/bin/d8 vendored Executable file

Binary file not shown.

BIN
vendor/artifacts/v8/bin/icudtl.dat vendored Normal file

Binary file not shown.

Binary file not shown.

8
vendor/artifacts/v8/build/args.gn vendored Normal file
View File

@ -0,0 +1,8 @@
dcheck_always_on = false
is_debug = false
target_cpu = "x64"
is_component_build = true
symbol_level = 1
v8_enable_backtrace = true
v8_enable_i18n_support = true
treat_warnings_as_errors = false

View File

@ -0,0 +1 @@
{"arch": "x64", "asan": false, "atomic_object_field_writes": true, "cet_shadow_stack": false, "cfi": false, "clang": true, "clang_coverage": false, "code_comments": false, "component_build": true, "concurrent_marking": true, "current_cpu": "x64", "dcheck_always_on": false, "debug_code": false, "DEBUG_defined": false, "debugging_features": false, "dict_property_const_tracking": false, "direct_handle": false, "disassembler": false, "dumpling": false, "full_debug": false, "gdbjit": false, "has_jitless": false, "sparkplug_plus": true, "has_maglev": true, "has_turbofan": true, "has_webassembly": true, "has_wasm_interpreter": false, "i18n": true, "temporal": true, "is_android": false, "is_ios": false, "is_linux": true, "js_shared_memory": true, "lite_mode": false, "local_off_stack_check": false, "mips_arch_variant": "", "mips_use_msa": false, "msan": false, "official_build": false, "pointer_compression": true, "pointer_compression_shared_cage": true, "runtime_call_stats": false, "sandbox": true, "sandbox_hardware_support": false, "simd_mips": false, "simulator_run": false, "single_generation": false, "slow_dchecks": false, "target_cpu": "x64", "tsan": false, "ubsan": false, "use_sanitizer": false, "v8_cfi": false, "v8_current_cpu": "x64", "v8_target_cpu": "x64", "verification_features": false, "verify_csa": false, "verify_heap": false, "verify_predictable": false, "wasm_random_fuzzers": true, "memory_corruption_api": false, "lower_limits_mode": false}

View File

@ -0,0 +1,27 @@
{
"v8_deprecation_warnings": true,
"v8_enable_31bit_smis_on_64bit_arch": false,
"v8_enable_direct_handle": false,
"v8_enable_extensible_ro_snapshot": true,
"v8_enable_gdbjit": false,
"v8_enable_hugepage": false,
"v8_enable_i18n_support": true,
"v8_enable_javascript_promise_hooks": false,
"v8_enable_lite_mode": false,
"v8_enable_map_packing": false,
"v8_enable_memory_accounting_checks": false,
"v8_enable_object_print": false,
"v8_enable_pointer_compression": true,
"v8_enable_pointer_compression_shared_cage": true,
"v8_enable_sandbox": true,
"v8_enable_short_builtin_calls": true,
"v8_enable_temporal_support": true,
"v8_enable_v8_checks": false,
"v8_enable_webassembly": true,
"v8_imminent_deprecation_warnings": true,
"v8_optimized_debug": true,
"v8_random_seed": "314159265",
"v8_use_default_hasher_secret": true,
"v8_use_perfetto": false,
"v8_use_siphash": false
}

BIN
vendor/artifacts/v8/data/icudtl.dat vendored Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,72 @@
# The V8 public C++ API
# Overview
The V8 public C++ API aims to support four use cases:
1. Enable applications that embed V8 (called the embedder) to configure and run
one or more instances of V8.
2. Expose ECMAScript-like capabilities to the embedder.
3. Enable the embedder to interact with ECMAScript by exposing API objects.
4. Provide access to the V8 debugger (inspector).
# Configuring and running an instance of V8
V8 requires access to certain OS-level primitives such as the ability to
schedule work on threads, or allocate memory.
The embedder can define how to access those primitives via the v8::Platform
interface. While V8 bundles a basic implementation, embedders are highly
encouraged to implement v8::Platform themselves.
Currently, the v8::ArrayBuffer::Allocator is passed to the v8::Isolate factory
method, however, conceptually it should also be part of the v8::Platform since
all instances of V8 should share one allocator.
Once the v8::Platform is configured, an v8::Isolate can be created. All
further interactions with V8 should explicitly reference the v8::Isolate they
refer to. All API methods should eventually take an v8::Isolate parameter.
When a given instance of V8 is no longer needed, it can be destroyed by
disposing the respective v8::Isolate. If the embedder wishes to free all memory
associated with the v8::Isolate, it has to first clear all global handles
associated with that v8::Isolate.
# ECMAScript-like capabilities
In general, the C++ API shouldn't enable capabilities that aren't available to
scripts running in V8. Experience has shown that it's not possible to maintain
such API methods in the long term. However, capabilities also available to
scripts, i.e., ones that are defined in the ECMAScript standard are there to
stay, and we can safely expose them to embedders.
The C++ API should also be pleasant to use, and not require learning new
paradigms. Similarly to how the API exposed to scripts aims to provide good
ergonomics, we should aim to provide a reasonable developer experience for this
API surface.
ECMAScript makes heavy use of exceptions, however, V8's C++ code doesn't use
C++ exceptions. Therefore, all API methods that can throw exceptions should
indicate so by returning a v8::Maybe<> or v8::MaybeLocal<> result,
and by taking a v8::Local<v8::Context> parameter that indicates in which
context a possible exception should be thrown.
# API objects
V8 allows embedders to define special objects that expose additional
capabilities and APIs to scripts. The most prominent example is exposing the
HTML DOM in Blink. Other examples are e.g. node.js. It is less clear what kind
of capabilities we want to expose via this API surface. As a rule of thumb, we
want to expose operations as defined in the WebIDL and HTML spec: we
assume that those requirements are somewhat stable, and that they are a
superset of the requirements of other embedders including node.js.
Ideally, the API surfaces defined in those specs hook into the ECMAScript spec
which in turn guarantees long-term stability of the API.
# The V8 inspector
All debugging capabilities of V8 should be exposed via the inspector protocol.
The exception to this are profiling features exposed via v8-profiler.h.
Changes to the inspector protocol need to ensure backwards compatibility and
commitment to maintain.

16
vendor/artifacts/v8/include/DEPS vendored Normal file
View File

@ -0,0 +1,16 @@
include_rules = [
# v8-inspector-protocol.h depends on generated files under include/inspector.
"+inspector",
"+cppgc/common.h",
# Used by v8-cppgc.h to bridge to cppgc.
"+cppgc/custom-space.h",
"+cppgc/heap-statistics.h",
"+cppgc/internal/conditional-stack-allocated.h",
"+cppgc/internal/write-barrier.h",
"+cppgc/type-traits.h",
"+cppgc/visitor.h",
"+perfetto",
# Used by v8-object.h, for Object::wrap().
"+cppgc/name-provider.h",
"+cppgc/garbage-collected.h",
]

View File

@ -0,0 +1,14 @@
# Metadata information for this directory.
#
# For more information on DIR_METADATA files, see:
# https://source.chromium.org/chromium/infra/infra/+/master:go/src/infra/tools/dirmd/README.md
#
# For the schema of this file, see Metadata message:
# https://source.chromium.org/chromium/infra/infra/+/master:go/src/infra/tools/dirmd/proto/dir_metadata.proto
monorail {
component: "Blink>JavaScript>API"
}
buganizer_public: {
component_id: 1456124
}

23
vendor/artifacts/v8/include/OWNERS vendored Normal file
View File

@ -0,0 +1,23 @@
cbruni@chromium.org
leszeks@chromium.org
mlippautz@chromium.org
verwaest@chromium.org
yangguo@chromium.org
per-file *DEPS=file:../COMMON_OWNERS
per-file v8-internal.h=file:../COMMON_OWNERS
per-file v8-debug.h=file:../src/debug/OWNERS
per-file js_protocol.pdl=file:../src/inspector/OWNERS
per-file v8-inspector*=file:../src/inspector/OWNERS
per-file v8-inspector*=file:../src/inspector/OWNERS
per-file v8-profiler.h=file:../src/profiler/OWNERS
# Needed by the auto_tag builder
per-file v8-version.h=v8-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com
# For branch updates:
per-file v8-version.h=file:../INFRA_OWNERS
per-file v8-version.h=vahl@chromium.org

View File

@ -0,0 +1,9 @@
include_rules = [
"-include",
"+v8config.h",
"+v8-platform.h",
"+v8-source-location.h",
"+cppgc",
"-src",
"+libplatform/libplatform.h",
]

View File

@ -0,0 +1,2 @@
bikineev@chromium.org
omerkatz@chromium.org

View File

@ -0,0 +1,135 @@
# Oilpan: C++ Garbage Collection
Oilpan is an open-source garbage collection library for C++ that can be used stand-alone or in collaboration with V8's JavaScript garbage collector.
Oilpan implements mark-and-sweep garbage collection (GC) with limited compaction (for a subset of objects).
**Key properties**
- Trace-based garbage collection;
- Incremental and concurrent marking;
- Incremental and concurrent sweeping;
- Precise on-heap memory layout;
- Conservative on-stack memory layout;
- Allows for collection with and without considering stack;
- Non-incremental and non-concurrent compaction for selected spaces;
See the [Hello World](https://chromium.googlesource.com/v8/v8/+/main/samples/cppgc/hello-world.cc) example on how to get started using Oilpan to manage C++ code.
Oilpan follows V8's project organization, see e.g. on how we accept [contributions](https://v8.dev/docs/contribute) and [provide a stable API](https://v8.dev/docs/api).
## Threading model
Oilpan features thread-local garbage collection and assumes heaps are not shared among threads.
In other words, objects are accessed and ultimately reclaimed by the garbage collector on the same thread that allocates them.
This allows Oilpan to run garbage collection in parallel with mutators running in other threads.
References to objects belonging to another thread's heap are modeled using cross-thread roots.
This is even true for on-heap to on-heap references.
Oilpan heaps may generally not be accessed from different threads unless otherwise noted.
## Heap partitioning
Oilpan's heaps are partitioned into spaces.
The space for an object is chosen depending on a number of criteria, e.g.:
- Objects over 64KiB are allocated in a large object space
- Objects can be assigned to a dedicated custom space.
Custom spaces can also be marked as compactable.
- Other objects are allocated in one of the normal page spaces bucketed depending on their size.
## Precise and conservative garbage collection
Oilpan supports two kinds of GCs:
1. **Conservative GC.**
A GC is called conservative when it is executed while the regular native stack is not empty.
In this case, the native stack might contain references to objects in Oilpan's heap, which should be kept alive.
The GC scans the native stack and treats the pointers discovered via the native stack as part of the root set.
This kind of GC is considered imprecise because values on stack other than references may accidentally appear as references to on-heap object, which means these objects will be kept alive despite being in practice unreachable from the application as an actual reference.
2. **Precise GC.**
A precise GC is triggered at the end of an event loop, which is controlled by an embedder via a platform.
At this point, it is guaranteed that there are no on-stack references pointing to Oilpan's heap.
This means there is no risk of confusing other value types with references.
Oilpan has precise knowledge of on-heap object layouts, and so it knows exactly where pointers lie in memory.
Oilpan can just start marking from the regular root set and collect all garbage precisely.
## Atomic, incremental and concurrent garbage collection
Oilpan has three modes of operation:
1. **Atomic GC.**
The entire GC cycle, including all its phases (e.g. see [Marking](#Marking-phase) and [Sweeping](#Sweeping-phase)), are executed back to back in a single pause.
This mode of operation is also known as Stop-The-World (STW) garbage collection.
It results in the most jank (due to a single long pause), but is overall the most efficient (e.g. no need for write barriers).
2. **Incremental GC.**
Garbage collection work is split up into multiple steps which are interleaved with the mutator, i.e. user code chunked into tasks.
Each step is a small chunk of work that is executed either as dedicated tasks between mutator tasks or, as needed, during mutator tasks.
Using incremental GC introduces the need for write barriers that record changes to the object graph so that a consistent state is observed and no objects are accidentally considered dead and reclaimed.
The incremental steps are followed by a smaller atomic pause to finalize garbage collection.
The smaller pause times, due to smaller chunks of work, helps with reducing jank.
3. **Concurrent GC.**
This is the most common type of GC.
It builds on top of incremental GC and offloads much of the garbage collection work away from the mutator thread and on to background threads.
Using concurrent GC allows the mutator thread to spend less time on GC and more on the actual mutator.
## Marking phase
The marking phase consists of the following steps:
1. Mark all objects in the root set.
2. Mark all objects transitively reachable from the root set by calling `Trace()` methods defined on each object.
3. Clear out all weak handles to unreachable objects and run weak callbacks.
The marking phase can be executed atomically in a stop-the-world manner, in which all 3 steps are executed one after the other.
Alternatively, it can also be executed incrementally/concurrently.
With incremental/concurrent marking, step 1 is executed in a short pause after which the mutator regains control.
Step 2 is repeatedly executed in an interleaved manner with the mutator.
When the GC is ready to finalize, i.e. step 2 is (almost) finished, another short pause is triggered in which step 2 is finished and step 3 is performed.
To prevent a user-after-free (UAF) issues it is required for Oilpan to know about all edges in the object graph.
This means that all pointers except on-stack pointers must be wrapped with Oilpan's handles (i.e., Persistent<>, Member<>, WeakMember<>).
Raw pointers to on-heap objects create an edge that Oilpan cannot observe and cause UAF issues
Thus, raw pointers shall not be used to reference on-heap objects (except for raw pointers on native stacks).
## Sweeping phase
The sweeping phase consists of the following steps:
1. Invoke pre-finalizers.
At this point, no destructors have been invoked and no memory has been reclaimed.
Pre-finalizers are allowed to access any other on-heap objects, even those that may get destructed.
2. Sweeping invokes destructors of the dead (unreachable) objects and reclaims memory to be reused by future allocations.
Assumptions should not be made about the order and the timing of their execution.
There is no guarantee on the order in which the destructors are invoked.
That's why destructors must not access any other on-heap objects (which might have already been destructed).
If some destructor unavoidably needs to access other on-heap objects, it will have to be converted to a pre-finalizer.
The pre-finalizer is allowed to access other on-heap objects.
The mutator is resumed before all destructors have ran.
For example, imagine a case where X is a client of Y, and Y holds a list of clients.
If the code relies on X's destructor removing X from the list, there is a risk that Y iterates the list and calls some method of X which may touch other on-heap objects.
This causes a use-after-free.
Care must be taken to make sure that X is explicitly removed from the list before the mutator resumes its execution in a way that doesn't rely on X's destructor (e.g. a pre-finalizer).
Similar to marking, sweeping can be executed in either an atomic stop-the-world manner or incrementally/concurrently.
With incremental/concurrent sweeping, step 2 is interleaved with mutator.
Incremental/concurrent sweeping can be atomically finalized in case it is needed to trigger another GC cycle.
Even with concurrent sweeping, destructors are guaranteed to run on the thread the object has been allocated on to preserve C++ semantics.
Notes:
* Weak processing runs only when the holder object of the WeakMember outlives the pointed object.
If the holder object and the pointed object die at the same time, weak processing doesn't run.
It is wrong to write code assuming that the weak processing always runs.
* Pre-finalizers are heavy because the thread needs to scan all pre-finalizers at each sweeping phase to determine which pre-finalizers should be invoked (the thread needs to invoke pre-finalizers of dead objects).
Adding pre-finalizers to frequently created objects should be avoided.

View File

@ -0,0 +1,358 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_ALLOCATION_H_
#define INCLUDE_CPPGC_ALLOCATION_H_
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <new>
#include <type_traits>
#include <utility>
#include "cppgc/custom-space.h"
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/gc-info.h"
#include "cppgc/type-traits.h"
#include "v8config.h" // NOLINT(build/include_directory)
#if defined(__has_attribute)
#if __has_attribute(assume_aligned)
#define CPPGC_DEFAULT_ALIGNED \
__attribute__((assume_aligned(api_constants::kDefaultAlignment)))
#define CPPGC_DOUBLE_WORD_ALIGNED \
__attribute__((assume_aligned(2 * api_constants::kDefaultAlignment)))
#endif // __has_attribute(assume_aligned)
#endif // defined(__has_attribute)
#if !defined(CPPGC_DEFAULT_ALIGNED)
#define CPPGC_DEFAULT_ALIGNED
#endif
#if !defined(CPPGC_DOUBLE_WORD_ALIGNED)
#define CPPGC_DOUBLE_WORD_ALIGNED
#endif
namespace cppgc {
/**
* AllocationHandle is used to allocate garbage-collected objects.
*/
class AllocationHandle;
class GarbageCollectedMixin;
namespace internal {
using AlignVal = std::align_val_t;
class MakeGarbageCollectedTraitInternal {
protected:
enum class CanContainMixins { kNo, kYes };
static inline void MarkObjectAsFullyConstructed(const void* payload) {
// See api_constants for an explanation of the constants.
std::atomic_ref<uint16_t> atomic_mutable_bitfield(
*const_cast<uint16_t*>(reinterpret_cast<const uint16_t*>(
reinterpret_cast<const uint8_t*>(payload) -
api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
// It's safe to split use load+store here (instead of a read-modify-write
// operation), since it's guaranteed that this 16-bit bitfield is only
// modified by a single thread. This is cheaper in terms of code bloat (on
// ARM) and performance.
uint16_t value = atomic_mutable_bitfield.load(std::memory_order_relaxed);
value |= api_constants::kFullyConstructedBitMask;
atomic_mutable_bitfield.store(value, std::memory_order_release);
}
// Dispatch based on compile-time information.
//
// Default implementation is for a custom space with >`kDefaultAlignment` byte
// alignment.
template <typename GCInfoType, typename CustomSpace, size_t alignment,
CanContainMixins contain_mixins>
struct AllocationDispatcher final {
static void* Invoke(AllocationHandle& handle, size_t size) {
static_assert(std::is_base_of_v<CustomSpaceBase, CustomSpace>,
"Custom space must inherit from CustomSpaceBase.");
static_assert(
!CustomSpace::kSupportsCompaction,
"Custom spaces that support compaction do not support allocating "
"objects with non-default (i.e. word-sized) alignment.");
if constexpr (contain_mixins == CanContainMixins::kYes) {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, static_cast<AlignVal>(alignment),
internal::GCInfoTrait<GCInfoType>::Index(),
CustomSpace::kSpaceIndex, kMixinTag);
} else {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, static_cast<AlignVal>(alignment),
internal::GCInfoTrait<GCInfoType>::Index(),
CustomSpace::kSpaceIndex);
}
}
};
// Fast path for regular allocations for the default space with
// `kDefaultAlignment` byte alignment.
template <typename GCInfoType, CanContainMixins contain_mixins>
struct AllocationDispatcher<GCInfoType, void,
api_constants::kDefaultAlignment, contain_mixins>
final {
static void* Invoke(AllocationHandle& handle, size_t size) {
if constexpr (contain_mixins == CanContainMixins::kYes) {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, internal::GCInfoTrait<GCInfoType>::Index(),
kMixinTag);
} else {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, internal::GCInfoTrait<GCInfoType>::Index());
}
}
};
// Default space with >`kDefaultAlignment` byte alignment.
template <typename GCInfoType, size_t alignment,
CanContainMixins contain_mixins>
struct AllocationDispatcher<GCInfoType, void, alignment, contain_mixins>
final {
static void* Invoke(AllocationHandle& handle, size_t size) {
if constexpr (contain_mixins == CanContainMixins::kYes) {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, static_cast<AlignVal>(alignment),
internal::GCInfoTrait<GCInfoType>::Index(), kMixinTag);
} else {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, static_cast<AlignVal>(alignment),
internal::GCInfoTrait<GCInfoType>::Index());
}
}
};
// Custom space with `kDefaultAlignment` byte alignment.
template <typename GCInfoType, typename CustomSpace,
CanContainMixins contain_mixins>
struct AllocationDispatcher<GCInfoType, CustomSpace,
api_constants::kDefaultAlignment, contain_mixins>
final {
static void* Invoke(AllocationHandle& handle, size_t size) {
static_assert(std::is_base_of_v<CustomSpaceBase, CustomSpace>,
"Custom space must inherit from CustomSpaceBase.");
if constexpr (contain_mixins == CanContainMixins::kYes) {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, internal::GCInfoTrait<GCInfoType>::Index(),
CustomSpace::kSpaceIndex, kMixinTag);
} else {
return MakeGarbageCollectedTraitInternal::Allocate(
handle, size, internal::GCInfoTrait<GCInfoType>::Index(),
CustomSpace::kSpaceIndex);
}
}
};
private:
inline const static struct MixinTag {
} kMixinTag;
V8_EXPORT static void* CPPGC_DEFAULT_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, GCInfoIndex);
V8_EXPORT static void* CPPGC_DOUBLE_WORD_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, AlignVal, GCInfoIndex);
V8_EXPORT static void* CPPGC_DEFAULT_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, GCInfoIndex, CustomSpaceIndex);
V8_EXPORT static void* CPPGC_DOUBLE_WORD_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, AlignVal, GCInfoIndex,
CustomSpaceIndex);
V8_EXPORT static void* CPPGC_DEFAULT_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, GCInfoIndex, MixinTag);
V8_EXPORT static void* CPPGC_DOUBLE_WORD_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, AlignVal, GCInfoIndex, MixinTag);
V8_EXPORT static void* CPPGC_DEFAULT_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, GCInfoIndex, CustomSpaceIndex,
MixinTag);
V8_EXPORT static void* CPPGC_DOUBLE_WORD_ALIGNED
Allocate(cppgc::AllocationHandle&, size_t, AlignVal, GCInfoIndex,
CustomSpaceIndex, MixinTag);
friend class HeapObjectHeader;
};
} // namespace internal
/**
* Base trait that provides utilities for advancers users that have custom
* allocation needs (e.g., overriding size). It's expected that users override
* MakeGarbageCollectedTrait (see below) and inherit from
* MakeGarbageCollectedTraitBase and make use of the low-level primitives
* offered to allocate and construct an object.
*/
template <typename T>
class MakeGarbageCollectedTraitBase
: private internal::MakeGarbageCollectedTraitInternal {
private:
static_assert(internal::IsGarbageCollectedType<T>::value,
"T needs to be a garbage collected object");
static_assert(!IsGarbageCollectedWithMixinTypeV<T> ||
sizeof(T) <=
internal::api_constants::kLargeObjectSizeThreshold,
"GarbageCollectedMixin may not be a large object");
protected:
/**
* Allocates memory for an object of type T.
*
* \param handle AllocationHandle identifying the heap to allocate the object
* on.
* \param size The size that should be reserved for the object.
* \returns the memory to construct an object of type T on.
*/
V8_INLINE static void* Allocate(AllocationHandle& handle, size_t size) {
static_assert(
std::is_base_of_v<typename T::ParentMostGarbageCollectedType, T>,
"U of GarbageCollected<U> must be a base of T. Check "
"GarbageCollected<T> base class inheritance.");
static constexpr size_t kWantedAlignment =
alignof(T) < internal::api_constants::kDefaultAlignment
? internal::api_constants::kDefaultAlignment
: alignof(T);
constexpr CanContainMixins kMayContainMixins =
std::is_base_of_v<GarbageCollectedMixin, T> ? CanContainMixins::kYes
: CanContainMixins::kNo;
static_assert(
kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment,
"Requested alignment larger than alignof(std::max_align_t) bytes. "
"Please file a bug to possibly get this restriction lifted.");
return AllocationDispatcher<
typename internal::GCInfoFolding<
T, typename T::ParentMostGarbageCollectedType>::ResultType,
typename SpaceTrait<T>::Space, kWantedAlignment,
kMayContainMixins>::Invoke(handle, size);
}
/**
* Marks an object as fully constructed, resulting in precise handling by the
* garbage collector.
*
* \param payload The base pointer the object is allocated at.
*/
V8_INLINE static void MarkObjectAsFullyConstructed(const void* payload) {
internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed(
payload);
}
};
/**
* Passed to MakeGarbageCollected to specify how many bytes should be appended
* to the allocated object.
*
* Example:
* \code
* class InlinedArray final : public GarbageCollected<InlinedArray> {
* public:
* explicit InlinedArray(size_t bytes) : size(bytes), byte_array(this + 1) {}
* void Trace(Visitor*) const {}
* size_t size;
* char* byte_array;
* };
*
* auto* inlined_array = MakeGarbageCollected<InlinedArray(
* GetAllocationHandle(), AdditionalBytes(4), 4);
* for (size_t i = 0; i < 4; i++) {
* Process(inlined_array->byte_array[i]);
* }
* \endcode
*/
struct AdditionalBytes {
constexpr explicit AdditionalBytes(size_t bytes) : value(bytes) {}
const size_t value;
};
/**
* Default trait class that specifies how to construct an object of type T.
* Advanced users may override how an object is constructed using the utilities
* that are provided through MakeGarbageCollectedTraitBase.
*
* Any trait overriding construction must
* - allocate through `MakeGarbageCollectedTraitBase<T>::Allocate`;
* - mark the object as fully constructed using
* `MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed`;
*/
template <typename T>
class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> {
public:
template <typename... Args>
static T* Call(AllocationHandle& handle, Args&&... args) {
void* memory =
MakeGarbageCollectedTraitBase<T>::Allocate(handle, sizeof(T));
T* object = ::new (memory) T(std::forward<Args>(args)...);
MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
return object;
}
template <typename... Args>
static T* Call(AllocationHandle& handle, AdditionalBytes additional_bytes,
Args&&... args) {
void* memory = MakeGarbageCollectedTraitBase<T>::Allocate(
handle, sizeof(T) + additional_bytes.value);
T* object = ::new (memory) T(std::forward<Args>(args)...);
MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
return object;
}
};
/**
* Allows users to specify a post-construction callback for specific types. The
* callback is invoked on the instance of type T right after it has been
* constructed. This can be useful when the callback requires a
* fully-constructed object to be able to dispatch to virtual methods.
*/
template <typename T, typename = void>
struct PostConstructionCallbackTrait {
static void Call(T*) {}
};
/**
* Constructs a managed object of type T where T transitively inherits from
* GarbageCollected.
*
* \param args List of arguments with which an instance of T will be
* constructed.
* \returns an instance of type T.
*/
template <typename T, typename... Args>
V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) {
T* object =
MakeGarbageCollectedTrait<T>::Call(handle, std::forward<Args>(args)...);
PostConstructionCallbackTrait<T>::Call(object);
return object;
}
/**
* Constructs a managed object of type T where T transitively inherits from
* GarbageCollected. Created objects will have additional bytes appended to
* it. Allocated memory would suffice for `sizeof(T) + additional_bytes`.
*
* \param additional_bytes Denotes how many bytes to append to T.
* \param args List of arguments with which an instance of T will be
* constructed.
* \returns an instance of type T.
*/
template <typename T, typename... Args>
V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle,
AdditionalBytes additional_bytes,
Args&&... args) {
T* object = MakeGarbageCollectedTrait<T>::Call(handle, additional_bytes,
std::forward<Args>(args)...);
PostConstructionCallbackTrait<T>::Call(object);
return object;
}
} // namespace cppgc
#undef CPPGC_DEFAULT_ALIGNED
#undef CPPGC_DOUBLE_WORD_ALIGNED
#endif // INCLUDE_CPPGC_ALLOCATION_H_

View File

@ -0,0 +1,28 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_COMMON_H_
#define INCLUDE_CPPGC_COMMON_H_
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
/**
* Indicator for the stack state of the embedder.
*/
enum class EmbedderStackState {
/**
* Stack may contain interesting heap pointers.
*/
kMayContainHeapPointers,
/**
* Stack does not contain any interesting heap pointers.
*/
kNoHeapPointers,
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_COMMON_H_

View File

@ -0,0 +1,466 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_
#define INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_
#include <atomic>
#include "cppgc/internal/persistent-node.h"
#include "cppgc/internal/pointer-policies.h"
#include "cppgc/persistent.h"
#include "cppgc/visitor.h"
namespace cppgc {
namespace internal {
// Wrapper around PersistentBase that allows accessing poisoned memory when
// using ASAN. This is needed as the GC of the heap that owns the value
// of a CTP, may clear it (heap termination, weakness) while the object
// holding the CTP may be poisoned as itself may be deemed dead.
class CrossThreadPersistentBase : public PersistentBase {
public:
CrossThreadPersistentBase() = default;
explicit CrossThreadPersistentBase(const void* raw) : PersistentBase(raw) {}
V8_CLANG_NO_SANITIZE("address") const void* GetValueFromGC() const {
return raw_;
}
V8_CLANG_NO_SANITIZE("address")
PersistentNode* GetNodeFromGC() const { return node_; }
V8_CLANG_NO_SANITIZE("address")
void ClearFromGC() const {
raw_ = nullptr;
SetNodeSafe(nullptr);
}
// GetNodeSafe() can be used for a thread-safe IsValid() check in a
// double-checked locking pattern. See ~BasicCrossThreadPersistent.
PersistentNode* GetNodeSafe() const {
return reinterpret_cast<std::atomic<PersistentNode*>*>(&node_)->load(
std::memory_order_acquire);
}
// The GC writes using SetNodeSafe() while holding the lock.
V8_CLANG_NO_SANITIZE("address")
void SetNodeSafe(PersistentNode* value) const {
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define V8_IS_ASAN 1
#endif
#endif
#ifdef V8_IS_ASAN
__atomic_store(&node_, &value, __ATOMIC_RELEASE);
#else // !V8_IS_ASAN
// Non-ASAN builds can use atomics. This also covers MSVC which does not
// have the __atomic_store intrinsic.
reinterpret_cast<std::atomic<PersistentNode*>*>(&node_)->store(
value, std::memory_order_release);
#endif // !V8_IS_ASAN
#undef V8_IS_ASAN
}
};
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
class BasicCrossThreadPersistent final : public CrossThreadPersistentBase,
public LocationPolicy,
private WeaknessPolicy,
private CheckingPolicy {
public:
using typename WeaknessPolicy::IsStrongPersistent;
using PointeeType = T;
~BasicCrossThreadPersistent() {
// This implements fast path for destroying empty/sentinel.
//
// Simplified version of `AssignUnsafe()` to allow calling without a
// complete type `T`. Uses double-checked locking with a simple thread-safe
// check for a valid handle based on a node.
if (GetNodeSafe()) {
PersistentRegionLock guard;
const void* old_value = GetValue();
// The fast path check (GetNodeSafe()) does not acquire the lock. Recheck
// validity while holding the lock to ensure the reference has not been
// cleared.
if (IsValid(old_value)) {
CrossThreadPersistentRegion& region =
this->GetPersistentRegion(old_value);
region.FreeNode(GetNode());
SetNode(nullptr);
} else {
CPPGC_DCHECK(!GetNode());
}
}
// No need to call SetValue() as the handle is not used anymore. This can
// leave behind stale sentinel values but will always destroy the underlying
// node.
}
BasicCrossThreadPersistent(SourceLocation loc = SourceLocation::Current())
: LocationPolicy(loc) {}
BasicCrossThreadPersistent(std::nullptr_t,
SourceLocation loc = SourceLocation::Current())
: LocationPolicy(loc) {}
BasicCrossThreadPersistent(SentinelPointer s,
SourceLocation loc = SourceLocation::Current())
: CrossThreadPersistentBase(s), LocationPolicy(loc) {}
BasicCrossThreadPersistent(T* raw,
SourceLocation loc = SourceLocation::Current())
: CrossThreadPersistentBase(raw), LocationPolicy(loc) {
if (!IsValid(raw)) return;
PersistentRegionLock guard;
CrossThreadPersistentRegion& region = this->GetPersistentRegion(raw);
SetNode(region.AllocateNode(this, &TraceAsRoot));
this->CheckPointer(raw);
}
class UnsafeCtorTag {
private:
UnsafeCtorTag() = default;
template <typename U, typename OtherWeaknessPolicy,
typename OtherLocationPolicy, typename OtherCheckingPolicy>
friend class BasicCrossThreadPersistent;
};
BasicCrossThreadPersistent(UnsafeCtorTag, T* raw,
SourceLocation loc = SourceLocation::Current())
: CrossThreadPersistentBase(raw), LocationPolicy(loc) {
if (!IsValid(raw)) return;
CrossThreadPersistentRegion& region = this->GetPersistentRegion(raw);
SetNode(region.AllocateNode(this, &TraceAsRoot));
this->CheckPointer(raw);
}
BasicCrossThreadPersistent(T& raw,
SourceLocation loc = SourceLocation::Current())
: BasicCrossThreadPersistent(&raw, loc) {}
template <typename U, typename MemberBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
BasicCrossThreadPersistent(
internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
MemberCheckingPolicy, MemberStorageType>
member,
SourceLocation loc = SourceLocation::Current())
: BasicCrossThreadPersistent(member.Get(), loc) {}
BasicCrossThreadPersistent(const BasicCrossThreadPersistent& other,
SourceLocation loc = SourceLocation::Current())
: BasicCrossThreadPersistent(loc) {
// Invoke operator=.
*this = other;
}
// Heterogeneous ctor.
template <typename U, typename OtherWeaknessPolicy,
typename OtherLocationPolicy, typename OtherCheckingPolicy,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
BasicCrossThreadPersistent(const BasicCrossThreadPersistent<
U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>& other,
SourceLocation loc = SourceLocation::Current())
: BasicCrossThreadPersistent(loc) {
*this = other;
}
BasicCrossThreadPersistent(
BasicCrossThreadPersistent&& other,
SourceLocation loc = SourceLocation::Current()) noexcept {
// Invoke operator=.
*this = std::move(other);
}
BasicCrossThreadPersistent& operator=(
const BasicCrossThreadPersistent& other) {
PersistentRegionLock guard;
AssignSafe(guard, other.Get());
return *this;
}
template <typename U, typename OtherWeaknessPolicy,
typename OtherLocationPolicy, typename OtherCheckingPolicy,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
BasicCrossThreadPersistent& operator=(
const BasicCrossThreadPersistent<U, OtherWeaknessPolicy,
OtherLocationPolicy,
OtherCheckingPolicy>& other) {
PersistentRegionLock guard;
AssignSafe(guard, other.Get());
return *this;
}
BasicCrossThreadPersistent& operator=(BasicCrossThreadPersistent&& other) {
if (this == &other) return *this;
Clear();
PersistentRegionLock guard;
PersistentBase::operator=(std::move(other));
LocationPolicy::operator=(std::move(other));
if (!IsValid(GetValue())) return *this;
GetNode()->UpdateOwner(this);
other.SetValue(nullptr);
other.SetNode(nullptr);
this->CheckPointer(Get());
return *this;
}
/**
* Assigns a raw pointer.
*
* Note: **Not thread-safe.**
*/
BasicCrossThreadPersistent& operator=(T* other) {
AssignUnsafe(other);
return *this;
}
// Assignment from member.
template <typename U, typename MemberBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
BasicCrossThreadPersistent& operator=(
internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
MemberCheckingPolicy, MemberStorageType>
member) {
return operator=(member.Get());
}
/**
* Assigns a nullptr.
*
* \returns the handle.
*/
BasicCrossThreadPersistent& operator=(std::nullptr_t) {
Clear();
return *this;
}
/**
* Assigns the sentinel pointer.
*
* \returns the handle.
*/
BasicCrossThreadPersistent& operator=(SentinelPointer s) {
PersistentRegionLock guard;
AssignSafe(guard, s);
return *this;
}
/**
* Returns a pointer to the stored object.
*
* Note: **Not thread-safe.**
*
* \returns a pointer to the stored object.
*/
// CFI cast exemption to allow passing SentinelPointer through T* and support
// heterogeneous assignments between different Member and Persistent handles
// based on their actual types.
V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const {
return static_cast<T*>(const_cast<void*>(GetValue()));
}
/**
* Clears the stored object.
*/
void Clear() {
PersistentRegionLock guard;
AssignSafe(guard, nullptr);
}
/**
* Returns a pointer to the stored object and releases it.
*
* Note: **Not thread-safe.**
*
* \returns a pointer to the stored object.
*/
T* Release() {
T* result = Get();
Clear();
return result;
}
/**
* Conversio to boolean.
*
* Note: **Not thread-safe.**
*
* \returns true if an actual object has been stored and false otherwise.
*/
explicit operator bool() const { return Get(); }
/**
* Conversion to object of type T.
*
* Note: **Not thread-safe.**
*
* \returns the object.
*/
operator T*() const { return Get(); }
/**
* Dereferences the stored object.
*
* Note: **Not thread-safe.**
*/
T* operator->() const { return Get(); }
T& operator*() const { return *Get(); }
template <typename U, typename OtherWeaknessPolicy = WeaknessPolicy,
typename OtherLocationPolicy = LocationPolicy,
typename OtherCheckingPolicy = CheckingPolicy>
BasicCrossThreadPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>
To() const {
using OtherBasicCrossThreadPersistent =
BasicCrossThreadPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>;
PersistentRegionLock guard;
return OtherBasicCrossThreadPersistent(
typename OtherBasicCrossThreadPersistent::UnsafeCtorTag(),
static_cast<U*>(Get()));
}
template <typename U = T,
typename = std::enable_if_t<!BasicCrossThreadPersistent<
U, WeaknessPolicy>::IsStrongPersistent::value>>
BasicCrossThreadPersistent<U, internal::StrongCrossThreadPersistentPolicy>
Lock() const {
return BasicCrossThreadPersistent<
U, internal::StrongCrossThreadPersistentPolicy>(*this);
}
private:
static bool IsValid(const void* ptr) {
return ptr && ptr != kSentinelPointer;
}
static void TraceAsRoot(RootVisitor& root_visitor, const void* ptr) {
root_visitor.Trace(*static_cast<const BasicCrossThreadPersistent*>(ptr));
}
void AssignUnsafe(T* ptr) {
const void* old_value = GetValue();
if (IsValid(old_value)) {
PersistentRegionLock guard;
old_value = GetValue();
// The fast path check (IsValid()) does not acquire the lock. Reload
// the value to ensure the reference has not been cleared.
if (IsValid(old_value)) {
CrossThreadPersistentRegion& region =
this->GetPersistentRegion(old_value);
if (IsValid(ptr) && (&region == &this->GetPersistentRegion(ptr))) {
SetValue(ptr);
this->CheckPointer(ptr);
return;
}
region.FreeNode(GetNode());
SetNode(nullptr);
} else {
CPPGC_DCHECK(!GetNode());
}
}
SetValue(ptr);
if (!IsValid(ptr)) return;
PersistentRegionLock guard;
SetNode(this->GetPersistentRegion(ptr).AllocateNode(this, &TraceAsRoot));
this->CheckPointer(ptr);
}
void AssignSafe(PersistentRegionLock&, T* ptr) {
PersistentRegionLock::AssertLocked();
const void* old_value = GetValue();
if (IsValid(old_value)) {
CrossThreadPersistentRegion& region =
this->GetPersistentRegion(old_value);
if (IsValid(ptr) && (&region == &this->GetPersistentRegion(ptr))) {
SetValue(ptr);
this->CheckPointer(ptr);
return;
}
region.FreeNode(GetNode());
SetNode(nullptr);
}
SetValue(ptr);
if (!IsValid(ptr)) return;
SetNode(this->GetPersistentRegion(ptr).AllocateNode(this, &TraceAsRoot));
this->CheckPointer(ptr);
}
void ClearFromGC() const {
if (IsValid(GetValueFromGC())) {
WeaknessPolicy::GetPersistentRegion(GetValueFromGC())
.FreeNode(GetNodeFromGC());
CrossThreadPersistentBase::ClearFromGC();
}
}
SourceLocation LocationFromGC() const {
return LocationPolicy::LocationFromGC();
}
// See Get() for details.
V8_CLANG_NO_SANITIZE("cfi-unrelated-cast")
T* GetFromGC() const {
return static_cast<T*>(const_cast<void*>(GetValueFromGC()));
}
friend class internal::RootVisitor;
};
template <typename T, typename LocationPolicy, typename CheckingPolicy>
struct IsWeak<
BasicCrossThreadPersistent<T, internal::WeakCrossThreadPersistentPolicy,
LocationPolicy, CheckingPolicy>>
: std::true_type {};
} // namespace internal
namespace subtle {
/**
* **DO NOT USE: Has known caveats, see below.**
*
* CrossThreadPersistent allows retaining objects from threads other than the
* thread the owning heap is operating on.
*
* Known caveats:
* - Does not protect the heap owning an object from terminating.
* - Reaching transitively through the graph is unsupported as objects may be
* moved concurrently on the thread owning the object.
*/
template <typename T>
using CrossThreadPersistent = internal::BasicCrossThreadPersistent<
T, internal::StrongCrossThreadPersistentPolicy>;
/**
* **DO NOT USE: Has known caveats, see below.**
*
* CrossThreadPersistent allows weakly retaining objects from threads other than
* the thread the owning heap is operating on.
*
* Known caveats:
* - Does not protect the heap owning an object from terminating.
* - Reaching transitively through the graph is unsupported as objects may be
* moved concurrently on the thread owning the object.
*/
template <typename T>
using WeakCrossThreadPersistent = internal::BasicCrossThreadPersistent<
T, internal::WeakCrossThreadPersistentPolicy>;
} // namespace subtle
} // namespace cppgc
#endif // INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_

View File

@ -0,0 +1,97 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_CUSTOM_SPACE_H_
#define INCLUDE_CPPGC_CUSTOM_SPACE_H_
#include <stddef.h>
namespace cppgc {
/**
* Index identifying a custom space.
*/
struct CustomSpaceIndex {
constexpr CustomSpaceIndex(size_t value) : value(value) {} // NOLINT
size_t value;
};
/**
* Top-level base class for custom spaces. Users must inherit from CustomSpace
* below.
*/
class CustomSpaceBase {
public:
virtual ~CustomSpaceBase() = default;
virtual CustomSpaceIndex GetCustomSpaceIndex() const = 0;
virtual bool IsCompactable() const = 0;
};
/**
* Base class custom spaces should directly inherit from. The class inheriting
* from `CustomSpace` must define `kSpaceIndex` as unique space index. These
* indices need for form a sequence starting at 0.
*
* Example:
* \code
* class CustomSpace1 : public CustomSpace<CustomSpace1> {
* public:
* static constexpr CustomSpaceIndex kSpaceIndex = 0;
* };
* class CustomSpace2 : public CustomSpace<CustomSpace2> {
* public:
* static constexpr CustomSpaceIndex kSpaceIndex = 1;
* };
* \endcode
*/
template <typename ConcreteCustomSpace>
class CustomSpace : public CustomSpaceBase {
public:
/**
* Compaction is only supported on spaces that manually manage slots
* recording.
*/
static constexpr bool kSupportsCompaction = false;
CustomSpaceIndex GetCustomSpaceIndex() const final {
return ConcreteCustomSpace::kSpaceIndex;
}
bool IsCompactable() const final {
return ConcreteCustomSpace::kSupportsCompaction;
}
};
/**
* User-overridable trait that allows pinning types to custom spaces.
*/
template <typename T, typename = void>
struct SpaceTrait {
using Space = void;
};
namespace internal {
template <typename CustomSpace>
struct IsAllocatedOnCompactableSpaceImpl {
static constexpr bool value = CustomSpace::kSupportsCompaction;
};
template <>
struct IsAllocatedOnCompactableSpaceImpl<void> {
// Non-custom spaces are by default not compactable.
static constexpr bool value = false;
};
template <typename T>
struct IsAllocatedOnCompactableSpace {
public:
static constexpr bool value =
IsAllocatedOnCompactableSpaceImpl<typename SpaceTrait<T>::Space>::value;
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_CUSTOM_SPACE_H_

View File

@ -0,0 +1,68 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
#define INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
#include <memory>
#include "cppgc/platform.h"
#include "libplatform/libplatform.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
/**
* Platform provided by cppgc. Uses V8's DefaultPlatform provided by
* libplatform internally. Exception: `GetForegroundTaskRunner()`, see below.
*/
class V8_EXPORT DefaultPlatform : public Platform {
public:
using IdleTaskSupport = v8::platform::IdleTaskSupport;
explicit DefaultPlatform(
int thread_pool_size = 0,
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
std::unique_ptr<TracingController> tracing_controller = {})
: v8_platform_(v8::platform::NewDefaultPlatform(
thread_pool_size, idle_task_support,
v8::platform::InProcessStackDumping::kDisabled,
std::move(tracing_controller))) {}
cppgc::PageAllocator* GetPageAllocator() override {
return v8_platform_->GetPageAllocator();
}
double MonotonicallyIncreasingTime() override {
return v8_platform_->MonotonicallyIncreasingTime();
}
std::shared_ptr<cppgc::TaskRunner> GetForegroundTaskRunner(
TaskPriority priority) override {
// V8's default platform creates a new task runner when passed the
// `v8::Isolate` pointer the first time. For non-default platforms this will
// require getting the appropriate task runner.
return v8_platform_->GetForegroundTaskRunner(kNoIsolate, priority);
}
std::unique_ptr<cppgc::JobHandle> PostJob(
cppgc::TaskPriority priority,
std::unique_ptr<cppgc::JobTask> job_task) override {
return v8_platform_->PostJob(priority, std::move(job_task));
}
TracingController* GetTracingController() override {
return v8_platform_->GetTracingController();
}
v8::Platform* GetV8Platform() const { return v8_platform_.get(); }
protected:
static constexpr v8::Isolate* kNoIsolate = nullptr;
std::unique_ptr<v8::Platform> v8_platform_;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_DEFAULT_PLATFORM_H_

View File

@ -0,0 +1,100 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
#define INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_
#include <cstddef>
#include "cppgc/allocation.h"
#include "cppgc/internal/logging.h"
#include "cppgc/type-traits.h"
namespace cppgc {
class HeapHandle;
namespace subtle {
template <typename T>
void FreeUnreferencedObject(HeapHandle& heap_handle, T& object);
template <typename T>
bool Resize(T& object, AdditionalBytes additional_bytes);
} // namespace subtle
namespace internal {
class ExplicitManagementImpl final {
private:
V8_EXPORT static void FreeUnreferencedObject(HeapHandle&, void*);
V8_EXPORT static bool Resize(void*, size_t);
template <typename T>
friend void subtle::FreeUnreferencedObject(HeapHandle&, T&);
template <typename T>
friend bool subtle::Resize(T&, AdditionalBytes);
};
} // namespace internal
namespace subtle {
/**
* Informs the garbage collector that `object` can be immediately reclaimed. The
* destructor may not be invoked immediately but only on next garbage
* collection.
*
* It is up to the embedder to guarantee that no other object holds a reference
* to `object` after calling `FreeUnreferencedObject()`. In case such a
* reference exists, it's use results in a use-after-free.
*
* To aid in using the API, `FreeUnreferencedObject()` may be called from
* destructors on objects that would be reclaimed in the same garbage collection
* cycle.
*
* \param heap_handle The corresponding heap.
* \param object Reference to an object that is of type `GarbageCollected` and
* should be immediately reclaimed.
*/
template <typename T>
void FreeUnreferencedObject(HeapHandle& heap_handle, T& object) {
static_assert(IsGarbageCollectedTypeV<T>,
"Object must be of type GarbageCollected.");
internal::ExplicitManagementImpl::FreeUnreferencedObject(heap_handle,
&object);
}
/**
* Tries to resize `object` of type `T` with additional bytes on top of
* sizeof(T). Resizing is only useful with trailing inlined storage, see e.g.
* `MakeGarbageCollected(AllocationHandle&, AdditionalBytes)`.
*
* `Resize()` performs growing or shrinking as needed and may skip the operation
* for internal reasons, see return value.
*
* It is up to the embedder to guarantee that in case of shrinking a larger
* object down, the reclaimed area is not used anymore. Any subsequent use
* results in a use-after-free.
*
* The `object` must be live when calling `Resize()`.
*
* \param object Reference to an object that is of type `GarbageCollected` and
* should be resized.
* \param additional_bytes Bytes in addition to sizeof(T) that the object should
* provide.
* \returns true when the operation was successful and the result can be relied
* on, and false otherwise.
*/
template <typename T>
bool Resize(T& object, AdditionalBytes additional_bytes) {
static_assert(IsGarbageCollectedTypeV<T>,
"Object must be of type GarbageCollected.");
return internal::ExplicitManagementImpl::Resize(
&object, sizeof(T) + additional_bytes.value);
}
} // namespace subtle
} // namespace cppgc
#endif // INCLUDE_CPPGC_EXPLICIT_MANAGEMENT_H_

View File

@ -0,0 +1,114 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_GARBAGE_COLLECTED_H_
#define INCLUDE_CPPGC_GARBAGE_COLLECTED_H_
#include "cppgc/internal/api-constants.h"
#include "cppgc/platform.h"
#include "cppgc/trace-trait.h"
#include "cppgc/type-traits.h"
namespace cppgc {
class Visitor;
/**
* Base class for managed objects. Only descendent types of `GarbageCollected`
* can be constructed using `MakeGarbageCollected()`. Must be inherited from as
* left-most base class.
*
* Types inheriting from GarbageCollected must provide a method of
* signature `void Trace(cppgc::Visitor*) const` that dispatchs all managed
* pointers to the visitor and delegates to garbage-collected base classes.
* The method must be virtual if the type is not directly a child of
* GarbageCollected and marked as final.
*
* \code
* // Example using final class.
* class FinalType final : public GarbageCollected<FinalType> {
* public:
* void Trace(cppgc::Visitor* visitor) const {
* // Dispatch using visitor->Trace(...);
* }
* };
*
* // Example using non-final base class.
* class NonFinalBase : public GarbageCollected<NonFinalBase> {
* public:
* virtual void Trace(cppgc::Visitor*) const {}
* };
*
* class FinalChild final : public NonFinalBase {
* public:
* void Trace(cppgc::Visitor* visitor) const final {
* // Dispatch using visitor->Trace(...);
* NonFinalBase::Trace(visitor);
* }
* };
* \endcode
*/
template <typename T>
class GarbageCollected {
public:
using IsGarbageCollectedTypeMarker = void;
using ParentMostGarbageCollectedType = T;
// Must use MakeGarbageCollected.
void* operator new(size_t) = delete;
void* operator new[](size_t) = delete;
// The garbage collector is taking care of reclaiming the object. Also,
// virtual destructor requires an unambiguous, accessible 'operator delete'.
void operator delete(void*) {
#ifdef V8_ENABLE_CHECKS
internal::Fatal(
"Manually deleting a garbage collected object is not allowed");
#endif // V8_ENABLE_CHECKS
}
void operator delete[](void*) = delete;
protected:
GarbageCollected() = default;
};
/**
* Base class for managed mixin objects. Such objects cannot be constructed
* directly but must be mixed into the inheritance hierarchy of a
* GarbageCollected object.
*
* Types inheriting from GarbageCollectedMixin must override a virtual method
* of signature `void Trace(cppgc::Visitor*) const` that dispatchs all managed
* pointers to the visitor and delegates to base classes.
*
* \code
* class Mixin : public GarbageCollectedMixin {
* public:
* void Trace(cppgc::Visitor* visitor) const override {
* // Dispatch using visitor->Trace(...);
* }
* };
* \endcode
*/
class GarbageCollectedMixin {
public:
using IsGarbageCollectedMixinTypeMarker = void;
// Must use MakeGarbageCollected.
void* operator new(size_t) = delete;
void* operator new[](size_t) = delete;
// The garbage collector is taking care of reclaiming the object.
// Not override the non-array varaint of `delete` to not conflict with the
// operator in GarbageCollected above.
void operator delete[](void*) = delete;
/**
* This Trace method must be overriden by objects inheriting from
* GarbageCollectedMixin.
*/
virtual void Trace(cppgc::Visitor*) const {}
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_GARBAGE_COLLECTED_H_

View File

@ -0,0 +1,309 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_HEAP_CONSISTENCY_H_
#define INCLUDE_CPPGC_HEAP_CONSISTENCY_H_
#include <cstddef>
#include "cppgc/internal/write-barrier.h"
#include "cppgc/macros.h"
#include "cppgc/member.h"
#include "cppgc/trace-trait.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
class HeapHandle;
namespace subtle {
/**
* **DO NOT USE: Use the appropriate managed types.**
*
* Consistency helpers that aid in maintaining a consistent internal state of
* the garbage collector.
*/
class HeapConsistency final {
public:
using WriteBarrierParams = internal::WriteBarrier::Params;
using WriteBarrierType = internal::WriteBarrier::Type;
/**
* Gets the required write barrier type for a specific write.
*
* \param slot Slot containing the pointer to the object. The slot itself
* must reside in an object that has been allocated using
* `MakeGarbageCollected()`.
* \param value The pointer to the object. May be an interior pointer to an
* interface of the actual object.
* \param params Parameters that may be used for actual write barrier calls.
* Only filled if return value indicates that a write barrier is needed. The
* contents of the `params` are an implementation detail.
* \returns whether a write barrier is needed and which barrier to invoke.
*/
static V8_INLINE WriteBarrierType GetWriteBarrierType(
const void* slot, const void* value, WriteBarrierParams& params) {
return internal::WriteBarrier::GetWriteBarrierType(slot, value, params);
}
/**
* Gets the required write barrier type for a specific write. This override is
* only used for all the BasicMember types.
*
* \param slot Slot containing the pointer to the object. The slot itself
* must reside in an object that has been allocated using
* `MakeGarbageCollected()`.
* \param value The pointer to the object held via `BasicMember`.
* \param params Parameters that may be used for actual write barrier calls.
* Only filled if return value indicates that a write barrier is needed. The
* contents of the `params` are an implementation detail.
* \returns whether a write barrier is needed and which barrier to invoke.
*/
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
static V8_INLINE WriteBarrierType GetWriteBarrierType(
const internal::BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& value,
WriteBarrierParams& params) {
return internal::WriteBarrier::GetWriteBarrierType(
value.GetRawSlot(), value.GetRawStorage(), params);
}
/**
* Gets the required write barrier type for a specific write.
*
* \param slot Slot to some part of an object. The object must not necessarily
have been allocated using `MakeGarbageCollected()` but can also live
off-heap or on stack.
* \param params Parameters that may be used for actual write barrier calls.
* Only filled if return value indicates that a write barrier is needed. The
* contents of the `params` are an implementation detail.
* \param callback Callback returning the corresponding heap handle. The
* callback is only invoked if the heap cannot otherwise be figured out. The
* callback must not allocate.
* \returns whether a write barrier is needed and which barrier to invoke.
*/
template <typename HeapHandleCallback>
static V8_INLINE WriteBarrierType
GetWriteBarrierType(const void* slot, WriteBarrierParams& params,
HeapHandleCallback callback) {
return internal::WriteBarrier::GetWriteBarrierType(slot, params, callback);
}
/**
* Gets the required write barrier type for a specific write.
* This version is meant to be used in conjunction with with a marking write
* barrier barrier which doesn't consider the slot.
*
* \param value The pointer to the object. May be an interior pointer to an
* interface of the actual object.
* \param params Parameters that may be used for actual write barrier calls.
* Only filled if return value indicates that a write barrier is needed. The
* contents of the `params` are an implementation detail.
* \returns whether a write barrier is needed and which barrier to invoke.
*/
static V8_INLINE WriteBarrierType
GetWriteBarrierType(const void* value, WriteBarrierParams& params) {
return internal::WriteBarrier::GetWriteBarrierType(value, params);
}
/**
* Conservative Dijkstra-style write barrier that processes an object if it
* has not yet been processed.
*
* \param params The parameters retrieved from `GetWriteBarrierType()`.
* \param object The pointer to the object. May be an interior pointer to
* an interface of the actual object.
*/
static V8_INLINE void DijkstraWriteBarrier(const WriteBarrierParams& params,
const void* object) {
internal::WriteBarrier::DijkstraMarkingBarrier(params, object);
}
/**
* Conservative Dijkstra-style write barrier that processes a range of
* elements if they have not yet been processed.
*
* \param params The parameters retrieved from `GetWriteBarrierType()`.
* \param first_element Pointer to the first element that should be processed.
* The slot itself must reside in an object that has been allocated using
* `MakeGarbageCollected()`.
* \param element_size Size of the element in bytes.
* \param number_of_elements Number of elements that should be processed,
* starting with `first_element`.
* \param trace_callback The trace callback that should be invoked for each
* element if necessary.
*/
static V8_INLINE void DijkstraWriteBarrierRange(
const WriteBarrierParams& params, const void* first_element,
size_t element_size, size_t number_of_elements,
TraceCallback trace_callback) {
internal::WriteBarrier::DijkstraMarkingBarrierRange(
params, first_element, element_size, number_of_elements,
trace_callback);
}
/**
* Steele-style write barrier that re-processes an object if it has already
* been processed.
*
* \param params The parameters retrieved from `GetWriteBarrierType()`.
* \param object The pointer to the object which must point to an object that
* has been allocated using `MakeGarbageCollected()`. Interior pointers are
* not supported.
*/
static V8_INLINE void SteeleWriteBarrier(const WriteBarrierParams& params,
const void* object) {
internal::WriteBarrier::SteeleMarkingBarrier(params, object);
}
/**
* Generational barrier for maintaining consistency when running with multiple
* generations.
*
* \param params The parameters retrieved from `GetWriteBarrierType()`.
* \param slot Slot containing the pointer to the object. The slot itself
* must reside in an object that has been allocated using
* `MakeGarbageCollected()`.
*/
static V8_INLINE void GenerationalBarrier(const WriteBarrierParams& params,
const void* slot) {
internal::WriteBarrier::GenerationalBarrier<
internal::WriteBarrier::GenerationalBarrierType::kPreciseSlot>(params,
slot);
}
/**
* Generational barrier for maintaining consistency when running with multiple
* generations. This version is used when slot contains uncompressed pointer.
*
* \param params The parameters retrieved from `GetWriteBarrierType()`.
* \param slot Uncompressed slot containing the direct pointer to the object.
* The slot itself must reside in an object that has been allocated using
* `MakeGarbageCollected()`.
*/
static V8_INLINE void GenerationalBarrierForUncompressedSlot(
const WriteBarrierParams& params, const void* uncompressed_slot) {
internal::WriteBarrier::GenerationalBarrier<
internal::WriteBarrier::GenerationalBarrierType::
kPreciseUncompressedSlot>(params, uncompressed_slot);
}
/**
* Generational barrier for source object that may contain outgoing pointers
* to objects in young generation.
*
* \param params The parameters retrieved from `GetWriteBarrierType()`.
* \param inner_pointer Pointer to the source object.
*/
static V8_INLINE void GenerationalBarrierForSourceObject(
const WriteBarrierParams& params, const void* inner_pointer) {
internal::WriteBarrier::GenerationalBarrier<
internal::WriteBarrier::GenerationalBarrierType::kImpreciseSlot>(
params, inner_pointer);
}
private:
HeapConsistency() = delete;
};
/**
* Disallows garbage collection finalizations. Any garbage collection triggers
* result in a crash when in this scope.
*
* Note that the garbage collector already covers paths that can lead to garbage
* collections, so user code does not require checking
* `IsGarbageCollectionAllowed()` before allocations.
*/
class V8_EXPORT V8_NODISCARD DisallowGarbageCollectionScope final {
CPPGC_STACK_ALLOCATED();
public:
/**
* \returns whether garbage collections are currently allowed.
*/
static bool IsGarbageCollectionAllowed(HeapHandle& heap_handle);
/**
* Enters a disallow garbage collection scope. Must be paired with `Leave()`.
* Prefer a scope instance of `DisallowGarbageCollectionScope`.
*
* \param heap_handle The corresponding heap.
*/
static void Enter(HeapHandle& heap_handle);
/**
* Leaves a disallow garbage collection scope. Must be paired with `Enter()`.
* Prefer a scope instance of `DisallowGarbageCollectionScope`.
*
* \param heap_handle The corresponding heap.
*/
static void Leave(HeapHandle& heap_handle);
/**
* Constructs a scoped object that automatically enters and leaves a disallow
* garbage collection scope based on its lifetime.
*
* \param heap_handle The corresponding heap.
*/
explicit DisallowGarbageCollectionScope(HeapHandle& heap_handle);
~DisallowGarbageCollectionScope();
DisallowGarbageCollectionScope(const DisallowGarbageCollectionScope&) =
delete;
DisallowGarbageCollectionScope& operator=(
const DisallowGarbageCollectionScope&) = delete;
private:
HeapHandle& heap_handle_;
};
/**
* Avoids invoking garbage collection finalizations. Already running garbage
* collection phase are unaffected by this scope.
*
* Should only be used temporarily as the scope has an impact on memory usage
* and follow up garbage collections.
*/
class V8_EXPORT V8_NODISCARD NoGarbageCollectionScope final {
CPPGC_STACK_ALLOCATED();
public:
/**
* Enters a no garbage collection scope. Must be paired with `Leave()`. Prefer
* a scope instance of `NoGarbageCollectionScope`.
*
* \param heap_handle The corresponding heap.
*/
static void Enter(HeapHandle& heap_handle);
/**
* Leaves a no garbage collection scope. Must be paired with `Enter()`. Prefer
* a scope instance of `NoGarbageCollectionScope`.
*
* \param heap_handle The corresponding heap.
*/
static void Leave(HeapHandle& heap_handle);
/**
* Constructs a scoped object that automatically enters and leaves a no
* garbage collection scope based on its lifetime.
*
* \param heap_handle The corresponding heap.
*/
explicit NoGarbageCollectionScope(HeapHandle& heap_handle);
~NoGarbageCollectionScope();
NoGarbageCollectionScope(const NoGarbageCollectionScope&) = delete;
NoGarbageCollectionScope& operator=(const NoGarbageCollectionScope&) = delete;
private:
HeapHandle& heap_handle_;
};
} // namespace subtle
} // namespace cppgc
#endif // INCLUDE_CPPGC_HEAP_CONSISTENCY_H_

View File

@ -0,0 +1,48 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_HEAP_HANDLE_H_
#define INCLUDE_CPPGC_HEAP_HANDLE_H_
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
class HeapBase;
class WriteBarrierTypeForCagedHeapPolicy;
class WriteBarrierTypeForNonCagedHeapPolicy;
} // namespace internal
/**
* Opaque handle used for additional heap APIs.
*/
class HeapHandle {
public:
// Deleted copy ctor to avoid treating the type by value.
HeapHandle(const HeapHandle&) = delete;
HeapHandle& operator=(const HeapHandle&) = delete;
private:
HeapHandle() = default;
V8_INLINE bool is_incremental_marking_in_progress() const {
return is_incremental_marking_in_progress_;
}
V8_INLINE bool is_young_generation_enabled() const {
return is_young_generation_enabled_;
}
bool is_incremental_marking_in_progress_ = false;
bool is_young_generation_enabled_ = false;
friend class internal::HeapBase;
friend class internal::WriteBarrierTypeForCagedHeapPolicy;
friend class internal::WriteBarrierTypeForNonCagedHeapPolicy;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_HEAP_HANDLE_H_

View File

@ -0,0 +1,82 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_HEAP_STATE_H_
#define INCLUDE_CPPGC_HEAP_STATE_H_
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
class HeapHandle;
namespace subtle {
/**
* Helpers to peek into heap-internal state.
*/
class V8_EXPORT HeapState final {
public:
/**
* Returns whether the garbage collector is marking. This API is experimental
* and is expected to be removed in future.
*
* \param heap_handle The corresponding heap.
* \returns true if the garbage collector is currently marking, and false
* otherwise.
*/
static bool IsMarking(const HeapHandle& heap_handle);
/*
* Returns whether the garbage collector is sweeping. This API is experimental
* and is expected to be removed in future.
*
* \param heap_handle The corresponding heap.
* \returns true if the garbage collector is currently sweeping, and false
* otherwise.
*/
static bool IsSweeping(const HeapHandle& heap_handle);
/*
* Returns whether the garbage collector is currently sweeping on the thread
* owning this heap. This API allows the caller to determine whether it has
* been called from a destructor of a managed object. This API is experimental
* and may be removed in future.
*
* \param heap_handle The corresponding heap.
* \returns true if the garbage collector is currently sweeping on this
* thread, and false otherwise.
*/
static bool IsSweepingOnOwningThread(const HeapHandle& heap_handle);
/**
* Returns whether the garbage collector is in the atomic pause, i.e., the
* mutator is stopped from running. This API is experimental and is expected
* to be removed in future.
*
* \param heap_handle The corresponding heap.
* \returns true if the garbage collector is currently in the atomic pause,
* and false otherwise.
*/
static bool IsInAtomicPause(const HeapHandle& heap_handle);
/**
* Returns whether the last garbage collection was finalized conservatively
* (i.e., with a non-empty stack). This API is experimental and is expected to
* be removed in future.
*
* \param heap_handle The corresponding heap.
* \returns true if the last garbage collection was finalized conservatively,
* and false otherwise.
*/
static bool PreviousGCWasConservative(const HeapHandle& heap_handle);
private:
HeapState() = delete;
};
} // namespace subtle
} // namespace cppgc
#endif // INCLUDE_CPPGC_HEAP_STATE_H_

View File

@ -0,0 +1,126 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_HEAP_STATISTICS_H_
#define INCLUDE_CPPGC_HEAP_STATISTICS_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace cppgc {
/**
* `HeapStatistics` contains memory consumption and utilization statistics for a
* cppgc heap.
*/
struct HeapStatistics final {
/**
* Specifies the detail level of the heap statistics. Brief statistics contain
* only the top-level allocated and used memory statistics for the entire
* heap. Detailed statistics also contain a break down per space and page, as
* well as freelist statistics and object type histograms. Note that used
* memory reported by brief statistics and detailed statistics might differ
* slightly.
*/
enum DetailLevel : uint8_t {
kBrief,
kDetailed,
};
/**
* Object statistics for a single type.
*/
struct ObjectStatsEntry {
/**
* Number of allocated bytes.
*/
size_t allocated_bytes;
/**
* Number of allocated objects.
*/
size_t object_count;
};
/**
* Page granularity statistics. For each page the statistics record the
* allocated memory size and overall used memory size for the page.
*/
struct PageStatistics {
/** Overall committed amount of memory for the page. */
size_t committed_size_bytes = 0;
/** Resident amount of memory held by the page. */
size_t resident_size_bytes = 0;
/** Amount of memory actually used on the page. */
size_t used_size_bytes = 0;
/**
* Statistics for object allocated on the page. If an object provides a
* name by inheriting from NameProvider, its name will be recorded in the
* statistics. Other objects, without an explicit name, are merged under a
* single type unless the CPPGC_SUPPORTS_OBJECT_NAME build flag is enabled.
*/
std::vector<ObjectStatsEntry> object_statistics;
};
/**
* Statistics of the freelist (used only in non-large object spaces). For
* each bucket in the freelist the statistics record the bucket size, the
* number of freelist entries in the bucket, and the overall allocated memory
* consumed by these freelist entries.
*/
struct FreeListStatistics {
/** bucket sizes in the freelist. */
std::vector<size_t> bucket_size;
/** number of freelist entries per bucket. */
std::vector<size_t> free_count;
/** memory size consumed by freelist entries per size. */
std::vector<size_t> free_size;
};
/**
* Space granularity statistics. For each space the statistics record the
* space name, the amount of allocated memory and overall used memory for the
* space. The statistics also contain statistics for each of the space's
* pages, its freelist and the objects allocated on the space.
*/
struct SpaceStatistics {
/** The space name */
std::string name;
/** Overall committed amount of memory for the heap. */
size_t committed_size_bytes = 0;
/** Resident amount of memory held by the heap. */
size_t resident_size_bytes = 0;
/** Amount of memory actually used on the space. */
size_t used_size_bytes = 0;
/** Statistics for each of the pages in the space. */
std::vector<PageStatistics> page_stats;
/** Statistics for the freelist of the space. */
FreeListStatistics free_list_stats;
};
/** Overall committed amount of memory for the heap. */
size_t committed_size_bytes = 0;
/** Resident amount of memory held by the heap. */
size_t resident_size_bytes = 0;
/** Amount of memory actually used on the heap. */
size_t used_size_bytes = 0;
/** Memory retained in the page pool, not used directly by the heap. */
size_t pooled_memory_size_bytes = 0;
/** Detail level of this HeapStatistics. */
DetailLevel detail_level;
/** Statistics for each of the spaces in the heap. Filled only when
* `detail_level` is `DetailLevel::kDetailed`. */
std::vector<SpaceStatistics> space_stats;
/**
* Vector of `cppgc::GarbageCollected` type names.
*/
std::vector<std::string> type_names;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_HEAP_STATISTICS_H_

219
vendor/artifacts/v8/include/cppgc/heap.h vendored Normal file
View File

@ -0,0 +1,219 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_HEAP_H_
#define INCLUDE_CPPGC_HEAP_H_
#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>
#include "cppgc/common.h"
#include "cppgc/custom-space.h"
#include "cppgc/platform.h"
#include "v8config.h" // NOLINT(build/include_directory)
/**
* cppgc - A C++ garbage collection library.
*/
namespace cppgc {
class AllocationHandle;
class HeapHandle;
/**
* Implementation details of cppgc. Those details are considered internal and
* may change at any point in time without notice. Users should never rely on
* the contents of this namespace.
*/
namespace internal {
class Heap;
} // namespace internal
/**
* A marker that captures the current stack start address.
*/
class V8_EXPORT StackStartMarker {
public:
StackStartMarker() : stack_start_(__builtin_frame_address(0)) {}
void* stack_start() const { return stack_start_; }
private:
void* stack_start_;
};
class V8_EXPORT Heap {
public:
/**
* Specifies the stack state the embedder is in.
*/
using StackState = EmbedderStackState;
/**
* Specifies whether conservative stack scanning is supported.
*/
enum class StackSupport : uint8_t {
/**
* Conservative stack scan is supported.
*/
kSupportsConservativeStackScan,
/**
* Conservative stack scan is not supported. Embedders may use this option
* when using custom infrastructure that is unsupported by the library.
*/
kNoConservativeStackScan,
};
/**
* Specifies supported marking types.
*/
enum class MarkingType : uint8_t {
/**
* Atomic stop-the-world marking. This option does not require any write
* barriers but is the most intrusive in terms of jank.
*/
kAtomic,
/**
* Incremental marking interleaves marking with the rest of the application
* workload on the same thread.
*/
kIncremental,
/**
* Incremental and concurrent marking.
*/
kIncrementalAndConcurrent
};
/**
* Specifies supported sweeping types.
*/
enum class SweepingType : uint8_t {
/**
* Atomic stop-the-world sweeping. All of sweeping is performed at once.
*/
kAtomic,
/**
* Incremental sweeping interleaves sweeping with the rest of the
* application workload on the same thread.
*/
kIncremental,
/**
* Incremental and concurrent sweeping. Sweeping is split and interleaved
* with the rest of the application.
*/
kIncrementalAndConcurrent
};
/**
* Constraints for a Heap setup.
*/
struct ResourceConstraints {
/**
* Allows the heap to grow to some initial size in bytes before triggering
* garbage collections. This is useful when it is known that applications
* need a certain minimum heap to run to avoid repeatedly invoking the
* garbage collector when growing the heap.
*/
size_t initial_heap_size_bytes = 0;
};
/**
* Options specifying Heap properties (e.g. custom spaces) when initializing a
* heap through `Heap::Create()`.
*/
struct HeapOptions {
/**
* Creates reasonable defaults for instantiating a Heap.
*
* \returns the HeapOptions that can be passed to `Heap::Create()`.
*/
static HeapOptions Default() { return {}; }
/**
* Custom spaces added to heap are required to have indices forming a
* numbered sequence starting at 0, i.e., their `kSpaceIndex` must
* correspond to the index they reside in the vector.
*/
std::vector<std::unique_ptr<CustomSpaceBase>> custom_spaces;
/**
* Specifies whether conservative stack scan is supported. When conservative
* stack scan is not supported, the collector may try to invoke
* garbage collections using non-nestable task, which are guaranteed to have
* no interesting stack, through the provided Platform. If such tasks are
* not supported by the Platform, the embedder must take care of invoking
* the GC through `ForceGarbageCollectionSlow()`.
*/
StackSupport stack_support = StackSupport::kSupportsConservativeStackScan;
/**
* Specifies which types of marking are supported by the heap.
*/
MarkingType marking_support = MarkingType::kIncrementalAndConcurrent;
/**
* Specifies which types of sweeping are supported by the heap.
*/
SweepingType sweeping_support = SweepingType::kIncrementalAndConcurrent;
/**
* Resource constraints specifying various properties that the internal
* GC scheduler follows.
*/
ResourceConstraints resource_constraints;
/**
* Optional marker representing the stack start of the thread creating the
* heap.
*/
std::optional<StackStartMarker> stack_start_marker = std::nullopt;
};
/**
* Creates a new heap that can be used for object allocation.
*
* \param platform implemented and provided by the embedder.
* \param options HeapOptions specifying various properties for the Heap.
* \returns a new Heap instance.
*/
static std::unique_ptr<Heap> Create(
std::shared_ptr<Platform> platform,
HeapOptions options = HeapOptions::Default());
virtual ~Heap() = default;
/**
* Forces garbage collection.
*
* \param source String specifying the source (or caller) triggering a
* forced garbage collection.
* \param reason String specifying the reason for the forced garbage
* collection.
* \param stack_state The embedder stack state, see StackState.
*/
void ForceGarbageCollectionSlow(
const char* source, const char* reason,
StackState stack_state = StackState::kMayContainHeapPointers);
/**
* \returns the opaque handle for allocating objects using
* `MakeGarbageCollected()`.
*/
AllocationHandle& GetAllocationHandle();
/**
* \returns the opaque heap handle which may be used to refer to this heap in
* other APIs. Valid as long as the underlying `Heap` is alive.
*/
HeapHandle& GetHeapHandle();
private:
Heap() = default;
friend class internal::Heap;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_HEAP_H_

View File

@ -0,0 +1,75 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_
#define INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_
#include <cstddef>
#include <cstdint>
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
// Embedders should not rely on this code!
// Internal constants to avoid exposing internal types on the API surface.
namespace api_constants {
constexpr size_t kKB = 1024;
constexpr size_t kMB = kKB * 1024;
constexpr size_t kGB = kMB * 1024;
// Offset of the uint16_t bitfield from the payload contaning the
// in-construction bit. This is subtracted from the payload pointer to get
// to the right bitfield.
static constexpr size_t kFullyConstructedBitFieldOffsetFromPayload =
2 * sizeof(uint16_t);
// Mask for in-construction bit.
static constexpr uint16_t kFullyConstructedBitMask = uint16_t{1} << 15;
static constexpr size_t kPageSizeBits = 17;
static constexpr size_t kPageSize = size_t{1} << kPageSizeBits;
static constexpr size_t kLargeObjectSizeThreshold = kPageSize / 2;
#if defined(CPPGC_POINTER_COMPRESSION)
#if defined(CPPGC_ENABLE_LARGER_CAGE)
constexpr unsigned kPointerCompressionShift = 3;
#else // !defined(CPPGC_ENABLE_LARGER_CAGE)
constexpr unsigned kPointerCompressionShift = 1;
#endif // !defined(CPPGC_ENABLE_LARGER_CAGE)
#endif // !defined(CPPGC_POINTER_COMPRESSION)
#if defined(CPPGC_CAGED_HEAP)
constexpr size_t kCagedHeapDefaultReservationSize =
static_cast<size_t>(4) * kGB;
#if defined(CPPGC_POINTER_COMPRESSION)
constexpr size_t kCagedHeapMaxReservationSize =
size_t{1} << (31 + kPointerCompressionShift);
#else // !defined(CPPGC_POINTER_COMPRESSION)
constexpr size_t kCagedHeapMaxReservationSize =
kCagedHeapDefaultReservationSize;
#endif // !defined(CPPGC_POINTER_COMPRESSION)
constexpr size_t kCagedHeapReservationAlignment = kCagedHeapMaxReservationSize;
#endif // defined(CPPGC_CAGED_HEAP)
static constexpr size_t kDefaultAlignment = sizeof(void*);
// Maximum support alignment for a type as in `alignof(T)`.
static constexpr size_t kMaxSupportedAlignment = 2 * kDefaultAlignment;
// Granularity of heap allocations.
constexpr size_t kAllocationGranularity = sizeof(void*);
// Default cacheline size.
constexpr size_t kCachelineSize = 64;
} // namespace api_constants
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_API_CONSTANTS_H_

View File

@ -0,0 +1,48 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_
#define INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_
#include <atomic>
namespace cppgc {
namespace internal {
// A flag which provides a fast check whether a scope may be entered on the
// current thread, without needing to access thread-local storage or mutex. Can
// have false positives (i.e., spuriously report that it might be entered), so
// it is expected that this will be used in tandem with a precise check that the
// scope is in fact entered on that thread.
//
// Example:
// g_frobnicating_flag.MightBeEntered() &&
// ThreadLocalFrobnicator().IsFrobnicating()
//
// Relaxed atomic operations are sufficient, since:
// - all accesses remain atomic
// - each thread must observe its own operations in order
// - no thread ever exits the flag more times than it enters (if used correctly)
// And so if a thread observes zero, it must be because it has observed an equal
// number of exits as entries.
class AtomicEntryFlag final {
public:
void Enter() { entries_.fetch_add(1, std::memory_order_relaxed); }
void Exit() { entries_.fetch_sub(1, std::memory_order_relaxed); }
// Returns false only if the current thread is not between a call to Enter
// and a call to Exit. Returns true if this thread or another thread may
// currently be in the scope guarded by this flag.
bool MightBeEntered() const {
return entries_.load(std::memory_order_relaxed) != 0;
}
private:
std::atomic_int entries_{0};
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_

View File

@ -0,0 +1,43 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_
#define INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_
#include "cppgc/heap-handle.h"
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/logging.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
// The class is needed in the header to allow for fast access to HeapHandle in
// the write barrier.
class BasePageHandle {
public:
static V8_INLINE BasePageHandle* FromPayload(void* payload) {
return reinterpret_cast<BasePageHandle*>(
reinterpret_cast<uintptr_t>(payload) & ~(api_constants::kPageSize - 1));
}
static V8_INLINE const BasePageHandle* FromPayload(const void* payload) {
return FromPayload(const_cast<void*>(payload));
}
HeapHandle& heap_handle() { return heap_handle_; }
const HeapHandle& heap_handle() const { return heap_handle_; }
protected:
explicit BasePageHandle(HeapHandle& heap_handle) : heap_handle_(heap_handle) {
CPPGC_DCHECK(reinterpret_cast<uintptr_t>(this) % api_constants::kPageSize ==
0);
}
HeapHandle& heap_handle_;
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_BASE_PAGE_HANDLE_H_

View File

@ -0,0 +1,117 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_
#include <array>
#include <cstddef>
#include <cstdint>
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/caged-heap.h"
#include "cppgc/internal/logging.h"
#include "cppgc/platform.h"
#include "v8config.h" // NOLINT(build/include_directory)
#if __cpp_lib_bitopts
#include <bit>
#endif // __cpp_lib_bitopts
#if defined(CPPGC_CAGED_HEAP)
namespace cppgc {
namespace internal {
class HeapBase;
class HeapBaseHandle;
#if defined(CPPGC_YOUNG_GENERATION)
// AgeTable is the bytemap needed for the fast generation check in the write
// barrier. AgeTable contains entries that correspond to 4096 bytes memory
// regions (cards). Each entry in the table represents generation of the objects
// that reside on the corresponding card (young, old or mixed).
class V8_EXPORT AgeTable final {
static constexpr size_t kRequiredSize = 1 * api_constants::kMB;
static constexpr size_t kAllocationGranularity =
api_constants::kAllocationGranularity;
public:
// Represents age of the objects living on a single card.
enum class Age : uint8_t { kOld, kYoung, kMixed };
// When setting age for a range, consider or ignore ages of the adjacent
// cards.
enum class AdjacentCardsPolicy : uint8_t { kConsider, kIgnore };
static constexpr size_t kCardSizeInBytes =
api_constants::kCagedHeapDefaultReservationSize / kRequiredSize;
static constexpr size_t CalculateAgeTableSizeForHeapSize(size_t heap_size) {
return heap_size / kCardSizeInBytes;
}
void SetAge(uintptr_t cage_offset, Age age) {
table_[card(cage_offset)] = age;
}
V8_INLINE Age GetAge(uintptr_t cage_offset) const {
return table_[card(cage_offset)];
}
void SetAgeForRange(uintptr_t cage_offset_begin, uintptr_t cage_offset_end,
Age age, AdjacentCardsPolicy adjacent_cards_policy);
Age GetAgeForRange(uintptr_t cage_offset_begin,
uintptr_t cage_offset_end) const;
void ResetForTesting();
private:
V8_INLINE size_t card(uintptr_t offset) const {
constexpr size_t kGranularityBits =
#if __cpp_lib_bitopts
std::countr_zero(static_cast<uint32_t>(kCardSizeInBytes));
#elif V8_HAS_BUILTIN_CTZ
__builtin_ctz(static_cast<uint32_t>(kCardSizeInBytes));
#else //! V8_HAS_BUILTIN_CTZ
// Hardcode and check with assert.
12;
#endif // !V8_HAS_BUILTIN_CTZ
static_assert((1 << kGranularityBits) == kCardSizeInBytes);
const size_t entry = offset >> kGranularityBits;
CPPGC_DCHECK(CagedHeapBase::GetAgeTableSize() > entry);
return entry;
}
#if defined(V8_CC_GNU)
// gcc disallows flexible arrays in otherwise empty classes.
Age table_[0];
#else // !defined(V8_CC_GNU)
Age table_[];
#endif // !defined(V8_CC_GNU)
};
#endif // CPPGC_YOUNG_GENERATION
struct CagedHeapLocalData final {
V8_INLINE static CagedHeapLocalData& Get() {
return *reinterpret_cast<CagedHeapLocalData*>(CagedHeapBase::GetBase());
}
static constexpr size_t CalculateLocalDataSizeForHeapSize(size_t heap_size) {
return AgeTable::CalculateAgeTableSizeForHeapSize(heap_size);
}
#if defined(CPPGC_YOUNG_GENERATION)
AgeTable age_table;
#endif
};
} // namespace internal
} // namespace cppgc
#endif // defined(CPPGC_CAGED_HEAP)
#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_LOCAL_DATA_H_

View File

@ -0,0 +1,64 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_
#define INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_
#include <climits>
#include <cstddef>
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/base-page-handle.h"
#include "v8config.h" // NOLINT(build/include_directory)
#if defined(CPPGC_CAGED_HEAP)
namespace cppgc {
namespace internal {
class V8_EXPORT CagedHeapBase {
public:
V8_INLINE static uintptr_t OffsetFromAddress(const void* address) {
return reinterpret_cast<uintptr_t>(address) &
(api_constants::kCagedHeapReservationAlignment - 1);
}
V8_INLINE static bool IsWithinCage(const void* address) {
CPPGC_DCHECK(g_heap_base_);
return (reinterpret_cast<uintptr_t>(address) &
~(api_constants::kCagedHeapReservationAlignment - 1)) ==
g_heap_base_;
}
V8_INLINE static bool AreWithinCage(const void* addr1, const void* addr2) {
#if defined(CPPGC_POINTER_COMPRESSION)
static constexpr size_t kHeapBaseShift =
31 + api_constants::kPointerCompressionShift;
#else // !defined(CPPGC_POINTER_COMPRESSION)
static constexpr size_t kHeapBaseShift = sizeof(uint32_t) * CHAR_BIT;
#endif // !defined(CPPGC_POINTER_COMPRESSION)
static_assert((static_cast<size_t>(1) << kHeapBaseShift) ==
api_constants::kCagedHeapMaxReservationSize);
CPPGC_DCHECK(g_heap_base_);
return !(((reinterpret_cast<uintptr_t>(addr1) ^ g_heap_base_) |
(reinterpret_cast<uintptr_t>(addr2) ^ g_heap_base_)) >>
kHeapBaseShift);
}
V8_INLINE static uintptr_t GetBase() { return g_heap_base_; }
V8_INLINE static size_t GetAgeTableSize() { return g_age_table_size_; }
private:
friend class CagedHeap;
static uintptr_t g_heap_base_;
static size_t g_age_table_size_;
};
} // namespace internal
} // namespace cppgc
#endif // defined(CPPGC_CAGED_HEAP)
#endif // INCLUDE_CPPGC_INTERNAL_CAGED_HEAP_H_

View File

@ -0,0 +1,46 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_COMPILER_SPECIFIC_H_
#define INCLUDE_CPPGC_INTERNAL_COMPILER_SPECIFIC_H_
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
#if defined(__has_attribute)
#define CPPGC_HAS_ATTRIBUTE(FEATURE) __has_attribute(FEATURE)
#else
#define CPPGC_HAS_ATTRIBUTE(FEATURE) 0
#endif
#if defined(__has_cpp_attribute)
#define CPPGC_HAS_CPP_ATTRIBUTE(FEATURE) __has_cpp_attribute(FEATURE)
#else
#define CPPGC_HAS_CPP_ATTRIBUTE(FEATURE) 0
#endif
// [[no_unique_address]] comes in C++20 but supported in clang with -std >=
// c++11.
#if defined(V8_CC_MSVC) && CPPGC_HAS_CPP_ATTRIBUTE(msvc::no_unique_address)
// Unfortunately MSVC ignores [[no_unique_address]] (see
// https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#msvc-extensions-and-abi),
// and clang-cl matches it for ABI compatibility reasons. We need to prefer
// [[msvc::no_unique_address]] when available if we actually want any effect.
#define CPPGC_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
#elif CPPGC_HAS_CPP_ATTRIBUTE(no_unique_address)
#define CPPGC_NO_UNIQUE_ADDRESS [[no_unique_address]]
#else
#define CPPGC_NO_UNIQUE_ADDRESS
#endif
#if CPPGC_HAS_ATTRIBUTE(unused)
#define CPPGC_UNUSED __attribute__((unused))
#else
#define CPPGC_UNUSED
#endif
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_COMPILER_SPECIFIC_H_

View File

@ -0,0 +1,41 @@
// Copyright 2025 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_CONDITIONAL_STACK_ALLOCATED_H_
#define INCLUDE_CPPGC_INTERNAL_CONDITIONAL_STACK_ALLOCATED_H_
#include <type_traits>
#include "cppgc/macros.h" // NOLINT(build/include_directory)
#include "cppgc/type-traits.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
// Base class that is marked as stack allocated if T is either marked as stack
// allocated or a traceable type.
template <typename T>
class ConditionalStackAllocatedBase;
template <typename T>
concept RequiresStackAllocated =
!std::is_void_v<T> &&
(cppgc::IsStackAllocatedType<T> || cppgc::internal::IsTraceableV<T> ||
cppgc::IsGarbageCollectedOrMixinTypeV<T>);
template <typename T>
requires(RequiresStackAllocated<T>)
class ConditionalStackAllocatedBase<T> {
public:
CPPGC_STACK_ALLOCATED();
};
template <typename T>
requires(!RequiresStackAllocated<T>)
class ConditionalStackAllocatedBase<T> {};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_CONDITIONAL_STACK_ALLOCATED_H_

View File

@ -0,0 +1,93 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_FINALIZER_TRAIT_H_
#define INCLUDE_CPPGC_INTERNAL_FINALIZER_TRAIT_H_
#include <type_traits>
#include "cppgc/type-traits.h"
namespace cppgc {
namespace internal {
using FinalizationCallback = void (*)(void*);
template <typename T, typename = void>
struct HasFinalizeGarbageCollectedObject : std::false_type {};
template <typename T>
struct HasFinalizeGarbageCollectedObject<
T,
std::void_t<decltype(std::declval<T>().FinalizeGarbageCollectedObject())>>
: std::true_type {};
// The FinalizerTraitImpl specifies how to finalize objects.
template <typename T, bool isFinalized>
struct FinalizerTraitImpl;
template <typename T>
struct FinalizerTraitImpl<T, true> {
private:
// Dispatch to custom FinalizeGarbageCollectedObject().
struct Custom {
static void Call(void* obj) {
static_cast<T*>(obj)->FinalizeGarbageCollectedObject();
}
};
// Dispatch to regular destructor.
struct Destructor {
static void Call(void* obj) { static_cast<T*>(obj)->~T(); }
};
using FinalizeImpl =
std::conditional_t<HasFinalizeGarbageCollectedObject<T>::value, Custom,
Destructor>;
public:
static void Finalize(void* obj) {
static_assert(sizeof(T), "T must be fully defined");
FinalizeImpl::Call(obj);
}
};
template <typename T>
struct FinalizerTraitImpl<T, false> {
static void Finalize(void* obj) {
static_assert(sizeof(T), "T must be fully defined");
}
};
// The FinalizerTrait is used to determine if a type requires finalization and
// what finalization means.
template <typename T>
struct FinalizerTrait {
private:
// Object has a finalizer if it has
// - a custom FinalizeGarbageCollectedObject method, or
// - a destructor.
static constexpr bool kNonTrivialFinalizer =
internal::HasFinalizeGarbageCollectedObject<T>::value ||
!std::is_trivially_destructible_v<std::remove_cv_t<T>>;
static void Finalize(void* obj) {
internal::FinalizerTraitImpl<T, kNonTrivialFinalizer>::Finalize(obj);
}
public:
static constexpr bool HasFinalizer() { return kNonTrivialFinalizer; }
// The callback used to finalize an object of type T.
static constexpr FinalizationCallback kCallback =
kNonTrivialFinalizer ? Finalize : nullptr;
};
template <typename T>
constexpr FinalizationCallback FinalizerTrait<T>::kCallback;
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_FINALIZER_TRAIT_H_

View File

@ -0,0 +1,150 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_GC_INFO_H_
#define INCLUDE_CPPGC_INTERNAL_GC_INFO_H_
#include <atomic>
#include <cstdint>
#include <type_traits>
#include "cppgc/internal/finalizer-trait.h"
#include "cppgc/internal/logging.h"
#include "cppgc/internal/name-trait.h"
#include "cppgc/trace-trait.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
using GCInfoIndex = uint16_t;
struct V8_EXPORT EnsureGCInfoIndexTrait final {
// Acquires a new GC info object and updates `registered_index` with the index
// that identifies that new info accordingly.
template <typename T>
V8_INLINE static GCInfoIndex EnsureIndex(
std::atomic<GCInfoIndex>& registered_index) {
return EnsureGCInfoIndexTraitDispatch<T>{}(registered_index);
}
private:
template <typename T, bool = FinalizerTrait<T>::HasFinalizer(),
bool = NameTrait<T>::HasNonHiddenName()>
struct EnsureGCInfoIndexTraitDispatch;
static GCInfoIndex V8_PRESERVE_MOST
EnsureGCInfoIndex(std::atomic<GCInfoIndex>&, TraceCallback,
FinalizationCallback, NameCallback);
static GCInfoIndex V8_PRESERVE_MOST EnsureGCInfoIndex(
std::atomic<GCInfoIndex>&, TraceCallback, FinalizationCallback);
static GCInfoIndex V8_PRESERVE_MOST
EnsureGCInfoIndex(std::atomic<GCInfoIndex>&, TraceCallback, NameCallback);
static GCInfoIndex V8_PRESERVE_MOST
EnsureGCInfoIndex(std::atomic<GCInfoIndex>&, TraceCallback);
};
#define DISPATCH(has_finalizer, has_non_hidden_name, function) \
template <typename T> \
struct EnsureGCInfoIndexTrait::EnsureGCInfoIndexTraitDispatch< \
T, has_finalizer, has_non_hidden_name> { \
V8_INLINE GCInfoIndex \
operator()(std::atomic<GCInfoIndex>& registered_index) { \
return function; \
} \
};
// ------------------------------------------------------- //
// DISPATCH(has_finalizer, has_non_hidden_name, function) //
// ------------------------------------------------------- //
DISPATCH(true, true, //
EnsureGCInfoIndex(registered_index, //
TraceTrait<T>::Trace, //
FinalizerTrait<T>::kCallback, //
NameTrait<T>::GetName)) //
DISPATCH(true, false, //
EnsureGCInfoIndex(registered_index, //
TraceTrait<T>::Trace, //
FinalizerTrait<T>::kCallback)) //
DISPATCH(false, true, //
EnsureGCInfoIndex(registered_index, //
TraceTrait<T>::Trace, //
NameTrait<T>::GetName)) //
DISPATCH(false, false, //
EnsureGCInfoIndex(registered_index, //
TraceTrait<T>::Trace)) //
#undef DISPATCH
// Trait determines how the garbage collector treats objects wrt. to traversing,
// finalization, and naming.
template <typename T>
struct GCInfoTrait final {
V8_INLINE static GCInfoIndex Index() {
static_assert(sizeof(T), "T must be fully defined");
static std::atomic<GCInfoIndex>
registered_index; // Uses zero initialization.
GCInfoIndex index = registered_index.load(std::memory_order_acquire);
if (V8_UNLIKELY(!index)) {
index = EnsureGCInfoIndexTrait::EnsureIndex<T>(registered_index);
CPPGC_DCHECK(index != 0);
CPPGC_DCHECK(index == registered_index.load(std::memory_order_acquire));
}
return index;
}
static constexpr void CheckCallbacksAreDefined() {
// No USE() macro available.
(void)static_cast<TraceCallback>(TraceTrait<T>::Trace);
(void)static_cast<FinalizationCallback>(FinalizerTrait<T>::kCallback);
(void)static_cast<NameCallback>(NameTrait<T>::GetName);
}
};
// Fold types based on finalizer behavior. Note that finalizer characteristics
// align with trace behavior, i.e., destructors are virtual when trace methods
// are and vice versa.
template <typename T, typename ParentMostGarbageCollectedType>
struct GCInfoFolding final {
static constexpr bool kHasVirtualDestructorAtBase =
std::has_virtual_destructor_v<ParentMostGarbageCollectedType>;
static constexpr bool kBothTypesAreTriviallyDestructible =
std::is_trivially_destructible_v<ParentMostGarbageCollectedType> &&
std::is_trivially_destructible_v<T>;
static constexpr bool kHasCustomFinalizerDispatchAtBase =
internal::HasFinalizeGarbageCollectedObject<
ParentMostGarbageCollectedType>::value;
#ifdef CPPGC_SUPPORTS_OBJECT_NAMES
static constexpr bool kWantsDetailedObjectNames = true;
#else // !CPPGC_SUPPORTS_OBJECT_NAMES
static constexpr bool kWantsDetailedObjectNames = false;
#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
// Always true. Forces the compiler to resolve callbacks which ensures that
// both modes don't break without requiring compiling a separate
// configuration. Only a single GCInfo (for `ResultType` below) will actually
// be instantiated but existence (and well-formedness) of all callbacks is
// checked.
static constexpr bool WantToFold() {
if constexpr ((kHasVirtualDestructorAtBase ||
kBothTypesAreTriviallyDestructible ||
kHasCustomFinalizerDispatchAtBase) &&
!kWantsDetailedObjectNames) {
GCInfoTrait<T>::CheckCallbacksAreDefined();
GCInfoTrait<ParentMostGarbageCollectedType>::CheckCallbacksAreDefined();
return true;
}
return false;
}
// Folding would regress name resolution when deriving names from C++
// class names as it would just folds a name to the base class name.
using ResultType =
std::conditional_t<WantToFold(), ParentMostGarbageCollectedType, T>;
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_GC_INFO_H_

View File

@ -0,0 +1,50 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_LOGGING_H_
#define INCLUDE_CPPGC_INTERNAL_LOGGING_H_
#include "cppgc/source-location.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
void V8_EXPORT DCheckImpl(const char*,
SourceLocation = SourceLocation::Current());
[[noreturn]] void V8_EXPORT
FatalImpl(const char*, SourceLocation = SourceLocation::Current());
// Used to ignore -Wunused-variable.
template <typename>
struct EatParams {};
#ifdef CPPGC_ENABLE_API_CHECKS
#define CPPGC_DCHECK_MSG(condition, message) \
do { \
if (V8_UNLIKELY(!(condition))) { \
::cppgc::internal::DCheckImpl(message); \
} \
} while (false)
#else // !CPPGC_ENABLE_API_CHECKS
#define CPPGC_DCHECK_MSG(condition, message) \
(static_cast<void>(::cppgc::internal::EatParams<decltype( \
static_cast<void>(condition), message)>{}))
#endif // !CPPGC_ENABLE_API_CHECKS
#define CPPGC_DCHECK(condition) CPPGC_DCHECK_MSG(condition, #condition)
#define CPPGC_CHECK_MSG(condition, message) \
do { \
if (V8_UNLIKELY(!(condition))) { \
::cppgc::internal::FatalImpl(message); \
} \
} while (false)
#define CPPGC_CHECK(condition) CPPGC_CHECK_MSG(condition, #condition)
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_LOGGING_H_

View File

@ -0,0 +1,305 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
#define INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
#include <atomic>
#include <cstddef>
#include <type_traits>
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/caged-heap.h"
#include "cppgc/internal/logging.h"
#include "cppgc/sentinel-pointer.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
enum class WriteBarrierSlotType {
kCompressed,
kUncompressed,
};
#if defined(CPPGC_POINTER_COMPRESSION)
#if defined(__clang__)
// Attribute const allows the compiler to assume that CageBaseGlobal::g_base_
// doesn't change (e.g. across calls) and thereby avoid redundant loads.
#define CPPGC_CONST __attribute__((const))
#define CPPGC_REQUIRE_CONSTANT_INIT \
__attribute__((require_constant_initialization))
#else // defined(__clang__)
#define CPPGC_CONST
#define CPPGC_REQUIRE_CONSTANT_INIT
#endif // defined(__clang__)
class V8_EXPORT CageBaseGlobal final {
public:
V8_INLINE CPPGC_CONST static uintptr_t Get() {
CPPGC_DCHECK(IsBaseConsistent());
return g_base_.base;
}
V8_INLINE CPPGC_CONST static bool IsSet() {
CPPGC_DCHECK(IsBaseConsistent());
return (g_base_.base & ~kLowerHalfWordMask) != 0;
}
private:
// We keep the lower halfword as ones to speed up decompression.
static constexpr uintptr_t kLowerHalfWordMask =
(api_constants::kCagedHeapReservationAlignment - 1);
static union alignas(api_constants::kCachelineSize) Base {
uintptr_t base;
char cache_line[api_constants::kCachelineSize];
} g_base_ CPPGC_REQUIRE_CONSTANT_INIT;
CageBaseGlobal() = delete;
V8_INLINE static bool IsBaseConsistent() {
return kLowerHalfWordMask == (g_base_.base & kLowerHalfWordMask);
}
friend class CageBaseGlobalUpdater;
};
#undef CPPGC_REQUIRE_CONSTANT_INIT
#undef CPPGC_CONST
class V8_TRIVIAL_ABI CompressedPointer final {
public:
struct AtomicInitializerTag {};
using IntegralType = uint32_t;
static constexpr auto kWriteBarrierSlotType =
WriteBarrierSlotType::kCompressed;
V8_INLINE CompressedPointer() : value_(0u) {}
V8_INLINE explicit CompressedPointer(const void* value,
AtomicInitializerTag) {
StoreAtomic(value);
}
V8_INLINE explicit CompressedPointer(const void* ptr)
: value_(Compress(ptr)) {}
V8_INLINE explicit CompressedPointer(std::nullptr_t) : value_(0u) {}
V8_INLINE explicit CompressedPointer(SentinelPointer)
: value_(kCompressedSentinel) {}
V8_INLINE const void* Load() const { return Decompress(value_); }
V8_INLINE const void* LoadAtomic() const {
return Decompress(
reinterpret_cast<const std::atomic<IntegralType>&>(value_).load(
std::memory_order_relaxed));
}
V8_INLINE void Store(const void* ptr) { value_ = Compress(ptr); }
V8_INLINE void StoreAtomic(const void* value) {
reinterpret_cast<std::atomic<IntegralType>&>(value_).store(
Compress(value), std::memory_order_relaxed);
}
V8_INLINE void Clear() { value_ = 0u; }
V8_INLINE bool IsCleared() const { return !value_; }
V8_INLINE bool IsSentinel() const { return value_ == kCompressedSentinel; }
V8_INLINE uint32_t GetAsInteger() const { return value_; }
V8_INLINE friend bool operator==(CompressedPointer a, CompressedPointer b) {
return a.value_ == b.value_;
}
V8_INLINE friend bool operator!=(CompressedPointer a, CompressedPointer b) {
return a.value_ != b.value_;
}
V8_INLINE friend bool operator<(CompressedPointer a, CompressedPointer b) {
return a.value_ < b.value_;
}
V8_INLINE friend bool operator<=(CompressedPointer a, CompressedPointer b) {
return a.value_ <= b.value_;
}
V8_INLINE friend bool operator>(CompressedPointer a, CompressedPointer b) {
return a.value_ > b.value_;
}
V8_INLINE friend bool operator>=(CompressedPointer a, CompressedPointer b) {
return a.value_ >= b.value_;
}
static V8_INLINE IntegralType Compress(const void* ptr) {
static_assert(SentinelPointer::kSentinelValue ==
1 << api_constants::kPointerCompressionShift,
"The compression scheme relies on the sentinel encoded as 1 "
"<< kPointerCompressionShift");
static constexpr size_t kGigaCageMask =
~(api_constants::kCagedHeapReservationAlignment - 1);
static constexpr size_t kPointerCompressionShiftMask =
(1 << api_constants::kPointerCompressionShift) - 1;
CPPGC_DCHECK(CageBaseGlobal::IsSet());
const uintptr_t base = CageBaseGlobal::Get();
CPPGC_DCHECK(!ptr || ptr == kSentinelPointer ||
(base & kGigaCageMask) ==
(reinterpret_cast<uintptr_t>(ptr) & kGigaCageMask));
CPPGC_DCHECK(
(reinterpret_cast<uintptr_t>(ptr) & kPointerCompressionShiftMask) == 0);
const auto uptr = reinterpret_cast<uintptr_t>(ptr);
// Shift the pointer and truncate.
auto compressed = static_cast<IntegralType>(
uptr >> api_constants::kPointerCompressionShift);
// Normal compressed pointers must have the MSB set. This is guaranteed by
// the cage alignment.
CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
(compressed & (1 << 31)));
return compressed;
}
static V8_INLINE void* Decompress(IntegralType ptr) {
CPPGC_DCHECK(CageBaseGlobal::IsSet());
const uintptr_t base = CageBaseGlobal::Get();
return Decompress(ptr, base);
}
static V8_INLINE void* Decompress(IntegralType ptr, uintptr_t base) {
CPPGC_DCHECK(CageBaseGlobal::IsSet());
CPPGC_DCHECK(base == CageBaseGlobal::Get());
// Sign-extend compressed pointer to full width. This ensure that normal
// pointers have only 1s in the base part of the address. It's also
// important to shift the unsigned value, as otherwise it would result in
// undefined behavior.
const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr))
<< api_constants::kPointerCompressionShift;
// Set the base part of the address for normal compressed pointers. Note
// that nullptr and the sentinel value do not have 1s in the base part and
// remain as-is in this operation.
return reinterpret_cast<void*>(mask & base);
}
// For a given memory `address`, this method iterates all possible pointers
// that can be reasonably recovered with the current compression scheme and
// passes them to `callback`.
template <typename Callback>
static V8_INLINE void VisitPossiblePointers(const void* address,
Callback callback);
private:
static constexpr IntegralType kCompressedSentinel =
SentinelPointer::kSentinelValue >>
api_constants::kPointerCompressionShift;
// All constructors initialize `value_`. Do not add a default value here as it
// results in a non-atomic write on some builds, even when the atomic version
// of the constructor is used.
IntegralType value_;
};
template <typename Callback>
// static
void CompressedPointer::VisitPossiblePointers(const void* address,
Callback callback) {
const uintptr_t base = CageBaseGlobal::Get();
CPPGC_DCHECK(base);
// We may have random compressed pointers on stack (e.g. due to inlined
// collections). These could be present in both halfwords.
const uint32_t compressed_low =
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(address));
callback(CompressedPointer::Decompress(compressed_low, base));
const uint32_t compressed_high = static_cast<uint32_t>(
reinterpret_cast<uintptr_t>(address) >> (sizeof(uint32_t) * CHAR_BIT));
callback(CompressedPointer::Decompress(compressed_high, base));
// Iterate possible intermediate values, see `Decompress()`. The intermediate
// value of decompressing is a 64-bit value where 35 bits are the offset. We
// don't assume sign extension is stored and recover that part.
//
// Note that this case conveniently also recovers the full pointer.
static constexpr uintptr_t kBitForIntermediateValue =
(sizeof(uint32_t) * CHAR_BIT) + api_constants::kPointerCompressionShift;
static constexpr uintptr_t kSignExtensionMask =
~((uintptr_t{1} << kBitForIntermediateValue) - 1);
const uintptr_t intermediate_sign_extended =
reinterpret_cast<uintptr_t>(address) | kSignExtensionMask;
callback(reinterpret_cast<void*>(intermediate_sign_extended & base));
}
#endif // defined(CPPGC_POINTER_COMPRESSION)
class V8_TRIVIAL_ABI RawPointer final {
public:
struct AtomicInitializerTag {};
using IntegralType = uintptr_t;
static constexpr auto kWriteBarrierSlotType =
WriteBarrierSlotType::kUncompressed;
V8_INLINE RawPointer() : ptr_(nullptr) {}
V8_INLINE explicit RawPointer(const void* ptr, AtomicInitializerTag) {
StoreAtomic(ptr);
}
V8_INLINE explicit RawPointer(const void* ptr) : ptr_(ptr) {}
V8_INLINE const void* Load() const { return ptr_; }
V8_INLINE const void* LoadAtomic() const {
return reinterpret_cast<const std::atomic<const void*>&>(ptr_).load(
std::memory_order_relaxed);
}
V8_INLINE void Store(const void* ptr) { ptr_ = ptr; }
V8_INLINE void StoreAtomic(const void* ptr) {
reinterpret_cast<std::atomic<const void*>&>(ptr_).store(
ptr, std::memory_order_relaxed);
}
V8_INLINE void Clear() { ptr_ = nullptr; }
V8_INLINE bool IsCleared() const { return !ptr_; }
V8_INLINE bool IsSentinel() const { return ptr_ == kSentinelPointer; }
V8_INLINE uintptr_t GetAsInteger() const {
return reinterpret_cast<uintptr_t>(ptr_);
}
V8_INLINE friend bool operator==(RawPointer a, RawPointer b) {
return a.ptr_ == b.ptr_;
}
V8_INLINE friend bool operator!=(RawPointer a, RawPointer b) {
return a.ptr_ != b.ptr_;
}
V8_INLINE friend bool operator<(RawPointer a, RawPointer b) {
return a.ptr_ < b.ptr_;
}
V8_INLINE friend bool operator<=(RawPointer a, RawPointer b) {
return a.ptr_ <= b.ptr_;
}
V8_INLINE friend bool operator>(RawPointer a, RawPointer b) {
return a.ptr_ > b.ptr_;
}
V8_INLINE friend bool operator>=(RawPointer a, RawPointer b) {
return a.ptr_ >= b.ptr_;
}
template <typename Callback>
static V8_INLINE void VisitPossiblePointers(const void* address,
Callback callback) {
// Pass along the full pointer.
return callback(const_cast<void*>(address));
}
private:
// All constructors initialize `ptr_`. Do not add a default value here as it
// results in a non-atomic write on some builds, even when the atomic version
// of the constructor is used.
const void* ptr_;
};
#if defined(CPPGC_POINTER_COMPRESSION)
using DefaultMemberStorage = CompressedPointer;
#else // !defined(CPPGC_POINTER_COMPRESSION)
using DefaultMemberStorage = RawPointer;
#endif // !defined(CPPGC_POINTER_COMPRESSION)
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_

View File

@ -0,0 +1,142 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
#define INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include "cppgc/name-provider.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
#if CPPGC_SUPPORTS_OBJECT_NAMES && defined(__clang__)
#define CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME 1
// Provides constexpr c-string storage for a name of fixed |Size| characters.
// Automatically appends terminating 0 byte.
template <size_t Size>
struct NameBuffer {
char name[Size + 1]{};
static constexpr NameBuffer FromCString(const char* str) {
NameBuffer result;
for (size_t i = 0; i < Size; ++i) result.name[i] = str[i];
result.name[Size] = 0;
return result;
}
};
template <typename T>
const char* GetTypename() {
static constexpr char kSelfPrefix[] =
"const char *cppgc::internal::GetTypename() [T =";
static_assert(__builtin_strncmp(__PRETTY_FUNCTION__, kSelfPrefix,
sizeof(kSelfPrefix) - 1) == 0,
"The prefix must match");
static constexpr const char* kTypenameStart =
__PRETTY_FUNCTION__ + sizeof(kSelfPrefix);
static constexpr size_t kTypenameSize =
__builtin_strlen(__PRETTY_FUNCTION__) - sizeof(kSelfPrefix) - 1;
// NameBuffer is an indirection that is needed to make sure that only a
// substring of __PRETTY_FUNCTION__ gets materialized in the binary.
static constexpr auto buffer =
NameBuffer<kTypenameSize>::FromCString(kTypenameStart);
return buffer.name;
}
#else
#define CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME 0
#endif
struct HeapObjectName {
const char* value;
bool name_was_hidden;
};
enum class HeapObjectNameForUnnamedObject : uint8_t {
kUseClassNameIfSupported,
kUseHiddenName,
};
class V8_EXPORT NameTraitBase {
protected:
static HeapObjectName GetNameFromTypeSignature(const char*);
};
// Trait that specifies how the garbage collector retrieves the name for a
// given object.
template <typename T>
class NameTrait final : public NameTraitBase {
public:
static constexpr bool HasNonHiddenName() {
#if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
return true;
#elif CPPGC_SUPPORTS_OBJECT_NAMES
return true;
#else // !CPPGC_SUPPORTS_OBJECT_NAMES
return std::is_base_of_v<NameProvider, T>;
#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
}
static HeapObjectName GetName(
const void* obj, HeapObjectNameForUnnamedObject name_retrieval_mode) {
return GetNameFor(static_cast<const T*>(obj), name_retrieval_mode);
}
private:
static HeapObjectName GetNameFor(const NameProvider* name_provider,
HeapObjectNameForUnnamedObject) {
// Objects inheriting from `NameProvider` are not considered unnamed as
// users already provided a name for them.
return {name_provider->GetHumanReadableName(), false};
}
static HeapObjectName GetNameFor(
const void*, HeapObjectNameForUnnamedObject name_retrieval_mode) {
if (name_retrieval_mode == HeapObjectNameForUnnamedObject::kUseHiddenName) {
return {NameProvider::kHiddenName, true};
}
#if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
return {GetTypename<T>(), false};
#elif CPPGC_SUPPORTS_OBJECT_NAMES
#if defined(V8_CC_GNU)
#define PRETTY_FUNCTION_VALUE __PRETTY_FUNCTION__
#elif defined(V8_CC_MSVC)
#define PRETTY_FUNCTION_VALUE __FUNCSIG__
#else
#define PRETTY_FUNCTION_VALUE nullptr
#endif
static const HeapObjectName leaky_name =
GetNameFromTypeSignature(PRETTY_FUNCTION_VALUE);
return leaky_name;
#undef PRETTY_FUNCTION_VALUE
#else // !CPPGC_SUPPORTS_OBJECT_NAMES
// We wanted to use a class name but were unable to provide one due to
// compiler limitations or build configuration. As such, return the hidden
// name with name_was_hidden=false, which will cause this object to be
// visible in the snapshot.
return {NameProvider::kHiddenName, false};
#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
}
};
using NameCallback = HeapObjectName (*)(const void*,
HeapObjectNameForUnnamedObject);
} // namespace internal
} // namespace cppgc
#undef CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
#endif // INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_

View File

@ -0,0 +1,219 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_
#define INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_
#include <array>
#include <memory>
#include <vector>
#include "cppgc/internal/logging.h"
#include "cppgc/trace-trait.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
class CrossThreadPersistentRegion;
class FatalOutOfMemoryHandler;
class HeapBase;
class RootVisitor;
// PersistentNode represents a variant of two states:
// 1) traceable node with a back pointer to the Persistent object;
// 2) freelist entry.
class PersistentNode final {
public:
PersistentNode() = default;
PersistentNode(const PersistentNode&) = delete;
PersistentNode& operator=(const PersistentNode&) = delete;
void InitializeAsUsedNode(void* owner, TraceRootCallback trace) {
CPPGC_DCHECK(trace);
owner_ = owner;
trace_ = trace;
}
void InitializeAsFreeNode(PersistentNode* next) {
next_ = next;
trace_ = nullptr;
}
void UpdateOwner(void* owner) {
CPPGC_DCHECK(IsUsed());
owner_ = owner;
}
PersistentNode* FreeListNext() const {
CPPGC_DCHECK(!IsUsed());
return next_;
}
void Trace(RootVisitor& root_visitor) const {
CPPGC_DCHECK(IsUsed());
trace_(root_visitor, owner_);
}
bool IsUsed() const { return trace_; }
void* owner() const {
CPPGC_DCHECK(IsUsed());
return owner_;
}
private:
// PersistentNode acts as a designated union:
// If trace_ != nullptr, owner_ points to the corresponding Persistent handle.
// Otherwise, next_ points to the next freed PersistentNode.
union {
void* owner_ = nullptr;
PersistentNode* next_;
};
TraceRootCallback trace_ = nullptr;
};
class V8_EXPORT PersistentRegionBase {
using PersistentNodeSlots = std::array<PersistentNode, 256u>;
public:
// Clears Persistent fields to avoid stale pointers after heap teardown.
~PersistentRegionBase();
PersistentRegionBase(const PersistentRegionBase&) = delete;
PersistentRegionBase& operator=(const PersistentRegionBase&) = delete;
void Iterate(RootVisitor&);
size_t NodesInUse() const;
void ClearAllUsedNodes();
protected:
explicit PersistentRegionBase(const FatalOutOfMemoryHandler& oom_handler);
PersistentNode* TryAllocateNodeFromFreeList(void* owner,
TraceRootCallback trace) {
PersistentNode* node = nullptr;
if (V8_LIKELY(free_list_head_)) {
node = free_list_head_;
free_list_head_ = free_list_head_->FreeListNext();
CPPGC_DCHECK(!node->IsUsed());
node->InitializeAsUsedNode(owner, trace);
nodes_in_use_++;
}
return node;
}
void FreeNode(PersistentNode* node) {
CPPGC_DCHECK(node);
CPPGC_DCHECK(node->IsUsed());
node->InitializeAsFreeNode(free_list_head_);
free_list_head_ = node;
CPPGC_DCHECK(nodes_in_use_ > 0);
nodes_in_use_--;
}
PersistentNode* RefillFreeListAndAllocateNode(void* owner,
TraceRootCallback trace);
private:
template <typename PersistentBaseClass>
void ClearAllUsedNodes();
void RefillFreeList();
std::vector<std::unique_ptr<PersistentNodeSlots>> nodes_;
PersistentNode* free_list_head_ = nullptr;
size_t nodes_in_use_ = 0;
const FatalOutOfMemoryHandler& oom_handler_;
friend class CrossThreadPersistentRegion;
};
// Variant of PersistentRegionBase that checks whether the allocation and
// freeing happens only on the thread that created the heap.
class V8_EXPORT PersistentRegion final : public PersistentRegionBase {
public:
V8_INLINE PersistentRegion(const HeapBase& heap,
const FatalOutOfMemoryHandler& oom_handler)
: PersistentRegionBase(oom_handler), heap_(heap) {
CPPGC_DCHECK(IsCreationThread());
}
// Clears Persistent fields to avoid stale pointers after heap teardown.
~PersistentRegion() = default;
PersistentRegion(const PersistentRegion&) = delete;
PersistentRegion& operator=(const PersistentRegion&) = delete;
V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) {
CPPGC_DCHECK(IsCreationThread());
auto* node = TryAllocateNodeFromFreeList(owner, trace);
if (V8_LIKELY(node)) return node;
// Slow path allocation allows for checking thread correspondence.
CPPGC_CHECK(IsCreationThread());
return RefillFreeListAndAllocateNode(owner, trace);
}
V8_INLINE void FreeNode(PersistentNode* node) {
CPPGC_DCHECK(IsCreationThread());
PersistentRegionBase::FreeNode(node);
}
private:
bool IsCreationThread();
const HeapBase& heap_;
};
// CrossThreadPersistent uses PersistentRegionBase but protects it using this
// lock when needed.
class V8_EXPORT PersistentRegionLock final {
public:
PersistentRegionLock();
~PersistentRegionLock();
static void AssertLocked();
};
// Variant of PersistentRegionBase that checks whether the PersistentRegionLock
// is locked.
class V8_EXPORT CrossThreadPersistentRegion final
: protected PersistentRegionBase {
public:
explicit CrossThreadPersistentRegion(const FatalOutOfMemoryHandler&);
// Clears Persistent fields to avoid stale pointers after heap teardown.
~CrossThreadPersistentRegion();
CrossThreadPersistentRegion(const CrossThreadPersistentRegion&) = delete;
CrossThreadPersistentRegion& operator=(const CrossThreadPersistentRegion&) =
delete;
V8_INLINE PersistentNode* AllocateNode(void* owner, TraceRootCallback trace) {
PersistentRegionLock::AssertLocked();
auto* node = TryAllocateNodeFromFreeList(owner, trace);
if (V8_LIKELY(node)) return node;
return RefillFreeListAndAllocateNode(owner, trace);
}
V8_INLINE void FreeNode(PersistentNode* node) {
PersistentRegionLock::AssertLocked();
PersistentRegionBase::FreeNode(node);
}
void Iterate(RootVisitor&);
size_t NodesInUse() const;
void ClearAllUsedNodes();
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_PERSISTENT_NODE_H_

View File

@ -0,0 +1,291 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_
#define INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_
#include <cstdint>
#include <type_traits>
#include "cppgc/internal/member-storage.h"
#include "cppgc/internal/write-barrier.h"
#include "cppgc/sentinel-pointer.h"
#include "cppgc/source-location.h"
#include "cppgc/type-traits.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
class HeapBase;
class PersistentRegion;
class CrossThreadPersistentRegion;
// Tags to distinguish between strong and weak member types.
class StrongMemberTag;
class WeakMemberTag;
class UntracedMemberTag;
struct DijkstraWriteBarrierPolicy {
// Since in initializing writes the source object is always white, having no
// barrier doesn't break the tri-color invariant.
V8_INLINE static void InitializingBarrier(const void*, const void*) {}
V8_INLINE static void InitializingBarrier(const void*, RawPointer storage) {
}
#if defined(CPPGC_POINTER_COMPRESSION)
V8_INLINE static void InitializingBarrier(const void*,
CompressedPointer storage) {}
#endif
template <WriteBarrierSlotType SlotType>
V8_INLINE static void AssigningBarrier(const void* slot,
const void* value) {
#ifdef CPPGC_SLIM_WRITE_BARRIER
if (V8_UNLIKELY(WriteBarrier::IsEnabled())) {
WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
}
#else // !CPPGC_SLIM_WRITE_BARRIER
WriteBarrier::Params params;
const WriteBarrier::Type type =
WriteBarrier::GetWriteBarrierType(slot, value, params);
WriteBarrier(type, params, slot, value);
#endif // !CPPGC_SLIM_WRITE_BARRIER
}
template <WriteBarrierSlotType SlotType>
V8_INLINE static void AssigningBarrier(const void* slot, RawPointer storage) {
static_assert(
SlotType == WriteBarrierSlotType::kUncompressed,
"Assigning storages of Member and UncompressedMember is not supported");
#ifdef CPPGC_SLIM_WRITE_BARRIER
if (V8_UNLIKELY(WriteBarrier::IsEnabled())) {
WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
}
#else // !CPPGC_SLIM_WRITE_BARRIER
WriteBarrier::Params params;
const WriteBarrier::Type type =
WriteBarrier::GetWriteBarrierType(slot, storage, params);
WriteBarrier(type, params, slot, storage.Load());
#endif // !CPPGC_SLIM_WRITE_BARRIER
}
#if defined(CPPGC_POINTER_COMPRESSION)
template <WriteBarrierSlotType SlotType>
V8_INLINE static void AssigningBarrier(const void* slot,
CompressedPointer storage) {
static_assert(
SlotType == WriteBarrierSlotType::kCompressed,
"Assigning storages of Member and UncompressedMember is not supported");
#ifdef CPPGC_SLIM_WRITE_BARRIER
if (V8_UNLIKELY(WriteBarrier::IsEnabled())) {
WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
}
#else // !CPPGC_SLIM_WRITE_BARRIER
WriteBarrier::Params params;
const WriteBarrier::Type type =
WriteBarrier::GetWriteBarrierType(slot, storage, params);
WriteBarrier(type, params, slot, storage.Load());
#endif // !CPPGC_SLIM_WRITE_BARRIER
}
#endif // defined(CPPGC_POINTER_COMPRESSION)
private:
V8_INLINE static void WriteBarrier(WriteBarrier::Type type,
const WriteBarrier::Params& params,
const void* slot, const void* value) {
switch (type) {
case WriteBarrier::Type::kGenerational:
WriteBarrier::GenerationalBarrier<
WriteBarrier::GenerationalBarrierType::kPreciseSlot>(params, slot);
break;
case WriteBarrier::Type::kMarking:
WriteBarrier::DijkstraMarkingBarrier(params, value);
break;
case WriteBarrier::Type::kNone:
break;
}
}
};
struct NoWriteBarrierPolicy {
V8_INLINE static void InitializingBarrier(const void*, const void*) {}
V8_INLINE static void InitializingBarrier(const void*, RawPointer storage) {}
#if defined(CPPGC_POINTER_COMPRESSION)
V8_INLINE static void InitializingBarrier(const void*,
CompressedPointer storage) {}
#endif
template <WriteBarrierSlotType>
V8_INLINE static void AssigningBarrier(const void*, const void*) {}
template <WriteBarrierSlotType, typename MemberStorage>
V8_INLINE static void AssigningBarrier(const void*, MemberStorage) {}
};
class V8_EXPORT SameThreadEnabledCheckingPolicyBase {
protected:
void CheckPointerImpl(const void* ptr, bool points_to_payload,
bool check_off_heap_assignments);
const HeapBase* heap_ = nullptr;
};
template <bool kCheckOffHeapAssignments>
class V8_EXPORT SameThreadEnabledCheckingPolicy
: private SameThreadEnabledCheckingPolicyBase {
protected:
template <typename T>
V8_INLINE void CheckPointer(RawPointer raw_pointer) {
if (raw_pointer.IsCleared() || raw_pointer.IsSentinel()) {
return;
}
CheckPointersImplTrampoline<T>::Call(
this, static_cast<const T*>(raw_pointer.Load()));
}
#if defined(CPPGC_POINTER_COMPRESSION)
template <typename T>
V8_INLINE void CheckPointer(CompressedPointer compressed_pointer) {
if (compressed_pointer.IsCleared() || compressed_pointer.IsSentinel()) {
return;
}
CheckPointersImplTrampoline<T>::Call(
this, static_cast<const T*>(compressed_pointer.Load()));
}
#endif
template <typename T>
void CheckPointer(const T* ptr) {
if (!ptr || (kSentinelPointer == ptr)) {
return;
}
CheckPointersImplTrampoline<T>::Call(this, ptr);
}
private:
template <typename T, bool = IsCompleteV<T>>
struct CheckPointersImplTrampoline {
static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) {
policy->CheckPointerImpl(ptr, false, kCheckOffHeapAssignments);
}
};
template <typename T>
struct CheckPointersImplTrampoline<T, true> {
static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) {
policy->CheckPointerImpl(ptr, IsGarbageCollectedTypeV<T>,
kCheckOffHeapAssignments);
}
};
};
class DisabledCheckingPolicy {
protected:
template <typename T>
V8_INLINE void CheckPointer(T*) {}
template <typename T>
V8_INLINE void CheckPointer(RawPointer) {}
#if defined(CPPGC_POINTER_COMPRESSION)
template <typename T>
V8_INLINE void CheckPointer(CompressedPointer) {}
#endif
};
#ifdef CPPGC_ENABLE_SLOW_API_CHECKS
// Off heap members are not connected to object graph and thus cannot ressurect
// dead objects.
using DefaultMemberCheckingPolicy =
SameThreadEnabledCheckingPolicy<false /* kCheckOffHeapAssignments*/>;
using DefaultPersistentCheckingPolicy =
SameThreadEnabledCheckingPolicy<true /* kCheckOffHeapAssignments*/>;
#else // !CPPGC_ENABLE_SLOW_API_CHECKS
using DefaultMemberCheckingPolicy = DisabledCheckingPolicy;
using DefaultPersistentCheckingPolicy = DisabledCheckingPolicy;
#endif // !CPPGC_ENABLE_SLOW_API_CHECKS
// For CT(W)P neither marking information (for value), nor objectstart bitmap
// (for slot) are guaranteed to be present because there's no synchronization
// between heaps after marking.
using DefaultCrossThreadPersistentCheckingPolicy = DisabledCheckingPolicy;
class KeepLocationPolicy {
public:
constexpr SourceLocation Location() const { return location_; }
protected:
constexpr KeepLocationPolicy() = default;
constexpr explicit KeepLocationPolicy(SourceLocation location)
: location_(location) {}
// When used in a CrossThreadPersistent the object could already be poisoned
// (e.g. when stored on a remote heap). In that case we would get an ASAN
// error when the local heap invokes this method before the remote heap runs
// the destructor.
V8_CLANG_NO_SANITIZE("address")
constexpr SourceLocation LocationFromGC() const { return location_; }
// KeepLocationPolicy must not copy underlying source locations.
KeepLocationPolicy(const KeepLocationPolicy&) = delete;
KeepLocationPolicy& operator=(const KeepLocationPolicy&) = delete;
// Location of the original moved from object should be preserved.
KeepLocationPolicy(KeepLocationPolicy&&) = default;
KeepLocationPolicy& operator=(KeepLocationPolicy&&) = default;
private:
SourceLocation location_;
};
class IgnoreLocationPolicy {
public:
constexpr SourceLocation Location() const { return {}; }
protected:
constexpr IgnoreLocationPolicy() = default;
constexpr explicit IgnoreLocationPolicy(SourceLocation) {}
constexpr SourceLocation LocationFromGC() const { return {}; }
};
#if CPPGC_SUPPORTS_OBJECT_NAMES
using DefaultLocationPolicy = KeepLocationPolicy;
#else
using DefaultLocationPolicy = IgnoreLocationPolicy;
#endif
struct StrongPersistentPolicy {
using IsStrongPersistent = std::true_type;
static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
};
struct WeakPersistentPolicy {
using IsStrongPersistent = std::false_type;
static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
};
struct StrongCrossThreadPersistentPolicy {
using IsStrongPersistent = std::true_type;
static V8_EXPORT CrossThreadPersistentRegion& GetPersistentRegion(
const void* object);
};
struct WeakCrossThreadPersistentPolicy {
using IsStrongPersistent = std::false_type;
static V8_EXPORT CrossThreadPersistentRegion& GetPersistentRegion(
const void* object);
};
// Forward declarations setting up the default policies.
template <typename T, typename WeaknessPolicy,
typename LocationPolicy = DefaultLocationPolicy,
typename CheckingPolicy = DefaultCrossThreadPersistentCheckingPolicy>
class BasicCrossThreadPersistent;
template <typename T, typename WeaknessPolicy,
typename LocationPolicy = DefaultLocationPolicy,
typename CheckingPolicy = DefaultPersistentCheckingPolicy>
class BasicPersistent;
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy = DefaultMemberCheckingPolicy,
typename StorageType = DefaultMemberStorage>
class BasicMember;
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_

View File

@ -0,0 +1,494 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_
#define INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_
#include <cstddef>
#include <cstdint>
#include "cppgc/heap-handle.h"
#include "cppgc/heap-state.h"
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/atomic-entry-flag.h"
#include "cppgc/internal/base-page-handle.h"
#include "cppgc/internal/member-storage.h"
#include "cppgc/platform.h"
#include "cppgc/sentinel-pointer.h"
#include "cppgc/trace-trait.h"
#include "v8config.h" // NOLINT(build/include_directory)
#if defined(CPPGC_CAGED_HEAP)
#include "cppgc/internal/caged-heap-local-data.h"
#include "cppgc/internal/caged-heap.h"
#endif
namespace cppgc {
class HeapHandle;
namespace internal {
#if defined(CPPGC_CAGED_HEAP)
class WriteBarrierTypeForCagedHeapPolicy;
#else // !CPPGC_CAGED_HEAP
class WriteBarrierTypeForNonCagedHeapPolicy;
#endif // !CPPGC_CAGED_HEAP
class V8_EXPORT WriteBarrier final {
public:
enum class Type : uint8_t {
kNone,
kMarking,
kGenerational,
};
enum class GenerationalBarrierType : uint8_t {
kPreciseSlot,
kPreciseUncompressedSlot,
kImpreciseSlot,
};
struct Params {
HeapHandle* heap = nullptr;
#if V8_ENABLE_CHECKS
Type type = Type::kNone;
#endif // !V8_ENABLE_CHECKS
#if defined(CPPGC_CAGED_HEAP)
uintptr_t slot_offset = 0;
uintptr_t value_offset = 0;
#endif // CPPGC_CAGED_HEAP
};
enum class ValueMode {
kValuePresent,
kNoValuePresent,
};
// Returns the required write barrier for a given `slot` and `value`.
static V8_INLINE Type GetWriteBarrierType(const void* slot, const void* value,
Params& params);
// Returns the required write barrier for a given `slot` and `value`.
template <typename MemberStorage>
static V8_INLINE Type GetWriteBarrierType(const void* slot, MemberStorage,
Params& params);
// Returns the required write barrier for a given `slot`.
template <typename HeapHandleCallback>
static V8_INLINE Type GetWriteBarrierType(const void* slot, Params& params,
HeapHandleCallback callback);
// Returns the required write barrier for a given `value`.
static V8_INLINE Type GetWriteBarrierType(const void* value, Params& params);
#ifdef CPPGC_SLIM_WRITE_BARRIER
// A write barrier that combines `GenerationalBarrier()` and
// `DijkstraMarkingBarrier()`. We only pass a single parameter here to clobber
// as few registers as possible.
template <WriteBarrierSlotType>
static V8_NOINLINE void V8_PRESERVE_MOST
CombinedWriteBarrierSlow(const void* slot);
#endif // CPPGC_SLIM_WRITE_BARRIER
static V8_INLINE void DijkstraMarkingBarrier(const Params& params,
const void* object);
static V8_INLINE void DijkstraMarkingBarrierRange(
const Params& params, const void* first_element, size_t element_size,
size_t number_of_elements, TraceCallback trace_callback);
static V8_INLINE void SteeleMarkingBarrier(const Params& params,
const void* object);
#if defined(CPPGC_YOUNG_GENERATION)
template <GenerationalBarrierType>
static V8_INLINE void GenerationalBarrier(const Params& params,
const void* slot);
#else // !CPPGC_YOUNG_GENERATION
template <GenerationalBarrierType>
static V8_INLINE void GenerationalBarrier(const Params& params,
const void* slot){}
#endif // CPPGC_YOUNG_GENERATION
#if V8_ENABLE_CHECKS
static void CheckParams(Type expected_type, const Params& params);
#else // !V8_ENABLE_CHECKS
static void CheckParams(Type expected_type, const Params& params) {}
#endif // !V8_ENABLE_CHECKS
// The FlagUpdater class allows cppgc internal to update
// |write_barrier_enabled_|.
class FlagUpdater;
static bool IsEnabled() { return write_barrier_enabled_.MightBeEntered(); }
private:
WriteBarrier() = delete;
#if defined(CPPGC_CAGED_HEAP)
using WriteBarrierTypePolicy = WriteBarrierTypeForCagedHeapPolicy;
#else // !CPPGC_CAGED_HEAP
using WriteBarrierTypePolicy = WriteBarrierTypeForNonCagedHeapPolicy;
#endif // !CPPGC_CAGED_HEAP
static void DijkstraMarkingBarrierSlow(const void* value);
static void DijkstraMarkingBarrierSlowWithSentinelCheck(const void* value);
static void DijkstraMarkingBarrierRangeSlow(HeapHandle& heap_handle,
const void* first_element,
size_t element_size,
size_t number_of_elements,
TraceCallback trace_callback);
static void SteeleMarkingBarrierSlow(const void* value);
static void SteeleMarkingBarrierSlowWithSentinelCheck(const void* value);
#if defined(CPPGC_YOUNG_GENERATION)
static CagedHeapLocalData& GetLocalData(HeapHandle&);
static void GenerationalBarrierSlow(const CagedHeapLocalData& local_data,
const AgeTable& age_table,
const void* slot, uintptr_t value_offset,
HeapHandle* heap_handle);
static void GenerationalBarrierForUncompressedSlotSlow(
const CagedHeapLocalData& local_data, const AgeTable& age_table,
const void* slot, uintptr_t value_offset, HeapHandle* heap_handle);
static void GenerationalBarrierForSourceObjectSlow(
const CagedHeapLocalData& local_data, const void* object,
HeapHandle* heap_handle);
#endif // CPPGC_YOUNG_GENERATION
static AtomicEntryFlag write_barrier_enabled_;
};
template <WriteBarrier::Type type>
V8_INLINE WriteBarrier::Type SetAndReturnType(WriteBarrier::Params& params) {
if constexpr (type == WriteBarrier::Type::kNone) {
return WriteBarrier::Type::kNone;
}
#if V8_ENABLE_CHECKS
params.type = type;
#endif // !V8_ENABLE_CHECKS
return type;
}
#if defined(CPPGC_CAGED_HEAP)
class V8_EXPORT WriteBarrierTypeForCagedHeapPolicy final {
public:
template <WriteBarrier::ValueMode value_mode, typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* slot, const void* value,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
return ValueModeDispatch<value_mode>::Get(slot, value, params, callback);
}
template <WriteBarrier::ValueMode value_mode, typename HeapHandleCallback,
typename MemberStorage>
static V8_INLINE WriteBarrier::Type Get(const void* slot, MemberStorage value,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
return ValueModeDispatch<value_mode>::Get(slot, value, params, callback);
}
template <WriteBarrier::ValueMode value_mode, typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* value,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
return GetNoSlot(value, params, callback);
}
private:
WriteBarrierTypeForCagedHeapPolicy() = delete;
template <typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type GetNoSlot(const void* value,
WriteBarrier::Params& params,
HeapHandleCallback) {
const bool within_cage = CagedHeapBase::IsWithinCage(value);
if (!within_cage) return WriteBarrier::Type::kNone;
// We know that |value| points either within the normal page or to the
// beginning of large-page, so extract the page header by bitmasking.
BasePageHandle* page =
BasePageHandle::FromPayload(const_cast<void*>(value));
HeapHandle& heap_handle = page->heap_handle();
if (V8_UNLIKELY(heap_handle.is_incremental_marking_in_progress())) {
return SetAndReturnType<WriteBarrier::Type::kMarking>(params);
}
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
template <WriteBarrier::ValueMode value_mode>
struct ValueModeDispatch;
};
template <>
struct WriteBarrierTypeForCagedHeapPolicy::ValueModeDispatch<
WriteBarrier::ValueMode::kValuePresent> {
template <typename HeapHandleCallback, typename MemberStorage>
static V8_INLINE WriteBarrier::Type Get(const void* slot,
MemberStorage storage,
WriteBarrier::Params& params,
HeapHandleCallback) {
if (V8_LIKELY(!WriteBarrier::IsEnabled())) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
return BarrierEnabledGet(slot, storage.Load(), params);
}
template <typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* slot, const void* value,
WriteBarrier::Params& params,
HeapHandleCallback) {
if (V8_LIKELY(!WriteBarrier::IsEnabled())) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
return BarrierEnabledGet(slot, value, params);
}
private:
static V8_INLINE WriteBarrier::Type BarrierEnabledGet(
const void* slot, const void* value, WriteBarrier::Params& params) {
const bool within_cage = CagedHeapBase::AreWithinCage(slot, value);
if (!within_cage) return WriteBarrier::Type::kNone;
// We know that |value| points either within the normal page or to the
// beginning of large-page, so extract the page header by bitmasking.
BasePageHandle* page =
BasePageHandle::FromPayload(const_cast<void*>(value));
HeapHandle& heap_handle = page->heap_handle();
if (V8_LIKELY(!heap_handle.is_incremental_marking_in_progress())) {
#if defined(CPPGC_YOUNG_GENERATION)
if (!heap_handle.is_young_generation_enabled()) {
return WriteBarrier::Type::kNone;
}
params.heap = &heap_handle;
params.slot_offset = CagedHeapBase::OffsetFromAddress(slot);
params.value_offset = CagedHeapBase::OffsetFromAddress(value);
return SetAndReturnType<WriteBarrier::Type::kGenerational>(params);
#else // !CPPGC_YOUNG_GENERATION
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
#endif // !CPPGC_YOUNG_GENERATION
}
// Use marking barrier.
params.heap = &heap_handle;
return SetAndReturnType<WriteBarrier::Type::kMarking>(params);
}
};
template <>
struct WriteBarrierTypeForCagedHeapPolicy::ValueModeDispatch<
WriteBarrier::ValueMode::kNoValuePresent> {
template <typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* slot, const void*,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
if (V8_LIKELY(!WriteBarrier::IsEnabled())) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
HeapHandle& handle = callback();
#if defined(CPPGC_YOUNG_GENERATION)
if (V8_LIKELY(!handle.is_incremental_marking_in_progress())) {
if (!handle.is_young_generation_enabled()) {
return WriteBarrier::Type::kNone;
}
params.heap = &handle;
// Check if slot is on stack.
if (V8_UNLIKELY(!CagedHeapBase::IsWithinCage(slot))) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
params.slot_offset = CagedHeapBase::OffsetFromAddress(slot);
return SetAndReturnType<WriteBarrier::Type::kGenerational>(params);
}
#else // !defined(CPPGC_YOUNG_GENERATION)
if (V8_UNLIKELY(!handle.is_incremental_marking_in_progress())) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
#endif // !defined(CPPGC_YOUNG_GENERATION)
params.heap = &handle;
return SetAndReturnType<WriteBarrier::Type::kMarking>(params);
}
};
#endif // CPPGC_CAGED_HEAP
class V8_EXPORT WriteBarrierTypeForNonCagedHeapPolicy final {
public:
template <WriteBarrier::ValueMode value_mode, typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* slot, const void* value,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
return ValueModeDispatch<value_mode>::Get(slot, value, params, callback);
}
template <WriteBarrier::ValueMode value_mode, typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* slot, RawPointer value,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
return ValueModeDispatch<value_mode>::Get(slot, value.Load(), params,
callback);
}
template <WriteBarrier::ValueMode value_mode, typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void* value,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
// The slot will never be used in `Get()` below.
return Get<WriteBarrier::ValueMode::kValuePresent>(nullptr, value, params,
callback);
}
private:
template <WriteBarrier::ValueMode value_mode>
struct ValueModeDispatch;
WriteBarrierTypeForNonCagedHeapPolicy() = delete;
};
template <>
struct WriteBarrierTypeForNonCagedHeapPolicy::ValueModeDispatch<
WriteBarrier::ValueMode::kValuePresent> {
template <typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void*, const void* object,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
// The following check covers nullptr as well as sentinel pointer.
if (object <= static_cast<void*>(kSentinelPointer)) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
if (V8_LIKELY(!WriteBarrier::IsEnabled())) {
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
// We know that |object| is within the normal page or in the beginning of a
// large page, so extract the page header by bitmasking.
BasePageHandle* page =
BasePageHandle::FromPayload(const_cast<void*>(object));
HeapHandle& heap_handle = page->heap_handle();
if (V8_LIKELY(heap_handle.is_incremental_marking_in_progress())) {
return SetAndReturnType<WriteBarrier::Type::kMarking>(params);
}
return SetAndReturnType<WriteBarrier::Type::kNone>(params);
}
};
template <>
struct WriteBarrierTypeForNonCagedHeapPolicy::ValueModeDispatch<
WriteBarrier::ValueMode::kNoValuePresent> {
template <typename HeapHandleCallback>
static V8_INLINE WriteBarrier::Type Get(const void*, const void*,
WriteBarrier::Params& params,
HeapHandleCallback callback) {
if (V8_UNLIKELY(WriteBarrier::IsEnabled())) {
HeapHandle& handle = callback();
if (V8_LIKELY(handle.is_incremental_marking_in_progress())) {
params.heap = &handle;
return SetAndReturnType<WriteBarrier::Type::kMarking>(params);
}
}
return WriteBarrier::Type::kNone;
}
};
// static
WriteBarrier::Type WriteBarrier::GetWriteBarrierType(
const void* slot, const void* value, WriteBarrier::Params& params) {
return WriteBarrierTypePolicy::Get<ValueMode::kValuePresent>(slot, value,
params, []() {});
}
// static
template <typename MemberStorage>
WriteBarrier::Type WriteBarrier::GetWriteBarrierType(
const void* slot, MemberStorage value, WriteBarrier::Params& params) {
return WriteBarrierTypePolicy::Get<ValueMode::kValuePresent>(slot, value,
params, []() {});
}
// static
template <typename HeapHandleCallback>
WriteBarrier::Type WriteBarrier::GetWriteBarrierType(
const void* slot, WriteBarrier::Params& params,
HeapHandleCallback callback) {
return WriteBarrierTypePolicy::Get<ValueMode::kNoValuePresent>(
slot, nullptr, params, callback);
}
// static
WriteBarrier::Type WriteBarrier::GetWriteBarrierType(
const void* value, WriteBarrier::Params& params) {
return WriteBarrierTypePolicy::Get<ValueMode::kValuePresent>(value, params,
[]() {});
}
// static
void WriteBarrier::DijkstraMarkingBarrier(const Params& params,
const void* object) {
CheckParams(Type::kMarking, params);
#if defined(CPPGC_CAGED_HEAP)
// Caged heap already filters out sentinels.
DijkstraMarkingBarrierSlow(object);
#else // !CPPGC_CAGED_HEAP
DijkstraMarkingBarrierSlowWithSentinelCheck(object);
#endif // !CPPGC_CAGED_HEAP
}
// static
void WriteBarrier::DijkstraMarkingBarrierRange(const Params& params,
const void* first_element,
size_t element_size,
size_t number_of_elements,
TraceCallback trace_callback) {
CheckParams(Type::kMarking, params);
DijkstraMarkingBarrierRangeSlow(*params.heap, first_element, element_size,
number_of_elements, trace_callback);
}
// static
void WriteBarrier::SteeleMarkingBarrier(const Params& params,
const void* object) {
CheckParams(Type::kMarking, params);
#if defined(CPPGC_CAGED_HEAP)
// Caged heap already filters out sentinels.
SteeleMarkingBarrierSlow(object);
#else // !CPPGC_CAGED_HEAP
SteeleMarkingBarrierSlowWithSentinelCheck(object);
#endif // !CPPGC_CAGED_HEAP
}
#if defined(CPPGC_YOUNG_GENERATION)
// static
template <WriteBarrier::GenerationalBarrierType type>
void WriteBarrier::GenerationalBarrier(const Params& params, const void* slot) {
CheckParams(Type::kGenerational, params);
const CagedHeapLocalData& local_data = CagedHeapLocalData::Get();
const AgeTable& age_table = local_data.age_table;
// Bail out if the slot (precise or imprecise) is in young generation.
if (V8_LIKELY(age_table.GetAge(params.slot_offset) ==
AgeTable::Age::kYoung)) {
return;
}
// Dispatch between different types of barriers.
// TODO(chromium:1029379): Consider reload local_data in the slow path to
// reduce register pressure.
if constexpr (type == GenerationalBarrierType::kPreciseSlot) {
GenerationalBarrierSlow(local_data, age_table, slot, params.value_offset,
params.heap);
} else if constexpr (type ==
GenerationalBarrierType::kPreciseUncompressedSlot) {
GenerationalBarrierForUncompressedSlotSlow(
local_data, age_table, slot, params.value_offset, params.heap);
} else {
GenerationalBarrierForSourceObjectSlow(local_data, slot, params.heap);
}
}
#endif // !CPPGC_YOUNG_GENERATION
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_INTERNAL_WRITE_BARRIER_H_

View File

@ -0,0 +1,78 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_LIVENESS_BROKER_H_
#define INCLUDE_CPPGC_LIVENESS_BROKER_H_
#include "cppgc/heap.h"
#include "cppgc/member.h"
#include "cppgc/sentinel-pointer.h"
#include "cppgc/trace-trait.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
class LivenessBrokerFactory;
} // namespace internal
/**
* The broker is passed to weak callbacks to allow (temporarily) querying
* the liveness state of an object. References to non-live objects must be
* cleared when `IsHeapObjectAlive()` returns false.
*
* \code
* class GCedWithCustomWeakCallback final
* : public GarbageCollected<GCedWithCustomWeakCallback> {
* public:
* UntracedMember<Bar> bar;
*
* void CustomWeakCallbackMethod(const LivenessBroker& broker) {
* if (!broker.IsHeapObjectAlive(bar))
* bar = nullptr;
* }
*
* void Trace(cppgc::Visitor* visitor) const {
* visitor->RegisterWeakCallbackMethod<
* GCedWithCustomWeakCallback,
* &GCedWithCustomWeakCallback::CustomWeakCallbackMethod>(this);
* }
* };
* \endcode
*/
class V8_EXPORT LivenessBroker final {
public:
template <typename T>
bool IsHeapObjectAlive(const T* object) const {
// - nullptr objects are considered alive to allow weakness to be used from
// stack while running into a conservative GC. Treating nullptr as dead
// would mean that e.g. custom collections could not be strongified on
// stack.
// - Sentinel pointers are also preserved in weakness and not cleared.
return !object || object == kSentinelPointer ||
IsHeapObjectAliveImpl(
TraceTrait<T>::GetTraceDescriptor(object).base_object_payload);
}
template <typename T>
bool IsHeapObjectAlive(const WeakMember<T>& weak_member) const {
return IsHeapObjectAlive<T>(weak_member.Get());
}
template <typename T>
bool IsHeapObjectAlive(const UntracedMember<T>& untraced_member) const {
return IsHeapObjectAlive<T>(untraced_member.Get());
}
private:
LivenessBroker() = default;
bool IsHeapObjectAliveImpl(const void*) const;
friend class internal::LivenessBrokerFactory;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_LIVENESS_BROKER_H_

View File

@ -0,0 +1,56 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_MACROS_H_
#define INCLUDE_CPPGC_MACROS_H_
#include <cstddef>
#include "cppgc/internal/compiler-specific.h"
namespace cppgc {
#define CPPGC_DISALLOW_NEW() \
public: \
using IsDisallowNewMarker CPPGC_UNUSED = int; \
void* operator new(size_t, void* location) { return location; } \
void* operator new(size_t) = delete; \
static_assert(true, "Force semicolon.")
// Use CPPGC_STACK_ALLOCATED if the object is only stack allocated.
// Add the CPPGC_STACK_ALLOCATED_IGNORE annotation on a case-by-case basis when
// enforcement of CPPGC_STACK_ALLOCATED should be suppressed.
#if defined(__clang__)
#define CPPGC_STACK_ALLOCATED() \
public: \
using IsStackAllocatedTypeMarker CPPGC_UNUSED = int; \
\
private: \
void* operator new(size_t) = delete; \
void* operator new(size_t, void*) = delete; \
static_assert(true, "Force semicolon.")
#define CPPGC_STACK_ALLOCATED_IGNORE(bug_or_reason) \
__attribute__((annotate("stack_allocated_ignore")))
#define CPPGC_PLUGIN_IGNORE(bug_or_reason) \
__attribute__((annotate("blink_gc_plugin_ignore"), \
annotate("stack_allocated_ignore")))
#else // !defined(__clang__)
#define CPPGC_STACK_ALLOCATED() static_assert(true, "Force semicolon.")
#define CPPGC_STACK_ALLOCATED_IGNORE(bug_or_reason)
#define CPPGC_PLUGIN_IGNORE(bug_or_reason)
#endif // !defined(__clang__)
template <typename T>
concept IsStackAllocatedType =
requires { typename T::IsStackAllocatedTypeMarker; };
} // namespace cppgc
#endif // INCLUDE_CPPGC_MACROS_H_

View File

@ -0,0 +1,669 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_MEMBER_H_
#define INCLUDE_CPPGC_MEMBER_H_
#include <atomic>
#include <cstddef>
#include <type_traits>
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/member-storage.h"
#include "cppgc/internal/pointer-policies.h"
#include "cppgc/sentinel-pointer.h"
#include "cppgc/type-traits.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace subtle {
class HeapConsistency;
template <typename, typename, typename>
class TaggedUncompressedMember;
} // namespace subtle
class Visitor;
namespace internal {
// MemberBase always refers to the object as const object and defers to
// BasicMember on casting to the right type as needed.
template <typename StorageType>
class V8_TRIVIAL_ABI MemberBase {
public:
using RawStorage = StorageType;
protected:
struct AtomicInitializerTag {};
V8_INLINE MemberBase() = default;
V8_INLINE explicit MemberBase(const void* value) : raw_(value) {}
V8_INLINE MemberBase(const void* value, AtomicInitializerTag)
: raw_(value, typename RawStorage::AtomicInitializerTag{}) {}
V8_INLINE explicit MemberBase(RawStorage raw) : raw_(raw) {}
V8_INLINE explicit MemberBase(std::nullptr_t) : raw_(nullptr) {}
V8_INLINE explicit MemberBase(SentinelPointer s) : raw_(s) {}
V8_INLINE const void** GetRawSlot() const {
return reinterpret_cast<const void**>(const_cast<MemberBase*>(this));
}
V8_INLINE const void* GetRaw() const { return raw_.Load(); }
V8_INLINE void SetRaw(void* value) { raw_.Store(value); }
V8_INLINE const void* GetRawAtomic() const { return raw_.LoadAtomic(); }
V8_INLINE void SetRawAtomic(const void* value) { raw_.StoreAtomic(value); }
V8_INLINE RawStorage GetRawStorage() const { return raw_; }
V8_INLINE void SetRawStorageAtomic(RawStorage other) {
reinterpret_cast<std::atomic<RawStorage>&>(raw_).store(
other, std::memory_order_relaxed);
}
V8_INLINE bool IsCleared() const { return raw_.IsCleared(); }
V8_INLINE void ClearFromGC() const { raw_.Clear(); }
private:
friend class MemberDebugHelper;
mutable RawStorage raw_;
};
// The basic class from which all Member classes are 'generated'.
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
class V8_TRIVIAL_ABI BasicMember final : private MemberBase<StorageType>,
private CheckingPolicy {
using Base = MemberBase<StorageType>;
public:
using PointeeType = T;
using RawStorage = typename Base::RawStorage;
V8_INLINE constexpr BasicMember() = default;
V8_INLINE constexpr BasicMember(std::nullptr_t) {} // NOLINT
V8_INLINE BasicMember(SentinelPointer s) : Base(s) {} // NOLINT
V8_INLINE BasicMember(T* raw) : Base(raw) { // NOLINT
InitializingWriteBarrier(raw);
CheckPointer(raw);
}
V8_INLINE BasicMember(T& raw) // NOLINT
: BasicMember(&raw) {}
// Atomic ctor. Using the AtomicInitializerTag forces BasicMember to
// initialize using atomic assignments. This is required for preventing
// data races with concurrent marking.
using AtomicInitializerTag = typename Base::AtomicInitializerTag;
V8_INLINE BasicMember(std::nullptr_t, AtomicInitializerTag atomic)
: Base(nullptr, atomic) {}
V8_INLINE BasicMember(SentinelPointer s, AtomicInitializerTag atomic)
: Base(s, atomic) {}
V8_INLINE BasicMember(T* raw, AtomicInitializerTag atomic)
: Base(raw, atomic) {
InitializingWriteBarrier(raw);
CheckPointer(raw);
}
V8_INLINE BasicMember(T& raw, AtomicInitializerTag atomic)
: BasicMember(&raw, atomic) {}
// Copy ctor.
V8_INLINE BasicMember(const BasicMember& other)
: BasicMember(other.GetRawStorage()) {}
// Heterogeneous copy constructors. When the source pointer have a different
// type, perform a compress-decompress round, because the source pointer may
// need to be adjusted.
template <typename U, typename OtherBarrierPolicy, typename OtherWeaknessTag,
typename OtherCheckingPolicy,
std::enable_if_t<IsDecayedSameV<T, U>>* = nullptr>
V8_INLINE BasicMember( // NOLINT
const BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
OtherCheckingPolicy, StorageType>& other)
: BasicMember(other.GetRawStorage()) {}
template <typename U, typename OtherBarrierPolicy, typename OtherWeaknessTag,
typename OtherCheckingPolicy,
std::enable_if_t<IsStrictlyBaseOfV<T, U>>* = nullptr>
V8_INLINE BasicMember( // NOLINT
const BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
OtherCheckingPolicy, StorageType>& other)
: BasicMember(other.Get()) {}
// Move ctor.
V8_INLINE BasicMember(BasicMember&& other) noexcept
: BasicMember(other.GetRawStorage()) {
other.Clear();
}
// Heterogeneous move constructors. When the source pointer have a different
// type, perform a compress-decompress round, because the source pointer may
// need to be adjusted.
template <typename U, typename OtherBarrierPolicy, typename OtherWeaknessTag,
typename OtherCheckingPolicy,
std::enable_if_t<IsDecayedSameV<T, U>>* = nullptr>
V8_INLINE BasicMember(
BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy,
StorageType>&& other) noexcept
: BasicMember(other.GetRawStorage()) {
other.Clear();
}
template <typename U, typename OtherBarrierPolicy, typename OtherWeaknessTag,
typename OtherCheckingPolicy,
std::enable_if_t<IsStrictlyBaseOfV<T, U>>* = nullptr>
V8_INLINE BasicMember(
BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy,
StorageType>&& other) noexcept
: BasicMember(other.Get()) {
other.Clear();
}
// Construction from Persistent.
template <typename U, typename PersistentWeaknessPolicy,
typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
V8_INLINE BasicMember(const BasicPersistent<U, PersistentWeaknessPolicy,
PersistentLocationPolicy,
PersistentCheckingPolicy>& p)
: BasicMember(p.Get()) {}
// Copy assignment.
V8_INLINE BasicMember& operator=(const BasicMember& other) {
return operator=(other.GetRawStorage());
}
// Heterogeneous copy assignment. When the source pointer have a different
// type, perform a compress-decompress round, because the source pointer may
// need to be adjusted.
template <typename U, typename OtherWeaknessTag, typename OtherBarrierPolicy,
typename OtherCheckingPolicy>
V8_INLINE BasicMember& operator=(
const BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
OtherCheckingPolicy, StorageType>& other) {
if constexpr (IsDecayedSameV<T, U>) {
return operator=(other.GetRawStorage());
} else {
static_assert(IsStrictlyBaseOfV<T, U>);
return operator=(other.Get());
}
}
// Move assignment.
V8_INLINE BasicMember& operator=(BasicMember&& other) noexcept {
operator=(other.GetRawStorage());
other.Clear();
return *this;
}
// Heterogeneous move assignment. When the source pointer have a different
// type, perform a compress-decompress round, because the source pointer may
// need to be adjusted.
template <typename U, typename OtherWeaknessTag, typename OtherBarrierPolicy,
typename OtherCheckingPolicy>
V8_INLINE BasicMember& operator=(
BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy,
StorageType>&& other) noexcept {
if constexpr (IsDecayedSameV<T, U>) {
operator=(other.GetRawStorage());
} else {
static_assert(IsStrictlyBaseOfV<T, U>);
operator=(other.Get());
}
other.Clear();
return *this;
}
// Assignment from Persistent.
template <typename U, typename PersistentWeaknessPolicy,
typename PersistentLocationPolicy,
typename PersistentCheckingPolicy,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
V8_INLINE BasicMember& operator=(
const BasicPersistent<U, PersistentWeaknessPolicy,
PersistentLocationPolicy, PersistentCheckingPolicy>&
other) {
return operator=(other.Get());
}
V8_INLINE BasicMember& operator=(T* other) {
Base::SetRawAtomic(other);
AssigningWriteBarrier(other);
CheckPointer(other);
return *this;
}
V8_INLINE BasicMember& operator=(std::nullptr_t) {
Clear();
return *this;
}
V8_INLINE BasicMember& operator=(SentinelPointer s) {
Base::SetRawAtomic(s);
return *this;
}
template <typename OtherWeaknessTag, typename OtherBarrierPolicy,
typename OtherCheckingPolicy>
V8_INLINE void Swap(BasicMember<T, OtherWeaknessTag, OtherBarrierPolicy,
OtherCheckingPolicy, StorageType>& other) {
auto tmp = GetRawStorage();
*this = other;
other = tmp;
}
V8_INLINE explicit operator bool() const { return !Base::IsCleared(); }
V8_INLINE operator T*() const { return Get(); }
V8_INLINE T* operator->() const { return Get(); }
V8_INLINE T& operator*() const { return *Get(); }
// CFI cast exemption to allow passing SentinelPointer through T* and support
// heterogeneous assignments between different Member and Persistent handles
// based on their actual types.
V8_INLINE V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const {
// Executed by the mutator, hence non atomic load.
//
// The const_cast below removes the constness from MemberBase storage. The
// following static_cast re-adds any constness if specified through the
// user-visible template parameter T.
return static_cast<T*>(const_cast<void*>(Base::GetRaw()));
}
V8_INLINE void Clear() { Base::SetRawStorageAtomic(RawStorage{}); }
V8_INLINE T* Release() {
T* result = Get();
Clear();
return result;
}
V8_INLINE const T** GetSlotForTesting() const {
return reinterpret_cast<const T**>(Base::GetRawSlot());
}
V8_INLINE RawStorage GetRawStorage() const { return Base::GetRawStorage(); }
private:
V8_INLINE explicit BasicMember(RawStorage raw) : Base(raw) {
InitializingWriteBarrier();
CheckPointer();
}
V8_INLINE BasicMember& operator=(RawStorage other) {
Base::SetRawStorageAtomic(other);
AssigningWriteBarrier();
CheckPointer();
return *this;
}
V8_INLINE const void* GetRawAtomic() const { return Base::GetRawAtomic(); }
V8_INLINE const T* GetAtomic() const {
return static_cast<const T*>(GetRawAtomic());
}
V8_INLINE void InitializingWriteBarrier(T* value) const {
WriteBarrierPolicy::InitializingBarrier(Base::GetRawSlot(), value);
}
V8_INLINE void InitializingWriteBarrier() const {
WriteBarrierPolicy::InitializingBarrier(Base::GetRawSlot(),
Base::GetRawStorage());
}
V8_INLINE void AssigningWriteBarrier(T* value) const {
WriteBarrierPolicy::template AssigningBarrier<
StorageType::kWriteBarrierSlotType>(Base::GetRawSlot(), value);
}
V8_INLINE void AssigningWriteBarrier() const {
WriteBarrierPolicy::template AssigningBarrier<
StorageType::kWriteBarrierSlotType>(Base::GetRawSlot(),
Base::GetRawStorage());
}
V8_INLINE void CheckPointer(T* value) {
CheckingPolicy::template CheckPointer<T>(value);
}
V8_INLINE void CheckPointer() {
CheckingPolicy::template CheckPointer<T>(Base::GetRawStorage());
}
V8_INLINE void ClearFromGC() const { Base::ClearFromGC(); }
V8_INLINE T* GetFromGC() const { return Get(); }
friend class cppgc::subtle::HeapConsistency;
template <typename, typename, typename>
friend class cppgc::subtle::TaggedUncompressedMember;
friend class cppgc::Visitor;
template <typename U>
friend struct cppgc::TraceTrait;
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename StorageType1>
friend class BasicMember;
};
// Member equality operators.
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessTag2,
typename WriteBarrierPolicy2, typename CheckingPolicy2,
typename StorageType>
V8_INLINE bool operator==(
const BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1,
StorageType>& member1,
const BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2,
StorageType>& member2) {
if constexpr (IsDecayedSameV<T1, T2>) {
// Check compressed pointers if types are the same.
return member1.GetRawStorage() == member2.GetRawStorage();
} else {
static_assert(IsStrictlyBaseOfV<T1, T2> || IsStrictlyBaseOfV<T2, T1>);
// Otherwise, check decompressed pointers.
return member1.Get() == member2.Get();
}
}
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessTag2,
typename WriteBarrierPolicy2, typename CheckingPolicy2,
typename StorageType>
V8_INLINE bool operator!=(
const BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1,
StorageType>& member1,
const BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2,
StorageType>& member2) {
return !(member1 == member2);
}
// Equality with raw pointers.
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType, typename U>
V8_INLINE bool operator==(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>& member,
U* raw) {
// Never allow comparison with erased pointers.
static_assert(!IsDecayedSameV<void, U>);
if constexpr (IsDecayedSameV<T, U>) {
// Check compressed pointers if types are the same.
return member.GetRawStorage() == StorageType(raw);
} else if constexpr (IsStrictlyBaseOfV<T, U>) {
// Cast the raw pointer to T, which may adjust the pointer.
return member.GetRawStorage() == StorageType(static_cast<T*>(raw));
} else {
// Otherwise, decompressed the member.
return member.Get() == raw;
}
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType, typename U>
V8_INLINE bool operator!=(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>& member,
U* raw) {
return !(member == raw);
}
template <typename T, typename U, typename WeaknessTag,
typename WriteBarrierPolicy, typename CheckingPolicy,
typename StorageType>
V8_INLINE bool operator==(
T* raw, const BasicMember<U, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& member) {
return member == raw;
}
template <typename T, typename U, typename WeaknessTag,
typename WriteBarrierPolicy, typename CheckingPolicy,
typename StorageType>
V8_INLINE bool operator!=(
T* raw, const BasicMember<U, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& member) {
return !(raw == member);
}
// Equality with sentinel.
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator==(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>& member,
SentinelPointer) {
return member.GetRawStorage().IsSentinel();
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator!=(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>& member,
SentinelPointer s) {
return !(member == s);
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator==(
SentinelPointer s, const BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& member) {
return member == s;
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator!=(
SentinelPointer s, const BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& member) {
return !(s == member);
}
// Equality with nullptr.
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator==(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>& member,
std::nullptr_t) {
return !static_cast<bool>(member);
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator!=(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>& member,
std::nullptr_t n) {
return !(member == n);
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator==(
std::nullptr_t n, const BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& member) {
return member == n;
}
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
V8_INLINE bool operator!=(
std::nullptr_t n, const BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>& member) {
return !(n == member);
}
// Relational operators.
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessTag2,
typename WriteBarrierPolicy2, typename CheckingPolicy2,
typename StorageType>
V8_INLINE bool operator<(
const BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1,
StorageType>& member1,
const BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2,
StorageType>& member2) {
static_assert(
IsDecayedSameV<T1, T2>,
"Comparison works only for same pointer type modulo cv-qualifiers");
return member1.GetRawStorage() < member2.GetRawStorage();
}
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessTag2,
typename WriteBarrierPolicy2, typename CheckingPolicy2,
typename StorageType>
V8_INLINE bool operator<=(
const BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1,
StorageType>& member1,
const BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2,
StorageType>& member2) {
static_assert(
IsDecayedSameV<T1, T2>,
"Comparison works only for same pointer type modulo cv-qualifiers");
return member1.GetRawStorage() <= member2.GetRawStorage();
}
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessTag2,
typename WriteBarrierPolicy2, typename CheckingPolicy2,
typename StorageType>
V8_INLINE bool operator>(
const BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1,
StorageType>& member1,
const BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2,
StorageType>& member2) {
static_assert(
IsDecayedSameV<T1, T2>,
"Comparison works only for same pointer type modulo cv-qualifiers");
return member1.GetRawStorage() > member2.GetRawStorage();
}
template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessTag2,
typename WriteBarrierPolicy2, typename CheckingPolicy2,
typename StorageType>
V8_INLINE bool operator>=(
const BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1,
StorageType>& member1,
const BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2,
StorageType>& member2) {
static_assert(
IsDecayedSameV<T1, T2>,
"Comparison works only for same pointer type modulo cv-qualifiers");
return member1.GetRawStorage() >= member2.GetRawStorage();
}
template <typename T, typename WriteBarrierPolicy, typename CheckingPolicy,
typename StorageType>
struct IsWeak<BasicMember<T, WeakMemberTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>> : std::true_type {};
} // namespace internal
/**
* Members are used in classes to contain strong pointers to other garbage
* collected objects. All Member fields of a class must be traced in the class'
* trace method.
*/
template <typename T>
using Member = internal::BasicMember<
T, internal::StrongMemberTag, internal::DijkstraWriteBarrierPolicy,
internal::DefaultMemberCheckingPolicy, internal::DefaultMemberStorage>;
/**
* WeakMember is similar to Member in that it is used to point to other garbage
* collected objects. However instead of creating a strong pointer to the
* object, the WeakMember creates a weak pointer, which does not keep the
* pointee alive. Hence if all pointers to to a heap allocated object are weak
* the object will be garbage collected. At the time of GC the weak pointers
* will automatically be set to null.
*/
template <typename T>
using WeakMember = internal::BasicMember<
T, internal::WeakMemberTag, internal::DijkstraWriteBarrierPolicy,
internal::DefaultMemberCheckingPolicy, internal::DefaultMemberStorage>;
/**
* UntracedMember is a pointer to an on-heap object that is not traced for some
* reason. Do not use this unless you know what you are doing. Keeping raw
* pointers to on-heap objects is prohibited unless used from stack. Pointee
* must be kept alive through other means.
*/
template <typename T>
using UntracedMember = internal::BasicMember<
T, internal::UntracedMemberTag, internal::NoWriteBarrierPolicy,
internal::DefaultMemberCheckingPolicy, internal::DefaultMemberStorage>;
namespace subtle {
/**
* UncompressedMember. Use with care in hot paths that would otherwise cause
* many decompression cycles.
*/
template <typename T>
using UncompressedMember = internal::BasicMember<
T, internal::StrongMemberTag, internal::DijkstraWriteBarrierPolicy,
internal::DefaultMemberCheckingPolicy, internal::RawPointer>;
#if defined(CPPGC_POINTER_COMPRESSION)
/**
* CompressedMember. Default implementation of cppgc::Member on builds with
* pointer compression.
*/
template <typename T>
using CompressedMember = internal::BasicMember<
T, internal::StrongMemberTag, internal::DijkstraWriteBarrierPolicy,
internal::DefaultMemberCheckingPolicy, internal::CompressedPointer>;
#endif // defined(CPPGC_POINTER_COMPRESSION)
} // namespace subtle
namespace internal {
struct Dummy;
static constexpr size_t kSizeOfMember = sizeof(Member<Dummy>);
static constexpr size_t kSizeOfUncompressedMember =
sizeof(subtle::UncompressedMember<Dummy>);
#if defined(CPPGC_POINTER_COMPRESSION)
static constexpr size_t kSizeofCompressedMember =
sizeof(subtle::CompressedMember<Dummy>);
#endif // defined(CPPGC_POINTER_COMPRESSION)
} // namespace internal
} // namespace cppgc
// Mark `BasicMember<T>` and `T*` as having a common reference type of `T*` (the
// type to which both can be converted or bound). This makes them satisfy
// `std::equality_comparable`, which allows usage like the following:
// ```
// HeapVector<Member<T>> v;
// T* e;
// auto it = std::ranges::find(v, e);
// ```
// Without this, the `find()` call above would fail to compile with an error
// about being unable to invoke `std::ranges::equal_to()`.
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType,
template <typename> typename TQ, template <typename> typename UQ>
struct std::basic_common_reference<
cppgc::internal::BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>,
T*, TQ, UQ> {
using type = T*;
};
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType,
template <typename> typename TQ, template <typename> typename UQ>
struct std::basic_common_reference<
T*,
cppgc::internal::BasicMember<T, WeaknessTag, WriteBarrierPolicy,
CheckingPolicy, StorageType>,
TQ, UQ> {
using type = T*;
};
#endif // INCLUDE_CPPGC_MEMBER_H_

View File

@ -0,0 +1,75 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_NAME_PROVIDER_H_
#define INCLUDE_CPPGC_NAME_PROVIDER_H_
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
/**
* NameProvider allows for providing a human-readable name for garbage-collected
* objects.
*
* There's two cases of names to distinguish:
* a. Explicitly specified names via using NameProvider. Such names are always
* preserved in the system.
* b. Internal names that Oilpan infers from a C++ type on the class hierarchy
* of the object. This is not necessarily the type of the actually
* instantiated object.
*
* Depending on the build configuration, Oilpan may hide names, i.e., represent
* them with kHiddenName, of case b. to avoid exposing internal details.
*/
class V8_EXPORT NameProvider {
public:
/**
* Name that is used when hiding internals.
*/
static constexpr const char kHiddenName[] = "InternalNode";
/**
* Name that is used in case compiler support is missing for composing a name
* from C++ types.
*/
static constexpr const char kNoNameDeducible[] = "<No name>";
/**
* Indicating whether the build supports extracting C++ names as object names.
*
* @returns true if C++ names should be hidden and represented by kHiddenName.
*/
static constexpr bool SupportsCppClassNamesAsObjectNames() {
#if CPPGC_SUPPORTS_OBJECT_NAMES
return true;
#else // !CPPGC_SUPPORTS_OBJECT_NAMES
return false;
#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
}
virtual ~NameProvider() = default;
/**
* Specifies a name for the garbage-collected object. Such names will never
* be hidden, as they are explicitly specified by the user of this API.
*
* Implementations of this function must not allocate garbage-collected
* objects or otherwise modify the cppgc heap.
*
* V8 may call this function while generating a heap snapshot or at other
* times. If V8 is currently generating a heap snapshot (according to
* HeapProfiler::IsTakingSnapshot), then the returned string must stay alive
* until the snapshot generation has completed. Otherwise, the returned string
* must stay alive forever. If you need a place to store a temporary string
* during snapshot generation, use HeapProfiler::CopyNameForHeapSnapshot.
*
* @returns a human readable name for the object.
*/
virtual const char* GetHumanReadableName() const = 0;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_NAME_PROVIDER_H_

View File

@ -0,0 +1,58 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_
#define INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_
#include <cstddef>
#include "cppgc/type-traits.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
struct V8_EXPORT BaseObjectSizeTrait {
protected:
static size_t GetObjectSizeForGarbageCollected(const void*);
static size_t GetObjectSizeForGarbageCollectedMixin(const void*);
};
} // namespace internal
namespace subtle {
/**
* Trait specifying how to get the size of an object that was allocated using
* `MakeGarbageCollected()`. Also supports querying the size with an inner
* pointer to a mixin.
*/
template <typename T, bool = IsGarbageCollectedMixinTypeV<T>>
struct ObjectSizeTrait;
template <typename T>
struct ObjectSizeTrait<T, false> : cppgc::internal::BaseObjectSizeTrait {
static_assert(sizeof(T), "T must be fully defined");
static_assert(IsGarbageCollectedTypeV<T>,
"T must be of type GarbageCollected or GarbageCollectedMixin");
static size_t GetSize(const T& object) {
return GetObjectSizeForGarbageCollected(&object);
}
};
template <typename T>
struct ObjectSizeTrait<T, true> : cppgc::internal::BaseObjectSizeTrait {
static_assert(sizeof(T), "T must be fully defined");
static size_t GetSize(const T& object) {
return GetObjectSizeForGarbageCollectedMixin(&object);
}
};
} // namespace subtle
} // namespace cppgc
#endif // INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_

View File

@ -0,0 +1,376 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_PERSISTENT_H_
#define INCLUDE_CPPGC_PERSISTENT_H_
#include <type_traits>
#include "cppgc/internal/persistent-node.h"
#include "cppgc/internal/pointer-policies.h"
#include "cppgc/sentinel-pointer.h"
#include "cppgc/source-location.h"
#include "cppgc/type-traits.h"
#include "cppgc/visitor.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
// PersistentBase always refers to the object as const object and defers to
// BasicPersistent on casting to the right type as needed.
class PersistentBase {
protected:
PersistentBase() = default;
explicit PersistentBase(const void* raw) : raw_(raw) {}
const void* GetValue() const { return raw_; }
void SetValue(const void* value) { raw_ = value; }
PersistentNode* GetNode() const { return node_; }
void SetNode(PersistentNode* node) { node_ = node; }
// Performs a shallow clear which assumes that internal persistent nodes are
// destroyed elsewhere.
void ClearFromGC() const {
raw_ = nullptr;
node_ = nullptr;
}
protected:
mutable const void* raw_ = nullptr;
mutable PersistentNode* node_ = nullptr;
friend class PersistentRegionBase;
};
// The basic class from which all Persistent classes are generated.
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
class BasicPersistent final : public PersistentBase,
public LocationPolicy,
private WeaknessPolicy,
private CheckingPolicy {
public:
using typename WeaknessPolicy::IsStrongPersistent;
using PointeeType = T;
// Null-state/sentinel constructors.
BasicPersistent( // NOLINT
SourceLocation loc = SourceLocation::Current())
: LocationPolicy(loc) {}
BasicPersistent(std::nullptr_t, // NOLINT
SourceLocation loc = SourceLocation::Current())
: LocationPolicy(loc) {}
BasicPersistent( // NOLINT
SentinelPointer s, SourceLocation loc = SourceLocation::Current())
: PersistentBase(s), LocationPolicy(loc) {}
// Raw value constructors.
BasicPersistent(T* raw, // NOLINT
SourceLocation loc = SourceLocation::Current())
: PersistentBase(raw), LocationPolicy(loc) {
if (!IsValid()) return;
SetNode(WeaknessPolicy::GetPersistentRegion(GetValue())
.AllocateNode(this, &TraceAsRoot));
this->CheckPointer(Get());
}
BasicPersistent(T& raw, // NOLINT
SourceLocation loc = SourceLocation::Current())
: BasicPersistent(&raw, loc) {}
// Copy ctor.
BasicPersistent(const BasicPersistent& other,
SourceLocation loc = SourceLocation::Current())
: BasicPersistent(other.Get(), loc) {}
// Heterogeneous ctor.
template <typename U, typename OtherWeaknessPolicy,
typename OtherLocationPolicy, typename OtherCheckingPolicy,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
// NOLINTNEXTLINE
BasicPersistent(
const BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>& other,
SourceLocation loc = SourceLocation::Current())
: BasicPersistent(other.Get(), loc) {}
// Move ctor. The heterogeneous move ctor is not supported since e.g.
// persistent can't reuse persistent node from weak persistent.
BasicPersistent(BasicPersistent&& other,
SourceLocation loc = SourceLocation::Current()) noexcept
: PersistentBase(std::move(other)), LocationPolicy(std::move(other)) {
if (!IsValid()) return;
GetNode()->UpdateOwner(this);
other.SetValue(nullptr);
other.SetNode(nullptr);
this->CheckPointer(Get());
}
// Constructor from member.
template <typename U, typename MemberBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
// NOLINTNEXTLINE
BasicPersistent(const internal::BasicMember<
U, MemberBarrierPolicy, MemberWeaknessTag,
MemberCheckingPolicy, MemberStorageType>& member,
SourceLocation loc = SourceLocation::Current())
: BasicPersistent(member.Get(), loc) {}
~BasicPersistent() { Clear(); }
// Copy assignment.
BasicPersistent& operator=(const BasicPersistent& other) {
return operator=(other.Get());
}
template <typename U, typename OtherWeaknessPolicy,
typename OtherLocationPolicy, typename OtherCheckingPolicy,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
BasicPersistent& operator=(
const BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>& other) {
return operator=(other.Get());
}
// Move assignment.
BasicPersistent& operator=(BasicPersistent&& other) noexcept {
if (this == &other) return *this;
Clear();
PersistentBase::operator=(std::move(other));
LocationPolicy::operator=(std::move(other));
if (!IsValid()) return *this;
GetNode()->UpdateOwner(this);
other.SetValue(nullptr);
other.SetNode(nullptr);
this->CheckPointer(Get());
return *this;
}
// Assignment from member.
template <typename U, typename MemberBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType,
typename = std::enable_if_t<std::is_base_of_v<T, U>>>
BasicPersistent& operator=(
const internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag,
MemberCheckingPolicy, MemberStorageType>&
member) {
return operator=(member.Get());
}
BasicPersistent& operator=(T* other) {
Assign(other);
return *this;
}
BasicPersistent& operator=(std::nullptr_t) {
Clear();
return *this;
}
BasicPersistent& operator=(SentinelPointer s) {
Assign(s);
return *this;
}
explicit operator bool() const { return Get(); }
// Historically we allow implicit conversions to T*.
// NOLINTNEXTLINE
operator T*() const { return Get(); }
T* operator->() const { return Get(); }
T& operator*() const { return *Get(); }
// CFI cast exemption to allow passing SentinelPointer through T* and support
// heterogeneous assignments between different Member and Persistent handles
// based on their actual types.
V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const {
// The const_cast below removes the constness from PersistentBase storage.
// The following static_cast re-adds any constness if specified through the
// user-visible template parameter T.
return static_cast<T*>(const_cast<void*>(GetValue()));
}
void Clear() {
// Simplified version of `Assign()` to allow calling without a complete type
// `T`.
if (IsValid()) {
WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode());
SetNode(nullptr);
}
SetValue(nullptr);
}
T* Release() {
T* result = Get();
Clear();
return result;
}
template <typename U, typename OtherWeaknessPolicy = WeaknessPolicy,
typename OtherLocationPolicy = LocationPolicy,
typename OtherCheckingPolicy = CheckingPolicy>
BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>
To() const {
return BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy,
OtherCheckingPolicy>(static_cast<U*>(Get()));
}
private:
static void TraceAsRoot(RootVisitor& root_visitor, const void* ptr) {
root_visitor.Trace(*static_cast<const BasicPersistent*>(ptr));
}
bool IsValid() const {
// Ideally, handling kSentinelPointer would be done by the embedder. On the
// other hand, having Persistent aware of it is beneficial since no node
// gets wasted.
return GetValue() != nullptr && GetValue() != kSentinelPointer;
}
void Assign(T* ptr) {
if (IsValid()) {
if (ptr && ptr != kSentinelPointer) {
// Simply assign the pointer reusing the existing node.
SetValue(ptr);
this->CheckPointer(ptr);
return;
}
WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode());
SetNode(nullptr);
}
SetValue(ptr);
if (!IsValid()) return;
SetNode(WeaknessPolicy::GetPersistentRegion(GetValue())
.AllocateNode(this, &TraceAsRoot));
this->CheckPointer(Get());
}
void ClearFromGC() const {
if (IsValid()) {
WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode());
PersistentBase::ClearFromGC();
}
}
// Set Get() for details.
V8_CLANG_NO_SANITIZE("cfi-unrelated-cast")
T* GetFromGC() const {
return static_cast<T*>(const_cast<void*>(GetValue()));
}
friend class internal::RootVisitor;
};
template <typename T1, typename WeaknessPolicy1, typename LocationPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessPolicy2,
typename LocationPolicy2, typename CheckingPolicy2>
bool operator==(const BasicPersistent<T1, WeaknessPolicy1, LocationPolicy1,
CheckingPolicy1>& p1,
const BasicPersistent<T2, WeaknessPolicy2, LocationPolicy2,
CheckingPolicy2>& p2) {
return p1.Get() == p2.Get();
}
template <typename T1, typename WeaknessPolicy1, typename LocationPolicy1,
typename CheckingPolicy1, typename T2, typename WeaknessPolicy2,
typename LocationPolicy2, typename CheckingPolicy2>
bool operator!=(const BasicPersistent<T1, WeaknessPolicy1, LocationPolicy1,
CheckingPolicy1>& p1,
const BasicPersistent<T2, WeaknessPolicy2, LocationPolicy2,
CheckingPolicy2>& p2) {
return !(p1 == p2);
}
template <typename T1, typename PersistentWeaknessPolicy,
typename PersistentLocationPolicy, typename PersistentCheckingPolicy,
typename T2, typename MemberWriteBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType>
bool operator==(
const BasicPersistent<T1, PersistentWeaknessPolicy,
PersistentLocationPolicy, PersistentCheckingPolicy>&
p,
const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
MemberCheckingPolicy, MemberStorageType>& m) {
return p.Get() == m.Get();
}
template <typename T1, typename PersistentWeaknessPolicy,
typename PersistentLocationPolicy, typename PersistentCheckingPolicy,
typename T2, typename MemberWriteBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType>
bool operator!=(
const BasicPersistent<T1, PersistentWeaknessPolicy,
PersistentLocationPolicy, PersistentCheckingPolicy>&
p,
const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
MemberCheckingPolicy, MemberStorageType>& m) {
return !(p == m);
}
template <typename T1, typename MemberWriteBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType, typename T2,
typename PersistentWeaknessPolicy, typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
bool operator==(
const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
MemberCheckingPolicy, MemberStorageType>& m,
const BasicPersistent<T1, PersistentWeaknessPolicy,
PersistentLocationPolicy, PersistentCheckingPolicy>&
p) {
return m.Get() == p.Get();
}
template <typename T1, typename MemberWriteBarrierPolicy,
typename MemberWeaknessTag, typename MemberCheckingPolicy,
typename MemberStorageType, typename T2,
typename PersistentWeaknessPolicy, typename PersistentLocationPolicy,
typename PersistentCheckingPolicy>
bool operator!=(
const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy,
MemberCheckingPolicy, MemberStorageType>& m,
const BasicPersistent<T1, PersistentWeaknessPolicy,
PersistentLocationPolicy, PersistentCheckingPolicy>&
p) {
return !(m == p);
}
template <typename T, typename LocationPolicy, typename CheckingPolicy>
struct IsWeak<BasicPersistent<T, internal::WeakPersistentPolicy, LocationPolicy,
CheckingPolicy>> : std::true_type {};
} // namespace internal
/**
* Persistent is a way to create a strong pointer from an off-heap object to
* another on-heap object. As long as the Persistent handle is alive the GC will
* keep the object pointed to alive. The Persistent handle is always a GC root
* from the point of view of the GC. Persistent must be constructed and
* destructed in the same thread.
*/
template <typename T>
using Persistent =
internal::BasicPersistent<T, internal::StrongPersistentPolicy>;
/**
* WeakPersistent is a way to create a weak pointer from an off-heap object to
* an on-heap object. The pointer is automatically cleared when the pointee gets
* collected. WeakPersistent must be constructed and destructed in the same
* thread.
*/
template <typename T>
using WeakPersistent =
internal::BasicPersistent<T, internal::WeakPersistentPolicy>;
} // namespace cppgc
#endif // INCLUDE_CPPGC_PERSISTENT_H_

View File

@ -0,0 +1,174 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_PLATFORM_H_
#define INCLUDE_CPPGC_PLATFORM_H_
#include <memory>
#include "cppgc/source-location.h"
#include "v8-platform.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
// TODO(v8:10346): Create separate includes for concepts that are not
// V8-specific.
using IdleTask = v8::IdleTask;
using JobHandle = v8::JobHandle;
using JobDelegate = v8::JobDelegate;
using JobTask = v8::JobTask;
using PageAllocator = v8::PageAllocator;
using Task = v8::Task;
using TaskPriority = v8::TaskPriority;
using TaskRunner = v8::TaskRunner;
using TracingController = v8::TracingController;
/**
* Platform interface used by Heap. Contains allocators and executors.
*/
class V8_EXPORT Platform {
public:
virtual ~Platform() = default;
/**
* \returns the allocator used by cppgc to allocate its heap and various
* support structures. Returning nullptr results in using the `PageAllocator`
* provided by `cppgc::InitializeProcess()` instead.
*/
virtual PageAllocator* GetPageAllocator() = 0;
/**
* Monotonically increasing time in seconds from an arbitrary fixed point in
* the past. This function is expected to return at least
* millisecond-precision values. For this reason,
* it is recommended that the fixed point be no further in the past than
* the epoch.
**/
virtual double MonotonicallyIncreasingTime() = 0;
/**
* Foreground task runner that should be used by a Heap.
*/
virtual std::shared_ptr<TaskRunner> GetForegroundTaskRunner() {
return GetForegroundTaskRunner(TaskPriority::kUserBlocking);
}
/**
* Returns a TaskRunner with a specific |priority| which can be used to post a
* task on the foreground thread.
*/
virtual std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
TaskPriority priority) {
return nullptr;
}
/**
* Posts `job_task` to run in parallel. Returns a `JobHandle` associated with
* the `Job`, which can be joined or canceled.
* This avoids degenerate cases:
* - Calling `CallOnWorkerThread()` for each work item, causing significant
* overhead.
* - Fixed number of `CallOnWorkerThread()` calls that split the work and
* might run for a long time. This is problematic when many components post
* "num cores" tasks and all expect to use all the cores. In these cases,
* the scheduler lacks context to be fair to multiple same-priority requests
* and/or ability to request lower priority work to yield when high priority
* work comes in.
* A canonical implementation of `job_task` looks like:
* \code
* class MyJobTask : public JobTask {
* public:
* MyJobTask(...) : worker_queue_(...) {}
* // JobTask implementation.
* void Run(JobDelegate* delegate) override {
* while (!delegate->ShouldYield()) {
* // Smallest unit of work.
* auto work_item = worker_queue_.TakeWorkItem(); // Thread safe.
* if (!work_item) return;
* ProcessWork(work_item);
* }
* }
*
* size_t GetMaxConcurrency() const override {
* return worker_queue_.GetSize(); // Thread safe.
* }
* };
*
* // ...
* auto handle = PostJob(TaskPriority::kUserVisible,
* std::make_unique<MyJobTask>(...));
* handle->Join();
* \endcode
*
* `PostJob()` and methods of the returned JobHandle/JobDelegate, must never
* be called while holding a lock that could be acquired by `JobTask::Run()`
* or `JobTask::GetMaxConcurrency()` -- that could result in a deadlock. This
* is because (1) `JobTask::GetMaxConcurrency()` may be invoked while holding
* internal lock (A), hence `JobTask::GetMaxConcurrency()` can only use a lock
* (B) if that lock is *never* held while calling back into `JobHandle` from
* any thread (A=>B/B=>A deadlock) and (2) `JobTask::Run()` or
* `JobTask::GetMaxConcurrency()` may be invoked synchronously from
* `JobHandle` (B=>JobHandle::foo=>B deadlock).
*
* A sufficient `PostJob()` implementation that uses the default Job provided
* in libplatform looks like:
* \code
* std::unique_ptr<JobHandle> PostJob(
* TaskPriority priority, std::unique_ptr<JobTask> job_task) override {
* return std::make_unique<DefaultJobHandle>(
* std::make_shared<DefaultJobState>(
* this, std::move(job_task), kNumThreads));
* }
* \endcode
*/
virtual std::unique_ptr<JobHandle> PostJob(
TaskPriority priority, std::unique_ptr<JobTask> job_task) {
return nullptr;
}
/**
* Returns an instance of a `TracingController`. This must be non-nullptr. The
* default implementation returns an empty `TracingController` that consumes
* trace data without effect.
*/
virtual TracingController* GetTracingController();
};
V8_EXPORT bool IsInitialized();
/**
* Process-global initialization of the garbage collector. Must be called before
* creating a Heap.
*
* Can be called multiple times when paired with `ShutdownProcess()`.
*
* \param page_allocator The allocator used for maintaining meta data. Must stay
* always alive and not change between multiple calls to InitializeProcess. If
* no allocator is provided, a default internal version will be used.
* \param desired_heap_size Desired amount of virtual address space to reserve
* for the heap, in bytes. Actual size will be clamped to minimum and maximum
* values based on compile-time settings and may be rounded up. If this
* parameter is zero, a default value will be used.
*/
V8_EXPORT void InitializeProcess(PageAllocator* page_allocator = nullptr,
size_t desired_heap_size = 0);
/**
* Must be called after destroying the last used heap. Some process-global
* metadata may not be returned and reused upon a subsequent
* `InitializeProcess()` call.
*/
V8_EXPORT void ShutdownProcess();
namespace internal {
V8_EXPORT void Fatal(const std::string& reason = std::string(),
SourceLocation = SourceLocation::Current());
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_PLATFORM_H_

View File

@ -0,0 +1,75 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_PREFINALIZER_H_
#define INCLUDE_CPPGC_PREFINALIZER_H_
#include "cppgc/internal/compiler-specific.h"
#include "cppgc/liveness-broker.h"
namespace cppgc {
namespace internal {
class V8_EXPORT PrefinalizerRegistration final {
public:
using Callback = bool (*)(const cppgc::LivenessBroker&, void*);
PrefinalizerRegistration(void*, Callback);
void* operator new(size_t, void* location) = delete;
void* operator new(size_t) = delete;
};
} // namespace internal
/**
* Macro must be used in the private section of `Class` and registers a
* prefinalization callback `void Class::PreFinalizer()`. The callback is
* invoked on garbage collection after the collector has found an object to be
* dead.
*
* Callback properties:
* - The callback is invoked before a possible destructor for the corresponding
* object.
* - The callback may access the whole object graph, irrespective of whether
* objects are considered dead or alive.
* - The callback is invoked on the same thread as the object was created on.
*
* Example:
* \code
* class WithPrefinalizer : public GarbageCollected<WithPrefinalizer> {
* CPPGC_USING_PRE_FINALIZER(WithPrefinalizer, Dispose);
*
* public:
* void Trace(Visitor*) const {}
* void Dispose() { prefinalizer_called = true; }
* ~WithPrefinalizer() {
* // prefinalizer_called == true
* }
* private:
* bool prefinalizer_called = false;
* };
* \endcode
*/
#define CPPGC_USING_PRE_FINALIZER(Class, PreFinalizer) \
public: \
static bool InvokePreFinalizer(const cppgc::LivenessBroker& liveness_broker, \
void* object) { \
static_assert(cppgc::IsGarbageCollectedOrMixinTypeV<Class>, \
"Only garbage collected objects can have prefinalizers"); \
Class* self = static_cast<Class*>(object); \
if (liveness_broker.IsHeapObjectAlive(self)) return false; \
self->PreFinalizer(); \
return true; \
} \
\
private: \
CPPGC_NO_UNIQUE_ADDRESS cppgc::internal::PrefinalizerRegistration \
prefinalizer_dummy_{this, Class::InvokePreFinalizer}; \
static_assert(true, "Force semicolon.")
} // namespace cppgc
#endif // INCLUDE_CPPGC_PREFINALIZER_H_

View File

@ -0,0 +1,36 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_PROCESS_HEAP_STATISTICS_H_
#define INCLUDE_CPPGC_PROCESS_HEAP_STATISTICS_H_
#include <atomic>
#include <cstddef>
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
class ProcessHeapStatisticsUpdater;
} // namespace internal
class V8_EXPORT ProcessHeapStatistics final {
public:
static size_t TotalAllocatedObjectSize() {
return total_allocated_object_size_.load(std::memory_order_relaxed);
}
static size_t TotalAllocatedSpace() {
return total_allocated_space_.load(std::memory_order_relaxed);
}
private:
static std::atomic_size_t total_allocated_space_;
static std::atomic_size_t total_allocated_object_size_;
friend class internal::ProcessHeapStatisticsUpdater;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_PROCESS_HEAP_STATISTICS_H_

View File

@ -0,0 +1,39 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_SENTINEL_POINTER_H_
#define INCLUDE_CPPGC_SENTINEL_POINTER_H_
#include <cstdint>
#include "cppgc/internal/api-constants.h"
namespace cppgc {
namespace internal {
// Special tag type used to denote some sentinel member. The semantics of the
// sentinel is defined by the embedder.
struct SentinelPointer {
#if defined(CPPGC_POINTER_COMPRESSION)
static constexpr intptr_t kSentinelValue =
1 << api_constants::kPointerCompressionShift;
#else // !defined(CPPGC_POINTER_COMPRESSION)
static constexpr intptr_t kSentinelValue = 0b10;
#endif // !defined(CPPGC_POINTER_COMPRESSION)
template <typename T>
operator T*() const {
return reinterpret_cast<T*>(kSentinelValue);
}
// Hidden friends.
friend bool operator==(SentinelPointer, SentinelPointer) { return true; }
friend bool operator!=(SentinelPointer, SentinelPointer) { return false; }
};
} // namespace internal
constexpr internal::SentinelPointer kSentinelPointer;
} // namespace cppgc
#endif // INCLUDE_CPPGC_SENTINEL_POINTER_H_

View File

@ -0,0 +1,16 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_SOURCE_LOCATION_H_
#define INCLUDE_CPPGC_SOURCE_LOCATION_H_
#include "v8-source-location.h"
namespace cppgc {
using SourceLocation = v8::SourceLocation;
} // namespace cppgc
#endif // INCLUDE_CPPGC_SOURCE_LOCATION_H_

View File

@ -0,0 +1,114 @@
// Copyright 2025 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_TAGGED_MEMBER_H_
#define INCLUDE_CPPGC_TAGGED_MEMBER_H_
#include <atomic>
#include <concepts>
#include <cstddef>
#include <type_traits>
#include "cppgc/internal/api-constants.h"
#include "cppgc/macros.h"
#include "cppgc/member.h"
#include "cppgc/visitor.h"
namespace cppgc::subtle {
// The class allows to store a Member along with a single bit tag. It uses
// distinct tag types, Tag1 and Tag2, to represent the two states of the tag.
// The tag is stored in the least significant bit of the pointer.
//
// Example usage:
// struct ParentTag {};
// struct ShadowHostTag {};
//
// /* Constructs a member with the pointer to parent tag: */
// TaggedUncompressedMember<Node, ParentTag, ShadowHostTag>
// m(ParentTag{}, parent);
template <typename Pointee, typename Tag1, typename Tag2>
class TaggedUncompressedMember final {
CPPGC_DISALLOW_NEW();
static constexpr uintptr_t kTagBit = 0b1;
static_assert(kTagBit < internal::api_constants::kAllocationGranularity,
"The tag must live in the alignment bits of the pointer.");
public:
TaggedUncompressedMember(Tag1, Pointee* ptr) : ptr_(ptr) {}
TaggedUncompressedMember(Tag2, Pointee* ptr)
: ptr_(reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(ptr) |
kTagBit)) {}
template <typename Tag>
Pointee* GetAs() const {
auto* raw = ptr_.Get();
if constexpr (std::same_as<Tag, Tag1>) {
CPPGC_DCHECK(Is<Tag1>());
return raw;
} else {
static_assert(std::same_as<Tag, Tag2>);
CPPGC_DCHECK(Is<Tag2>());
return GetUntagged();
}
}
template <typename Tag>
Pointee* TryGetAs() const {
auto* raw = ptr_.Get();
if constexpr (std::same_as<Tag, Tag1>) {
return (reinterpret_cast<uintptr_t>(raw) & kTagBit) ? nullptr : raw;
} else {
static_assert(std::same_as<Tag, Tag2>);
return (reinterpret_cast<uintptr_t>(raw) & kTagBit)
? reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(raw) &
~kTagBit)
: nullptr;
}
}
Pointee* GetUntagged() const {
return reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(ptr_.Get()) &
~kTagBit);
}
template <typename Tag>
void SetAs(Pointee* pointee) {
if constexpr (std::same_as<Tag, Tag1>) {
ptr_ = pointee;
} else {
static_assert(std::same_as<Tag, Tag2>);
ptr_ = reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(pointee) |
kTagBit);
}
}
template <typename Tag>
bool Is() const {
const bool tag_set = reinterpret_cast<uintptr_t>(ptr_.Get()) & kTagBit;
if constexpr (std::same_as<Tag, Tag1>) {
return !tag_set;
} else {
static_assert(std::same_as<Tag, Tag2>);
return tag_set;
}
}
void Trace(Visitor* v) const {
// Construct an untagged pointer and pass it to Visitor::Trace(). The plugin
// would warn that ptr_ is untraced, which is why CPPGC_PLUGIN_IGNORE is
// used.
auto* untagged = reinterpret_cast<Pointee*>(
reinterpret_cast<uintptr_t>(ptr_.GetRawAtomic()) & ~kTagBit);
UncompressedMember<Pointee> temp(untagged);
v->Trace(temp);
}
private:
CPPGC_PLUGIN_IGNORE("See Trace()") UncompressedMember<Pointee> ptr_;
};
} // namespace cppgc::subtle
#endif // INCLUDE_CPPGC_TAGGED_MEMBER_H_

View File

@ -0,0 +1,106 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_TESTING_H_
#define INCLUDE_CPPGC_TESTING_H_
#include "cppgc/common.h"
#include "cppgc/macros.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
class HeapHandle;
/**
* Namespace contains testing helpers.
*/
namespace testing {
/**
* Overrides the state of the stack with the provided value. Parameters passed
* to explicit garbage collection calls still take precedence. Must not be
* nested.
*
* This scope is useful to make the garbage collector consider the stack when
* tasks that invoke garbage collection (through the provided platform) contain
* interesting pointers on its stack.
*/
class V8_EXPORT V8_NODISCARD OverrideEmbedderStackStateScope final {
CPPGC_STACK_ALLOCATED();
public:
/**
* Constructs a scoped object that automatically enters and leaves the scope.
*
* \param heap_handle The corresponding heap.
*/
explicit OverrideEmbedderStackStateScope(HeapHandle& heap_handle,
EmbedderStackState state);
~OverrideEmbedderStackStateScope();
OverrideEmbedderStackStateScope(const OverrideEmbedderStackStateScope&) =
delete;
OverrideEmbedderStackStateScope& operator=(
const OverrideEmbedderStackStateScope&) = delete;
private:
HeapHandle& heap_handle_;
};
/**
* Testing interface for managed heaps that allows for controlling garbage
* collection timings. Embedders should use this class when testing the
* interaction of their code with incremental/concurrent garbage collection.
*/
class V8_EXPORT StandaloneTestingHeap final {
public:
explicit StandaloneTestingHeap(HeapHandle&);
/**
* Start an incremental garbage collection.
*/
void StartGarbageCollection();
/**
* Perform an incremental step. This will also schedule concurrent steps if
* needed.
*
* \param stack_state The state of the stack during the step.
*/
bool PerformMarkingStep(EmbedderStackState stack_state);
/**
* Finalize the current garbage collection cycle atomically.
* Assumes that garbage collection is in progress.
*
* \param stack_state The state of the stack for finalizing the garbage
* collection cycle.
*/
void FinalizeGarbageCollection(EmbedderStackState stack_state);
/**
* Toggle main thread marking on/off. Allows to stress concurrent marking
* (e.g. to better detect data races).
*
* \param should_mark Denotes whether the main thread should contribute to
* marking. Defaults to true.
*/
void ToggleMainThreadMarking(bool should_mark);
/**
* Force enable compaction for the next garbage collection cycle.
*/
void ForceCompactionForNextGarbageCollection();
private:
HeapHandle& heap_handle_;
};
V8_EXPORT bool IsHeapObjectOld(void*);
} // namespace testing
} // namespace cppgc
#endif // INCLUDE_CPPGC_TESTING_H_

View File

@ -0,0 +1,127 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_TRACE_TRAIT_H_
#define INCLUDE_CPPGC_TRACE_TRAIT_H_
#include <type_traits>
#include "cppgc/type-traits.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
class Visitor;
namespace internal {
class RootVisitor;
using TraceRootCallback = void (*)(RootVisitor&, const void* object);
// Implementation of the default TraceTrait handling GarbageCollected and
// GarbageCollectedMixin.
template <typename T,
bool = IsGarbageCollectedMixinTypeV<std::remove_const_t<T>>>
struct TraceTraitImpl;
} // namespace internal
/**
* Callback for invoking tracing on a given object.
*
* \param visitor The visitor to dispatch to.
* \param object The object to invoke tracing on.
*/
using TraceCallback = void (*)(Visitor* visitor, const void* object);
/**
* Describes how to trace an object, i.e., how to visit all Oilpan-relevant
* fields of an object.
*/
struct TraceDescriptor {
/**
* Adjusted base pointer, i.e., the pointer to the class inheriting directly
* from GarbageCollected, of the object that is being traced.
*/
const void* base_object_payload;
/**
* Callback for tracing the object.
*/
TraceCallback callback;
};
/**
* Callback for getting a TraceDescriptor for a given address.
*
* \param address Possibly inner address of an object.
* \returns a TraceDescriptor for the provided address.
*/
using TraceDescriptorCallback = TraceDescriptor (*)(const void* address);
namespace internal {
struct V8_EXPORT TraceTraitFromInnerAddressImpl {
static TraceDescriptor GetTraceDescriptor(const void* address);
};
/**
* Trait specifying how the garbage collector processes an object of type T.
*
* Advanced users may override handling by creating a specialization for their
* type.
*/
template <typename T>
struct TraceTraitBase {
static_assert(internal::IsTraceableV<T>, "T must have a Trace() method");
/**
* Accessor for retrieving a TraceDescriptor to process an object of type T.
*
* \param self The object to be processed.
* \returns a TraceDescriptor to process the object.
*/
static TraceDescriptor GetTraceDescriptor(const void* self) {
return internal::TraceTraitImpl<T>::GetTraceDescriptor(
static_cast<const T*>(self));
}
/**
* Function invoking the tracing for an object of type T.
*
* \param visitor The visitor to dispatch to.
* \param self The object to invoke tracing on.
*/
static void Trace(Visitor* visitor, const void* self) {
static_cast<const T*>(self)->Trace(visitor);
}
};
} // namespace internal
template <typename T>
struct TraceTrait : public internal::TraceTraitBase<T> {};
namespace internal {
template <typename T>
struct TraceTraitImpl<T, false> {
static_assert(IsGarbageCollectedTypeV<T>,
"T must be of type GarbageCollected or GarbageCollectedMixin");
static TraceDescriptor GetTraceDescriptor(const void* self) {
return {self, TraceTrait<T>::Trace};
}
};
template <typename T>
struct TraceTraitImpl<T, true> {
static TraceDescriptor GetTraceDescriptor(const void* self) {
return internal::TraceTraitFromInnerAddressImpl::GetTraceDescriptor(self);
}
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_TRACE_TRAIT_H_

View File

@ -0,0 +1,272 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_TYPE_TRAITS_H_
#define INCLUDE_CPPGC_TYPE_TRAITS_H_
// This file should stay with minimal dependencies to allow embedder to check
// against Oilpan types without including any other parts.
#include <cstddef>
#include <type_traits>
#include <utility>
namespace cppgc {
class Visitor;
namespace internal {
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
class BasicMember;
struct DijkstraWriteBarrierPolicy;
struct NoWriteBarrierPolicy;
class StrongMemberTag;
class UntracedMemberTag;
class WeakMemberTag;
// Not supposed to be specialized by the user.
template <typename T>
struct IsWeak : std::false_type {};
// IsTraceMethodConst is used to verify that all Trace methods are marked as
// const. It is equivalent to IsTraceable but for a non-const object.
template <typename T, typename = void>
struct IsTraceMethodConst : std::false_type {};
template <typename T>
struct IsTraceMethodConst<T, std::void_t<decltype(std::declval<const T>().Trace(
std::declval<Visitor*>()))>> : std::true_type {
};
template <typename T, typename = void>
struct IsTraceable : std::false_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct IsTraceable<
T, std::void_t<decltype(std::declval<T>().Trace(std::declval<Visitor*>()))>>
: std::true_type {
// All Trace methods should be marked as const. If an object of type
// 'T' is traceable then any object of type 'const T' should also
// be traceable.
static_assert(IsTraceMethodConst<T>(),
"Trace methods should be marked as const.");
};
template <typename T>
constexpr bool IsTraceableV = IsTraceable<T>::value;
template <typename T, typename = void>
struct HasGarbageCollectedMixinTypeMarker : std::false_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct HasGarbageCollectedMixinTypeMarker<
T, std::void_t<
typename std::remove_const_t<T>::IsGarbageCollectedMixinTypeMarker>>
: std::true_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T, typename = void>
struct HasGarbageCollectedTypeMarker : std::false_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct HasGarbageCollectedTypeMarker<
T,
std::void_t<typename std::remove_const_t<T>::IsGarbageCollectedTypeMarker>>
: std::true_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value,
bool = HasGarbageCollectedMixinTypeMarker<T>::value>
struct IsGarbageCollectedMixinType : std::false_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct IsGarbageCollectedMixinType<T, false, true> : std::true_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value>
struct IsGarbageCollectedType : std::false_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct IsGarbageCollectedType<T, true> : std::true_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct IsGarbageCollectedOrMixinType
: std::integral_constant<bool, IsGarbageCollectedType<T>::value ||
IsGarbageCollectedMixinType<T>::value> {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T, bool = (HasGarbageCollectedTypeMarker<T>::value &&
HasGarbageCollectedMixinTypeMarker<T>::value)>
struct IsGarbageCollectedWithMixinType : std::false_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename T>
struct IsGarbageCollectedWithMixinType<T, true> : std::true_type {
static_assert(sizeof(T), "T must be fully defined");
};
template <typename BasicMemberCandidate, typename WeaknessTag,
typename WriteBarrierPolicy>
struct IsSubclassOfBasicMemberTemplate {
private:
template <typename T, typename CheckingPolicy, typename StorageType>
static std::true_type SubclassCheck(
const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
StorageType>*);
static std::false_type SubclassCheck(...);
public:
static constexpr bool value = decltype(SubclassCheck(
std::declval<std::decay_t<BasicMemberCandidate>*>()))::value;
};
template <typename T,
bool = IsSubclassOfBasicMemberTemplate<
T, StrongMemberTag, DijkstraWriteBarrierPolicy>::value>
struct IsMemberType : std::false_type {};
template <typename T>
struct IsMemberType<T, true> : std::true_type {};
template <typename T, bool = IsSubclassOfBasicMemberTemplate<
T, WeakMemberTag, DijkstraWriteBarrierPolicy>::value>
struct IsWeakMemberType : std::false_type {};
template <typename T>
struct IsWeakMemberType<T, true> : std::true_type {};
template <typename T, bool = IsSubclassOfBasicMemberTemplate<
T, UntracedMemberTag, NoWriteBarrierPolicy>::value>
struct IsUntracedMemberType : std::false_type {};
template <typename T>
struct IsUntracedMemberType<T, true> : std::true_type {};
template <typename T>
struct IsComplete {
private:
template <typename U, size_t = sizeof(U)>
static std::true_type IsSizeOfKnown(U*);
static std::false_type IsSizeOfKnown(...);
public:
static constexpr bool value =
decltype(IsSizeOfKnown(std::declval<T*>()))::value;
};
template <typename T, typename U>
constexpr bool IsDecayedSameV =
std::is_same_v<std::decay_t<T>, std::decay_t<U>>;
template <typename B, typename D>
constexpr bool IsStrictlyBaseOfV =
std::is_base_of_v<std::decay_t<B>, std::decay_t<D>> &&
!IsDecayedSameV<B, D>;
template <typename T>
constexpr bool IsAnyMemberTypeV = false;
template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
typename CheckingPolicy, typename StorageType>
constexpr bool IsAnyMemberTypeV<internal::BasicMember<
T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy, StorageType>> = true;
} // namespace internal
/**
* Value is true for types that inherit from `GarbageCollectedMixin` but not
* `GarbageCollected<T>` (i.e., they are free mixins), and false otherwise.
*/
template <typename T>
constexpr bool IsGarbageCollectedMixinTypeV =
internal::IsGarbageCollectedMixinType<T>::value;
/**
* Value is true for types that inherit from `GarbageCollected<T>`, and false
* otherwise.
*/
template <typename T>
constexpr bool IsGarbageCollectedTypeV =
internal::IsGarbageCollectedType<T>::value;
/**
* Value is true for types that inherit from either `GarbageCollected<T>` or
* `GarbageCollectedMixin`, and false otherwise.
*/
template <typename T>
constexpr bool IsGarbageCollectedOrMixinTypeV =
internal::IsGarbageCollectedOrMixinType<T>::value;
/**
* Value is true for types that inherit from `GarbageCollected<T>` and
* `GarbageCollectedMixin`, and false otherwise.
*/
template <typename T>
constexpr bool IsGarbageCollectedWithMixinTypeV =
internal::IsGarbageCollectedWithMixinType<T>::value;
/**
* Value is true for types of type `Member<T>`, and false otherwise.
*/
template <typename T>
constexpr bool IsMemberTypeV = internal::IsMemberType<T>::value;
/**
* Value is true for types of type `UntracedMember<T>`, and false otherwise.
*/
template <typename T>
constexpr bool IsUntracedMemberTypeV = internal::IsUntracedMemberType<T>::value;
/**
* Value is true for types of type `WeakMember<T>`, and false otherwise.
*/
template <typename T>
constexpr bool IsWeakMemberTypeV = internal::IsWeakMemberType<T>::value;
/**
* Value is true for types that are considered weak references, and false
* otherwise.
*/
template <typename T>
constexpr bool IsWeakV = internal::IsWeak<T>::value;
/**
* Value is true for types that are complete, and false otherwise.
*/
template <typename T>
constexpr bool IsCompleteV = internal::IsComplete<T>::value;
/**
* Value is true for member types `Member<T>` and `WeakMember<T>`.
*/
template <typename T>
constexpr bool IsMemberOrWeakMemberTypeV =
IsMemberTypeV<T> || IsWeakMemberTypeV<T>;
/**
* Value is true for any member type.
*/
template <typename T>
constexpr bool IsAnyMemberTypeV = internal::IsAnyMemberTypeV<std::decay_t<T>>;
} // namespace cppgc
#endif // INCLUDE_CPPGC_TYPE_TRAITS_H_

View File

@ -0,0 +1,550 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_VISITOR_H_
#define INCLUDE_CPPGC_VISITOR_H_
#include <type_traits>
#include "cppgc/custom-space.h"
#include "cppgc/garbage-collected.h"
#include "cppgc/internal/logging.h"
#include "cppgc/internal/member-storage.h"
#include "cppgc/internal/pointer-policies.h"
#include "cppgc/liveness-broker.h"
#include "cppgc/macros.h"
#include "cppgc/member.h"
#include "cppgc/sentinel-pointer.h"
#include "cppgc/source-location.h"
#include "cppgc/trace-trait.h"
#include "cppgc/type-traits.h"
namespace cppgc {
namespace internal {
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
class BasicCrossThreadPersistent;
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
class BasicPersistent;
class ConservativeTracingVisitor;
class VisitorBase;
class VisitorFactory;
} // namespace internal
using WeakCallback = void (*)(const LivenessBroker&, const void*);
/**
* An ephemeron pair is used to conditionally retain an object.
* The `value` will be kept alive only if the `key` is alive.
*/
template <typename K, typename V>
struct EphemeronPair {
CPPGC_DISALLOW_NEW();
EphemeronPair(K* k, V* v) : key(k), value(v) {}
WeakMember<K> key;
Member<V> value;
void ClearKeyAndValueIfKeyIsDead(const LivenessBroker& broker) {
if (!broker.IsHeapObjectAlive(key)) {
key = nullptr;
value = nullptr;
}
}
void Trace(Visitor* visitor) const;
};
/**
* Visitor passed to trace methods. All managed pointers must have called the
* Visitor's trace method on them.
*
* \code
* class Foo final : public GarbageCollected<Foo> {
* public:
* void Trace(Visitor* visitor) const {
* visitor->Trace(foo_);
* visitor->Trace(weak_foo_);
* }
* private:
* Member<Foo> foo_;
* WeakMember<Foo> weak_foo_;
* };
* \endcode
*/
class V8_EXPORT Visitor {
public:
class Key {
private:
Key() = default;
friend class internal::VisitorFactory;
};
explicit Visitor(Key) {}
virtual ~Visitor() = default;
/**
* Trace method for Member.
*
* \param member Member reference retaining an object.
*/
template <typename T>
void Trace(const Member<T>& member) {
const T* value = member.GetAtomic();
CPPGC_DCHECK(value != kSentinelPointer);
TraceImpl(value);
}
/**
* Trace method for WeakMember.
*
* \param weak_member WeakMember reference weakly retaining an object.
*/
template <typename T>
void Trace(const WeakMember<T>& weak_member) {
static_assert(sizeof(T), "Pointee type must be fully defined.");
static_assert(internal::IsGarbageCollectedOrMixinType<T>::value,
"T must be GarbageCollected or GarbageCollectedMixin type");
static_assert(!internal::IsAllocatedOnCompactableSpace<T>::value,
"Weak references to compactable objects are not allowed");
const T* value = weak_member.GetAtomic();
// Bailout assumes that WeakMember emits write barrier.
if (!value) {
return;
}
CPPGC_DCHECK(value != kSentinelPointer);
VisitWeak(value, TraceTrait<T>::GetTraceDescriptor(value),
&HandleWeak<WeakMember<T>>, &weak_member);
}
#if defined(CPPGC_POINTER_COMPRESSION)
/**
* Trace method for UncompressedMember.
*
* \param member UncompressedMember reference retaining an object.
*/
template <typename T>
void Trace(const subtle::UncompressedMember<T>& member) {
const T* value = member.GetAtomic();
CPPGC_DCHECK(value != kSentinelPointer);
TraceImpl(value);
}
#endif // defined(CPPGC_POINTER_COMPRESSION)
template <typename T>
void TraceMultiple(const subtle::UncompressedMember<T>* start, size_t len) {
static_assert(sizeof(T), "Pointee type must be fully defined.");
static_assert(internal::IsGarbageCollectedOrMixinType<T>::value,
"T must be GarbageCollected or GarbageCollectedMixin type");
VisitMultipleUncompressedMember(start, len,
&TraceTrait<T>::GetTraceDescriptor);
}
template <typename T,
std::enable_if_t<!std::is_same_v<
Member<T>, subtle::UncompressedMember<T>>>* = nullptr>
void TraceMultiple(const Member<T>* start, size_t len) {
static_assert(sizeof(T), "Pointee type must be fully defined.");
static_assert(internal::IsGarbageCollectedOrMixinType<T>::value,
"T must be GarbageCollected or GarbageCollectedMixin type");
#if defined(CPPGC_POINTER_COMPRESSION)
static_assert(std::is_same_v<Member<T>, subtle::CompressedMember<T>>,
"Member and CompressedMember must be the same.");
VisitMultipleCompressedMember(start, len,
&TraceTrait<T>::GetTraceDescriptor);
#endif // defined(CPPGC_POINTER_COMPRESSION)
}
/**
* Trace method for inlined objects that are not allocated themselves but
* otherwise follow managed heap layout and have a Trace() method.
*
* \param object reference of the inlined object.
*/
template <typename T>
void Trace(const T& object) {
static_assert(!IsGarbageCollectedOrMixinTypeV<T>);
#if V8_ENABLE_CHECKS
// This object is embedded in potentially multiple nested objects. The
// outermost object must not be in construction as such objects are (a) not
// processed immediately, and (b) only processed conservatively if not
// otherwise possible.
CheckObjectNotInConstruction(&object);
#endif // V8_ENABLE_CHECKS
TraceTrait<T>::Trace(this, &object);
}
template <typename T>
void TraceMultiple(const T* start, size_t len) {
#if V8_ENABLE_CHECKS
// This object is embedded in potentially multiple nested objects. The
// outermost object must not be in construction as such objects are (a) not
// processed immediately, and (b) only processed conservatively if not
// otherwise possible.
CheckObjectNotInConstruction(start);
#endif // V8_ENABLE_CHECKS
for (size_t i = 0; i < len; ++i) {
const T* object = &start[i];
if constexpr (std::is_polymorphic_v<T>) {
// The object's vtable may be uninitialized in which case the object is
// not traced.
if (*reinterpret_cast<const uintptr_t*>(object) == 0) continue;
}
TraceTrait<T>::Trace(this, object);
}
}
/**
* Registers a weak callback method on the object of type T. See
* LivenessBroker for an usage example.
*
* \param object of type T specifying a weak callback method.
*/
template <typename T, void (T::*method)(const LivenessBroker&)>
void RegisterWeakCallbackMethod(const T* object) {
RegisterWeakCallback(&WeakCallbackMethodDelegate<T, method>, object);
}
/**
* Trace method for EphemeronPair.
*
* \param ephemeron_pair EphemeronPair reference weakly retaining a key object
* and strongly retaining a value object in case the key object is alive.
*/
template <typename K, typename V>
void Trace(const EphemeronPair<K, V>& ephemeron_pair) {
TraceEphemeron(ephemeron_pair.key, &ephemeron_pair.value);
RegisterWeakCallbackMethod<
EphemeronPair<K, V>, &EphemeronPair<K, V>::ClearKeyAndValueIfKeyIsDead>(
&ephemeron_pair);
}
/**
* Trace method for a single ephemeron. Used for tracing a raw ephemeron in
* which the `key` and `value` are kept separately.
*
* \param weak_member_key WeakMember reference weakly retaining a key object.
* \param member_value Member reference with ephemeron semantics.
*/
template <typename KeyType, typename ValueType>
void TraceEphemeron(const WeakMember<KeyType>& weak_member_key,
const Member<ValueType>* member_value) {
const KeyType* key = weak_member_key.GetAtomic();
if (!key) return;
// `value` must always be non-null.
CPPGC_DCHECK(member_value);
const ValueType* value = member_value->GetAtomic();
if (!value) return;
// KeyType and ValueType may refer to GarbageCollectedMixin.
TraceDescriptor value_desc =
TraceTrait<ValueType>::GetTraceDescriptor(value);
CPPGC_DCHECK(value_desc.base_object_payload);
const void* key_base_object_payload =
TraceTrait<KeyType>::GetTraceDescriptor(key).base_object_payload;
CPPGC_DCHECK(key_base_object_payload);
VisitEphemeron(key_base_object_payload, value, value_desc);
}
/**
* Trace method for a single ephemeron. Used for tracing a raw ephemeron in
* which the `key` and `value` are kept separately. Note that this overload
* is for non-GarbageCollected `value`s that can be traced though.
*
* \param key `WeakMember` reference weakly retaining a key object.
* \param value Reference weakly retaining a value object. Note that
* `ValueType` here should not be `Member`. It is expected that
* `TraceTrait<ValueType>::GetTraceDescriptor(value)` returns a
* `TraceDescriptor` with a null base pointer but a valid trace method.
*/
template <typename KeyType, typename ValueType>
void TraceEphemeron(const WeakMember<KeyType>& weak_member_key,
const ValueType* value) {
static_assert(!IsGarbageCollectedOrMixinTypeV<ValueType>,
"garbage-collected types must use WeakMember and Member");
const KeyType* key = weak_member_key.GetAtomic();
if (!key) return;
// `value` must always be non-null.
CPPGC_DCHECK(value);
TraceDescriptor value_desc =
TraceTrait<ValueType>::GetTraceDescriptor(value);
// `value_desc.base_object_payload` must be null as this override is only
// taken for non-garbage-collected values.
CPPGC_DCHECK(!value_desc.base_object_payload);
// KeyType might be a GarbageCollectedMixin.
const void* key_base_object_payload =
TraceTrait<KeyType>::GetTraceDescriptor(key).base_object_payload;
CPPGC_DCHECK(key_base_object_payload);
VisitEphemeron(key_base_object_payload, value, value_desc);
}
/**
* Trace method that strongifies a WeakMember.
*
* \param weak_member WeakMember reference retaining an object.
*/
template <typename T>
void TraceStrongly(const WeakMember<T>& weak_member) {
const T* value = weak_member.GetAtomic();
CPPGC_DCHECK(value != kSentinelPointer);
TraceImpl(value);
}
/**
* Trace method for retaining containers strongly.
*
* \param object reference to the container.
*/
template <typename T>
void TraceStrongContainer(const T* object) {
TraceImpl(object);
}
/**
* Trace method for retaining containers weakly. Note that weak containers
* should emit write barriers.
*
* \param object reference to the container.
* \param callback to be invoked.
* \param callback_data custom data that is passed to the callback.
*/
template <typename T>
void TraceWeakContainer(const T* object, WeakCallback callback,
const void* callback_data) {
if (!object) return;
VisitWeakContainer(object, TraceTrait<T>::GetTraceDescriptor(object),
TraceTrait<T>::GetWeakTraceDescriptor(object), callback,
callback_data);
}
/**
* Registers a slot containing a reference to an object allocated on a
* compactable space. Such references maybe be arbitrarily moved by the GC.
*
* \param slot location of reference to object that might be moved by the GC.
* The slot must contain an uncompressed pointer.
*/
template <typename T>
void RegisterMovableReference(const T** slot) {
static_assert(internal::IsAllocatedOnCompactableSpace<T>::value,
"Only references to objects allocated on compactable spaces "
"should be registered as movable slots.");
static_assert(!IsGarbageCollectedMixinTypeV<T>,
"Mixin types do not support compaction.");
HandleMovableReference(reinterpret_cast<const void**>(slot));
}
/**
* Registers a weak callback that is invoked during garbage collection.
*
* \param callback to be invoked.
* \param data custom data that is passed to the callback.
*/
virtual void RegisterWeakCallback(WeakCallback callback, const void* data) {}
/**
* Defers tracing an object from a concurrent thread to the mutator thread.
* Should be called by Trace methods of types that are not safe to trace
* concurrently.
*
* \param parameter tells the trace callback which object was deferred.
* \param callback to be invoked for tracing on the mutator thread.
* \param deferred_size size of deferred object.
*
* \returns false if the object does not need to be deferred (i.e. currently
* traced on the mutator thread) and true otherwise (i.e. currently traced on
* a concurrent thread).
*/
virtual V8_WARN_UNUSED_RESULT bool DeferTraceToMutatorThreadIfConcurrent(
const void* parameter, TraceCallback callback, size_t deferred_size) {
// By default tracing is not deferred.
return false;
}
/**
* Checks whether the visitor is running concurrently to the mutator or not.
*/
virtual bool IsConcurrent() const { return false; }
protected:
virtual void Visit(const void* self, TraceDescriptor) {}
virtual void VisitWeak(const void* self, TraceDescriptor, WeakCallback,
const void* weak_member) {}
virtual void VisitEphemeron(const void* key, const void* value,
TraceDescriptor value_desc) {}
virtual void VisitWeakContainer(const void* self, TraceDescriptor strong_desc,
TraceDescriptor weak_desc,
WeakCallback callback, const void* data) {}
virtual void HandleMovableReference(const void**) {}
virtual void VisitMultipleUncompressedMember(
const void* start, size_t len,
TraceDescriptorCallback get_trace_descriptor) {
// Default implementation merely delegates to Visit().
const char* it = static_cast<const char*>(start);
const char* end = it + len * internal::kSizeOfUncompressedMember;
for (; it < end; it += internal::kSizeOfUncompressedMember) {
const auto* current = reinterpret_cast<const internal::RawPointer*>(it);
const void* object = current->LoadAtomic();
if (!object) continue;
Visit(object, get_trace_descriptor(object));
}
}
#if defined(CPPGC_POINTER_COMPRESSION)
virtual void VisitMultipleCompressedMember(
const void* start, size_t len,
TraceDescriptorCallback get_trace_descriptor) {
// Default implementation merely delegates to Visit().
const char* it = static_cast<const char*>(start);
const char* end = it + len * internal::kSizeofCompressedMember;
for (; it < end; it += internal::kSizeofCompressedMember) {
const auto* current =
reinterpret_cast<const internal::CompressedPointer*>(it);
const void* object = current->LoadAtomic();
if (!object) continue;
Visit(object, get_trace_descriptor(object));
}
}
#endif // defined(CPPGC_POINTER_COMPRESSION)
private:
template <typename T, void (T::*method)(const LivenessBroker&)>
static void WeakCallbackMethodDelegate(const LivenessBroker& info,
const void* self) {
// Callback is registered through a potential const Trace method but needs
// to be able to modify fields. See HandleWeak.
(const_cast<T*>(static_cast<const T*>(self))->*method)(info);
}
template <typename PointerType>
static void HandleWeak(const LivenessBroker& info, const void* object) {
const PointerType* weak = static_cast<const PointerType*>(object);
if (!info.IsHeapObjectAlive(weak->GetFromGC())) {
weak->ClearFromGC();
}
}
template <typename T>
void TraceImpl(const T* t) {
static_assert(sizeof(T), "Pointee type must be fully defined.");
static_assert(internal::IsGarbageCollectedOrMixinType<T>::value,
"T must be GarbageCollected or GarbageCollectedMixin type");
if (!t) {
return;
}
Visit(t, TraceTrait<T>::GetTraceDescriptor(t));
}
#if V8_ENABLE_CHECKS
void CheckObjectNotInConstruction(const void* address);
#endif // V8_ENABLE_CHECKS
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
friend class internal::BasicCrossThreadPersistent;
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
friend class internal::BasicPersistent;
friend class internal::ConservativeTracingVisitor;
friend class internal::VisitorBase;
};
template <typename K, typename V>
void EphemeronPair<K, V>::Trace(Visitor* visitor) const {
visitor->TraceEphemeron(key, value);
}
namespace internal {
class V8_EXPORT RootVisitor {
public:
explicit RootVisitor(Visitor::Key) {}
virtual ~RootVisitor() = default;
template <typename AnyStrongPersistentType,
std::enable_if_t<
AnyStrongPersistentType::IsStrongPersistent::value>* = nullptr>
void Trace(const AnyStrongPersistentType& p) {
using PointeeType = typename AnyStrongPersistentType::PointeeType;
const void* object = Extract(p);
if (!object) {
return;
}
VisitRoot(object, TraceTrait<PointeeType>::GetTraceDescriptor(object),
ExtractLocation(p));
}
template <typename AnyWeakPersistentType,
std::enable_if_t<
!AnyWeakPersistentType::IsStrongPersistent::value>* = nullptr>
void Trace(const AnyWeakPersistentType& p) {
using PointeeType = typename AnyWeakPersistentType::PointeeType;
static_assert(!internal::IsAllocatedOnCompactableSpace<PointeeType>::value,
"Weak references to compactable objects are not allowed");
const void* object = Extract(p);
if (!object) {
return;
}
VisitWeakRoot(object, TraceTrait<PointeeType>::GetTraceDescriptor(object),
&HandleWeak<AnyWeakPersistentType>, &p, ExtractLocation(p));
}
protected:
virtual void VisitRoot(const void*, TraceDescriptor, SourceLocation) {}
virtual void VisitWeakRoot(const void* self, TraceDescriptor, WeakCallback,
const void* weak_root, SourceLocation) {}
private:
template <typename AnyPersistentType>
static const void* Extract(AnyPersistentType& p) {
using PointeeType = typename AnyPersistentType::PointeeType;
static_assert(sizeof(PointeeType),
"Persistent's pointee type must be fully defined");
static_assert(internal::IsGarbageCollectedOrMixinType<PointeeType>::value,
"Persistent's pointee type must be GarbageCollected or "
"GarbageCollectedMixin");
return p.GetFromGC();
}
template <typename AnyPersistentType>
static SourceLocation ExtractLocation(AnyPersistentType& p) {
return p.Location();
}
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
typename CheckingPolicy>
static SourceLocation ExtractLocation(
const internal::BasicCrossThreadPersistent<
T, WeaknessPolicy, LocationPolicy, CheckingPolicy>& p) {
return p.LocationFromGC();
}
template <typename PointerType>
static void HandleWeak(const LivenessBroker& info, const void* object) {
const PointerType* weak = static_cast<const PointerType*>(object);
if (!info.IsHeapObjectAlive(weak->GetFromGC())) {
weak->ClearFromGC();
}
}
};
} // namespace internal
} // namespace cppgc
#endif // INCLUDE_CPPGC_VISITOR_H_

View File

@ -0,0 +1,60 @@
// This file is generated by Exported_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef v8_inspector_protocol_Debugger_api_h
#define v8_inspector_protocol_Debugger_api_h
#include "v8-inspector.h"
namespace v8_inspector {
namespace protocol {
#ifndef v8_inspector_protocol_exported_api_h
#define v8_inspector_protocol_exported_api_h
class V8_EXPORT Exported {
public:
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
virtual ~Exported() { }
};
#endif // !defined(v8_inspector_protocol_exported_api_h)
namespace Debugger {
namespace API {
// ------------- Enums.
namespace Paused {
namespace ReasonEnum {
V8_EXPORT extern const char* Ambiguous;
V8_EXPORT extern const char* Assert;
V8_EXPORT extern const char* CSPViolation;
V8_EXPORT extern const char* DebugCommand;
V8_EXPORT extern const char* DOM;
V8_EXPORT extern const char* EventListener;
V8_EXPORT extern const char* Exception;
V8_EXPORT extern const char* Instrumentation;
V8_EXPORT extern const char* OOM;
V8_EXPORT extern const char* Other;
V8_EXPORT extern const char* PromiseRejection;
V8_EXPORT extern const char* XHR;
V8_EXPORT extern const char* Step;
} // ReasonEnum
} // Paused
// ------------- Types.
class V8_EXPORT SearchMatch : public Exported {
public:
static std::unique_ptr<protocol::Debugger::API::SearchMatch> fromBinary(const uint8_t* data, size_t length);
};
} // namespace API
} // namespace Debugger
} // namespace v8_inspector
} // namespace protocol
#endif // !defined(v8_inspector_protocol_Debugger_api_h)

View File

@ -0,0 +1,52 @@
// This file is generated by Exported_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef v8_inspector_protocol_Runtime_api_h
#define v8_inspector_protocol_Runtime_api_h
#include "v8-inspector.h"
namespace v8_inspector {
namespace protocol {
#ifndef v8_inspector_protocol_exported_api_h
#define v8_inspector_protocol_exported_api_h
class V8_EXPORT Exported {
public:
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
virtual ~Exported() { }
};
#endif // !defined(v8_inspector_protocol_exported_api_h)
namespace Runtime {
namespace API {
// ------------- Enums.
// ------------- Types.
class V8_EXPORT RemoteObject : public Exported {
public:
static std::unique_ptr<protocol::Runtime::API::RemoteObject> fromBinary(const uint8_t* data, size_t length);
};
class V8_EXPORT StackTrace : public Exported {
public:
static std::unique_ptr<protocol::Runtime::API::StackTrace> fromBinary(const uint8_t* data, size_t length);
};
class V8_EXPORT StackTraceId : public Exported {
public:
static std::unique_ptr<protocol::Runtime::API::StackTraceId> fromBinary(const uint8_t* data, size_t length);
};
} // namespace API
} // namespace Runtime
} // namespace v8_inspector
} // namespace protocol
#endif // !defined(v8_inspector_protocol_Runtime_api_h)

View File

@ -0,0 +1,42 @@
// This file is generated by Exported_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef v8_inspector_protocol_Schema_api_h
#define v8_inspector_protocol_Schema_api_h
#include "v8-inspector.h"
namespace v8_inspector {
namespace protocol {
#ifndef v8_inspector_protocol_exported_api_h
#define v8_inspector_protocol_exported_api_h
class V8_EXPORT Exported {
public:
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
virtual ~Exported() { }
};
#endif // !defined(v8_inspector_protocol_exported_api_h)
namespace Schema {
namespace API {
// ------------- Enums.
// ------------- Types.
class V8_EXPORT Domain : public Exported {
public:
static std::unique_ptr<protocol::Schema::API::Domain> fromBinary(const uint8_t* data, size_t length);
};
} // namespace API
} // namespace Schema
} // namespace v8_inspector
} // namespace protocol
#endif // !defined(v8_inspector_protocol_Schema_api_h)

View File

@ -0,0 +1,997 @@
{
"version": { "major": "1", "minor": "2" },
"domains": [
{
"domain": "Schema",
"description": "Provides information about the protocol schema.",
"types": [
{
"id": "Domain",
"type": "object",
"description": "Description of the protocol domain.",
"exported": true,
"properties": [
{ "name": "name", "type": "string", "description": "Domain name." },
{ "name": "version", "type": "string", "description": "Domain version." }
]
}
],
"commands": [
{
"name": "getDomains",
"description": "Returns supported domains.",
"handlers": ["browser", "renderer"],
"returns": [
{ "name": "domains", "type": "array", "items": { "$ref": "Domain" }, "description": "List of supported domains." }
]
}
]
},
{
"domain": "Runtime",
"description": "Runtime domain exposes JavaScript runtime by means of remote evaluation and mirror objects. Evaluation results are returned as mirror object that expose object type, string representation and unique identifier that can be used for further object reference. Original objects are maintained in memory unless they are either explicitly released or are released along with the other objects in their object group.",
"types": [
{
"id": "ScriptId",
"type": "string",
"description": "Unique script identifier."
},
{
"id": "RemoteObjectId",
"type": "string",
"description": "Unique object identifier."
},
{
"id": "UnserializableValue",
"type": "string",
"enum": ["Infinity", "NaN", "-Infinity", "-0"],
"description": "Primitive value which cannot be JSON-stringified."
},
{
"id": "RemoteObject",
"type": "object",
"description": "Mirror object referencing original JavaScript object.",
"exported": true,
"properties": [
{ "name": "type", "type": "string", "enum": ["object", "function", "undefined", "string", "number", "boolean", "symbol"], "description": "Object type." },
{ "name": "subtype", "type": "string", "optional": true, "enum": ["array", "null", "node", "regexp", "date", "map", "set", "iterator", "generator", "error", "proxy", "promise", "typedarray"], "description": "Object subtype hint. Specified for <code>object</code> type values only." },
{ "name": "className", "type": "string", "optional": true, "description": "Object class (constructor) name. Specified for <code>object</code> type values only." },
{ "name": "value", "type": "any", "optional": true, "description": "Remote object value in case of primitive values or JSON values (if it was requested)." },
{ "name": "unserializableValue", "$ref": "UnserializableValue", "optional": true, "description": "Primitive value which can not be JSON-stringified does not have <code>value</code>, but gets this property." },
{ "name": "description", "type": "string", "optional": true, "description": "String representation of the object." },
{ "name": "objectId", "$ref": "RemoteObjectId", "optional": true, "description": "Unique object identifier (for non-primitive values)." },
{ "name": "preview", "$ref": "ObjectPreview", "optional": true, "description": "Preview containing abbreviated property values. Specified for <code>object</code> type values only.", "experimental": true },
{ "name": "customPreview", "$ref": "CustomPreview", "optional": true, "experimental": true}
]
},
{
"id": "CustomPreview",
"type": "object",
"experimental": true,
"properties": [
{ "name": "header", "type": "string"},
{ "name": "hasBody", "type": "boolean"},
{ "name": "formatterObjectId", "$ref": "RemoteObjectId"},
{ "name": "bindRemoteObjectFunctionId", "$ref": "RemoteObjectId" },
{ "name": "configObjectId", "$ref": "RemoteObjectId", "optional": true }
]
},
{
"id": "ObjectPreview",
"type": "object",
"experimental": true,
"description": "Object containing abbreviated remote object value.",
"properties": [
{ "name": "type", "type": "string", "enum": ["object", "function", "undefined", "string", "number", "boolean", "symbol"], "description": "Object type." },
{ "name": "subtype", "type": "string", "optional": true, "enum": ["array", "null", "node", "regexp", "date", "map", "set", "iterator", "generator", "error"], "description": "Object subtype hint. Specified for <code>object</code> type values only." },
{ "name": "description", "type": "string", "optional": true, "description": "String representation of the object." },
{ "name": "overflow", "type": "boolean", "description": "True iff some of the properties or entries of the original object did not fit." },
{ "name": "properties", "type": "array", "items": { "$ref": "PropertyPreview" }, "description": "List of the properties." },
{ "name": "entries", "type": "array", "items": { "$ref": "EntryPreview" }, "optional": true, "description": "List of the entries. Specified for <code>map</code> and <code>set</code> subtype values only." }
]
},
{
"id": "PropertyPreview",
"type": "object",
"experimental": true,
"properties": [
{ "name": "name", "type": "string", "description": "Property name." },
{ "name": "type", "type": "string", "enum": ["object", "function", "undefined", "string", "number", "boolean", "symbol", "accessor"], "description": "Object type. Accessor means that the property itself is an accessor property." },
{ "name": "value", "type": "string", "optional": true, "description": "User-friendly property value string." },
{ "name": "valuePreview", "$ref": "ObjectPreview", "optional": true, "description": "Nested value preview." },
{ "name": "subtype", "type": "string", "optional": true, "enum": ["array", "null", "node", "regexp", "date", "map", "set", "iterator", "generator", "error"], "description": "Object subtype hint. Specified for <code>object</code> type values only." }
]
},
{
"id": "EntryPreview",
"type": "object",
"experimental": true,
"properties": [
{ "name": "key", "$ref": "ObjectPreview", "optional": true, "description": "Preview of the key. Specified for map-like collection entries." },
{ "name": "value", "$ref": "ObjectPreview", "description": "Preview of the value." }
]
},
{
"id": "PropertyDescriptor",
"type": "object",
"description": "Object property descriptor.",
"properties": [
{ "name": "name", "type": "string", "description": "Property name or symbol description." },
{ "name": "value", "$ref": "RemoteObject", "optional": true, "description": "The value associated with the property." },
{ "name": "writable", "type": "boolean", "optional": true, "description": "True if the value associated with the property may be changed (data descriptors only)." },
{ "name": "get", "$ref": "RemoteObject", "optional": true, "description": "A function which serves as a getter for the property, or <code>undefined</code> if there is no getter (accessor descriptors only)." },
{ "name": "set", "$ref": "RemoteObject", "optional": true, "description": "A function which serves as a setter for the property, or <code>undefined</code> if there is no setter (accessor descriptors only)." },
{ "name": "configurable", "type": "boolean", "description": "True if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object." },
{ "name": "enumerable", "type": "boolean", "description": "True if this property shows up during enumeration of the properties on the corresponding object." },
{ "name": "wasThrown", "type": "boolean", "optional": true, "description": "True if the result was thrown during the evaluation." },
{ "name": "isOwn", "optional": true, "type": "boolean", "description": "True if the property is owned for the object." },
{ "name": "symbol", "$ref": "RemoteObject", "optional": true, "description": "Property symbol object, if the property is of the <code>symbol</code> type." }
]
},
{
"id": "InternalPropertyDescriptor",
"type": "object",
"description": "Object internal property descriptor. This property isn't normally visible in JavaScript code.",
"properties": [
{ "name": "name", "type": "string", "description": "Conventional property name." },
{ "name": "value", "$ref": "RemoteObject", "optional": true, "description": "The value associated with the property." }
]
},
{
"id": "CallArgument",
"type": "object",
"description": "Represents function call argument. Either remote object id <code>objectId</code>, primitive <code>value</code>, unserializable primitive value or neither of (for undefined) them should be specified.",
"properties": [
{ "name": "value", "type": "any", "optional": true, "description": "Primitive value." },
{ "name": "unserializableValue", "$ref": "UnserializableValue", "optional": true, "description": "Primitive value which can not be JSON-stringified." },
{ "name": "objectId", "$ref": "RemoteObjectId", "optional": true, "description": "Remote object handle." }
]
},
{
"id": "ExecutionContextId",
"type": "integer",
"description": "Id of an execution context."
},
{
"id": "ExecutionContextDescription",
"type": "object",
"description": "Description of an isolated world.",
"properties": [
{ "name": "id", "$ref": "ExecutionContextId", "description": "Unique id of the execution context. It can be used to specify in which execution context script evaluation should be performed." },
{ "name": "origin", "type": "string", "description": "Execution context origin." },
{ "name": "name", "type": "string", "description": "Human readable name describing given context." },
{ "name": "auxData", "type": "object", "optional": true, "description": "Embedder-specific auxiliary data." }
]
},
{
"id": "ExceptionDetails",
"type": "object",
"description": "Detailed information about exception (or error) that was thrown during script compilation or execution.",
"properties": [
{ "name": "exceptionId", "type": "integer", "description": "Exception id." },
{ "name": "text", "type": "string", "description": "Exception text, which should be used together with exception object when available." },
{ "name": "lineNumber", "type": "integer", "description": "Line number of the exception location (0-based)." },
{ "name": "columnNumber", "type": "integer", "description": "Column number of the exception location (0-based)." },
{ "name": "scriptId", "$ref": "ScriptId", "optional": true, "description": "Script ID of the exception location." },
{ "name": "url", "type": "string", "optional": true, "description": "URL of the exception location, to be used when the script was not reported." },
{ "name": "stackTrace", "$ref": "StackTrace", "optional": true, "description": "JavaScript stack trace if available." },
{ "name": "exception", "$ref": "RemoteObject", "optional": true, "description": "Exception object if available." },
{ "name": "executionContextId", "$ref": "ExecutionContextId", "optional": true, "description": "Identifier of the context where exception happened." }
]
},
{
"id": "Timestamp",
"type": "number",
"description": "Number of milliseconds since epoch."
},
{
"id": "CallFrame",
"type": "object",
"description": "Stack entry for runtime errors and assertions.",
"properties": [
{ "name": "functionName", "type": "string", "description": "JavaScript function name." },
{ "name": "scriptId", "$ref": "ScriptId", "description": "JavaScript script id." },
{ "name": "url", "type": "string", "description": "JavaScript script name or url." },
{ "name": "lineNumber", "type": "integer", "description": "JavaScript script line number (0-based)." },
{ "name": "columnNumber", "type": "integer", "description": "JavaScript script column number (0-based)." }
]
},
{
"id": "StackTrace",
"type": "object",
"description": "Call frames for assertions or error messages.",
"exported": true,
"properties": [
{ "name": "description", "type": "string", "optional": true, "description": "String label of this stack trace. For async traces this may be a name of the function that initiated the async call." },
{ "name": "callFrames", "type": "array", "items": { "$ref": "CallFrame" }, "description": "JavaScript function name." },
{ "name": "parent", "$ref": "StackTrace", "optional": true, "description": "Asynchronous JavaScript stack trace that preceded this stack, if available." }
]
}
],
"commands": [
{
"name": "evaluate",
"async": true,
"parameters": [
{ "name": "expression", "type": "string", "description": "Expression to evaluate." },
{ "name": "objectGroup", "type": "string", "optional": true, "description": "Symbolic group name that can be used to release multiple objects." },
{ "name": "includeCommandLineAPI", "type": "boolean", "optional": true, "description": "Determines whether Command Line API should be available during the evaluation." },
{ "name": "silent", "type": "boolean", "optional": true, "description": "In silent mode exceptions thrown during evaluation are not reported and do not pause execution. Overrides <code>setPauseOnException</code> state." },
{ "name": "contextId", "$ref": "ExecutionContextId", "optional": true, "description": "Specifies in which execution context to perform evaluation. If the parameter is omitted the evaluation will be performed in the context of the inspected page." },
{ "name": "returnByValue", "type": "boolean", "optional": true, "description": "Whether the result is expected to be a JSON object that should be sent by value." },
{ "name": "generatePreview", "type": "boolean", "optional": true, "experimental": true, "description": "Whether preview should be generated for the result." },
{ "name": "userGesture", "type": "boolean", "optional": true, "experimental": true, "description": "Whether execution should be treated as initiated by user in the UI." },
{ "name": "awaitPromise", "type": "boolean", "optional":true, "description": "Whether execution should wait for promise to be resolved. If the result of evaluation is not a Promise, it's considered to be an error." }
],
"returns": [
{ "name": "result", "$ref": "RemoteObject", "description": "Evaluation result." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails", "optional": true, "description": "Exception details."}
],
"description": "Evaluates expression on global object."
},
{
"name": "awaitPromise",
"async": true,
"parameters": [
{ "name": "promiseObjectId", "$ref": "RemoteObjectId", "description": "Identifier of the promise." },
{ "name": "returnByValue", "type": "boolean", "optional": true, "description": "Whether the result is expected to be a JSON object that should be sent by value." },
{ "name": "generatePreview", "type": "boolean", "optional": true, "description": "Whether preview should be generated for the result." }
],
"returns": [
{ "name": "result", "$ref": "RemoteObject", "description": "Promise result. Will contain rejected value if promise was rejected." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails", "optional": true, "description": "Exception details if stack strace is available."}
],
"description": "Add handler to promise with given promise object id."
},
{
"name": "callFunctionOn",
"async": true,
"parameters": [
{ "name": "objectId", "$ref": "RemoteObjectId", "description": "Identifier of the object to call function on." },
{ "name": "functionDeclaration", "type": "string", "description": "Declaration of the function to call." },
{ "name": "arguments", "type": "array", "items": { "$ref": "CallArgument", "description": "Call argument." }, "optional": true, "description": "Call arguments. All call arguments must belong to the same JavaScript world as the target object." },
{ "name": "silent", "type": "boolean", "optional": true, "description": "In silent mode exceptions thrown during evaluation are not reported and do not pause execution. Overrides <code>setPauseOnException</code> state." },
{ "name": "returnByValue", "type": "boolean", "optional": true, "description": "Whether the result is expected to be a JSON object which should be sent by value." },
{ "name": "generatePreview", "type": "boolean", "optional": true, "experimental": true, "description": "Whether preview should be generated for the result." },
{ "name": "userGesture", "type": "boolean", "optional": true, "experimental": true, "description": "Whether execution should be treated as initiated by user in the UI." },
{ "name": "awaitPromise", "type": "boolean", "optional":true, "description": "Whether execution should wait for promise to be resolved. If the result of evaluation is not a Promise, it's considered to be an error." }
],
"returns": [
{ "name": "result", "$ref": "RemoteObject", "description": "Call result." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails", "optional": true, "description": "Exception details."}
],
"description": "Calls function with given declaration on the given object. Object group of the result is inherited from the target object."
},
{
"name": "getProperties",
"parameters": [
{ "name": "objectId", "$ref": "RemoteObjectId", "description": "Identifier of the object to return properties for." },
{ "name": "ownProperties", "optional": true, "type": "boolean", "description": "If true, returns properties belonging only to the element itself, not to its prototype chain." },
{ "name": "accessorPropertiesOnly", "optional": true, "type": "boolean", "description": "If true, returns accessor properties (with getter/setter) only; internal properties are not returned either.", "experimental": true },
{ "name": "generatePreview", "type": "boolean", "optional": true, "experimental": true, "description": "Whether preview should be generated for the results." }
],
"returns": [
{ "name": "result", "type": "array", "items": { "$ref": "PropertyDescriptor" }, "description": "Object properties." },
{ "name": "internalProperties", "optional": true, "type": "array", "items": { "$ref": "InternalPropertyDescriptor" }, "description": "Internal object properties (only of the element itself)." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails", "optional": true, "description": "Exception details."}
],
"description": "Returns properties of a given object. Object group of the result is inherited from the target object."
},
{
"name": "releaseObject",
"parameters": [
{ "name": "objectId", "$ref": "RemoteObjectId", "description": "Identifier of the object to release." }
],
"description": "Releases remote object with given id."
},
{
"name": "releaseObjectGroup",
"parameters": [
{ "name": "objectGroup", "type": "string", "description": "Symbolic object group name." }
],
"description": "Releases all remote objects that belong to a given group."
},
{
"name": "runIfWaitingForDebugger",
"description": "Tells inspected instance to run if it was waiting for debugger to attach."
},
{
"name": "enable",
"description": "Enables reporting of execution contexts creation by means of <code>executionContextCreated</code> event. When the reporting gets enabled the event will be sent immediately for each existing execution context."
},
{
"name": "disable",
"description": "Disables reporting of execution contexts creation."
},
{
"name": "discardConsoleEntries",
"description": "Discards collected exceptions and console API calls."
},
{
"name": "setCustomObjectFormatterEnabled",
"parameters": [
{
"name": "enabled",
"type": "boolean"
}
],
"experimental": true
},
{
"name": "compileScript",
"parameters": [
{ "name": "expression", "type": "string", "description": "Expression to compile." },
{ "name": "sourceURL", "type": "string", "description": "Source url to be set for the script." },
{ "name": "persistScript", "type": "boolean", "description": "Specifies whether the compiled script should be persisted." },
{ "name": "executionContextId", "$ref": "ExecutionContextId", "optional": true, "description": "Specifies in which execution context to perform script run. If the parameter is omitted the evaluation will be performed in the context of the inspected page." }
],
"returns": [
{ "name": "scriptId", "$ref": "ScriptId", "optional": true, "description": "Id of the script." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails", "optional": true, "description": "Exception details."}
],
"description": "Compiles expression."
},
{
"name": "runScript",
"async": true,
"parameters": [
{ "name": "scriptId", "$ref": "ScriptId", "description": "Id of the script to run." },
{ "name": "executionContextId", "$ref": "ExecutionContextId", "optional": true, "description": "Specifies in which execution context to perform script run. If the parameter is omitted the evaluation will be performed in the context of the inspected page." },
{ "name": "objectGroup", "type": "string", "optional": true, "description": "Symbolic group name that can be used to release multiple objects." },
{ "name": "silent", "type": "boolean", "optional": true, "description": "In silent mode exceptions thrown during evaluation are not reported and do not pause execution. Overrides <code>setPauseOnException</code> state." },
{ "name": "includeCommandLineAPI", "type": "boolean", "optional": true, "description": "Determines whether Command Line API should be available during the evaluation." },
{ "name": "returnByValue", "type": "boolean", "optional": true, "description": "Whether the result is expected to be a JSON object which should be sent by value." },
{ "name": "generatePreview", "type": "boolean", "optional": true, "description": "Whether preview should be generated for the result." },
{ "name": "awaitPromise", "type": "boolean", "optional": true, "description": "Whether execution should wait for promise to be resolved. If the result of evaluation is not a Promise, it's considered to be an error." }
],
"returns": [
{ "name": "result", "$ref": "RemoteObject", "description": "Run result." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails", "optional": true, "description": "Exception details."}
],
"description": "Runs script with given id in a given context."
}
],
"events": [
{
"name": "executionContextCreated",
"parameters": [
{ "name": "context", "$ref": "ExecutionContextDescription", "description": "A newly created execution contex." }
],
"description": "Issued when new execution context is created."
},
{
"name": "executionContextDestroyed",
"parameters": [
{ "name": "executionContextId", "$ref": "ExecutionContextId", "description": "Id of the destroyed context" }
],
"description": "Issued when execution context is destroyed."
},
{
"name": "executionContextsCleared",
"description": "Issued when all executionContexts were cleared in browser"
},
{
"name": "exceptionThrown",
"description": "Issued when exception was thrown and unhandled.",
"parameters": [
{ "name": "timestamp", "$ref": "Timestamp", "description": "Timestamp of the exception." },
{ "name": "exceptionDetails", "$ref": "ExceptionDetails" }
]
},
{
"name": "exceptionRevoked",
"description": "Issued when unhandled exception was revoked.",
"parameters": [
{ "name": "reason", "type": "string", "description": "Reason describing why exception was revoked." },
{ "name": "exceptionId", "type": "integer", "description": "The id of revoked exception, as reported in <code>exceptionUnhandled</code>." }
]
},
{
"name": "consoleAPICalled",
"description": "Issued when console API was called.",
"parameters": [
{ "name": "type", "type": "string", "enum": ["log", "debug", "info", "error", "warning", "dir", "dirxml", "table", "trace", "clear", "startGroup", "startGroupCollapsed", "endGroup", "assert", "profile", "profileEnd"], "description": "Type of the call." },
{ "name": "args", "type": "array", "items": { "$ref": "RemoteObject" }, "description": "Call arguments." },
{ "name": "executionContextId", "$ref": "ExecutionContextId", "description": "Identifier of the context where the call was made." },
{ "name": "timestamp", "$ref": "Timestamp", "description": "Call timestamp." },
{ "name": "stackTrace", "$ref": "StackTrace", "optional": true, "description": "Stack trace captured when the call was made." }
]
},
{
"name": "inspectRequested",
"description": "Issued when object should be inspected (for example, as a result of inspect() command line API call).",
"parameters": [
{ "name": "object", "$ref": "RemoteObject" },
{ "name": "hints", "type": "object" }
]
}
]
},
{
"domain": "Debugger",
"description": "Debugger domain exposes JavaScript debugging capabilities. It allows setting and removing breakpoints, stepping through execution, exploring stack traces, etc.",
"dependencies": ["Runtime"],
"types": [
{
"id": "BreakpointId",
"type": "string",
"description": "Breakpoint identifier."
},
{
"id": "CallFrameId",
"type": "string",
"description": "Call frame identifier."
},
{
"id": "Location",
"type": "object",
"properties": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Script identifier as reported in the <code>Debugger.scriptParsed</code>." },
{ "name": "lineNumber", "type": "integer", "description": "Line number in the script (0-based)." },
{ "name": "columnNumber", "type": "integer", "optional": true, "description": "Column number in the script (0-based)." }
],
"description": "Location in the source code."
},
{
"id": "ScriptPosition",
"experimental": true,
"type": "object",
"properties": [
{ "name": "lineNumber", "type": "integer" },
{ "name": "columnNumber", "type": "integer" }
],
"description": "Location in the source code."
},
{
"id": "CallFrame",
"type": "object",
"properties": [
{ "name": "callFrameId", "$ref": "CallFrameId", "description": "Call frame identifier. This identifier is only valid while the virtual machine is paused." },
{ "name": "functionName", "type": "string", "description": "Name of the JavaScript function called on this call frame." },
{ "name": "functionLocation", "$ref": "Location", "optional": true, "experimental": true, "description": "Location in the source code." },
{ "name": "location", "$ref": "Location", "description": "Location in the source code." },
{ "name": "scopeChain", "type": "array", "items": { "$ref": "Scope" }, "description": "Scope chain for this call frame." },
{ "name": "this", "$ref": "Runtime.RemoteObject", "description": "<code>this</code> object for this call frame." },
{ "name": "returnValue", "$ref": "Runtime.RemoteObject", "optional": true, "description": "The value being returned, if the function is at return point." }
],
"description": "JavaScript call frame. Array of call frames form the call stack."
},
{
"id": "Scope",
"type": "object",
"properties": [
{ "name": "type", "type": "string", "enum": ["global", "local", "with", "closure", "catch", "block", "script"], "description": "Scope type." },
{ "name": "object", "$ref": "Runtime.RemoteObject", "description": "Object representing the scope. For <code>global</code> and <code>with</code> scopes it represents the actual object; for the rest of the scopes, it is artificial transient object enumerating scope variables as its properties." },
{ "name": "name", "type": "string", "optional": true },
{ "name": "startLocation", "$ref": "Location", "optional": true, "description": "Location in the source code where scope starts" },
{ "name": "endLocation", "$ref": "Location", "optional": true, "description": "Location in the source code where scope ends" }
],
"description": "Scope description."
},
{
"id": "SearchMatch",
"type": "object",
"description": "Search match for resource.",
"exported": true,
"properties": [
{ "name": "lineNumber", "type": "number", "description": "Line number in resource content." },
{ "name": "lineContent", "type": "string", "description": "Line with match content." }
],
"experimental": true
}
],
"commands": [
{
"name": "enable",
"description": "Enables debugger for the given page. Clients should not assume that the debugging has been enabled until the result for this command is received."
},
{
"name": "disable",
"description": "Disables debugger for given page."
},
{
"name": "setBreakpointsActive",
"parameters": [
{ "name": "active", "type": "boolean", "description": "New value for breakpoints active state." }
],
"description": "Activates / deactivates all breakpoints on the page."
},
{
"name": "setSkipAllPauses",
"parameters": [
{ "name": "skip", "type": "boolean", "description": "New value for skip pauses state." }
],
"description": "Makes page not interrupt on any pauses (breakpoint, exception, dom exception etc)."
},
{
"name": "setBreakpointByUrl",
"parameters": [
{ "name": "lineNumber", "type": "integer", "description": "Line number to set breakpoint at." },
{ "name": "url", "type": "string", "optional": true, "description": "URL of the resources to set breakpoint on." },
{ "name": "urlRegex", "type": "string", "optional": true, "description": "Regex pattern for the URLs of the resources to set breakpoints on. Either <code>url</code> or <code>urlRegex</code> must be specified." },
{ "name": "columnNumber", "type": "integer", "optional": true, "description": "Offset in the line to set breakpoint at." },
{ "name": "condition", "type": "string", "optional": true, "description": "Expression to use as a breakpoint condition. When specified, debugger will only stop on the breakpoint if this expression evaluates to true." }
],
"returns": [
{ "name": "breakpointId", "$ref": "BreakpointId", "description": "Id of the created breakpoint for further reference." },
{ "name": "locations", "type": "array", "items": { "$ref": "Location" }, "description": "List of the locations this breakpoint resolved into upon addition." }
],
"description": "Sets JavaScript breakpoint at given location specified either by URL or URL regex. Once this command is issued, all existing parsed scripts will have breakpoints resolved and returned in <code>locations</code> property. Further matching script parsing will result in subsequent <code>breakpointResolved</code> events issued. This logical breakpoint will survive page reloads."
},
{
"name": "setBreakpoint",
"parameters": [
{ "name": "location", "$ref": "Location", "description": "Location to set breakpoint in." },
{ "name": "condition", "type": "string", "optional": true, "description": "Expression to use as a breakpoint condition. When specified, debugger will only stop on the breakpoint if this expression evaluates to true." }
],
"returns": [
{ "name": "breakpointId", "$ref": "BreakpointId", "description": "Id of the created breakpoint for further reference." },
{ "name": "actualLocation", "$ref": "Location", "description": "Location this breakpoint resolved into." }
],
"description": "Sets JavaScript breakpoint at a given location."
},
{
"name": "removeBreakpoint",
"parameters": [
{ "name": "breakpointId", "$ref": "BreakpointId" }
],
"description": "Removes JavaScript breakpoint."
},
{
"name": "continueToLocation",
"parameters": [
{ "name": "location", "$ref": "Location", "description": "Location to continue to." }
],
"description": "Continues execution until specific location is reached."
},
{
"name": "stepOver",
"description": "Steps over the statement."
},
{
"name": "stepInto",
"description": "Steps into the function call."
},
{
"name": "stepOut",
"description": "Steps out of the function call."
},
{
"name": "pause",
"description": "Stops on the next JavaScript statement."
},
{
"name": "resume",
"description": "Resumes JavaScript execution."
},
{
"name": "searchInContent",
"parameters": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Id of the script to search in." },
{ "name": "query", "type": "string", "description": "String to search for." },
{ "name": "caseSensitive", "type": "boolean", "optional": true, "description": "If true, search is case sensitive." },
{ "name": "isRegex", "type": "boolean", "optional": true, "description": "If true, treats string parameter as regex." }
],
"returns": [
{ "name": "result", "type": "array", "items": { "$ref": "SearchMatch" }, "description": "List of search matches." }
],
"experimental": true,
"description": "Searches for given string in script content."
},
{
"name": "setScriptSource",
"parameters": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Id of the script to edit." },
{ "name": "scriptSource", "type": "string", "description": "New content of the script." },
{ "name": "dryRun", "type": "boolean", "optional": true, "description": " If true the change will not actually be applied. Dry run may be used to get result description without actually modifying the code." }
],
"returns": [
{ "name": "callFrames", "type": "array", "optional": true, "items": { "$ref": "CallFrame" }, "description": "New stack trace in case editing has happened while VM was stopped." },
{ "name": "stackChanged", "type": "boolean", "optional": true, "description": "Whether current call stack was modified after applying the changes." },
{ "name": "asyncStackTrace", "$ref": "Runtime.StackTrace", "optional": true, "description": "Async stack trace, if any." },
{ "name": "exceptionDetails", "optional": true, "$ref": "Runtime.ExceptionDetails", "description": "Exception details if any." }
],
"description": "Edits JavaScript source live."
},
{
"name": "restartFrame",
"parameters": [
{ "name": "callFrameId", "$ref": "CallFrameId", "description": "Call frame identifier to evaluate on." }
],
"returns": [
{ "name": "callFrames", "type": "array", "items": { "$ref": "CallFrame" }, "description": "New stack trace." },
{ "name": "asyncStackTrace", "$ref": "Runtime.StackTrace", "optional": true, "description": "Async stack trace, if any." }
],
"description": "Restarts particular call frame from the beginning."
},
{
"name": "getScriptSource",
"parameters": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Id of the script to get source for." }
],
"returns": [
{ "name": "scriptSource", "type": "string", "description": "Script source." }
],
"description": "Returns source for the script with given id."
},
{
"name": "setPauseOnExceptions",
"parameters": [
{ "name": "state", "type": "string", "enum": ["none", "uncaught", "all"], "description": "Pause on exceptions mode." }
],
"description": "Defines pause on exceptions state. Can be set to stop on all exceptions, uncaught exceptions or no exceptions. Initial pause on exceptions state is <code>none</code>."
},
{
"name": "evaluateOnCallFrame",
"parameters": [
{ "name": "callFrameId", "$ref": "CallFrameId", "description": "Call frame identifier to evaluate on." },
{ "name": "expression", "type": "string", "description": "Expression to evaluate." },
{ "name": "objectGroup", "type": "string", "optional": true, "description": "String object group name to put result into (allows rapid releasing resulting object handles using <code>releaseObjectGroup</code>)." },
{ "name": "includeCommandLineAPI", "type": "boolean", "optional": true, "description": "Specifies whether command line API should be available to the evaluated expression, defaults to false." },
{ "name": "silent", "type": "boolean", "optional": true, "description": "In silent mode exceptions thrown during evaluation are not reported and do not pause execution. Overrides <code>setPauseOnException</code> state." },
{ "name": "returnByValue", "type": "boolean", "optional": true, "description": "Whether the result is expected to be a JSON object that should be sent by value." },
{ "name": "generatePreview", "type": "boolean", "optional": true, "experimental": true, "description": "Whether preview should be generated for the result." }
],
"returns": [
{ "name": "result", "$ref": "Runtime.RemoteObject", "description": "Object wrapper for the evaluation result." },
{ "name": "exceptionDetails", "$ref": "Runtime.ExceptionDetails", "optional": true, "description": "Exception details."}
],
"description": "Evaluates expression on a given call frame."
},
{
"name": "setVariableValue",
"parameters": [
{ "name": "scopeNumber", "type": "integer", "description": "0-based number of scope as was listed in scope chain. Only 'local', 'closure' and 'catch' scope types are allowed. Other scopes could be manipulated manually." },
{ "name": "variableName", "type": "string", "description": "Variable name." },
{ "name": "newValue", "$ref": "Runtime.CallArgument", "description": "New variable value." },
{ "name": "callFrameId", "$ref": "CallFrameId", "description": "Id of callframe that holds variable." }
],
"description": "Changes value of variable in a callframe. Object-based scopes are not supported and must be mutated manually."
},
{
"name": "setAsyncCallStackDepth",
"parameters": [
{ "name": "maxDepth", "type": "integer", "description": "Maximum depth of async call stacks. Setting to <code>0</code> will effectively disable collecting async call stacks (default)." }
],
"description": "Enables or disables async call stacks tracking."
},
{
"name": "setBlackboxPatterns",
"parameters": [
{ "name": "patterns", "type": "array", "items": { "type": "string" }, "description": "Array of regexps that will be used to check script url for blackbox state." }
],
"experimental": true,
"description": "Replace previous blackbox patterns with passed ones. Forces backend to skip stepping/pausing in scripts with url matching one of the patterns. VM will try to leave blackboxed script by performing 'step in' several times, finally resorting to 'step out' if unsuccessful."
},
{
"name": "setBlackboxedRanges",
"parameters": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Id of the script." },
{ "name": "positions", "type": "array", "items": { "$ref": "ScriptPosition" } }
],
"experimental": true,
"description": "Makes backend skip steps in the script in blackboxed ranges. VM will try leave blacklisted scripts by performing 'step in' several times, finally resorting to 'step out' if unsuccessful. Positions array contains positions where blackbox state is changed. First interval isn't blackboxed. Array should be sorted."
}
],
"events": [
{
"name": "scriptParsed",
"parameters": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Identifier of the script parsed." },
{ "name": "url", "type": "string", "description": "URL or name of the script parsed (if any)." },
{ "name": "startLine", "type": "integer", "description": "Line offset of the script within the resource with given URL (for script tags)." },
{ "name": "startColumn", "type": "integer", "description": "Column offset of the script within the resource with given URL." },
{ "name": "endLine", "type": "integer", "description": "Last line of the script." },
{ "name": "endColumn", "type": "integer", "description": "Length of the last line of the script." },
{ "name": "executionContextId", "$ref": "Runtime.ExecutionContextId", "description": "Specifies script creation context." },
{ "name": "hash", "type": "string", "description": "Content hash of the script."},
{ "name": "executionContextAuxData", "type": "object", "optional": true, "description": "Embedder-specific auxiliary data." },
{ "name": "isLiveEdit", "type": "boolean", "optional": true, "description": "True, if this script is generated as a result of the live edit operation.", "experimental": true },
{ "name": "sourceMapURL", "type": "string", "optional": true, "description": "URL of source map associated with script (if any)." },
{ "name": "hasSourceURL", "type": "boolean", "optional": true, "description": "True, if this script has sourceURL.", "experimental": true }
],
"description": "Fired when virtual machine parses script. This event is also fired for all known and uncollected scripts upon enabling debugger."
},
{
"name": "scriptFailedToParse",
"parameters": [
{ "name": "scriptId", "$ref": "Runtime.ScriptId", "description": "Identifier of the script parsed." },
{ "name": "url", "type": "string", "description": "URL or name of the script parsed (if any)." },
{ "name": "startLine", "type": "integer", "description": "Line offset of the script within the resource with given URL (for script tags)." },
{ "name": "startColumn", "type": "integer", "description": "Column offset of the script within the resource with given URL." },
{ "name": "endLine", "type": "integer", "description": "Last line of the script." },
{ "name": "endColumn", "type": "integer", "description": "Length of the last line of the script." },
{ "name": "executionContextId", "$ref": "Runtime.ExecutionContextId", "description": "Specifies script creation context." },
{ "name": "hash", "type": "string", "description": "Content hash of the script."},
{ "name": "executionContextAuxData", "type": "object", "optional": true, "description": "Embedder-specific auxiliary data." },
{ "name": "sourceMapURL", "type": "string", "optional": true, "description": "URL of source map associated with script (if any)." },
{ "name": "hasSourceURL", "type": "boolean", "optional": true, "description": "True, if this script has sourceURL.", "experimental": true }
],
"description": "Fired when virtual machine fails to parse the script."
},
{
"name": "breakpointResolved",
"parameters": [
{ "name": "breakpointId", "$ref": "BreakpointId", "description": "Breakpoint unique identifier." },
{ "name": "location", "$ref": "Location", "description": "Actual breakpoint location." }
],
"description": "Fired when breakpoint is resolved to an actual script and location."
},
{
"name": "paused",
"parameters": [
{ "name": "callFrames", "type": "array", "items": { "$ref": "CallFrame" }, "description": "Call stack the virtual machine stopped on." },
{ "name": "reason", "type": "string", "enum": [ "XHR", "DOM", "EventListener", "exception", "assert", "debugCommand", "promiseRejection", "other" ], "description": "Pause reason.", "exported": true },
{ "name": "data", "type": "object", "optional": true, "description": "Object containing break-specific auxiliary properties." },
{ "name": "hitBreakpoints", "type": "array", "optional": true, "items": { "type": "string" }, "description": "Hit breakpoints IDs" },
{ "name": "asyncStackTrace", "$ref": "Runtime.StackTrace", "optional": true, "description": "Async stack trace, if any." }
],
"description": "Fired when the virtual machine stopped on breakpoint or exception or any other stop criteria."
},
{
"name": "resumed",
"description": "Fired when the virtual machine resumed execution."
}
]
},
{
"domain": "Console",
"description": "This domain is deprecated - use Runtime or Log instead.",
"dependencies": ["Runtime"],
"deprecated": true,
"types": [
{
"id": "ConsoleMessage",
"type": "object",
"description": "Console message.",
"properties": [
{ "name": "source", "type": "string", "enum": ["xml", "javascript", "network", "console-api", "storage", "appcache", "rendering", "security", "other", "deprecation", "worker"], "description": "Message source." },
{ "name": "level", "type": "string", "enum": ["log", "warning", "error", "debug", "info"], "description": "Message severity." },
{ "name": "text", "type": "string", "description": "Message text." },
{ "name": "url", "type": "string", "optional": true, "description": "URL of the message origin." },
{ "name": "line", "type": "integer", "optional": true, "description": "Line number in the resource that generated this message (1-based)." },
{ "name": "column", "type": "integer", "optional": true, "description": "Column number in the resource that generated this message (1-based)." }
]
}
],
"commands": [
{
"name": "enable",
"description": "Enables console domain, sends the messages collected so far to the client by means of the <code>messageAdded</code> notification."
},
{
"name": "disable",
"description": "Disables console domain, prevents further console messages from being reported to the client."
},
{
"name": "clearMessages",
"description": "Does nothing."
}
],
"events": [
{
"name": "messageAdded",
"parameters": [
{ "name": "message", "$ref": "ConsoleMessage", "description": "Console message that has been added." }
],
"description": "Issued when new console message is added."
}
]
},
{
"domain": "Profiler",
"dependencies": ["Runtime", "Debugger"],
"types": [
{
"id": "ProfileNode",
"type": "object",
"description": "Profile node. Holds callsite information, execution statistics and child nodes.",
"properties": [
{ "name": "id", "type": "integer", "description": "Unique id of the node." },
{ "name": "callFrame", "$ref": "Runtime.CallFrame", "description": "Function location." },
{ "name": "hitCount", "type": "integer", "optional": true, "experimental": true, "description": "Number of samples where this node was on top of the call stack." },
{ "name": "children", "type": "array", "items": { "type": "integer" }, "optional": true, "description": "Child node ids." },
{ "name": "deoptReason", "type": "string", "optional": true, "description": "The reason of being not optimized. The function may be deoptimized or marked as don't optimize."},
{ "name": "positionTicks", "type": "array", "items": { "$ref": "PositionTickInfo" }, "optional": true, "experimental": true, "description": "An array of source position ticks." }
]
},
{
"id": "Profile",
"type": "object",
"description": "Profile.",
"properties": [
{ "name": "nodes", "type": "array", "items": { "$ref": "ProfileNode" }, "description": "The list of profile nodes. First item is the root node." },
{ "name": "startTime", "type": "number", "description": "Profiling start timestamp in microseconds." },
{ "name": "endTime", "type": "number", "description": "Profiling end timestamp in microseconds." },
{ "name": "samples", "optional": true, "type": "array", "items": { "type": "integer" }, "description": "Ids of samples top nodes." },
{ "name": "timeDeltas", "optional": true, "type": "array", "items": { "type": "integer" }, "description": "Time intervals between adjacent samples in microseconds. The first delta is relative to the profile startTime." }
]
},
{
"id": "PositionTickInfo",
"type": "object",
"experimental": true,
"description": "Specifies a number of samples attributed to a certain source position.",
"properties": [
{ "name": "line", "type": "integer", "description": "Source line number (1-based)." },
{ "name": "ticks", "type": "integer", "description": "Number of samples attributed to the source line." }
]
}
],
"commands": [
{
"name": "enable"
},
{
"name": "disable"
},
{
"name": "setSamplingInterval",
"parameters": [
{ "name": "interval", "type": "integer", "description": "New sampling interval in microseconds." }
],
"description": "Changes CPU profiler sampling interval. Must be called before CPU profiles recording started."
},
{
"name": "start"
},
{
"name": "stop",
"returns": [
{ "name": "profile", "$ref": "Profile", "description": "Recorded profile." }
]
}
],
"events": [
{
"name": "consoleProfileStarted",
"parameters": [
{ "name": "id", "type": "string" },
{ "name": "location", "$ref": "Debugger.Location", "description": "Location of console.profile()." },
{ "name": "title", "type": "string", "optional": true, "description": "Profile title passed as an argument to console.profile()." }
],
"description": "Sent when new profile recodring is started using console.profile() call."
},
{
"name": "consoleProfileFinished",
"parameters": [
{ "name": "id", "type": "string" },
{ "name": "location", "$ref": "Debugger.Location", "description": "Location of console.profileEnd()." },
{ "name": "profile", "$ref": "Profile" },
{ "name": "title", "type": "string", "optional": true, "description": "Profile title passed as an argument to console.profile()." }
]
}
]
},
{
"domain": "HeapProfiler",
"dependencies": ["Runtime"],
"experimental": true,
"types": [
{
"id": "HeapSnapshotObjectId",
"type": "string",
"description": "Heap snapshot object id."
},
{
"id": "SamplingHeapProfileNode",
"type": "object",
"description": "Sampling Heap Profile node. Holds callsite information, allocation statistics and child nodes.",
"properties": [
{ "name": "callFrame", "$ref": "Runtime.CallFrame", "description": "Function location." },
{ "name": "selfSize", "type": "number", "description": "Allocations size in bytes for the node excluding children." },
{ "name": "children", "type": "array", "items": { "$ref": "SamplingHeapProfileNode" }, "description": "Child nodes." }
]
},
{
"id": "SamplingHeapProfile",
"type": "object",
"description": "Profile.",
"properties": [
{ "name": "head", "$ref": "SamplingHeapProfileNode" }
]
}
],
"commands": [
{
"name": "enable"
},
{
"name": "disable"
},
{
"name": "startTrackingHeapObjects",
"parameters": [
{ "name": "trackAllocations", "type": "boolean", "optional": true }
]
},
{
"name": "stopTrackingHeapObjects",
"parameters": [
{ "name": "reportProgress", "type": "boolean", "optional": true, "description": "If true 'reportHeapSnapshotProgress' events will be generated while snapshot is being taken when the tracking is stopped." }
]
},
{
"name": "takeHeapSnapshot",
"parameters": [
{ "name": "reportProgress", "type": "boolean", "optional": true, "description": "If true 'reportHeapSnapshotProgress' events will be generated while snapshot is being taken." }
]
},
{
"name": "collectGarbage"
},
{
"name": "getObjectByHeapObjectId",
"parameters": [
{ "name": "objectId", "$ref": "HeapSnapshotObjectId" },
{ "name": "objectGroup", "type": "string", "optional": true, "description": "Symbolic group name that can be used to release multiple objects." }
],
"returns": [
{ "name": "result", "$ref": "Runtime.RemoteObject", "description": "Evaluation result." }
]
},
{
"name": "addInspectedHeapObject",
"parameters": [
{ "name": "heapObjectId", "$ref": "HeapSnapshotObjectId", "description": "Heap snapshot object id to be accessible by means of $x command line API." }
],
"description": "Enables console to refer to the node with given id via $x (see Command Line API for more details $x functions)."
},
{
"name": "getHeapObjectId",
"parameters": [
{ "name": "objectId", "$ref": "Runtime.RemoteObjectId", "description": "Identifier of the object to get heap object id for." }
],
"returns": [
{ "name": "heapSnapshotObjectId", "$ref": "HeapSnapshotObjectId", "description": "Id of the heap snapshot object corresponding to the passed remote object id." }
]
},
{
"name": "startSampling",
"parameters": [
{ "name": "samplingInterval", "type": "number", "optional": true, "description": "Average sample interval in bytes. Poisson distribution is used for the intervals. The default value is 32768 bytes." }
]
},
{
"name": "stopSampling",
"returns": [
{ "name": "profile", "$ref": "SamplingHeapProfile", "description": "Recorded sampling heap profile." }
]
}
],
"events": [
{
"name": "addHeapSnapshotChunk",
"parameters": [
{ "name": "chunk", "type": "string" }
]
},
{
"name": "resetProfiles"
},
{
"name": "reportHeapSnapshotProgress",
"parameters": [
{ "name": "done", "type": "integer" },
{ "name": "total", "type": "integer" },
{ "name": "finished", "type": "boolean", "optional": true }
]
},
{
"name": "lastSeenObjectId",
"description": "If heap objects tracking has been started then backend regulary sends a current value for last seen object id and corresponding timestamp. If the were changes in the heap since last event then one or more heapStatsUpdate events will be sent before a new lastSeenObjectId event.",
"parameters": [
{ "name": "lastSeenObjectId", "type": "integer" },
{ "name": "timestamp", "type": "number" }
]
},
{
"name": "heapStatsUpdate",
"description": "If heap objects tracking has been started then backend may send update for one or more fragments",
"parameters": [
{ "name": "statsUpdate", "type": "array", "items": { "type": "integer" }, "description": "An array of triplets. Each triplet describes a fragment. The first integer is the fragment index, the second integer is a total count of objects for the fragment, the third integer is a total size of the objects for the fragment."}
]
}
]
}]
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
include_rules = [
"+libplatform/libplatform-export.h",
]
specific_include_rules = {
"libplatform\.h": [
"+libplatform/v8-tracing.h",
],
}

View File

@ -0,0 +1,29 @@
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_LIBPLATFORM_LIBPLATFORM_EXPORT_H_
#define V8_LIBPLATFORM_LIBPLATFORM_EXPORT_H_
#if defined(_WIN32)
#ifdef BUILDING_V8_PLATFORM_SHARED
#define V8_PLATFORM_EXPORT __declspec(dllexport)
#elif USING_V8_PLATFORM_SHARED
#define V8_PLATFORM_EXPORT __declspec(dllimport)
#else
#define V8_PLATFORM_EXPORT
#endif // BUILDING_V8_PLATFORM_SHARED
#else // defined(_WIN32)
// Setup for Linux shared library export.
#if defined(BUILDING_V8_PLATFORM_SHARED) || USING_V8_PLATFORM_SHARED
#define V8_PLATFORM_EXPORT __attribute__((visibility("default")))
#else
#define V8_PLATFORM_EXPORT
#endif // defined(BUILDING_V8_PLATFORM_SHARED) || ...
#endif // defined(_WIN32)
#endif // V8_LIBPLATFORM_LIBPLATFORM_EXPORT_H_

View File

@ -0,0 +1,112 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_LIBPLATFORM_LIBPLATFORM_H_
#define V8_LIBPLATFORM_LIBPLATFORM_H_
#include <memory>
#include "libplatform/libplatform-export.h"
#include "libplatform/v8-tracing.h"
#include "v8-platform.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
namespace platform {
enum class IdleTaskSupport { kDisabled, kEnabled };
enum class InProcessStackDumping { kDisabled, kEnabled };
enum class MessageLoopBehavior : bool {
kDoNotWait = false,
kWaitForWork = true
};
enum class PriorityMode : bool { kDontApply, kApply };
/**
* Returns a new instance of the default v8::Platform implementation.
*
* The caller will take ownership of the returned pointer. |thread_pool_size|
* is the number of worker threads to allocate for background jobs. If a value
* of zero is passed, a suitable default based on the current number of
* processors online will be chosen.
* If |idle_task_support| is enabled then the platform will accept idle
* tasks (IdleTasksEnabled will return true) and will rely on the embedder
* calling v8::platform::RunIdleTasks to process the idle tasks.
* If |tracing_controller| is nullptr, the default platform will create a
* v8::platform::TracingController instance and use it.
* If |priority_mode| is PriorityMode::kApply, the default platform will use
* multiple task queues executed by threads different system-level priorities
* (where available) to schedule tasks.
*/
V8_PLATFORM_EXPORT std::unique_ptr<v8::Platform> NewDefaultPlatform(
int thread_pool_size = 0,
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
InProcessStackDumping in_process_stack_dumping =
InProcessStackDumping::kDisabled,
std::unique_ptr<v8::TracingController> tracing_controller = {},
PriorityMode priority_mode = PriorityMode::kDontApply);
/**
* The same as NewDefaultPlatform but disables the worker thread pool.
* It must be used with the --single-threaded V8 flag.
*/
V8_PLATFORM_EXPORT std::unique_ptr<v8::Platform>
NewSingleThreadedDefaultPlatform(
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
InProcessStackDumping in_process_stack_dumping =
InProcessStackDumping::kDisabled,
std::unique_ptr<v8::TracingController> tracing_controller = {});
/**
* Returns a new instance of the default v8::JobHandle implementation.
*
* The job will be executed by spawning up to |num_worker_threads| many worker
* threads on the provided |platform| with the given |priority|.
*/
V8_PLATFORM_EXPORT std::unique_ptr<v8::JobHandle> NewDefaultJobHandle(
v8::Platform* platform, v8::TaskPriority priority,
std::unique_ptr<v8::JobTask> job_task, size_t num_worker_threads);
/**
* Pumps the message loop for the given isolate.
*
* The caller has to make sure that this is called from the right thread.
* Returns true if a task was executed, and false otherwise. If the call to
* PumpMessageLoop is nested within another call to PumpMessageLoop, only
* nestable tasks may run. Otherwise, any task may run. Unless requested through
* the |behavior| parameter, this call does not block if no task is pending. The
* |platform| has to be created using |NewDefaultPlatform|.
*/
V8_PLATFORM_EXPORT bool PumpMessageLoop(
v8::Platform* platform, v8::Isolate* isolate,
MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
/**
* Runs pending idle tasks for at most |idle_time_in_seconds| seconds.
*
* The caller has to make sure that this is called from the right thread.
* This call does not block if no task is pending. The |platform| has to be
* created using |NewDefaultPlatform|.
*/
V8_PLATFORM_EXPORT void RunIdleTasks(v8::Platform* platform,
v8::Isolate* isolate,
double idle_time_in_seconds);
/**
* Notifies the given platform about the Isolate getting deleted soon. Has to be
* called for all Isolates which are deleted - unless we're shutting down the
* platform.
*
* The |platform| has to be created using |NewDefaultPlatform|.
*
*/
V8_PLATFORM_EXPORT void NotifyIsolateShutdown(v8::Platform* platform,
Isolate* isolate);
} // namespace platform
} // namespace v8
#endif // V8_LIBPLATFORM_LIBPLATFORM_H_

View File

@ -0,0 +1,343 @@
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_LIBPLATFORM_V8_TRACING_H_
#define V8_LIBPLATFORM_V8_TRACING_H_
#include <atomic>
#include <memory>
#include <unordered_set>
#include <vector>
#include "libplatform/libplatform-export.h"
#include "v8-platform.h" // NOLINT(build/include_directory)
namespace perfetto {
#if defined(V8_USE_PERFETTO_JSON_EXPORT)
namespace trace_processor {
class TraceProcessorStorage;
}
#endif // defined(V8_USE_PERFETTO_JSON_EXPORT)
class TracingSession;
}
namespace v8 {
namespace base {
class Mutex;
} // namespace base
namespace platform {
namespace tracing {
class TraceEventListener;
const int kTraceMaxNumArgs = 2;
class V8_PLATFORM_EXPORT TraceObject {
public:
union ArgValue {
uint64_t as_uint;
int64_t as_int;
double as_double;
const void* as_pointer;
const char* as_string;
};
TraceObject() = default;
~TraceObject();
void Initialize(
char phase, const uint8_t* category_enabled_flag, const char* name,
const char* scope, uint64_t id, uint64_t bind_id, int num_args,
const char** arg_names, const uint8_t* arg_types,
const uint64_t* arg_values,
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
unsigned int flags, int64_t timestamp, int64_t cpu_timestamp);
void UpdateDuration(int64_t timestamp, int64_t cpu_timestamp);
void InitializeForTesting(
char phase, const uint8_t* category_enabled_flag, const char* name,
const char* scope, uint64_t id, uint64_t bind_id, int num_args,
const char** arg_names, const uint8_t* arg_types,
const uint64_t* arg_values,
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
unsigned int flags, int pid, int tid, int64_t ts, int64_t tts,
uint64_t duration, uint64_t cpu_duration);
int pid() const { return pid_; }
int tid() const { return tid_; }
char phase() const { return phase_; }
const uint8_t* category_enabled_flag() const {
return category_enabled_flag_;
}
const char* name() const { return name_; }
const char* scope() const { return scope_; }
uint64_t id() const { return id_; }
uint64_t bind_id() const { return bind_id_; }
int num_args() const { return num_args_; }
const char** arg_names() { return arg_names_; }
uint8_t* arg_types() { return arg_types_; }
ArgValue* arg_values() { return arg_values_; }
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables() {
return arg_convertables_;
}
unsigned int flags() const { return flags_; }
int64_t ts() { return ts_; }
int64_t tts() { return tts_; }
uint64_t duration() { return duration_; }
uint64_t cpu_duration() { return cpu_duration_; }
private:
int pid_;
int tid_;
char phase_;
const char* name_;
const char* scope_;
const uint8_t* category_enabled_flag_;
uint64_t id_;
uint64_t bind_id_;
int num_args_ = 0;
const char* arg_names_[kTraceMaxNumArgs];
uint8_t arg_types_[kTraceMaxNumArgs];
ArgValue arg_values_[kTraceMaxNumArgs];
std::unique_ptr<v8::ConvertableToTraceFormat>
arg_convertables_[kTraceMaxNumArgs];
char* parameter_copy_storage_ = nullptr;
unsigned int flags_;
int64_t ts_;
int64_t tts_;
uint64_t duration_;
uint64_t cpu_duration_;
// Disallow copy and assign
TraceObject(const TraceObject&) = delete;
void operator=(const TraceObject&) = delete;
};
class V8_PLATFORM_EXPORT TraceWriter {
public:
TraceWriter() = default;
virtual ~TraceWriter() = default;
virtual void AppendTraceEvent(TraceObject* trace_event) = 0;
virtual void Flush() = 0;
static TraceWriter* CreateJSONTraceWriter(std::ostream& stream);
static TraceWriter* CreateJSONTraceWriter(std::ostream& stream,
const std::string& tag);
static TraceWriter* CreateSystemInstrumentationTraceWriter();
private:
// Disallow copy and assign
TraceWriter(const TraceWriter&) = delete;
void operator=(const TraceWriter&) = delete;
};
class V8_PLATFORM_EXPORT TraceBufferChunk {
public:
explicit TraceBufferChunk(uint32_t seq);
void Reset(uint32_t new_seq);
bool IsFull() const { return next_free_ == kChunkSize; }
TraceObject* AddTraceEvent(size_t* event_index);
TraceObject* GetEventAt(size_t index) { return &chunk_[index]; }
uint32_t seq() const { return seq_; }
size_t size() const { return next_free_; }
static const size_t kChunkSize = 64;
private:
size_t next_free_ = 0;
TraceObject chunk_[kChunkSize];
uint32_t seq_;
// Disallow copy and assign
TraceBufferChunk(const TraceBufferChunk&) = delete;
void operator=(const TraceBufferChunk&) = delete;
};
class V8_PLATFORM_EXPORT TraceBuffer {
public:
TraceBuffer() = default;
virtual ~TraceBuffer() = default;
virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0;
virtual TraceObject* GetEventByHandle(uint64_t handle) = 0;
virtual bool Flush() = 0;
static const size_t kRingBufferChunks = 1024;
static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks,
TraceWriter* trace_writer);
private:
// Disallow copy and assign
TraceBuffer(const TraceBuffer&) = delete;
void operator=(const TraceBuffer&) = delete;
};
// Options determines how the trace buffer stores data.
enum TraceRecordMode {
// Record until the trace buffer is full.
RECORD_UNTIL_FULL,
// Record until the user ends the trace. The trace buffer is a fixed size
// and we use it as a ring buffer during recording.
RECORD_CONTINUOUSLY,
// Record until the trace buffer is full, but with a huge buffer size.
RECORD_AS_MUCH_AS_POSSIBLE,
// Echo to console. Events are discarded.
ECHO_TO_CONSOLE,
};
class V8_PLATFORM_EXPORT TraceConfig {
public:
typedef std::vector<std::string> StringList;
static TraceConfig* CreateDefaultTraceConfig();
TraceConfig() : enable_systrace_(false), enable_argument_filter_(false) {}
TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
const StringList& GetEnabledCategories() const {
return included_categories_;
}
bool IsSystraceEnabled() const { return enable_systrace_; }
bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; }
void EnableSystrace() { enable_systrace_ = true; }
void EnableArgumentFilter() { enable_argument_filter_ = true; }
void AddIncludedCategory(const char* included_category);
bool IsCategoryGroupEnabled(const char* category_group) const;
private:
TraceRecordMode record_mode_;
bool enable_systrace_ : 1;
bool enable_argument_filter_ : 1;
StringList included_categories_;
// Disallow copy and assign
TraceConfig(const TraceConfig&) = delete;
void operator=(const TraceConfig&) = delete;
};
#if defined(_MSC_VER)
#define V8_PLATFORM_NON_EXPORTED_BASE(code) \
__pragma(warning(suppress : 4275)) code
#else
#define V8_PLATFORM_NON_EXPORTED_BASE(code) code
#endif // defined(_MSC_VER)
/**
* V8 Tracing controller default implementation.
*
* Will become obsolete in Perfetto build
* (v8_use_perfetto_json_export = true).
*/
class V8_PLATFORM_EXPORT TracingController
: public V8_PLATFORM_NON_EXPORTED_BASE(v8::TracingController) {
public:
TracingController();
~TracingController() override;
#if defined(V8_USE_PERFETTO)
// Must be called before StartTracing() if V8_USE_PERFETTO is true. Provides
// the output stream for the JSON trace data.
void InitializeForPerfetto(std::ostream* output_stream);
// Provide an optional listener for testing that will receive trace events.
// Must be called before StartTracing().
void SetTraceEventListenerForTesting(TraceEventListener* listener);
#else // defined(V8_USE_PERFETTO)
// The pointer returned from GetCategoryGroupEnabled() points to a value with
// zero or more of the following bits. Used in this class only. The
// TRACE_EVENT macros should only use the value as a bool. These values must
// be in sync with macro values in TraceEvent.h in Blink.
enum CategoryGroupEnabledFlags {
// Category group enabled for the recording mode.
ENABLED_FOR_RECORDING = 1 << 0,
// Category group enabled by SetEventCallbackEnabled().
ENABLED_FOR_EVENT_CALLBACK = 1 << 2,
// Category group enabled to export events to ETW.
ENABLED_FOR_ETW_EXPORT = 1 << 3
};
// Takes ownership of |trace_buffer|.
void Initialize(TraceBuffer* trace_buffer);
// v8::TracingController implementation.
const uint8_t* GetCategoryGroupEnabled(const char* category_group) override;
uint64_t AddTraceEvent(
char phase, const uint8_t* category_enabled_flag, const char* name,
const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
const char** arg_names, const uint8_t* arg_types,
const uint64_t* arg_values,
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
unsigned int flags) override;
uint64_t AddTraceEventWithTimestamp(
char phase, const uint8_t* category_enabled_flag, const char* name,
const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
const char** arg_names, const uint8_t* arg_types,
const uint64_t* arg_values,
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
unsigned int flags, int64_t timestamp) override;
void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
const char* name, uint64_t handle) override;
static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag);
void AddTraceStateObserver(
v8::TracingController::TraceStateObserver* observer) override;
void RemoveTraceStateObserver(
v8::TracingController::TraceStateObserver* observer) override;
#endif // !defined(V8_USE_PERFETTO)
void StartTracing(TraceConfig* trace_config);
void StopTracing();
protected:
#if !defined(V8_USE_PERFETTO)
virtual int64_t CurrentTimestampMicroseconds();
virtual int64_t CurrentCpuTimestampMicroseconds();
#endif // !defined(V8_USE_PERFETTO)
private:
#if !defined(V8_USE_PERFETTO)
void UpdateCategoryGroupEnabledFlag(size_t category_index);
void UpdateCategoryGroupEnabledFlags();
#endif // !defined(V8_USE_PERFETTO)
std::unique_ptr<base::Mutex> mutex_;
std::unique_ptr<TraceConfig> trace_config_;
std::atomic_bool recording_{false};
#if defined(V8_USE_PERFETTO_JSON_EXPORT)
std::unique_ptr<perfetto::trace_processor::TraceProcessorStorage>
trace_processor_;
#endif
#if defined(V8_USE_PERFETTO)
std::ostream* output_stream_ = nullptr;
TraceEventListener* listener_for_testing_ = nullptr;
std::unique_ptr<perfetto::TracingSession> tracing_session_;
#else // !defined(V8_USE_PERFETTO)
std::unordered_set<v8::TracingController::TraceStateObserver*> observers_;
std::unique_ptr<TraceBuffer> trace_buffer_;
#endif // !defined(V8_USE_PERFETTO)
// Disallow copy and assign
TracingController(const TracingController&) = delete;
void operator=(const TracingController&) = delete;
};
#undef V8_PLATFORM_NON_EXPORTED_BASE
} // namespace tracing
} // namespace platform
} // namespace v8
#endif // V8_LIBPLATFORM_V8_TRACING_H_

View File

@ -0,0 +1,625 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_ARRAY_BUFFER_H_
#define INCLUDE_V8_ARRAY_BUFFER_H_
#include <stddef.h>
#include <memory>
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-memory-span.h" // NOLINT(build/include_directory)
#include "v8-object.h" // NOLINT(build/include_directory)
#include "v8-platform.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class SharedArrayBuffer;
#if defined(V8_COMPRESS_POINTERS) && \
!defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
class IsolateGroup;
#endif
#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
// Defined using gn arg `v8_array_buffer_internal_field_count`.
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
#endif
enum class ArrayBufferCreationMode { kInternalized, kExternalized };
enum class BackingStoreInitializationMode { kZeroInitialized, kUninitialized };
enum class BackingStoreOnFailureMode { kReturnNull, kOutOfMemory };
/**
* A wrapper around the backing store (i.e. the raw memory) of an array buffer.
* See a document linked in http://crbug.com/v8/9908 for more information.
*
* The allocation and destruction of backing stores is generally managed by
* V8. Clients should always use standard C++ memory ownership types (i.e.
* std::unique_ptr and std::shared_ptr) to manage lifetimes of backing stores
* properly, since V8 internal objects may alias backing stores.
*
* This object does not keep the underlying |ArrayBuffer::Allocator| alive by
* default. Use Isolate::CreateParams::array_buffer_allocator_shared when
* creating the Isolate to make it hold a reference to the allocator itself.
*/
class V8_EXPORT BackingStore : public v8::internal::BackingStoreBase {
public:
~BackingStore();
/**
* Return a pointer to the beginning of the memory block for this backing
* store. The pointer is only valid as long as this backing store object
* lives.
*/
void* Data() const;
/**
* The length (in bytes) of this backing store.
*/
size_t ByteLength() const;
/**
* The maximum length (in bytes) that this backing store may grow to.
*
* If this backing store was created for a resizable ArrayBuffer or a growable
* SharedArrayBuffer, it is >= ByteLength(). Otherwise it is ==
* ByteLength().
*/
size_t MaxByteLength() const;
/**
* Indicates whether the backing store was created for an ArrayBuffer or
* a SharedArrayBuffer.
*/
bool IsShared() const;
/**
* Indicates whether the backing store is immutable.
*/
bool IsImmutable() const;
/**
* Indicates whether the backing store was created for a resizable ArrayBuffer
* or a growable SharedArrayBuffer, and thus may be resized by user JavaScript
* code.
*/
bool IsResizableByUserJavaScript() const;
/**
* Prevent implicit instantiation of operator delete with size_t argument.
* The size_t argument would be incorrect because ptr points to the
* internal BackingStore object.
*/
void operator delete(void* ptr) { ::operator delete(ptr); }
/**
* This callback is used only if the memory block for a BackingStore cannot be
* allocated with an ArrayBuffer::Allocator. In such cases the destructor of
* the BackingStore invokes the callback to free the memory block.
*/
using DeleterCallback = void (*)(void* data, size_t length,
void* deleter_data);
/**
* If the memory block of a BackingStore is static or is managed manually,
* then this empty deleter along with nullptr deleter_data can be passed to
* ArrayBuffer::NewBackingStore to indicate that.
*
* The manually managed case should be used with caution and only when it
* is guaranteed that the memory block freeing happens after detaching its
* ArrayBuffer.
*/
static void EmptyDeleter(void* data, size_t length, void* deleter_data);
private:
/**
* See [Shared]ArrayBuffer::GetBackingStore and
* [Shared]ArrayBuffer::NewBackingStore.
*/
BackingStore();
};
#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
// Use v8::BackingStore::DeleterCallback instead.
using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
void* deleter_data);
#endif
/**
* An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
*/
class V8_EXPORT ArrayBuffer : public Object {
public:
/**
* A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory.
* The allocator is a global V8 setting. It has to be set via
* Isolate::CreateParams.
*
* Memory allocated through this allocator by V8 is accounted for as external
* memory by V8. Note that V8 keeps track of the memory for all internalized
* |ArrayBuffer|s. Responsibility for tracking external memory (using
* Isolate::AdjustAmountOfExternalAllocatedMemory) is handed over to the
* embedder upon externalization and taken over upon internalization (creating
* an internalized buffer from an existing buffer).
*
* Note that it is unsafe to call back into V8 from any of the allocator
* functions.
*/
class V8_EXPORT Allocator {
public:
virtual ~Allocator() = default;
/**
* Allocate |length| bytes. Return nullptr if allocation is not successful.
* Memory should be initialized to zeroes.
*/
virtual void* Allocate(size_t length) = 0;
/**
* Allocate |length| bytes. Return nullptr if allocation is not successful.
* Memory does not have to be initialized.
*/
virtual void* AllocateUninitialized(size_t length) = 0;
/**
* Free the memory block of size |length|, pointed to by |data|.
* That memory is guaranteed to be previously allocated by |Allocate|.
*/
virtual void Free(void* data, size_t length) = 0;
/**
* Returns a size_t that determines the largest ArrayBuffer that can be
* allocated. Override if your Allocator is more restrictive than the
* default. Will only be called once, and the value returned will be
* cached.
* Should not return a value that is larger than kMaxByteLength.
*/
virtual size_t MaxAllocationSize() const { return kMaxByteLength; }
/**
* ArrayBuffer allocation mode. kNormal is a malloc/free style allocation,
* while kReservation is for larger allocations with the ability to set
* access permissions.
*/
enum class AllocationMode { kNormal, kReservation };
/**
* Returns page allocator used by this Allocator instance.
*
* When the sandbox used by Allocator it is expected that this returns
* sandbox's page allocator.
* Otherwise, it should return system page allocator.
*/
virtual PageAllocator* GetPageAllocator() { return nullptr; }
#if defined(V8_COMPRESS_POINTERS) && \
!defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
/**
* Convenience allocator.
*
* When the sandbox is enabled, this allocator will allocate its backing
* memory inside the sandbox that belongs to the passed isolate group.
* Otherwise, it will rely on malloc/free.
*
* Caller takes ownership, i.e. the returned object needs to be freed using
* |delete allocator| once it is no longer in use.
*/
static Allocator* NewDefaultAllocator(const IsolateGroup& group);
#endif // defined(V8_COMPRESS_POINTERS) &&
// !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
/**
* Convenience allocator.
*
* When the sandbox is enabled, this allocator will allocate its backing
* memory inside the default global sandbox. Otherwise, it will rely on
* malloc/free.
*
* Caller takes ownership, i.e. the returned object needs to be freed using
* |delete allocator| once it is no longer in use.
*/
static Allocator* NewDefaultAllocator();
};
/**
* Data length in bytes.
*/
size_t ByteLength() const;
/**
* Maximum length in bytes.
*/
size_t MaxByteLength() const;
/**
* Attempt to create a new ArrayBuffer. Allocate |byte_length| bytes.
* Allocated memory will be owned by a created ArrayBuffer and
* will be deallocated when it is garbage-collected,
* unless the object is externalized. If allocation fails, the Maybe
* returned will be empty.
*/
static MaybeLocal<ArrayBuffer> MaybeNew(
Isolate* isolate, size_t byte_length,
BackingStoreInitializationMode initialization_mode =
BackingStoreInitializationMode::kZeroInitialized);
/**
* Create a new ArrayBuffer. Allocate |byte_length| bytes, which are either
* zero-initialized or uninitialized. Allocated memory will be owned by a
* created ArrayBuffer and will be deallocated when it is garbage-collected,
* unless the object is externalized.
*/
static Local<ArrayBuffer> New(
Isolate* isolate, size_t byte_length,
BackingStoreInitializationMode initialization_mode =
BackingStoreInitializationMode::kZeroInitialized);
/**
* Create a new ArrayBuffer with an existing backing store.
* The created array keeps a reference to the backing store until the array
* is garbage collected. Note that the IsExternal bit does not affect this
* reference from the array to the backing store.
*
* In future IsExternal bit will be removed. Until then the bit is set as
* follows. If the backing store does not own the underlying buffer, then
* the array is created in externalized state. Otherwise, the array is created
* in internalized state. In the latter case the array can be transitioned
* to the externalized state using Externalize(backing_store).
*/
static Local<ArrayBuffer> New(Isolate* isolate,
std::shared_ptr<BackingStore> backing_store);
/**
* Returns a new standalone BackingStore that is allocated using the array
* buffer allocator of the isolate. The allocation can either be zero
* initialized, or uninitialized. The result can be later passed to
* ArrayBuffer::New.
*
* If the allocator returns nullptr, then the function may cause GCs in the
* given isolate and re-try the allocation.
*
* If GCs do not help and on_failure is kOutOfMemory, then the
* function will crash with an out-of-memory error.
*
* Otherwise if GCs do not help (or the allocation is too large for GCs to
* help) and on_failure is kReturnNull, then a null result is returned.
*/
static std::unique_ptr<BackingStore> NewBackingStore(
Isolate* isolate, size_t byte_length,
BackingStoreInitializationMode initialization_mode =
BackingStoreInitializationMode::kZeroInitialized,
BackingStoreOnFailureMode on_failure =
BackingStoreOnFailureMode::kOutOfMemory);
/**
* Returns a new standalone BackingStore that takes over the ownership of
* the given buffer. The destructor of the BackingStore invokes the given
* deleter callback.
*
* The result can be later passed to ArrayBuffer::New. The raw pointer
* to the buffer must not be passed again to any V8 API function.
*/
static std::unique_ptr<BackingStore> NewBackingStore(
void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
void* deleter_data);
/**
* Returns a new resizable standalone BackingStore that is allocated using the
* array buffer allocator of the isolate. The result can be later passed to
* ArrayBuffer::New.
*
* |byte_length| must be <= |max_byte_length|.
*
* This function is usable without an isolate. Unlike |NewBackingStore| calls
* with an isolate, GCs cannot be triggered, and there are no
* retries. Allocation failure will cause the function to crash with an
* out-of-memory error.
*/
static std::unique_ptr<BackingStore> NewResizableBackingStore(
size_t byte_length, size_t max_byte_length);
/**
* Returns true if this ArrayBuffer may be detached.
*/
bool IsDetachable() const;
/**
* Returns true if this ArrayBuffer has been detached.
*/
bool WasDetached() const;
/**
* Returns true if this ArrayBuffer is immutable.
*/
bool IsImmutable() const;
/**
* Detaches this ArrayBuffer and all its views (typed arrays).
* Detaching sets the byte length of the buffer and all typed arrays to zero,
* preventing JavaScript from ever accessing underlying backing store.
* ArrayBuffer should have been externalized and must be detachable.
*/
V8_DEPRECATED(
"Use the version which takes a key parameter (passing a null handle is "
"ok).")
void Detach();
/**
* Detaches this ArrayBuffer and all its views (typed arrays).
* Detaching sets the byte length of the buffer and all typed arrays to zero,
* preventing JavaScript from ever accessing underlying backing store.
* ArrayBuffer should have been externalized and must be detachable. Returns
* Nothing if the key didn't pass the [[ArrayBufferDetachKey]] check,
* Just(true) otherwise.
*/
V8_WARN_UNUSED_RESULT Maybe<bool> Detach(v8::Local<v8::Value> key);
/**
* Sets the ArrayBufferDetachKey.
*/
void SetDetachKey(v8::Local<v8::Value> key);
/**
* Get a shared pointer to the backing store of this array buffer. This
* pointer coordinates the lifetime management of the internal storage
* with any live ArrayBuffers on the heap, even across isolates. The embedder
* should not attempt to manage lifetime of the storage through other means.
*
* The returned shared pointer will not be empty, even if the ArrayBuffer has
* been detached. Use |WasDetached| to tell if it has been detached instead.
*/
std::shared_ptr<BackingStore> GetBackingStore();
/**
* More efficient shortcut for
* GetBackingStore()->IsResizableByUserJavaScript().
*/
bool IsResizableByUserJavaScript() const;
/**
* More efficient shortcut for GetBackingStore()->Data(). The returned pointer
* is valid as long as the ArrayBuffer is alive.
*/
void* Data() const;
V8_INLINE static ArrayBuffer* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<ArrayBuffer*>(value);
}
static constexpr int kInternalFieldCount =
V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
static constexpr int kEmbedderFieldCount = kInternalFieldCount;
#if V8_ENABLE_SANDBOX
static constexpr size_t kMaxByteLength =
internal::kMaxSafeBufferSizeForSandbox;
#elif V8_HOST_ARCH_32_BIT
static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
#else
// The maximum safe integer (2^53 - 1).
static constexpr size_t kMaxByteLength =
static_cast<size_t>((uint64_t{1} << 53) - 1);
#endif
private:
ArrayBuffer();
static void CheckCast(Value* obj);
friend class TypedArray;
};
#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
#endif
/**
* A base class for an instance of one of "views" over ArrayBuffer,
* including TypedArrays and DataView (ES6 draft 15.13).
*/
class V8_EXPORT ArrayBufferView : public Object {
public:
/**
* Returns underlying ArrayBuffer.
*/
Local<ArrayBuffer> Buffer();
/**
* Byte offset in |Buffer|.
*/
size_t ByteOffset();
/**
* Size of a view in bytes.
*/
size_t ByteLength();
/**
* Copy the contents of the ArrayBufferView's buffer to an embedder defined
* memory without additional overhead that calling ArrayBufferView::Buffer
* might incur.
*
* Will write at most min(|byte_length|, ByteLength) bytes starting at
* ByteOffset of the underlying buffer to the memory starting at |dest|.
* Returns the number of bytes actually written.
*/
size_t CopyContents(void* dest, size_t byte_length);
/**
* Returns the contents of the ArrayBufferView's buffer as a MemorySpan. If
* the contents are on the V8 heap, they get copied into `storage`. Otherwise
* a view into the off-heap backing store is returned. The provided storage
* should be at least as large as the maximum on-heap size of a TypedArray,
* was defined in gn with `typed_array_max_size_in_heap`. The default value is
* 64 bytes.
*/
v8::MemorySpan<uint8_t> GetContents(v8::MemorySpan<uint8_t> storage);
/**
* Returns true if ArrayBufferView's backing ArrayBuffer has already been
* allocated.
*/
bool HasBuffer() const;
V8_INLINE static ArrayBufferView* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<ArrayBufferView*>(value);
}
static constexpr int kInternalFieldCount =
V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
static const int kEmbedderFieldCount = kInternalFieldCount;
private:
ArrayBufferView();
static void CheckCast(Value* obj);
};
/**
* An instance of DataView constructor (ES6 draft 15.13.7).
*/
class V8_EXPORT DataView : public ArrayBufferView {
public:
static Local<DataView> New(Local<ArrayBuffer> array_buffer,
size_t byte_offset, size_t length);
static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
size_t byte_offset, size_t length);
V8_INLINE static DataView* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<DataView*>(value);
}
private:
DataView();
static void CheckCast(Value* obj);
};
/**
* An instance of the built-in SharedArrayBuffer constructor.
*/
class V8_EXPORT SharedArrayBuffer : public Object {
public:
/**
* Data length in bytes.
*/
size_t ByteLength() const;
/**
* Maximum length in bytes.
*/
size_t MaxByteLength() const;
/**
* Create a new SharedArrayBuffer. Allocate |byte_length| bytes, which are
* either zero-initialized or uninitialized. Allocated memory will be owned by
* a created SharedArrayBuffer and will be deallocated when it is
* garbage-collected, unless the object is externalized.
*/
static Local<SharedArrayBuffer> New(
Isolate* isolate, size_t byte_length,
BackingStoreInitializationMode initialization_mode =
BackingStoreInitializationMode::kZeroInitialized);
/**
* Create a new SharedArrayBuffer. Allocate |byte_length| bytes, which are
* either zero-initialized or uninitialized. Allocated memory will be owned by
* a created SharedArrayBuffer and will be deallocated when it is
* garbage-collected, unless the object is externalized. If allocation
* fails, the Maybe returned will be empty.
*/
static MaybeLocal<SharedArrayBuffer> MaybeNew(
Isolate* isolate, size_t byte_length,
BackingStoreInitializationMode initialization_mode =
BackingStoreInitializationMode::kZeroInitialized);
/**
* Create a new SharedArrayBuffer with an existing backing store.
* The created array keeps a reference to the backing store until the array
* is garbage collected. Note that the IsExternal bit does not affect this
* reference from the array to the backing store.
*
* In future IsExternal bit will be removed. Until then the bit is set as
* follows. If the backing store does not own the underlying buffer, then
* the array is created in externalized state. Otherwise, the array is created
* in internalized state. In the latter case the array can be transitioned
* to the externalized state using Externalize(backing_store).
*/
static Local<SharedArrayBuffer> New(
Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
/**
* Returns a new standalone BackingStore that is allocated using the array
* buffer allocator of the isolate. The allocation can either be zero
* initialized, or uninitialized. The result can be later passed to
* SharedArrayBuffer::New.
*
* If the allocator returns nullptr, then the function may cause GCs in the
* given isolate and re-try the allocation.
*
* If on_failure is kOutOfMemory and GCs do not help, then the function will
* crash with an out-of-memory error.
*
* Otherwise, if on_failure is kReturnNull and GCs do not help (or the
* byte_length is so large that the allocation cannot succeed), then a null
* result is returned.
*/
static std::unique_ptr<BackingStore> NewBackingStore(
Isolate* isolate, size_t byte_length,
BackingStoreInitializationMode initialization_mode =
BackingStoreInitializationMode::kZeroInitialized,
BackingStoreOnFailureMode on_failure =
BackingStoreOnFailureMode::kOutOfMemory);
/**
* Returns a new standalone BackingStore that takes over the ownership of
* the given buffer. The destructor of the BackingStore invokes the given
* deleter callback.
*
* The result can be later passed to SharedArrayBuffer::New. The raw pointer
* to the buffer must not be passed again to any V8 functions.
*/
static std::unique_ptr<BackingStore> NewBackingStore(
void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
void* deleter_data);
/**
* Get a shared pointer to the backing store of this array buffer. This
* pointer coordinates the lifetime management of the internal storage
* with any live ArrayBuffers on the heap, even across isolates. The embedder
* should not attempt to manage lifetime of the storage through other means.
*/
std::shared_ptr<BackingStore> GetBackingStore();
/**
* More efficient shortcut for GetBackingStore()->Data(). The returned pointer
* is valid as long as the ArrayBuffer is alive.
*/
void* Data() const;
V8_INLINE static SharedArrayBuffer* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<SharedArrayBuffer*>(value);
}
static constexpr int kInternalFieldCount =
V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
private:
SharedArrayBuffer();
static void CheckCast(Value* obj);
};
} // namespace v8
#endif // INCLUDE_V8_ARRAY_BUFFER_H_

View File

@ -0,0 +1,537 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_ISOLATE_CALLBACKS_H_
#define INCLUDE_V8_ISOLATE_CALLBACKS_H_
#include <stddef.h>
#include <functional>
#include <string>
#include "cppgc/common.h"
#include "v8-data.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-promise.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
#if defined(V8_OS_WIN)
struct _EXCEPTION_POINTERS;
#endif
namespace v8 {
template <typename T>
class FunctionCallbackInfo;
class Isolate;
class Message;
class Module;
class Object;
class Promise;
class ScriptOrModule;
class String;
class UnboundScript;
class Value;
/**
* A JIT code event is issued each time code is added, moved or removed.
*
* \note removal events are not currently issued.
*/
struct JitCodeEvent {
enum EventType {
CODE_ADDED,
CODE_MOVED,
CODE_REMOVED,
CODE_ADD_LINE_POS_INFO,
CODE_START_LINE_INFO_RECORDING,
CODE_END_LINE_INFO_RECORDING
};
// Definition of the code position type. The "POSITION" type means the place
// in the source code which are of interest when making stack traces to
// pin-point the source location of a stack frame as close as possible.
// The "STATEMENT_POSITION" means the place at the beginning of each
// statement, and is used to indicate possible break locations.
enum PositionType { POSITION, STATEMENT_POSITION };
// There are three different kinds of CodeType, one for JIT code generated
// by the optimizing compiler, one for byte code generated for the
// interpreter, and one for code generated from Wasm. For JIT_CODE and
// WASM_CODE, |code_start| points to the beginning of jitted assembly code,
// while for BYTE_CODE events, |code_start| points to the first bytecode of
// the interpreted function.
enum CodeType { BYTE_CODE, JIT_CODE, WASM_CODE };
// Type of event.
EventType type;
CodeType code_type;
// Start of the instructions.
void* code_start;
// Size of the instructions.
size_t code_len;
// Script info for CODE_ADDED event.
Local<UnboundScript> script;
// User-defined data for *_LINE_INFO_* event. It's used to hold the source
// code line information which is returned from the
// CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
// CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
void* user_data;
struct name_t {
// Name of the object associated with the code, note that the string is not
// zero-terminated.
const char* str;
// Number of chars in str.
size_t len;
};
struct line_info_t {
// PC offset
size_t offset;
// Code position
size_t pos;
// The position type.
PositionType position_type;
};
struct wasm_source_info_t {
// Source file name.
const char* filename;
// Length of filename.
size_t filename_size;
// Line number table, which maps offsets of JITted code to line numbers of
// source file.
const line_info_t* line_number_table;
// Number of entries in the line number table.
size_t line_number_table_size;
};
wasm_source_info_t* wasm_source_info = nullptr;
union {
// Only valid for CODE_ADDED.
struct name_t name;
// Only valid for CODE_ADD_LINE_POS_INFO
struct line_info_t line_info;
// New location of instructions. Only valid for CODE_MOVED.
void* new_code_start;
};
Isolate* isolate;
};
/**
* Option flags passed to the SetJitCodeEventHandler function.
*/
enum JitCodeEventOptions {
kJitCodeEventDefault = 0,
// Generate callbacks for already existent code.
kJitCodeEventEnumExisting = 1,
kLastJitCodeEventOption = kJitCodeEventEnumExisting
};
/**
* Callback function passed to SetJitCodeEventHandler.
*
* \param event code add, move or removal event.
*/
using JitCodeEventHandler = void (*)(const JitCodeEvent* event);
// --- Garbage Collection Callbacks ---
/**
* Applications can register callback functions which will be called before and
* after certain garbage collection operations. Allocations are not allowed in
* the callback functions, you therefore cannot manipulate objects (set or
* delete properties for example) since it is possible such operations will
* result in the allocation of objects.
* TODO(v8:12612): Deprecate kGCTypeMinorMarkSweep after updating blink.
*/
enum GCType {
kGCTypeScavenge = 1 << 0,
kGCTypeMinorMarkSweep = 1 << 1,
kGCTypeMarkSweepCompact = 1 << 2,
kGCTypeIncrementalMarking = 1 << 3,
kGCTypeProcessWeakCallbacks = 1 << 4,
kGCTypeAll = kGCTypeScavenge | kGCTypeMinorMarkSweep |
kGCTypeMarkSweepCompact | kGCTypeIncrementalMarking |
kGCTypeProcessWeakCallbacks
};
/**
* GCCallbackFlags is used to notify additional information about the GC
* callback.
* - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
* constructing retained object infos.
* - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
* - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
* is called synchronously without getting posted to an idle task.
* - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
* in a phase where V8 is trying to collect all available garbage
* (e.g., handling a low memory notification).
* - kGCCallbackScheduleIdleGarbageCollection: The GC callback is called to
* trigger an idle garbage collection.
*/
enum GCCallbackFlags {
kNoGCCallbackFlags = 0,
kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1,
kGCCallbackFlagForced = 1 << 2,
kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3,
kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4,
kGCCallbackFlagCollectAllExternalMemory = 1 << 5,
kGCCallbackScheduleIdleGarbageCollection = 1 << 6,
kGCCallbackFlagLastResort = 1 << 7,
};
using GCCallback = void (*)(GCType type, GCCallbackFlags flags);
using InterruptCallback = void (*)(Isolate* isolate, void* data);
using PrintCurrentStackTraceFilterCallback =
bool (*)(Isolate* isolate, Local<String> script_name);
/**
* This callback is invoked when the heap size is close to the heap limit and
* V8 is likely to abort with out-of-memory error.
* The callback can extend the heap limit by returning a value that is greater
* than the current_heap_limit. The initial heap limit is the limit that was
* set after heap setup.
*/
using NearHeapLimitCallback = size_t (*)(void* data, size_t current_heap_limit,
size_t initial_heap_limit);
/**
* Callback function passed to SetUnhandledExceptionCallback.
*/
#if defined(V8_OS_WIN)
using UnhandledExceptionCallback =
int (*)(_EXCEPTION_POINTERS* exception_pointers);
#endif
// --- Counters Callbacks ---
using CounterLookupCallback = int* (*)(const char* name);
using CreateHistogramCallback = void* (*)(const char* name, int min, int max,
size_t buckets);
using AddHistogramSampleCallback = void (*)(void* histogram, int sample);
// --- Exceptions ---
using FatalErrorCallback = void (*)(const char* location, const char* message);
struct OOMDetails {
bool is_heap_oom = false;
const char* detail = nullptr;
};
using OOMErrorCallback = void (*)(const char* location,
const OOMDetails& details);
using OOMErrorCallbackWithData = void (*)(const char* location,
const OOMDetails& details,
void* data);
using MessageCallback = void (*)(Local<Message> message, Local<Value> data);
// --- Tracing ---
enum LogEventStatus : int { kStart = 0, kEnd = 1, kLog = 2 };
using LogEventCallback = void (*)(const char* name,
int /* LogEventStatus */ status);
// --- Crashkeys Callback ---
enum class CrashKeyId {
kIsolateAddress,
kReadonlySpaceFirstPageAddress,
kMapSpaceFirstPageAddress V8_ENUM_DEPRECATE_SOON("Map space got removed"),
kOldSpaceFirstPageAddress,
kCodeRangeBaseAddress,
kCodeSpaceFirstPageAddress,
kDumpType,
kSnapshotChecksumCalculated,
kSnapshotChecksumExpected,
};
using AddCrashKeyCallback = void (*)(CrashKeyId id, const std::string& value);
// --- CrashKeyString Callbacks ---
using CrashKey = void*;
enum class CrashKeySize { Size32, Size64, Size256, Size1024 };
using AllocateCrashKeyStringCallback =
std::function<CrashKey(const char key[], CrashKeySize size)>;
using SetCrashKeyStringCallback =
std::function<void(CrashKey key, const std::string_view value)>;
// --- Enter/Leave Script Callback ---
using BeforeCallEnteredCallback = void (*)(Isolate*);
using CallCompletedCallback = void (*)(Isolate*);
// --- Modify Code Generation From Strings Callback ---
struct ModifyCodeGenerationFromStringsResult {
// If true, proceed with the codegen algorithm. Otherwise, block it.
bool codegen_allowed = false;
// Overwrite the original source with this string, if present.
// Use the original source if empty.
// This field is considered only if codegen_allowed is true.
MaybeLocal<String> modified_source;
};
/**
* Callback to check if codegen is allowed from a source object, and convert
* the source to string if necessary. See: ModifyCodeGenerationFromStrings.
*/
using ModifyCodeGenerationFromStringsCallback =
ModifyCodeGenerationFromStringsResult (*)(Local<Context> context,
Local<Value> source);
using ModifyCodeGenerationFromStringsCallback2 =
ModifyCodeGenerationFromStringsResult (*)(Local<Context> context,
Local<Value> source,
bool is_code_like);
// --- Failed Access Check Callback ---
/**
* Access type specification.
*/
enum AccessType {
ACCESS_GET,
ACCESS_SET,
ACCESS_HAS,
ACCESS_DELETE,
ACCESS_KEYS
};
using FailedAccessCheckCallback = void (*)(Local<Object> target,
AccessType type, Local<Value> data);
// --- WebAssembly compilation callbacks ---
using ExtensionCallback = bool (*)(const FunctionCallbackInfo<Value>&);
using AllowWasmCodeGenerationCallback = bool (*)(Local<Context> context,
Local<String> source);
// --- Callback for APIs defined on v8-supported objects, but implemented
// by the embedder. Example: WebAssembly.{compile|instantiate}Streaming ---
using ApiImplementationCallback = void (*)(const FunctionCallbackInfo<Value>&);
// --- Callback for WebAssembly.compileStreaming ---
using WasmStreamingCallback = void (*)(const FunctionCallbackInfo<Value>&);
enum class WasmAsyncSuccess { kSuccess, kFail };
// --- Callback called when async WebAssembly operations finish ---
using WasmAsyncResolvePromiseCallback = void (*)(
Isolate* isolate, Local<Context> context, Local<Promise::Resolver> resolver,
Local<Value> result, WasmAsyncSuccess success);
// --- Callback for loading source map file for Wasm profiling support
using WasmLoadSourceMapCallback = Local<String> (*)(Isolate* isolate,
const char* name);
// --- Callback for checking if WebAssembly Custom Descriptors are enabled ---
using WasmCustomDescriptorsEnabledCallback = bool (*)(Local<Context> context);
// --- Callback for checking if the SharedArrayBuffer constructor is enabled ---
using SharedArrayBufferConstructorEnabledCallback =
bool (*)(Local<Context> context);
// --- Callback for getting high resolution timestamps in Temporal ---
using TemporalHostSystemUTCEpochNanosecondsCallback =
int64_t (*)(Local<Context> context);
/**
* Import phases in import requests.
*/
enum class ModuleImportPhase {
kSource,
kDefer,
kEvaluation,
};
/**
* HostImportModuleDynamicallyCallback is called when we
* require the embedder to load a module. This is used as part of the dynamic
* import syntax.
*
* The referrer contains metadata about the script/module that calls
* import.
*
* The specifier is the name of the module that should be imported.
*
* The import_attributes are import attributes for this request in the form:
* [key1, value1, key2, value2, ...] where the keys and values are of type
* v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and
* returned from ModuleRequest::GetImportAttributes(), this array does not
* contain the source Locations of the attributes.
*
* The embedder must compile, instantiate, evaluate the Module, and
* obtain its namespace object.
*
* The Promise returned from this function is forwarded to userland
* JavaScript. The embedder must resolve this promise with the module
* namespace object. In case of an exception, the embedder must reject
* this promise with the exception. If the promise creation itself
* fails (e.g. due to stack overflow), the embedder must propagate
* that exception by returning an empty MaybeLocal.
*/
using HostImportModuleDynamicallyCallback = MaybeLocal<Promise> (*)(
Local<Context> context, Local<Data> host_defined_options,
Local<Value> resource_name, Local<String> specifier,
Local<FixedArray> import_attributes);
/**
* HostImportModuleWithPhaseDynamicallyCallback is called when we
* require the embedder to load a module with a specific phase. This is used
* as part of the dynamic import syntax.
*
* The referrer contains metadata about the script/module that calls
* import.
*
* The specifier is the name of the module that should be imported.
*
* The phase is the phase of the import requested.
*
* The import_attributes are import attributes for this request in the form:
* [key1, value1, key2, value2, ...] where the keys and values are of type
* v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and
* returned from ModuleRequest::GetImportAttributes(), this array does not
* contain the source Locations of the attributes.
*
* The Promise returned from this function is forwarded to userland
* JavaScript. The embedder must resolve this promise according to the phase
* requested:
* - For ModuleImportPhase::kSource, the promise must be resolved with a
* compiled ModuleSource object, or rejected with a SyntaxError if the
* module does not support source representation.
* - For ModuleImportPhase::kEvaluation, the promise must be resolved with a
* ModuleNamespace object of a module that has been compiled, instantiated,
* and evaluated.
*
* In case of an exception, the embedder must reject this promise with the
* exception. If the promise creation itself fails (e.g. due to stack
* overflow), the embedder must propagate that exception by returning an empty
* MaybeLocal.
*
* This callback is still experimental and is only invoked for source phase
* imports.
*/
using HostImportModuleWithPhaseDynamicallyCallback = MaybeLocal<Promise> (*)(
Local<Context> context, Local<Data> host_defined_options,
Local<Value> resource_name, Local<String> specifier,
ModuleImportPhase phase, Local<FixedArray> import_attributes);
/**
* Callback for requesting a compile hint for a function from the embedder. The
* first parameter is the position of the function in source code and the second
* parameter is embedder data to be passed back.
*/
using CompileHintCallback = bool (*)(int, void*);
/**
* HostInitializeImportMetaObjectCallback is called the first time import.meta
* is accessed for a module. Subsequent access will reuse the same value.
*
* The method combines two implementation-defined abstract operations into one:
* HostGetImportMetaProperties and HostFinalizeImportMeta.
*
* The embedder should use v8::Object::CreateDataProperty to add properties on
* the meta object.
*/
using HostInitializeImportMetaObjectCallback = void (*)(Local<Context> context,
Local<Module> module,
Local<Object> meta);
/**
* HostCreateShadowRealmContextCallback is called each time a ShadowRealm is
* being constructed in the initiator_context.
*
* The method combines Context creation and implementation defined abstract
* operation HostInitializeShadowRealm into one.
*
* The embedder should use v8::Context::New or v8::Context:NewFromSnapshot to
* create a new context. If the creation fails, the embedder must propagate
* that exception by returning an empty MaybeLocal.
*/
using HostCreateShadowRealmContextCallback =
MaybeLocal<Context> (*)(Local<Context> initiator_context);
/**
* IsJSApiWrapperNativeErrorCallback is called on an JSApiWrapper object to
* determine if Error.isError should return true or false. For instance, in an
* HTML embedder, DOMExceptions return true when passed to Error.isError.
*/
using IsJSApiWrapperNativeErrorCallback = bool (*)(Isolate* isolate,
Local<Object> obj);
/**
* PrepareStackTraceCallback is called when the stack property of an error is
* first accessed. The return value will be used as the stack value. If this
* callback is registed, the |Error.prepareStackTrace| API will be disabled.
* |sites| is an array of call sites, specified in
* https://v8.dev/docs/stack-trace-api
*/
using PrepareStackTraceCallback = MaybeLocal<Value> (*)(Local<Context> context,
Local<Value> error,
Local<Array> sites);
#if defined(V8_OS_WIN)
/**
* Callback to selectively enable ETW tracing based on the document URL.
* Implemented by the embedder, it should never call back into V8.
*
* Windows allows passing additional data to the ETW EnableCallback:
* https://learn.microsoft.com/en-us/windows/win32/api/evntprov/nc-evntprov-penablecallback
*
* This data can be configured in a WPR (Windows Performance Recorder)
* profile, adding a CustomFilter to an EventProvider like the following:
*
* <EventProvider Id=".." Name="57277741-3638-4A4B-BDBA-0AC6E45DA56C" Level="5">
* <CustomFilter Type="0x80000000" Value="AQABAAAAAAA..." />
* </EventProvider>
*
* Where:
* - Name="57277741-3638-4A4B-BDBA-0AC6E45DA56C" is the GUID of the V8
* ETW provider, (see src/libplatform/etw/etw-provider-win.h),
* - Type="0x80000000" is EVENT_FILTER_TYPE_SCHEMATIZED,
* - Value="AQABAAAAAA..." is a base64-encoded byte array that is
* base64-decoded by Windows and passed to the ETW enable callback in
* the 'PEVENT_FILTER_DESCRIPTOR FilterData' argument; see:
* https://learn.microsoft.com/en-us/windows/win32/api/evntprov/ns-evntprov-event_filter_descriptor.
*
* This array contains a struct EVENT_FILTER_HEADER followed by a
* variable length payload, and as payload we pass a string in JSON format,
* with a list of regular expressions that should match the document URL
* in order to enable ETW tracing:
* {
* "version": "2.0",
* "filtered_urls": [
* "https:\/\/.*\.chromium\.org\/.*", "https://v8.dev/";, "..."
* ],
* "trace_interpreter_frames": true
* }
*/
using FilterETWSessionByURLCallback =
bool (*)(Local<Context> context, const std::string& etw_filter_payload);
struct FilterETWSessionByURLResult {
// If true, enable ETW tracing for the current isolate.
bool enable_etw_tracing;
// If true, also enables ETW tracing for interpreter stack frames.
bool trace_interpreter_frames;
};
using FilterETWSessionByURL2Callback = FilterETWSessionByURLResult (*)(
Local<Context> context, const std::string& etw_filter_payload);
#endif // V8_OS_WIN
} // namespace v8
#endif // INCLUDE_V8_ISOLATE_CALLBACKS_H_

View File

@ -0,0 +1,183 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_CONTAINER_H_
#define INCLUDE_V8_CONTAINER_H_
#include <stddef.h>
#include <stdint.h>
#include <functional>
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-object.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
class Isolate;
/**
* An instance of the built-in array constructor (ECMA-262, 15.4.2).
*/
class V8_EXPORT Array : public Object {
public:
uint32_t Length() const;
/**
* Creates a JavaScript array with the given length. If the length
* is negative the returned array will have length 0.
*/
static Local<Array> New(Isolate* isolate, int length = 0);
/**
* Creates a JavaScript array out of a Local<Value> array in C++
* with a known length.
*/
static Local<Array> New(Isolate* isolate, Local<Value>* elements,
size_t length);
V8_INLINE static Array* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<Array*>(value);
}
/**
* Creates a JavaScript array from a provided callback.
*
* \param context The v8::Context to create the array in.
* \param length The length of the array to be created.
* \param next_value_callback The callback that is invoked to retrieve
* elements for the array. The embedder can signal that the array
* initialization should be aborted by throwing an exception and returning
* an empty MaybeLocal.
* \returns The v8::Array if all elements were constructed successfully and an
* empty MaybeLocal otherwise.
*/
static MaybeLocal<Array> New(
Local<Context> context, size_t length,
std::function<MaybeLocal<v8::Value>()> next_value_callback);
enum class CallbackResult {
kException,
kBreak,
kContinue,
};
using IterationCallback = CallbackResult (*)(uint32_t index,
Local<Value> element,
void* data);
/**
* Calls {callback} for every element of this array, passing {callback_data}
* as its {data} parameter.
* This function will typically be faster than calling {Get()} repeatedly.
* As a consequence of being optimized for low overhead, the provided
* callback must adhere to the following restrictions:
* - It must not allocate any V8 objects and continue iterating; it may
* allocate (e.g. an error message/object) and then immediately terminate
* the iteration.
* - It must not modify the array being iterated.
* - It must not call back into V8 (unless it can guarantee that such a
* call does not violate the above restrictions, which is difficult).
* - The {Local<Value> element} must not "escape", i.e. must not be assigned
* to any other {Local}. Creating a {Global} from it, or updating a
* v8::TypecheckWitness with it, is safe.
* These restrictions may be lifted in the future if use cases arise that
* justify a slower but more robust implementation.
*
* Returns {Nothing} on exception; use a {TryCatch} to catch and handle this
* exception.
* When the {callback} returns {kException}, iteration is terminated
* immediately, returning {Nothing}. By returning {kBreak}, the callback
* can request non-exceptional early termination of the iteration.
*/
Maybe<void> Iterate(Local<Context> context, IterationCallback callback,
void* callback_data);
private:
Array();
static void CheckCast(Value* obj);
};
/**
* An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1).
*/
class V8_EXPORT Map : public Object {
public:
size_t Size() const;
void Clear();
V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
Local<Value> key);
V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context,
Local<Value> key,
Local<Value> value);
V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
Local<Value> key);
V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
Local<Value> key);
/**
* Returns an array of length Size() * 2, where index N is the Nth key and
* index N + 1 is the Nth value.
*/
Local<Array> AsArray() const;
/**
* Creates a new empty Map.
*/
static Local<Map> New(Isolate* isolate);
V8_INLINE static Map* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<Map*>(value);
}
private:
Map();
static void CheckCast(Value* obj);
};
/**
* An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1).
*/
class V8_EXPORT Set : public Object {
public:
size_t Size() const;
void Clear();
V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context,
Local<Value> key);
V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
Local<Value> key);
V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
Local<Value> key);
/**
* Returns an array of the keys in this Set.
*/
Local<Array> AsArray() const;
/**
* Creates a new empty Set.
*/
static Local<Set> New(Isolate* isolate);
V8_INLINE static Set* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<Set*>(value);
}
private:
Set();
static void CheckCast(Value* obj);
};
} // namespace v8
#endif // INCLUDE_V8_CONTAINER_H_

588
vendor/artifacts/v8/include/v8-context.h vendored Normal file
View File

@ -0,0 +1,588 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_CONTEXT_H_
#define INCLUDE_V8_CONTEXT_H_
#include <stdint.h>
#include <vector>
#include "v8-data.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-maybe.h" // NOLINT(build/include_directory)
#include "v8-snapshot.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Function;
class MicrotaskQueue;
class Object;
class ObjectTemplate;
class Value;
class String;
/**
* A container for extension names.
*/
class V8_EXPORT ExtensionConfiguration {
public:
ExtensionConfiguration() : name_count_(0), names_(nullptr) {}
ExtensionConfiguration(int name_count, const char* names[])
: name_count_(name_count), names_(names) {}
const char** begin() const { return &names_[0]; }
const char** end() const { return &names_[name_count_]; }
private:
const int name_count_;
const char** names_;
};
/**
* A sandboxed execution context with its own set of built-in objects
* and functions.
*/
class V8_EXPORT Context : public Data {
public:
/**
* Returns the global proxy object.
*
* Global proxy object is a thin wrapper whose prototype points to actual
* context's global object with the properties like Object, etc. This is done
* that way for security reasons (for more details see
* https://wiki.mozilla.org/Gecko:SplitWindow).
*
* Please note that changes to global proxy object prototype most probably
* would break VM---v8 expects only global object as a prototype of global
* proxy object.
*/
Local<Object> Global();
/**
* Detaches the global object from its context before
* the global object can be reused to create a new context.
*/
void DetachGlobal();
/**
* Creates a new context and returns a handle to the newly allocated
* context.
*
* \param isolate The isolate in which to create the context.
*
* \param extensions An optional extension configuration containing
* the extensions to be installed in the newly created context.
*
* \param global_template An optional object template from which the
* global object for the newly created context will be created.
*
* \param global_object An optional global object to be reused for
* the newly created context. This global object must have been
* created by a previous call to Context::New with the same global
* template. The state of the global object will be completely reset
* and only object identify will remain.
*
* \param internal_fields_deserializer An optional callback used
* to deserialize fields set by
* v8::Object::SetAlignedPointerInInternalField() in wrapper objects
* from the default context snapshot. It should match the
* SerializeInternalFieldsCallback() used by
* v8::SnapshotCreator::SetDefaultContext() when the default context
* snapshot is created. It does not need to be configured if the default
* context snapshot contains no wrapper objects with pointer internal
* fields, or if no custom startup snapshot is configured
* in the v8::CreateParams used to create the isolate.
*
* \param microtask_queue An optional microtask queue used to manage
* the microtasks created in this context. If not set the per-isolate
* default microtask queue would be used.
*
* \param context_data_deserializer An optional callback used
* to deserialize embedder data set by
* v8::Context::SetAlignedPointerInEmbedderData() in the default
* context from the default context snapshot. It does not need to be
* configured if the default context snapshot contains no pointer embedder
* data, or if no custom startup snapshot is configured in the
* v8::CreateParams used to create the isolate.
*
* \param api_wrapper_deserializer An optional callback used to deserialize
* API wrapper objects that was initially set with v8::Object::Wrap() and then
* serialized using SerializeAPIWrapperCallback.
*/
static Local<Context> New(
Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
MaybeLocal<ObjectTemplate> global_template = MaybeLocal<ObjectTemplate>(),
MaybeLocal<Value> global_object = MaybeLocal<Value>(),
DeserializeInternalFieldsCallback internal_fields_deserializer =
DeserializeInternalFieldsCallback(),
MicrotaskQueue* microtask_queue = nullptr,
DeserializeContextDataCallback context_data_deserializer =
DeserializeContextDataCallback(),
DeserializeAPIWrapperCallback api_wrapper_deserializer =
DeserializeAPIWrapperCallback());
/**
* Create a new context from a (non-default) context snapshot. There
* is no way to provide a global object template since we do not create
* a new global object from template, but we can reuse a global object.
*
* \param isolate See v8::Context::New().
*
* \param context_snapshot_index The index of the context snapshot to
* deserialize from. Use v8::Context::New() for the default snapshot.
*
* \param internal_fields_deserializer An optional callback used
* to deserialize fields set by
* v8::Object::SetAlignedPointerInInternalField() in wrapper objects
* from the default context snapshot. It does not need to be
* configured if there are no wrapper objects with no internal
* pointer fields in the default context snapshot or if no startup
* snapshot is configured when the isolate is created.
*
* \param extensions See v8::Context::New().
*
* \param global_object See v8::Context::New().
*
* \param internal_fields_deserializer Similar to
* internal_fields_deserializer in v8::Context::New() but applies to
* the context specified by the context_snapshot_index.
*
* \param microtask_queue See v8::Context::New().
*
* \param context_data_deserializer Similar to
* context_data_deserializer in v8::Context::New() but applies to
* the context specified by the context_snapshot_index.
*
*\param api_wrapper_deserializer Similar to api_wrapper_deserializer in
* v8::Context::New() but applies to the context specified by the
* context_snapshot_index.
*/
static MaybeLocal<Context> FromSnapshot(
Isolate* isolate, size_t context_snapshot_index,
DeserializeInternalFieldsCallback internal_fields_deserializer =
DeserializeInternalFieldsCallback(),
ExtensionConfiguration* extensions = nullptr,
MaybeLocal<Value> global_object = MaybeLocal<Value>(),
MicrotaskQueue* microtask_queue = nullptr,
DeserializeContextDataCallback context_data_deserializer =
DeserializeContextDataCallback(),
DeserializeAPIWrapperCallback api_wrapper_deserializer =
DeserializeAPIWrapperCallback());
/**
* Returns an global object that isn't backed by an actual context.
*
* The global template needs to have access checks with handlers installed.
* If an existing global object is passed in, the global object is detached
* from its context.
*
* Note that this is different from a detached context where all accesses to
* the global proxy will fail. Instead, the access check handlers are invoked.
*
* It is also not possible to detach an object returned by this method.
* Instead, the access check handlers need to return nothing to achieve the
* same effect.
*
* It is possible, however, to create a new context from the global object
* returned by this method.
*/
static MaybeLocal<Object> NewRemoteContext(
Isolate* isolate, Local<ObjectTemplate> global_template,
MaybeLocal<Value> global_object = MaybeLocal<Value>());
/**
* Sets the security token for the context. To access an object in
* another context, the security tokens must match.
*/
void SetSecurityToken(Local<Value> token);
/** Restores the security token to the default value. */
void UseDefaultSecurityToken();
/** Returns the security token of this context.*/
Local<Value> GetSecurityToken();
/**
* Enter this context. After entering a context, all code compiled
* and run is compiled and run in this context. If another context
* is already entered, this old context is saved so it can be
* restored when the new context is exited.
*/
void Enter();
/**
* Exit this context. Exiting the current context restores the
* context that was in place when entering the current context.
*/
void Exit();
/**
* Delegate to help with Deep freezing embedder-specific objects (such as
* JSApiObjects) that can not be frozen natively.
*/
class DeepFreezeDelegate {
public:
/**
* Performs embedder-specific operations to freeze the provided embedder
* object. The provided object *will* be frozen by DeepFreeze after this
* function returns, so only embedder-specific objects need to be frozen.
* This function *may not* create new JS objects or perform JS allocations.
* Any v8 objects reachable from the provided embedder object that should
* also be considered for freezing should be added to the children_out
* parameter. Returns true if the operation completed successfully.
*/
virtual bool FreezeEmbedderObjectAndGetChildren(
Local<Object> obj, LocalVector<Object>& children_out) = 0;
};
/**
* Attempts to recursively freeze all objects reachable from this context.
* Some objects (generators, iterators, non-const closures) can not be frozen
* and will cause this method to throw an error. An optional delegate can be
* provided to help freeze embedder-specific objects.
*
* Freezing occurs in two steps:
* 1. "Marking" where we iterate through all objects reachable by this
* context, accumulating a list of objects that need to be frozen and
* looking for objects that can't be frozen. This step is separated because
* it is more efficient when we can assume there is no garbage collection.
* 2. "Freezing" where we go through the list of objects and freezing them.
* This effectively requires copying them so it may trigger garbage
* collection.
*/
Maybe<void> DeepFreeze(DeepFreezeDelegate* delegate = nullptr);
/** Returns the microtask queue associated with a current context. */
MicrotaskQueue* GetMicrotaskQueue();
/** Sets the microtask queue associated with the current context. */
void SetMicrotaskQueue(MicrotaskQueue* queue);
/**
* The field at kDebugIdIndex used to be reserved for the inspector.
* It now serves no purpose.
*/
enum EmbedderDataFields { kDebugIdIndex = 0 };
/**
* Return the number of fields allocated for embedder data.
*/
uint32_t GetNumberOfEmbedderDataFields();
/**
* Gets the embedder data with the given index, which must have been set by a
* previous call to SetEmbedderData with the same index.
*/
V8_INLINE Local<Data> GetEmbedderDataV2(int index);
/**
* Sets the embedder data with the given index, growing the data as
* needed. Note that index 0 currently has a special meaning for Chrome's
* debugger.
*/
void SetEmbedderDataV2(int index, Local<Data> value);
/**
* Gets the embedder data with the given index, which must have been set by a
* previous call to SetEmbedderData with the same index.
*/
V8_DEPRECATE_SOON("Use GetEmbedderDataV2 instead")
V8_INLINE Local<Value> GetEmbedderData(int index);
/**
* Gets the binding object used by V8 extras. Extra natives get a reference
* to this object and can use it to "export" functionality by adding
* properties. Extra natives can also "import" functionality by accessing
* properties added by the embedder using the V8 API.
*/
Local<Object> GetExtrasBindingObject();
/**
* Sets the embedder data with the given index, growing the data as
* needed. Note that index 0 currently has a special meaning for Chrome's
* debugger.
*/
V8_DEPRECATE_SOON("Use SetEmbedderDataV2 instead")
void SetEmbedderData(int index, Local<Value> value);
/**
* Gets a 2-byte-aligned native pointer from the embedder data with the given
* index, which must have been set by a previous call to
* SetAlignedPointerInEmbedderData with the same index. Note that index 0
* currently has a special meaning for Chrome's debugger.
*/
V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
EmbedderDataTypeTag tag);
V8_INLINE void* GetAlignedPointerFromEmbedderData(int index,
EmbedderDataTypeTag tag);
V8_DEPRECATED(
"Use GetAlignedPointerFromEmbedderData with EmbedderDataTypeTag "
"parameter instead.")
V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate,
int index) {
return GetAlignedPointerFromEmbedderData(isolate, index,
kEmbedderDataTypeTagDefault);
}
V8_DEPRECATED(
"Use GetAlignedPointerFromEmbedderData with EmbedderDataTypeTag "
"parameter instead.")
V8_INLINE void* GetAlignedPointerFromEmbedderData(int index) {
return GetAlignedPointerFromEmbedderData(index,
kEmbedderDataTypeTagDefault);
}
void SetAlignedPointerInEmbedderData(int index, void* value,
EmbedderDataTypeTag tag);
/**
* Sets a 2-byte-aligned native pointer in the embedder data with the given
* index, growing the data as needed. Note that index 0 currently has a
* special meaning for Chrome's debugger.
*/
V8_DEPRECATED(
"Use SetAlignedPointerInEmbedderData with EmbedderDataTypeTag parameter "
"instead.")
void SetAlignedPointerInEmbedderData(int index, void* value) {
SetAlignedPointerInEmbedderData(index, value, kEmbedderDataTypeTagDefault);
}
/**
* Control whether code generation from strings is allowed. Calling
* this method with false will disable 'eval' and the 'Function'
* constructor for code running in this context. If 'eval' or the
* 'Function' constructor are used an exception will be thrown.
*
* If code generation from strings is not allowed the
* V8::ModifyCodeGenerationFromStringsCallback callback will be invoked if
* set before blocking the call to 'eval' or the 'Function'
* constructor. If that callback returns true, the call will be
* allowed, otherwise an exception will be thrown. If no callback is
* set an exception will be thrown.
*/
void AllowCodeGenerationFromStrings(bool allow);
/**
* Returns true if code generation from strings is allowed for the context.
* For more details see AllowCodeGenerationFromStrings(bool) documentation.
*/
bool IsCodeGenerationFromStringsAllowed() const;
/**
* Sets the error description for the exception that is thrown when
* code generation from strings is not allowed and 'eval' or the 'Function'
* constructor are called.
*/
void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
/**
* Sets the error description for the exception that is thrown when
* wasm code generation is not allowed.
*/
void SetErrorMessageForWasmCodeGeneration(Local<String> message);
/**
* Return data that was previously attached to the context snapshot via
* SnapshotCreator, and removes the reference to it.
* Repeated call with the same index returns an empty MaybeLocal.
*/
template <class T>
V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
/**
* If callback is set, abort any attempt to execute JavaScript in this
* context, call the specified callback, and throw an exception.
* To unset abort, pass nullptr as callback.
*/
using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
Local<Context> context);
void SetAbortScriptExecution(AbortScriptExecutionCallback callback);
/**
* Set callback for getting high resolution timestamps in Temporal.
*/
using TemporalHostSystemUTCEpochNanosecondsCallback =
int64_t (*)(Local<Context> context);
void SetTemporalHostSystemUTCEpochNanosecondsCallback(
TemporalHostSystemUTCEpochNanosecondsCallback callback);
/**
* Set or clear hooks to be invoked for promise lifecycle operations.
* To clear a hook, set it to an empty v8::Function. Each function will
* receive the observed promise as the first argument. If a chaining
* operation is used on a promise, the init will additionally receive
* the parent promise as the second argument.
*/
void SetPromiseHooks(Local<Function> init_hook, Local<Function> before_hook,
Local<Function> after_hook,
Local<Function> resolve_hook);
bool HasTemplateLiteralObject(Local<Value> object);
/**
* Stack-allocated class which sets the execution context for all
* operations executed within a local scope.
*/
class V8_NODISCARD Scope {
public:
explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
context_->Enter();
}
V8_INLINE ~Scope() { context_->Exit(); }
private:
Local<Context> context_;
};
/**
* Stack-allocated class to support the backup incumbent settings object
* stack.
* https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
*/
class V8_EXPORT V8_NODISCARD BackupIncumbentScope final {
public:
/**
* |backup_incumbent_context| is pushed onto the backup incumbent settings
* object stack.
*/
explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
~BackupIncumbentScope();
private:
friend class internal::Isolate;
uintptr_t JSStackComparableAddressPrivate() const {
return js_stack_comparable_address_;
}
Local<Context> backup_incumbent_context_;
uintptr_t js_stack_comparable_address_ = 0;
const BackupIncumbentScope* prev_ = nullptr;
};
V8_INLINE static Context* Cast(Data* data);
private:
friend class Value;
friend class Script;
friend class Object;
friend class Function;
static void CheckCast(Data* obj);
internal::ValueHelper::InternalRepresentationType GetDataFromSnapshotOnce(
size_t index);
Local<Value> SlowGetEmbedderData(int index);
Local<Data> SlowGetEmbedderDataV2(int index);
void* SlowGetAlignedPointerFromEmbedderData(int index,
EmbedderDataTypeTag tag);
};
// --- Implementation ---
Local<Value> Context::GetEmbedderData(int index) {
#ifndef V8_ENABLE_CHECKS
using A = internal::Address;
using I = internal::Internals;
A ctx = internal::ValueHelper::ValueAsAddress(this);
A embedder_data =
I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
int value_offset =
I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
A value = I::ReadRawField<A>(embedder_data, value_offset);
#ifdef V8_COMPRESS_POINTERS
// We read the full pointer value and then decompress it in order to avoid
// dealing with potential endianness issues.
value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
#endif
auto* isolate = I::GetCurrentIsolate();
return Local<Value>::New(isolate, value);
#else
return SlowGetEmbedderData(index);
#endif
}
V8_INLINE Local<Data> Context::GetEmbedderDataV2(int index) {
#ifndef V8_ENABLE_CHECKS
using A = internal::Address;
using I = internal::Internals;
A ctx = internal::ValueHelper::ValueAsAddress(this);
A embedder_data =
I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
int value_offset =
I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
A value = I::ReadRawField<A>(embedder_data, value_offset);
#ifdef V8_COMPRESS_POINTERS
// We read the full pointer value and then decompress it in order to avoid
// dealing with potential endianness issues.
value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
#endif
auto* isolate = I::GetCurrentIsolate();
return Local<Data>::New(isolate, value);
#else
return SlowGetEmbedderDataV2(index);
#endif
}
void* Context::GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
EmbedderDataTypeTag tag) {
#if !defined(V8_ENABLE_CHECKS)
using A = internal::Address;
using I = internal::Internals;
A ctx = internal::ValueHelper::ValueAsAddress(this);
A embedder_data =
I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
int value_offset = I::kEmbedderDataArrayHeaderSize +
(I::kEmbedderDataSlotSize * index) +
I::kEmbedderDataSlotExternalPointerOffset;
return reinterpret_cast<void*>(I::ReadExternalPointerField(
isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
#else
return SlowGetAlignedPointerFromEmbedderData(index, tag);
#endif
}
void* Context::GetAlignedPointerFromEmbedderData(int index,
EmbedderDataTypeTag tag) {
#if !defined(V8_ENABLE_CHECKS)
using A = internal::Address;
using I = internal::Internals;
A ctx = internal::ValueHelper::ValueAsAddress(this);
A embedder_data =
I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
int value_offset = I::kEmbedderDataArrayHeaderSize +
(I::kEmbedderDataSlotSize * index) +
I::kEmbedderDataSlotExternalPointerOffset;
Isolate* isolate = I::GetCurrentIsolateForSandbox();
return reinterpret_cast<void*>(I::ReadExternalPointerField(
isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
#else
return SlowGetAlignedPointerFromEmbedderData(index, tag);
#endif
}
template <class T>
MaybeLocal<T> Context::GetDataFromSnapshotOnce(size_t index) {
if (auto repr = GetDataFromSnapshotOnce(index);
repr != internal::ValueHelper::kEmpty) {
internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
return Local<T>::FromRepr(repr);
}
return {};
}
Context* Context::Cast(v8::Data* data) {
#ifdef V8_ENABLE_CHECKS
CheckCast(data);
#endif
return static_cast<Context*>(data);
}
} // namespace v8
#endif // INCLUDE_V8_CONTEXT_H_

View File

@ -0,0 +1,56 @@
// Copyright 2025 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_HEAP_EXTERNAL_H_
#define INCLUDE_V8_HEAP_EXTERNAL_H_
#include "cppgc/type-traits.h" // NOLINT(build/include_directory)
#include "v8-sandbox.h" // NOLINT(build/include_directory)
#include "v8-value.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Isolate;
/**
* A JavaScript value that wraps a `cppgc::GarbageCollected<T>` object allocated
* on the managed C++ heap (CppHeap). This type of value is mainly used to
* associate C++ data structures which aren't exposed to JavaScript with
* JavaScript objects.
*/
class V8_EXPORT CppHeapExternal : public Data {
public:
template <typename T>
static Local<CppHeapExternal> New(Isolate* isolate, T* value,
CppHeapPointerTag tag) {
static_assert(cppgc::IsGarbageCollectedTypeV<T>,
"Object must be of type GarbageCollected.");
return NewImpl(isolate, value, tag);
}
V8_INLINE static CppHeapExternal* Cast(Data* data) {
#ifdef V8_ENABLE_CHECKS
CheckCast(data);
#endif
return static_cast<CppHeapExternal*>(data);
}
template <typename T>
T* Value(Isolate* isolate, CppHeapPointerTagRange tag_range) const {
static_assert(cppgc::IsGarbageCollectedTypeV<T>,
"Object must be of type GarbageCollected.");
return static_cast<T*>(ValueImpl(isolate, tag_range));
}
private:
static void CheckCast(v8::Data* obj);
static Local<CppHeapExternal> NewImpl(Isolate* isolate, void* value,
CppHeapPointerTag tag);
void* ValueImpl(Isolate*, CppHeapPointerTagRange tag_range) const;
};
} // namespace v8
#endif // INCLUDE_V8_HEAP_EXTERNAL_H_

199
vendor/artifacts/v8/include/v8-cppgc.h vendored Normal file
View File

@ -0,0 +1,199 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_CPPGC_H_
#define INCLUDE_V8_CPPGC_H_
#include <cstdint>
#include <memory>
#include <vector>
#include "cppgc/common.h"
#include "cppgc/custom-space.h"
#include "cppgc/heap-statistics.h"
#include "cppgc/visitor.h"
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8-platform.h" // NOLINT(build/include_directory)
#include "v8-traced-handle.h" // NOLINT(build/include_directory)
namespace cppgc {
class AllocationHandle;
class HeapHandle;
} // namespace cppgc
namespace v8 {
class Object;
namespace internal {
class CppHeap;
} // namespace internal
class CustomSpaceStatisticsReceiver;
struct V8_EXPORT CppHeapCreateParams {
explicit CppHeapCreateParams(
std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces)
: custom_spaces(std::move(custom_spaces)) {}
CppHeapCreateParams(const CppHeapCreateParams&) = delete;
CppHeapCreateParams& operator=(const CppHeapCreateParams&) = delete;
std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces;
/**
* Specifies which kind of marking are supported by the heap. The type may be
* further reduced via runtime flags when attaching the heap to an Isolate.
*/
cppgc::Heap::MarkingType marking_support =
cppgc::Heap::MarkingType::kIncrementalAndConcurrent;
/**
* Specifies which kind of sweeping is supported by the heap. The type may be
* further reduced via runtime flags when attaching the heap to an Isolate.
*/
cppgc::Heap::SweepingType sweeping_support =
cppgc::Heap::SweepingType::kIncrementalAndConcurrent;
/**
* Optional marker representing the stack start of the thread creating the
* heap.
*/
std::optional<cppgc::StackStartMarker> stack_start_marker = std::nullopt;
};
/**
* A heap for allocating managed C++ objects.
*
* Similar to v8::Isolate, the heap may only be accessed from one thread at a
* time. The heap may be used from different threads using the
* v8::Locker/v8::Unlocker APIs which is different from generic Oilpan.
*/
class V8_EXPORT CppHeap {
public:
static std::unique_ptr<CppHeap> Create(v8::Platform* platform,
const CppHeapCreateParams& params);
virtual ~CppHeap() = default;
/**
* \returns the opaque handle for allocating objects using
* `MakeGarbageCollected()`.
*/
cppgc::AllocationHandle& GetAllocationHandle();
/**
* \returns the opaque heap handle which may be used to refer to this heap in
* other APIs. Valid as long as the underlying `CppHeap` is alive.
*/
cppgc::HeapHandle& GetHeapHandle();
/**
* Terminate clears all roots and performs multiple garbage collections to
* reclaim potentially newly created objects in destructors.
*
* After this call, object allocation is prohibited.
*/
V8_DEPRECATED("Terminate gets automatically called in the CppHeap destructor")
void Terminate();
/**
* \param detail_level specifies whether should return detailed
* statistics or only brief summary statistics.
* \returns current CppHeap statistics regarding memory consumption
* and utilization.
*/
cppgc::HeapStatistics CollectStatistics(
cppgc::HeapStatistics::DetailLevel detail_level);
/**
* Collects statistics for the given spaces and reports them to the receiver.
*
* \param custom_spaces a collection of custom space indices.
* \param receiver an object that gets the results.
*/
void CollectCustomSpaceStatisticsAtLastGC(
std::vector<cppgc::CustomSpaceIndex> custom_spaces,
std::unique_ptr<CustomSpaceStatisticsReceiver> receiver);
/**
* Enables a detached mode that allows testing garbage collection using
* `cppgc::testing` APIs. Once used, the heap cannot be attached to an
* `Isolate` anymore.
*/
void EnableDetachedGarbageCollectionsForTesting();
/**
* Performs a stop-the-world garbage collection for testing purposes.
*
* \param stack_state The stack state to assume for the garbage collection.
*/
void CollectGarbageForTesting(cppgc::EmbedderStackState stack_state);
/**
* Performs a stop-the-world minor garbage collection for testing purposes.
*
* \param stack_state The stack state to assume for the garbage collection.
*/
void CollectGarbageInYoungGenerationForTesting(
cppgc::EmbedderStackState stack_state);
private:
CppHeap() = default;
friend class internal::CppHeap;
};
class JSVisitor : public cppgc::Visitor {
public:
explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {}
~JSVisitor() override = default;
void Trace(const TracedReferenceBase& ref) {
if (ref.IsEmptyThreadSafe()) return;
Visit(ref);
}
protected:
using cppgc::Visitor::Visit;
virtual void Visit(const TracedReferenceBase& ref) {}
};
/**
* Provided as input to `CppHeap::CollectCustomSpaceStatisticsAtLastGC()`.
*
* Its method is invoked with the results of the statistic collection.
*/
class CustomSpaceStatisticsReceiver {
public:
virtual ~CustomSpaceStatisticsReceiver() = default;
/**
* Reports the size of a space at the last GC. It is called for each space
* that was requested in `CollectCustomSpaceStatisticsAtLastGC()`.
*
* \param space_index The index of the space.
* \param bytes The total size of live objects in the space at the last GC.
* It is zero if there was no GC yet.
*/
virtual void AllocatedBytes(cppgc::CustomSpaceIndex space_index,
size_t bytes) = 0;
};
} // namespace v8
namespace cppgc {
template <typename T>
struct TraceTrait<v8::TracedReference<T>> {
static cppgc::TraceDescriptor GetTraceDescriptor(const void* self) {
return {nullptr, Trace};
}
static void Trace(Visitor* visitor, const void* self) {
static_cast<v8::JSVisitor*>(visitor)->Trace(
*static_cast<const v8::TracedReference<T>*>(self));
}
};
} // namespace cppgc
#endif // INCLUDE_V8_CPPGC_H_

96
vendor/artifacts/v8/include/v8-data.h vendored Normal file
View File

@ -0,0 +1,96 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_DATA_H_
#define INCLUDE_V8_DATA_H_
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
/**
* The superclass of objects that can reside on V8's heap.
*/
class V8_EXPORT Data {
public:
/**
* Returns true if this data is a |v8::Value|.
*/
bool IsValue() const;
/**
* Returns true if this data is a |v8::Module|.
*/
bool IsModule() const;
/**
* Returns true if this data is a |v8::ModuleRequest|.
*/
bool IsModuleRequest() const;
/**
* Returns tru if this data is a |v8::FixedArray|
*/
bool IsFixedArray() const;
/**
* Returns true if this data is a |v8::Private|.
*/
bool IsPrivate() const;
/**
* Returns true if this data is a |v8::ObjectTemplate|.
*/
bool IsObjectTemplate() const;
/**
* Returns true if this data is a |v8::FunctionTemplate|.
*/
bool IsFunctionTemplate() const;
/**
* Returns true if this data is a |v8::DictionaryTemplate|.
*/
bool IsDictionaryTemplate() const;
/**
* Returns true if this data is a |v8::Context|.
*/
bool IsContext() const;
/**
* Returns true if this value is a `CppHeapExternal` object.
*/
bool IsCppHeapExternal() const;
private:
Data() = delete;
};
/**
* A fixed-sized array with elements of type Data.
*/
class V8_EXPORT FixedArray : public Data {
public:
int Length() const;
Local<Data> Get(int i) const;
V8_INLINE static FixedArray* Cast(Data* data) {
#ifdef V8_ENABLE_CHECKS
CheckCast(data);
#endif
return reinterpret_cast<FixedArray*>(data);
}
private:
static void CheckCast(Data* obj);
};
} // namespace v8
#endif // INCLUDE_V8_DATA_H_

57
vendor/artifacts/v8/include/v8-date.h vendored Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_DATE_H_
#define INCLUDE_V8_DATE_H_
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-object.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
/**
* An instance of the built-in Date constructor (ECMA-262, 15.9).
*/
class V8_EXPORT Date : public Object {
public:
static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context,
double time);
static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
Local<Context> context,
Local<String> date_string);
/**
* A specialization of Value::NumberValue that is more efficient
* because we know the structure of this object.
*/
double ValueOf() const;
/**
* Generates ISO string representation.
*/
v8::Local<v8::String> ToISOString() const;
/**
* Generates UTC string representation.
*/
v8::Local<v8::String> ToUTCString() const;
V8_INLINE static Date* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<Date*>(value);
}
private:
static void CheckCast(Value* obj);
};
} // namespace v8
#endif // INCLUDE_V8_DATE_H_

231
vendor/artifacts/v8/include/v8-debug.h vendored Normal file
View File

@ -0,0 +1,231 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_DEBUG_H_
#define INCLUDE_V8_DEBUG_H_
#include <stdint.h>
#include "v8-script.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Isolate;
class String;
/**
* A single JavaScript stack frame.
*/
class V8_EXPORT StackFrame {
public:
/**
* Returns the source location, 0-based, for the associated function call.
*/
Location GetLocation() const;
/**
* Returns the number, 1-based, of the line for the associate function call.
* This method will return Message::kNoLineNumberInfo if it is unable to
* retrieve the line number, or if kLineNumber was not passed as an option
* when capturing the StackTrace.
*/
int GetLineNumber() const { return GetLocation().GetLineNumber() + 1; }
/**
* Returns the 1-based column offset on the line for the associated function
* call.
* This method will return Message::kNoColumnInfo if it is unable to retrieve
* the column number, or if kColumnOffset was not passed as an option when
* capturing the StackTrace.
*/
int GetColumn() const { return GetLocation().GetColumnNumber() + 1; }
/**
* Returns zero based source position (character offset) for the associated
* function.
*/
int GetSourcePosition() const;
/**
* Returns the id of the script for the function for this StackFrame.
* This method will return Message::kNoScriptIdInfo if it is unable to
* retrieve the script id, or if kScriptId was not passed as an option when
* capturing the StackTrace.
*/
int GetScriptId() const;
/**
* Returns the name of the resource that contains the script for the
* function for this StackFrame.
*/
Local<String> GetScriptName() const;
/**
* Returns the name of the resource that contains the script for the
* function for this StackFrame or sourceURL value if the script name
* is undefined and its source ends with //# sourceURL=... string or
* deprecated //@ sourceURL=... string.
*/
Local<String> GetScriptNameOrSourceURL() const;
/**
* Returns the source of the script for the function for this StackFrame.
*/
Local<String> GetScriptSource() const;
/**
* Returns the source mapping URL (if one is present) of the script for
* the function for this StackFrame.
*/
Local<String> GetScriptSourceMappingURL() const;
/**
* Returns the name of the function associated with this stack frame.
*/
Local<String> GetFunctionName() const;
/**
* Returns whether or not the associated function is compiled via a call to
* eval().
*/
bool IsEval() const;
/**
* Returns whether or not the associated function is called as a
* constructor via "new".
*/
bool IsConstructor() const;
/**
* Returns whether or not the associated functions is defined in wasm.
*/
bool IsWasm() const;
/**
* Returns whether or not the associated function is defined by the user.
*/
bool IsUserJavaScript() const;
};
/**
* Representation of a JavaScript stack trace. The information collected is a
* snapshot of the execution stack and the information remains valid after
* execution continues.
*/
class V8_EXPORT StackTrace {
public:
/**
* Flags that determine what information is placed captured for each
* StackFrame when grabbing the current stack trace.
* Note: these options are deprecated and we always collect all available
* information (kDetailed).
*/
enum StackTraceOptions {
kLineNumber = 1,
kColumnOffset = 1 << 1 | kLineNumber,
kScriptName = 1 << 2,
kFunctionName = 1 << 3,
kIsEval = 1 << 4,
kIsConstructor = 1 << 5,
kScriptNameOrSourceURL = 1 << 6,
kScriptId = 1 << 7,
kExposeFramesAcrossSecurityOrigins = 1 << 8,
kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
};
struct ScriptIdAndContext {
int id;
v8::Local<v8::Context> context;
};
struct ScriptData {
int id;
v8::Local<v8::Function> function;
v8::Local<v8::Context> context;
};
/**
* Returns the (unique) ID of this stack trace.
*/
int GetID() const;
/**
* Returns a StackFrame at a particular index.
*/
Local<StackFrame> GetFrame(Isolate* isolate, uint32_t index) const;
/**
* Returns the number of StackFrames.
*/
int GetFrameCount() const;
/**
* Grab a snapshot of the current JavaScript execution stack.
*
* \param frame_limit The maximum number of stack frames we want to capture.
* \param options Enumerates the set of things we will capture for each
* StackFrame.
*/
static Local<StackTrace> CurrentStackTrace(
Isolate* isolate, int frame_limit, StackTraceOptions options = kDetailed);
/**
* Returns the first valid script name or source URL starting at the top of
* the JS stack. The returned string is either an empty handle if no script
* name/url was found or a non-zero-length string.
*
* This method is equivalent to calling StackTrace::CurrentStackTrace and
* walking the resulting frames from the beginning until a non-empty script
* name/url is found. The difference is that this method won't allocate
* a stack trace.
*/
static Local<String> CurrentScriptNameOrSourceURL(Isolate* isolate);
/**
* Returns the first valid script id at the top of the JS stack. The returned
* value is Message::kNoScriptIdInfo if no id was found.
*
* This method is equivalent to calling StackTrace::CurrentStackTrace and
* walking the resulting frames from the beginning until a non-empty id is
* found. The difference is that this method won't allocate a stack trace.
*/
static int CurrentScriptId(Isolate* isolate);
/**
* Writes up to the first `frame_data.size()` valid script ids and function
* contexts at the top of the JS stack into the given span. Returns a span
* sized to the number of frames worth of data written. It's similar to the
* CurrentStackTrace method but doesn't allocate a stack trace. Further, it
* skips frames that don't have valid script ids or function contexts. The
* final difference is that the script id written for evals or regexp is that
* of the script that ran eval() or regexp, not the current context.
*
*/
V8_DEPRECATE_SOON("Use CurrentScriptData instead")
static v8::MemorySpan<v8::StackTrace::ScriptIdAndContext>
CurrentScriptIdsAndContexts(Isolate* isolate,
v8::MemorySpan<ScriptIdAndContext> frame_data);
/**
* Writes up to the first `frame_data.size()` valid script ids, functions, and
* contexts at the top of the JS stack into the given span. Returns a span
* sized to the number of frames worth of data written. It's similar to the
* CurrentStackTrace method but doesn't allocate a stack trace. Further, it
* skips non-js frames and frames that don't have valid script ids or function
* contexts. The final difference is that the script id written for evals or
* regexp is that of the script that ran eval() or regexp, not the current
* context.
*
* WARNING: This is an unfinished experimental feature. Semantics and
* implementation may change frequently.
*/
static v8::MemorySpan<v8::StackTrace::ScriptData> CurrentScriptData(
Isolate* isolate, v8::MemorySpan<ScriptData> frame_data);
};
} // namespace v8
#endif // INCLUDE_V8_DEBUG_H_

View File

@ -0,0 +1,54 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_EMBEDDER_HEAP_H_
#define INCLUDE_V8_EMBEDDER_HEAP_H_
#include "v8-traced-handle.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
namespace internal {
class TracedHandles;
} // namespace internal
class Isolate;
class Value;
/**
* Handler for embedder roots on non-unified heap garbage collections.
*/
class V8_EXPORT EmbedderRootsHandler {
public:
virtual ~EmbedderRootsHandler() = default;
EmbedderRootsHandler() = default;
/**
* Used in combination with |IsRoot|. Called by V8 when an
* object that is backed by a handle is reclaimed by a non-tracing garbage
* collection. It is up to the embedder to reset the original handle.
*
* Note that the |handle| is different from the handle that the embedder holds
* for retaining the object. It is up to the embedder to find the original
* handle via the object or class id.
*/
virtual void ResetRoot(const v8::TracedReference<v8::Value>& handle) = 0;
/**
* Similar to |ResetRoot()|, but opportunistic. The function is called in
* parallel for different handles and as such must be thread-safe. In case,
* |false| is returned, |ResetRoot()| will be recalled for the same handle.
*/
virtual bool TryResetRoot(const v8::TracedReference<v8::Value>& handle) {
return false;
}
private:
friend class internal::TracedHandles;
};
} // namespace v8
#endif // INCLUDE_V8_EMBEDDER_HEAP_H_

View File

@ -0,0 +1,52 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_EMBEDDER_STATE_SCOPE_H_
#define INCLUDE_V8_EMBEDDER_STATE_SCOPE_H_
#include <memory>
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
namespace internal {
class EmbedderState;
} // namespace internal
// A StateTag represents a possible state of the embedder.
enum class EmbedderStateTag : uint8_t {
// reserved
EMPTY = 0,
OTHER = 1,
// embedder can define any state after
};
// A stack-allocated class that manages an embedder state on the isolate.
// After an EmbedderState scope has been created, a new embedder state will be
// pushed on the isolate stack.
class V8_EXPORT EmbedderStateScope {
public:
EmbedderStateScope(Isolate* isolate, Local<v8::Context> context,
EmbedderStateTag tag);
~EmbedderStateScope();
private:
// Declaring operator new and delete as deleted is not spec compliant.
// Therefore declare them private instead to disable dynamic alloc
void* operator new(size_t size);
void* operator new[](size_t size);
void operator delete(void*, size_t);
void operator delete[](void*, size_t);
std::unique_ptr<internal::EmbedderState> embedder_state_;
};
} // namespace v8
#endif // INCLUDE_V8_EMBEDDER_STATE_SCOPE_H_

View File

@ -0,0 +1,298 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_EXCEPTION_H_
#define INCLUDE_V8_EXCEPTION_H_
#include <stddef.h>
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-object.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
class Isolate;
class Message;
class StackTrace;
class String;
class Value;
namespace internal {
class Isolate;
class ThreadLocalTop;
} // namespace internal
/**
* Create new error objects by calling the corresponding error object
* constructor with the message.
*/
class V8_EXPORT Exception {
public:
static Local<Value> RangeError(Local<String> message,
Local<Value> options = {});
static Local<Value> ReferenceError(Local<String> message,
Local<Value> options = {});
static Local<Value> SyntaxError(Local<String> message,
Local<Value> options = {});
static Local<Value> TypeError(Local<String> message,
Local<Value> options = {});
static Local<Value> WasmCompileError(Local<String> message,
Local<Value> options = {});
static Local<Value> WasmLinkError(Local<String> message,
Local<Value> options = {});
static Local<Value> WasmRuntimeError(Local<String> message,
Local<Value> options = {});
static Local<Value> WasmSuspendError(Local<String> message,
Local<Value> options = {});
static Local<Value> Error(Local<String> message, Local<Value> options = {});
/**
* Creates an error message for the given exception.
* Will try to reconstruct the original stack trace from the exception value,
* or capture the current stack trace if not available.
*/
static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
/**
* Returns the original stack trace that was captured at the creation time
* of a given exception, or an empty handle if not available.
*/
static Local<StackTrace> GetStackTrace(Local<Value> exception);
/**
* Captures the current stack trace and attaches it to the given object in the
* form of `stack` property.
*/
static Maybe<bool> CaptureStackTrace(Local<Context> context,
Local<Object> object);
};
/**
* This is a part of experimental Api and might be changed without further
* notice.
* Do not use it.
*/
enum class ExceptionContext : uint32_t {
kUnknown,
kConstructor,
kOperation,
kAttributeGet,
kAttributeSet,
kIndexedQuery,
kIndexedGetter,
kIndexedDescriptor,
kIndexedSetter,
kIndexedDefiner,
kIndexedDeleter,
kNamedQuery,
kNamedGetter,
kNamedDescriptor,
kNamedSetter,
kNamedDefiner,
kNamedDeleter,
kNamedEnumerator
};
/**
* This is a part of experimental Api and might be changed without further
* notice.
* Do not use it.
*/
class ExceptionPropagationMessage {
public:
ExceptionPropagationMessage(v8::Isolate* isolate, Local<Object> exception,
Local<String> interface_name,
Local<String> property_name,
ExceptionContext exception_context)
: isolate_(isolate),
exception_(exception),
interface_name_(interface_name),
property_name_(property_name),
exception_context_(exception_context) {}
V8_INLINE Isolate* GetIsolate() const { return isolate_; }
V8_INLINE Local<Object> GetException() const { return exception_; }
V8_INLINE Local<String> GetInterfaceName() const { return interface_name_; }
V8_INLINE Local<String> GetPropertyName() const { return property_name_; }
V8_INLINE ExceptionContext GetExceptionContext() const {
return exception_context_;
}
private:
Isolate* isolate_;
Local<Object> exception_;
Local<String> interface_name_;
Local<String> property_name_;
ExceptionContext exception_context_;
};
using ExceptionPropagationCallback =
void (*)(ExceptionPropagationMessage message);
/**
* An external exception handler.
*/
class V8_EXPORT TryCatch {
public:
/**
* Creates a new try/catch block and registers it with v8. Note that
* all TryCatch blocks should be stack allocated because the memory
* location itself is compared against JavaScript try/catch blocks.
*/
explicit TryCatch(Isolate* isolate);
/**
* Unregisters and deletes this try/catch block.
*/
~TryCatch();
/**
* Returns true if an exception has been caught by this try/catch block.
*/
bool HasCaught() const;
/**
* For certain types of exceptions, it makes no sense to continue execution.
*
* If CanContinue returns false, the correct action is to perform any C++
* cleanup needed and then return. If CanContinue returns false and
* HasTerminated returns true, it is possible to call
* CancelTerminateExecution in order to continue calling into the engine.
*/
bool CanContinue() const;
/**
* Returns true if an exception has been caught due to script execution
* being terminated.
*
* There is no JavaScript representation of an execution termination
* exception. Such exceptions are thrown when the TerminateExecution
* methods are called to terminate a long-running script.
*
* If such an exception has been thrown, HasTerminated will return true,
* indicating that it is possible to call CancelTerminateExecution in order
* to continue calling into the engine.
*/
bool HasTerminated() const;
/**
* Throws the exception caught by this TryCatch in a way that avoids
* it being caught again by this same TryCatch. As with ThrowException
* it is illegal to execute any JavaScript operations after calling
* ReThrow; the caller must return immediately to where the exception
* is caught.
*/
Local<Value> ReThrow();
/**
* Returns the exception caught by this try/catch block. If no exception has
* been caught an empty handle is returned.
*/
Local<Value> Exception() const;
/**
* Returns the .stack property of an object. If no .stack
* property is present an empty handle is returned.
*/
V8_WARN_UNUSED_RESULT static MaybeLocal<Value> StackTrace(
Local<Context> context, Local<Value> exception);
/**
* Returns the .stack property of the thrown object. If no .stack property is
* present or if this try/catch block has not caught an exception, an empty
* handle is returned.
*/
V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace(
Local<Context> context) const;
/**
* Returns the message associated with this exception. If there is
* no message associated an empty handle is returned.
*/
Local<v8::Message> Message() const;
/**
* Clears any exceptions that may have been caught by this try/catch block.
* After this method has been called, HasCaught() will return false. Cancels
* the scheduled exception if it is caught and ReThrow() is not called before.
*
* It is not necessary to clear a try/catch block before using it again; if
* another exception is thrown the previously caught exception will just be
* overwritten. However, it is often a good idea since it makes it easier
* to determine which operation threw a given exception.
*/
void Reset();
/**
* Set verbosity of the external exception handler.
*
* By default, exceptions that are caught by an external exception
* handler are not reported. Call SetVerbose with true on an
* external exception handler to have exceptions caught by the
* handler reported as if they were not caught.
*/
void SetVerbose(bool value);
/**
* Returns true if verbosity is enabled.
*/
bool IsVerbose() const;
/**
* Set whether or not this TryCatch should capture a Message object
* which holds source information about where the exception
* occurred. True by default.
*/
void SetCaptureMessage(bool value);
TryCatch(const TryCatch&) = delete;
void operator=(const TryCatch&) = delete;
private:
// Declaring operator new and delete as deleted is not spec compliant.
// Therefore declare them private instead to disable dynamic alloc
void* operator new(size_t size);
void* operator new[](size_t size);
void operator delete(void*, size_t);
void operator delete[](void*, size_t);
/**
* There are cases when the raw address of C++ TryCatch object cannot be
* used for comparisons with addresses into the JS stack. The cases are:
* 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
* 2) Address sanitizer allocates local C++ object in the heap when
* UseAfterReturn mode is enabled.
* This method returns address that can be used for comparisons with
* addresses into the JS stack. When neither simulator nor ASAN's
* UseAfterReturn is enabled, then the address returned will be the address
* of the C++ try catch handler itself.
*/
internal::Address JSStackComparableAddressPrivate() {
return js_stack_comparable_address_;
}
void ResetInternal();
// Helper methods for internal::Isolate.
bool capture_message() const;
void set_can_continue(bool value);
bool rethrow() const;
void set_rethrow(bool value);
internal::Isolate* i_isolate_;
TryCatch* next_;
void* exception_;
void* message_obj_;
internal::Address js_stack_comparable_address_;
uint8_t flags_;
friend class internal::Isolate;
friend class internal::ThreadLocalTop;
};
} // namespace v8
#endif // INCLUDE_V8_EXCEPTION_H_

View File

@ -0,0 +1,60 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_EXTENSION_H_
#define INCLUDE_V8_EXTENSION_H_
#include <memory>
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-primitive.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class FunctionTemplate;
// --- Extensions ---
/**
* Ignore
*/
class V8_EXPORT Extension {
public:
// Note that the strings passed into this constructor must live as long
// as the Extension itself.
Extension(const char* name, const char* source = nullptr, int dep_count = 0,
const char** deps = nullptr, int source_length = -1);
virtual ~Extension() { delete source_; }
virtual Local<FunctionTemplate> GetNativeFunctionTemplate(
Isolate* isolate, Local<String> name) {
return Local<FunctionTemplate>();
}
const char* name() const { return name_; }
const String::ExternalOneByteStringResource* source() const {
return source_;
}
int dependency_count() const { return dep_count_; }
const char** dependencies() const { return deps_; }
void set_auto_enable(bool value) { auto_enable_ = value; }
bool auto_enable() { return auto_enable_; }
// Disallow copying and assigning.
Extension(const Extension&) = delete;
void operator=(const Extension&) = delete;
private:
const char* name_;
String::ExternalOneByteStringResource* source_;
int dep_count_;
const char** deps_;
bool auto_enable_;
};
void V8_EXPORT RegisterExtension(std::unique_ptr<Extension>);
} // namespace v8
#endif // INCLUDE_V8_EXTENSION_H_

View File

@ -0,0 +1,60 @@
// Copyright 2024 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_EXTERNAL_MEMORY_ACCOUNTER_H_
#define INCLUDE_EXTERNAL_MEMORY_ACCOUNTER_H_
#include <stdint.h>
#include "v8-isolate.h"
namespace v8 {
/**
* This class is used to give V8 an indication of the amount of externally
* allocated memory that is kept alive by JavaScript objects. V8 uses this to
* decide when to perform garbage collections. Registering externally allocated
* memory will trigger garbage collections more often than it would otherwise in
* an attempt to garbage collect the JavaScript objects that keep the externally
* allocated memory alive. Instances of ExternalMemoryAccounter check that the
* reported external memory is back to 0 on destruction.
*/
class V8_EXPORT ExternalMemoryAccounter {
public:
/**
* Returns the amount of external memory registered for `isolate`.
*/
static int64_t GetTotalAmountOfExternalAllocatedMemoryForTesting(
const Isolate* isolate);
ExternalMemoryAccounter() = default;
~ExternalMemoryAccounter();
ExternalMemoryAccounter(ExternalMemoryAccounter&&);
ExternalMemoryAccounter& operator=(ExternalMemoryAccounter&&);
ExternalMemoryAccounter(const ExternalMemoryAccounter&) = delete;
ExternalMemoryAccounter& operator=(const ExternalMemoryAccounter&) = delete;
/**
* Reports an increase of `size` bytes of external memory.
*/
void Increase(Isolate* isolate, size_t size);
/**
* Reports an update of `delta` bytes of external memory.
*/
void Update(Isolate* isolate, int64_t delta);
/**
* Reports an decrease of `size` bytes of external memory.
*/
void Decrease(Isolate* isolate, size_t size);
private:
#ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
size_t amount_of_external_memory_ = 0;
v8::Isolate* isolate_ = nullptr;
#endif
};
} // namespace v8
#endif // INCLUDE_EXTERNAL_MEMORY_ACCOUNTER_H_

View File

@ -0,0 +1,66 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_EXTERNAL_H_
#define INCLUDE_V8_EXTERNAL_H_
#include "v8-value.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Isolate;
/**
* A tag for external pointers. Objects with different C++ types should use
* different values of ExternalPointerTypeTag when using v8::External. The
* allowed range is 0..V8_EXTERNAL_POINTER_TAG_COUNT - 1. If this is not
* sufficient, V8_EXTERNAL_POINTER_TAG_COUNT can be increased.
*/
using ExternalPointerTypeTag = uint16_t;
constexpr ExternalPointerTypeTag kExternalPointerTypeTagDefault = 0;
/**
* A JavaScript value that wraps a C++ void*. This type of value is mainly used
* to associate C++ data structures with JavaScript objects.
*/
class V8_EXPORT External : public Value {
public:
/**
* Creates a new External object.
*
* \param isolate The isolate for the external object.
* \param value The C++ pointer value.
* \param tag The type tag of the external pointer. If type tags are not used
* in the embedder, the default value `kExternalPointerTypeTagDefault` can be
* used.
* \return The new External object.
*/
static Local<External> New(Isolate* isolate, void* value,
ExternalPointerTypeTag tag);
V8_INLINE static External* Cast(Data* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<External*>(value);
}
/**
* Returns the value of the external pointer.
*
* \param tag The type tag of the external pointer. If type tags are not used
* in the embedder, the default value `kExternalPointerTypeTagDefault` can be
* used.
* \return The value of the external pointer.
*/
void* Value(ExternalPointerTypeTag tag) const;
private:
static void CheckCast(v8::Data* obj);
};
} // namespace v8
#endif // INCLUDE_V8_EXTERNAL_H_

View File

@ -0,0 +1,784 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_FAST_API_CALLS_H_
#define INCLUDE_V8_FAST_API_CALLS_H_
/**
* This file provides additional API on top of the default one for making
* API calls, which come from embedder C++ functions. The functions are being
* called directly from optimized code, doing all the necessary typechecks
* in the compiler itself, instead of on the embedder side. Hence the "fast"
* in the name. Example usage might look like:
*
* \code
* void FastMethod(int param, bool another_param);
*
* v8::FunctionTemplate::New(isolate, SlowCallback, data,
* signature, length, constructor_behavior
* side_effect_type,
* &v8::CFunction::Make(FastMethod));
* \endcode
*
* By design, fast calls are limited by the following requirements, which
* the embedder should enforce themselves:
* - they should not allocate on the JS heap;
* - they should not trigger JS execution.
* To enforce them, the embedder could use the existing
* v8::Isolate::DisallowJavascriptExecutionScope and a utility similar to
* Blink's NoAllocationScope:
* https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/platform/heap/thread_state_scopes.h;l=16
*
* Due to these limitations, it's not directly possible to report errors by
* throwing a JS exception or to otherwise do an allocation. There is an
* alternative way of creating fast calls that supports falling back to the
* slow call and then performing the necessary allocation. When one creates
* the fast method by using CFunction::MakeWithFallbackSupport instead of
* CFunction::Make, the fast callback gets as last parameter an output variable,
* through which it can request falling back to the slow call. So one might
* declare their method like:
*
* \code
* void FastMethodWithFallback(int param, FastApiCallbackOptions& options);
* \endcode
*
* If the callback wants to signal an error condition or to perform an
* allocation, it must set options.fallback to true and do an early return from
* the fast method. Then V8 checks the value of options.fallback and if it's
* true, falls back to executing the SlowCallback, which is capable of reporting
* the error (either by throwing a JS exception or logging to the console) or
* doing the allocation. It's the embedder's responsibility to ensure that the
* fast callback is idempotent up to the point where error and fallback
* conditions are checked, because otherwise executing the slow callback might
* produce visible side-effects twice.
*
* An example for custom embedder type support might employ a way to wrap/
* unwrap various C++ types in JSObject instances, e.g:
*
* \code
*
* // Helper method with a check for field count.
* template <typename T, int offset>
* inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
* assert(offset < wrapper->InternalFieldCount());
* return reinterpret_cast<T*>(
* wrapper->GetAlignedPointerFromInternalField(offset));
* }
*
* class CustomEmbedderType {
* public:
* // Returns the raw C object from a wrapper JS object.
* static CustomEmbedderType* Unwrap(v8::Local<v8::Object> wrapper) {
* return GetInternalField<CustomEmbedderType,
* kV8EmbedderWrapperObjectIndex>(wrapper);
* }
* static void FastMethod(v8::Local<v8::Object> receiver_obj, int param) {
* CustomEmbedderType* receiver = static_cast<CustomEmbedderType*>(
* receiver_obj->GetAlignedPointerFromInternalField(
* kV8EmbedderWrapperObjectIndex));
*
* // Type checks are already done by the optimized code.
* // Then call some performance-critical method like:
* // receiver->Method(param);
* }
*
* static void SlowMethod(
* const v8::FunctionCallbackInfo<v8::Value>& info) {
* v8::Local<v8::Object> instance =
* v8::Local<v8::Object>::Cast(info.Holder());
* CustomEmbedderType* receiver = Unwrap(instance);
* // TODO: Do type checks and extract {param}.
* receiver->Method(param);
* }
* };
*
* // The following setup function can be templatized based on
* // the {embedder_object} argument.
* void SetupCustomEmbedderObject(v8::Isolate* isolate,
* v8::Local<v8::Context> context,
* CustomEmbedderType* embedder_object) {
* v8::CFunction c_func =
* MakeV8CFunction(CustomEmbedderType::FastMethod);
*
* Local<v8::FunctionTemplate> method_template =
* v8::FunctionTemplate::New(
* isolate, CustomEmbedderType::SlowMethod, v8::Local<v8::Value>(),
* v8::Local<v8::Signature>(), 1, v8::ConstructorBehavior::kAllow,
* v8::SideEffectType::kHasSideEffect, &c_func);
*
* v8::Local<v8::ObjectTemplate> object_template =
* v8::ObjectTemplate::New(isolate);
* object_template->SetInternalFieldCount(
* kV8EmbedderWrapperObjectIndex + 1);
* object_template->Set(isolate, "method", method_template);
*
* // Instantiate the wrapper JS object.
* v8::Local<v8::Object> object =
* object_template->NewInstance(context).ToLocalChecked();
* object->SetAlignedPointerInInternalField(
* kV8EmbedderWrapperObjectIndex,
* reinterpret_cast<void*>(embedder_object));
*
* // TODO: Expose {object} where it's necessary.
* }
* \endcode
*
* For instance if {object} is exposed via a global "obj" variable,
* one could write in JS:
* function hot_func() {
* obj.method(42);
* }
* and once {hot_func} gets optimized, CustomEmbedderType::FastMethod
* will be called instead of the slow version, with the following arguments:
* receiver := the {embedder_object} from above
* param := 42
*
* Currently supported return types:
* - void
* - bool
* - int32_t
* - uint32_t
* - float32_t
* - float64_t
* Currently supported argument types:
* - pointer to an embedder type
* - JavaScript array of primitive types
* - bool
* - int32_t
* - uint32_t
* - int64_t
* - uint64_t
* - float32_t
* - float64_t
*
* The 64-bit integer types currently have the IDL (unsigned) long long
* semantics: https://heycam.github.io/webidl/#abstract-opdef-converttoint
* In the future we'll extend the API to also provide conversions from/to
* BigInt to preserve full precision.
* The floating point types currently have the IDL (unrestricted) semantics,
* which is the only one used by WebGL. We plan to add support also for
* restricted floats/doubles, similarly to the BigInt conversion policies.
* We also differ from the specific NaN bit pattern that WebIDL prescribes
* (https://heycam.github.io/webidl/#es-unrestricted-float) in that Blink
* passes NaN values as-is, i.e. doesn't normalize them.
*
* To be supported types:
* - TypedArrays and ArrayBuffers
* - arrays of embedder types
*
*
* The API offers a limited support for function overloads:
*
* \code
* void FastMethod_2Args(int param, bool another_param);
* void FastMethod_3Args(int param, bool another_param, int third_param);
*
* v8::CFunction fast_method_2args_c_func =
* MakeV8CFunction(FastMethod_2Args);
* v8::CFunction fast_method_3args_c_func =
* MakeV8CFunction(FastMethod_3Args);
* const v8::CFunction fast_method_overloads[] = {fast_method_2args_c_func,
* fast_method_3args_c_func};
* Local<v8::FunctionTemplate> method_template =
* v8::FunctionTemplate::NewWithCFunctionOverloads(
* isolate, SlowCallback, data, signature, length,
* constructor_behavior, side_effect_type,
* {fast_method_overloads, 2});
* \endcode
*
* In this example a single FunctionTemplate is associated to multiple C++
* functions. The overload resolution is currently only based on the number of
* arguments passed in a call. For example, if this method_template is
* registered with a wrapper JS object as described above, a call with two
* arguments:
* obj.method(42, true);
* will result in a fast call to FastMethod_2Args, while a call with three or
* more arguments:
* obj.method(42, true, 11);
* will result in a fast call to FastMethod_3Args. Instead a call with less than
* two arguments, like:
* obj.method(42);
* would not result in a fast call but would fall back to executing the
* associated SlowCallback.
*/
#include <stddef.h>
#include <stdint.h>
#include <tuple>
#include <type_traits>
#include "v8-external.h" // NOLINT(build/include_directory)
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-typed-array.h" // NOLINT(build/include_directory)
#include "v8-value.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Isolate;
class CTypeInfo {
public:
enum class Type : uint8_t {
kVoid,
kBool,
kUint8,
kInt32,
kUint32,
kInt64,
kUint64,
kFloat32,
kFloat64,
kPointer,
kV8Value,
kSeqOneByteString,
kApiObject, // This will be deprecated once all users have
// migrated from v8::ApiObject to v8::Local<v8::Value>.
kAny, // This is added to enable untyped representation of fast
// call arguments for test purposes. It can represent any of
// the other types stored in the same memory as a union
// (see AnyCType declared below). This allows for
// uniform passing of arguments w.r.t. their location
// (in a register or on the stack), independent of their
// actual type. It's currently used by the arm64 simulator
// and can be added to the other simulators as well when fast
// calls having both GP and FP params need to be supported.
};
// kCallbackOptionsType is not part of the Type enum
// because it is only used internally. Use value 255 that is larger
// than any valid Type enum.
static constexpr Type kCallbackOptionsType = Type(255);
enum class Flags : uint8_t {
kNone = 0,
kAllowSharedBit = 1 << 0, // Must be an ArrayBuffer or TypedArray
kEnforceRangeBit = 1 << 1, // T must be integral
kClampBit = 1 << 2, // T must be integral
kIsRestrictedBit = 1 << 3, // T must be float or double
};
explicit constexpr CTypeInfo(Type type, Flags flags = Flags::kNone)
: type_(type), flags_(flags) {}
typedef uint32_t Identifier;
explicit constexpr CTypeInfo(Identifier identifier)
: type_(static_cast<Type>((identifier >> 8) & 255)),
flags_(static_cast<Flags>(identifier & 255)) {}
constexpr Identifier GetId() const {
return static_cast<uint8_t>(type_) << 8 |
static_cast<uint8_t>(flags_);
}
constexpr Type GetType() const { return type_; }
constexpr Flags GetFlags() const { return flags_; }
static constexpr bool IsIntegralType(Type type) {
return type == Type::kUint8 || type == Type::kInt32 ||
type == Type::kUint32 || type == Type::kInt64 ||
type == Type::kUint64;
}
static constexpr bool IsFloatingPointType(Type type) {
return type == Type::kFloat32 || type == Type::kFloat64;
}
static constexpr bool IsPrimitive(Type type) {
return IsIntegralType(type) || IsFloatingPointType(type) ||
type == Type::kBool;
}
private:
Type type_;
Flags flags_;
};
struct FastOneByteString {
const char* data;
uint32_t length;
};
class V8_EXPORT CFunctionInfo {
public:
enum class Int64Representation : uint8_t {
kNumber = 0, // Use numbers to represent 64 bit integers.
kBigInt = 1, // Use BigInts to represent 64 bit integers.
};
// Construct a struct to hold a CFunction's type information.
// |return_info| describes the function's return type.
// |arg_info| is an array of |arg_count| CTypeInfos describing the
// arguments. Only the last argument may be of the special type
// CTypeInfo::kCallbackOptionsType.
CFunctionInfo(const CTypeInfo& return_info, unsigned int arg_count,
const CTypeInfo* arg_info,
Int64Representation repr = Int64Representation::kNumber);
const CTypeInfo& ReturnInfo() const { return return_info_; }
// The argument count, not including the v8::FastApiCallbackOptions
// if present.
unsigned int ArgumentCount() const {
return HasOptions() ? arg_count_ - 1 : arg_count_;
}
Int64Representation GetInt64Representation() const { return repr_; }
// |index| must be less than ArgumentCount().
// Note: if the last argument passed on construction of CFunctionInfo
// has type CTypeInfo::kCallbackOptionsType, it is not included in
// ArgumentCount().
const CTypeInfo& ArgumentInfo(unsigned int index) const;
bool HasOptions() const {
// The options arg is always the last one.
return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
CTypeInfo::kCallbackOptionsType;
}
private:
const CTypeInfo return_info_;
const Int64Representation repr_;
const unsigned int arg_count_;
const CTypeInfo* arg_info_;
};
struct FastApiCallbackOptions;
// Provided for testing.
union V8_TRIVIAL_ABI AnyCType {
AnyCType() : int64_value(0) {}
#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
// In this case, Local<T> is not trivially copyable and the implicit
// copy constructor and copy assignment for the union are deleted.
AnyCType(const AnyCType& other) : int64_value(other.int64_value) {}
AnyCType& operator=(const AnyCType& other) {
int64_value = other.int64_value;
return *this;
}
#endif
bool bool_value;
int32_t int32_value;
uint32_t uint32_value;
int64_t int64_value;
uint64_t uint64_value;
float float_value;
double double_value;
void* pointer_value;
Local<Object> object_value;
Local<Array> sequence_value;
const FastOneByteString* string_value;
FastApiCallbackOptions* options_value;
};
static_assert(
sizeof(AnyCType) == 8,
"The union AnyCType should have size == 64 bits, as this is assumed "
"by FastApiCallLoweringReducer.");
class V8_EXPORT CFunction {
public:
constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
const CTypeInfo& ReturnInfo() const { return type_info_->ReturnInfo(); }
const CTypeInfo& ArgumentInfo(unsigned int index) const {
return type_info_->ArgumentInfo(index);
}
unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); }
const void* GetAddress() const { return address_; }
CFunctionInfo::Int64Representation GetInt64Representation() const {
return type_info_->GetInt64Representation();
}
const CFunctionInfo* GetTypeInfo() const { return type_info_; }
enum class OverloadResolution { kImpossible, kAtRuntime, kAtCompileTime };
template <typename F>
static CFunction Make(F* func,
CFunctionInfo::Int64Representation int64_rep =
CFunctionInfo::Int64Representation::kNumber) {
CFunction result = ArgUnwrap<F*>::Make(func, int64_rep);
result.GetInt64Representation();
return result;
}
// Provided for testing purposes.
template <typename R, typename... Args, typename R_Patch,
typename... Args_Patch>
static CFunction Make(R (*func)(Args...),
R_Patch (*patching_func)(Args_Patch...),
CFunctionInfo::Int64Representation int64_rep =
CFunctionInfo::Int64Representation::kNumber) {
CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func, int64_rep);
static_assert(
sizeof...(Args_Patch) == sizeof...(Args),
"The patching function must have the same number of arguments.");
c_func.address_ = reinterpret_cast<void*>(patching_func);
return c_func;
}
CFunction(const void* address, const CFunctionInfo* type_info);
private:
const void* address_;
const CFunctionInfo* type_info_;
template <typename F>
class ArgUnwrap {
static_assert(sizeof(F) != sizeof(F),
"CFunction must be created from a function pointer.");
};
template <typename R, typename... Args>
class ArgUnwrap<R (*)(Args...)> {
public:
static CFunction Make(R (*func)(Args...),
CFunctionInfo::Int64Representation int64_rep =
CFunctionInfo::Int64Representation::kNumber);
};
};
/**
* A struct which may be passed to a fast call callback, like so:
* \code
* void FastMethodWithOptions(int param, FastApiCallbackOptions& options);
* \endcode
*/
struct FastApiCallbackOptions {
/**
* Creates a new instance of FastApiCallbackOptions for testing purpose. The
* returned instance may be filled with mock data.
*/
static FastApiCallbackOptions CreateForTesting(Isolate* isolate) {
return {};
}
v8::Isolate* isolate = nullptr;
/**
* The `data` passed to the FunctionTemplate constructor, or `undefined`.
*/
v8::Local<v8::Value> data;
};
namespace internal {
// Helper to count the number of occurances of `T` in `List`
template <typename T, typename... List>
struct count : std::integral_constant<int, 0> {};
template <typename T, typename... Args>
struct count<T, T, Args...>
: std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
template <typename T, typename U, typename... Args>
struct count<T, U, Args...> : count<T, Args...> {};
template <CFunctionInfo::Int64Representation Representation,
typename RetBuilder, typename... ArgBuilders>
class CFunctionInfoImpl : public CFunctionInfo {
static constexpr int kOptionsArgCount =
count<FastApiCallbackOptions&, ArgBuilders...>();
static constexpr int kReceiverCount = 1;
static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
"Only one options parameter is supported.");
static_assert(sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
"The receiver or the options argument is missing.");
public:
constexpr CFunctionInfoImpl()
: CFunctionInfo(RetBuilder::Build(), sizeof...(ArgBuilders),
arg_info_storage_, Representation),
arg_info_storage_{ArgBuilders::Build()...} {
constexpr CTypeInfo::Type kReturnType = RetBuilder::Build().GetType();
static_assert(kReturnType == CTypeInfo::Type::kVoid ||
kReturnType == CTypeInfo::Type::kBool ||
kReturnType == CTypeInfo::Type::kInt32 ||
kReturnType == CTypeInfo::Type::kUint32 ||
kReturnType == CTypeInfo::Type::kInt64 ||
kReturnType == CTypeInfo::Type::kUint64 ||
kReturnType == CTypeInfo::Type::kFloat32 ||
kReturnType == CTypeInfo::Type::kFloat64 ||
kReturnType == CTypeInfo::Type::kPointer ||
kReturnType == CTypeInfo::Type::kAny,
"String and api object values are not currently "
"supported return types.");
}
private:
const CTypeInfo arg_info_storage_[sizeof...(ArgBuilders)];
};
template <typename T>
struct TypeInfoHelper {
static_assert(sizeof(T) != sizeof(T), "This type is not supported");
};
#define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
template <> \
struct TypeInfoHelper<T> { \
static constexpr CTypeInfo::Flags Flags() { \
return CTypeInfo::Flags::kNone; \
} \
\
static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
};
template <CTypeInfo::Type type>
struct CTypeInfoTraits {};
#define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
template <> \
struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
using ctype = CType; \
};
#define PRIMITIVE_C_TYPES(V) \
V(bool, kBool) \
V(uint8_t, kUint8) \
V(int32_t, kInt32) \
V(uint32_t, kUint32) \
V(int64_t, kInt64) \
V(uint64_t, kUint64) \
V(float, kFloat32) \
V(double, kFloat64) \
V(void*, kPointer)
// Same as above, but includes deprecated types for compatibility.
#define ALL_C_TYPES(V) \
PRIMITIVE_C_TYPES(V) \
V(void, kVoid) \
V(v8::Local<v8::Value>, kV8Value) \
V(v8::Local<v8::Object>, kV8Value) \
V(v8::Local<v8::Array>, kV8Value) \
V(AnyCType, kAny)
// ApiObject was a temporary solution to wrap the pointer to the v8::Value.
// Please use v8::Local<v8::Value> in new code for the arguments and
// v8::Local<v8::Object> for the receiver, as ApiObject will be deprecated.
ALL_C_TYPES(SPECIALIZE_GET_TYPE_INFO_HELPER_FOR)
PRIMITIVE_C_TYPES(DEFINE_TYPE_INFO_TRAITS)
#undef PRIMITIVE_C_TYPES
#undef ALL_C_TYPES
#undef TYPED_ARRAY_C_TYPES
template <>
struct TypeInfoHelper<FastApiCallbackOptions&> {
static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; }
static constexpr CTypeInfo::Type Type() {
return CTypeInfo::kCallbackOptionsType;
}
};
template <>
struct TypeInfoHelper<const FastOneByteString&> {
static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; }
static constexpr CTypeInfo::Type Type() {
return CTypeInfo::Type::kSeqOneByteString;
}
};
#define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
static_assert(((COND) == 0) || (ASSERTION), MSG)
} // namespace internal
template <typename T, CTypeInfo::Flags... Flags>
class V8_EXPORT CTypeInfoBuilder {
public:
using BaseType = T;
static constexpr CTypeInfo Build() {
constexpr CTypeInfo::Flags kFlags =
MergeFlags(internal::TypeInfoHelper<T>::Flags(), Flags...);
constexpr CTypeInfo::Type kType = internal::TypeInfoHelper<T>::Type();
STATIC_ASSERT_IMPLIES(
uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
CTypeInfo::IsIntegralType(kType),
"kEnforceRangeBit is only allowed for integral types.");
STATIC_ASSERT_IMPLIES(
uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
CTypeInfo::IsIntegralType(kType),
"kClampBit is only allowed for integral types.");
STATIC_ASSERT_IMPLIES(
uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
CTypeInfo::IsFloatingPointType(kType),
"kIsRestrictedBit is only allowed for floating point types.");
// Return the same type with the merged flags.
return CTypeInfo(internal::TypeInfoHelper<T>::Type(), kFlags);
}
private:
template <typename... Rest>
static constexpr CTypeInfo::Flags MergeFlags(CTypeInfo::Flags flags,
Rest... rest) {
return CTypeInfo::Flags(uint8_t(flags) | uint8_t(MergeFlags(rest...)));
}
static constexpr CTypeInfo::Flags MergeFlags() { return CTypeInfo::Flags(0); }
};
namespace internal {
template <typename RetBuilder, typename... ArgBuilders>
class CFunctionBuilderWithFunction {
public:
explicit constexpr CFunctionBuilderWithFunction(const void* fn) : fn_(fn) {}
template <CTypeInfo::Flags... Flags>
constexpr auto Ret() {
return CFunctionBuilderWithFunction<
CTypeInfoBuilder<typename RetBuilder::BaseType, Flags...>,
ArgBuilders...>(fn_);
}
template <unsigned int N, CTypeInfo::Flags... Flags>
constexpr auto Arg() {
// Return a copy of the builder with the Nth arg builder merged with
// template parameter pack Flags.
return ArgImpl<N, Flags...>(
std::make_index_sequence<sizeof...(ArgBuilders)>());
}
// Provided for testing purposes.
template <typename Ret, typename... Args>
auto Patch(Ret (*patching_func)(Args...)) {
static_assert(
sizeof...(Args) == sizeof...(ArgBuilders),
"The patching function must have the same number of arguments.");
fn_ = reinterpret_cast<void*>(patching_func);
return *this;
}
template <CFunctionInfo::Int64Representation Representation =
CFunctionInfo::Int64Representation::kNumber>
auto Build() {
static CFunctionInfoImpl<Representation, RetBuilder, ArgBuilders...>
instance;
return CFunction(fn_, &instance);
}
private:
template <bool Merge, unsigned int N, CTypeInfo::Flags... Flags>
struct GetArgBuilder;
// Returns the same ArgBuilder as the one at index N, including its flags.
// Flags in the template parameter pack are ignored.
template <unsigned int N, CTypeInfo::Flags... Flags>
struct GetArgBuilder<false, N, Flags...> {
using type = std::tuple_element_t<N, std::tuple<ArgBuilders...>>;
};
// Returns an ArgBuilder with the same base type as the one at index N,
// but merges the flags with the flags in the template parameter pack.
template <unsigned int N, CTypeInfo::Flags... Flags>
struct GetArgBuilder<true, N, Flags...> {
using type = CTypeInfoBuilder<
typename std::tuple_element_t<N, std::tuple<ArgBuilders...>>::BaseType,
std::tuple_element_t<N, std::tuple<ArgBuilders...>>::Build().GetFlags(),
Flags...>;
};
// Return a copy of the CFunctionBuilder, but merges the Flags on
// ArgBuilder index N with the new Flags passed in the template parameter
// pack.
template <unsigned int N, CTypeInfo::Flags... Flags, size_t... I>
constexpr auto ArgImpl(std::index_sequence<I...>) {
return CFunctionBuilderWithFunction<
RetBuilder, typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
}
const void* fn_;
};
class CFunctionBuilder {
public:
constexpr CFunctionBuilder() {}
template <typename R, typename... Args>
constexpr auto Fn(R (*fn)(Args...)) {
return CFunctionBuilderWithFunction<CTypeInfoBuilder<R>,
CTypeInfoBuilder<Args>...>(
reinterpret_cast<const void*>(fn));
}
};
} // namespace internal
// static
template <typename R, typename... Args>
CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(
R (*func)(Args...), CFunctionInfo::Int64Representation int64_rep) {
if (int64_rep == CFunctionInfo::Int64Representation::kNumber) {
return internal::CFunctionBuilder().Fn(func).Build();
}
return internal::CFunctionBuilder()
.Fn(func)
.template Build<CFunctionInfo::Int64Representation::kBigInt>();
}
using CFunctionBuilder = internal::CFunctionBuilder;
static constexpr CTypeInfo kTypeInfoInt32 = CTypeInfo(CTypeInfo::Type::kInt32);
static constexpr CTypeInfo kTypeInfoFloat64 =
CTypeInfo(CTypeInfo::Type::kFloat64);
/**
* Copies the contents of this JavaScript array to a C++ buffer with
* a given max_length. A CTypeInfo is passed as an argument,
* instructing different rules for conversion (e.g. restricted float/double).
* The element type T of the destination array must match the C type
* corresponding to the CTypeInfo (specified by CTypeInfoTraits).
* If the array length is larger than max_length or the array is of
* unsupported type, the operation will fail, returning false. Generally, an
* array which contains objects, undefined, null or anything not convertible
* to the requested destination type, is considered unsupported. The operation
* returns true on success. `type_info` will be used for conversions.
*/
template <CTypeInfo::Identifier type_info_id, typename T>
bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer(
Local<Array> src, T* dst, uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT
TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
int32_t>(Local<Array> src, int32_t* dst,
uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT
TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
uint32_t>(Local<Array> src, uint32_t* dst,
uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT
TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
float>(Local<Array> src, float* dst,
uint32_t max_length);
template <>
bool V8_EXPORT V8_WARN_UNUSED_RESULT
TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
double>(Local<Array> src, double* dst,
uint32_t max_length);
constexpr v8::ExternalPointerTypeTag kFastAPIPointerTag =
V8_EXTERNAL_POINTER_TAG_COUNT - 1;
} // namespace v8
#endif // INCLUDE_V8_FAST_API_CALLS_H_

View File

@ -0,0 +1,82 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_FORWARD_H_
#define INCLUDE_V8_FORWARD_H_
// This header is intended to be used by headers that pass around V8 types,
// either by pointer or using Local<Type>. The full definitions can be included
// either via v8.h or the more fine-grained headers.
#include "v8-local-handle.h" // NOLINT(build/include_directory)
namespace v8 {
class AccessorSignature;
class Array;
class ArrayBuffer;
class ArrayBufferView;
class BigInt;
class BigInt64Array;
class BigIntObject;
class BigUint64Array;
class Boolean;
class BooleanObject;
class Context;
class DataView;
class Data;
class Date;
class DictionaryTemplate;
class Extension;
class External;
class FixedArray;
class Float32Array;
class Float64Array;
class Function;
template <class F>
class FunctionCallbackInfo;
class FunctionTemplate;
class Int16Array;
class Int32;
class Int32Array;
class Int8Array;
class Integer;
class Isolate;
class Map;
class Module;
class Name;
class Number;
class NumberObject;
class Object;
class ObjectTemplate;
class Platform;
class Primitive;
class Private;
class Promise;
class Proxy;
class RegExp;
class Script;
class Set;
class SharedArrayBuffer;
class Signature;
class String;
class StringObject;
class Symbol;
class SymbolObject;
class Template;
class TryCatch;
class TypedArray;
class Uint16Array;
class Uint32;
class Uint32Array;
class Uint8Array;
class Uint8ClampedArray;
class UnboundModuleScript;
class Value;
class WasmMemoryObject;
class WasmModuleObject;
} // namespace v8
#endif // INCLUDE_V8_FORWARD_H_

View File

@ -0,0 +1,714 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_FUNCTION_CALLBACK_H_
#define INCLUDE_V8_FUNCTION_CALLBACK_H_
#include <cstdint>
#include <limits>
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-primitive.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
template <typename T>
class BasicTracedReference;
template <typename T>
class Global;
class Object;
class Value;
namespace internal {
class FunctionCallbackArguments;
class PropertyCallbackArguments;
class Builtins;
} // namespace internal
namespace debug {
class ConsoleCallArguments;
} // namespace debug
namespace api_internal {
V8_EXPORT v8::Local<v8::Value> GetFunctionTemplateData(
v8::Isolate* isolate, v8::Local<v8::Data> raw_target);
} // namespace api_internal
template <typename T>
class ReturnValue {
public:
template <class S>
V8_INLINE ReturnValue(const ReturnValue<S>& that) : value_(that.value_) {
static_assert(std::is_base_of_v<T, S>, "type check");
}
// Handle-based setters.
template <typename S>
V8_INLINE void Set(const Global<S>& handle);
template <typename S>
V8_INLINE void SetNonEmpty(const Global<S>& handle);
template <typename S>
V8_INLINE void Set(const BasicTracedReference<S>& handle);
template <typename S>
V8_INLINE void SetNonEmpty(const BasicTracedReference<S>& handle);
template <typename S>
V8_INLINE void Set(const Local<S> handle);
template <typename S>
V8_INLINE void SetNonEmpty(const Local<S> handle);
// Fast primitive number setters.
V8_INLINE void Set(bool value);
V8_INLINE void Set(double i);
V8_INLINE void Set(int16_t i);
V8_INLINE void Set(int32_t i);
V8_INLINE void Set(int64_t i);
V8_INLINE void Set(uint16_t i);
V8_INLINE void Set(uint32_t i);
V8_INLINE void Set(uint64_t i);
// Fast JS primitive setters.
V8_INLINE void SetNull();
V8_INLINE void SetUndefined();
V8_INLINE void SetFalse();
V8_INLINE void SetEmptyString();
// Convenience getter for the Isolate.
V8_INLINE Isolate* GetIsolate() const;
// Pointer setter: Uncompilable to prevent inadvertent misuse.
template <typename S>
V8_INLINE void Set(S* whatever);
// Getter. Creates a new Local<> so it comes with a certain performance
// hit. If the ReturnValue was not yet set, this will return the undefined
// value.
V8_INLINE Local<Value> Get() const;
private:
template <class F>
friend class ReturnValue;
template <class F>
friend class FunctionCallbackInfo;
template <class F>
friend class PropertyCallbackInfo;
template <class F, class G, class H>
friend class PersistentValueMapBase;
V8_INLINE void SetInternal(internal::Address value);
// Default value depends on <T>:
// - <void> -> true_value,
// - <v8::Boolean> -> true_value,
// - <v8::Integer> -> 0,
// - <v8::Value> -> undefined_value,
// - <v8::Array> -> undefined_value.
V8_INLINE void SetDefaultValue();
V8_INLINE explicit ReturnValue(internal::Address* slot);
// See FunctionCallbackInfo.
static constexpr int kIsolateValueIndex = -1;
internal::Address* value_;
};
/**
* The argument information given to function call callbacks. This
* class provides access to information about the context of the call,
* including the receiver, the number and values of arguments, and
* the holder of the function.
*/
template <typename T>
class FunctionCallbackInfo {
public:
/** The number of available arguments. */
V8_INLINE int Length() const;
/**
* Accessor for the available arguments. Returns `undefined` if the index
* is out of bounds.
*/
V8_INLINE Local<Value> operator[](int i) const;
/** Returns the receiver. This corresponds to the "this" value. */
V8_INLINE Local<Object> This() const;
/** For construct calls, this returns the "new.target" value. */
V8_INLINE Local<Value> NewTarget() const;
/** Indicates whether this is a regular call or a construct call. */
V8_INLINE bool IsConstructCall() const;
/** The data argument specified when creating the callback. */
V8_INLINE Local<Value> Data() const;
/** The current Isolate. */
V8_INLINE Isolate* GetIsolate() const;
/** The ReturnValue for the call. */
V8_INLINE ReturnValue<T> GetReturnValue() const;
private:
friend class internal::FunctionCallbackArguments;
friend class internal::CustomArguments<FunctionCallbackInfo>;
friend class debug::ConsoleCallArguments;
friend void internal::PrintFunctionCallbackInfo(void*);
using I = internal::Internals;
// Frame block, matches the layout of ApiCallbackExitFrame.
// See ApiCallbackExitFrameConstants.
enum {
//
// Optional frame arguments block (exists only for API_CONSTRUCT_EXIT
// frame).
// Frame arguments block.
kNewTargetIndex = -1,
//
// Mandatory part, exists for both API_CALLBACK_EXIT and API_CONSTRUCT_EXIT
// frames.
//
// Frame arguments block.
kArgcIndex,
// Regular ExitFrame structure.
kFrameSPIndex,
kFrameTypeIndex,
kFrameConstantPoolIndex, // Optional, see I::kFrameCPSlotCount.
kFrameFPIndex = kFrameConstantPoolIndex + I::kFrameCPSlotCount,
kFramePCIndex,
// Api arguments block, starts at kFirstArgumentIndex.
kFirstApiArgumentIndex,
kIsolateIndex = kFirstApiArgumentIndex,
kReturnValueIndex,
kContextIndex,
kTargetIndex,
// JS args block, starts at kFrameFirstImplicitArgsIndex.
kReceiverIndex,
kFirstJSArgumentIndex,
// Mandatory part includes receiver.
kArgsLength = kReceiverIndex + 1,
// Optional part size (exists only for API_CONSTRUCT_EXIT frame).
kOptionalArgsLength = 1,
// The length of just Api arguments part.
kApiArgsLength = kReceiverIndex - kFirstApiArgumentIndex,
};
static_assert(kArgcIndex == 0);
static_assert(ReturnValue<Value>::kIsolateValueIndex ==
kIsolateIndex - kReturnValueIndex);
internal::Address* address_of_first_argument() const {
return &values_[kFirstJSArgumentIndex];
}
V8_INLINE FunctionCallbackInfo() = default;
// FunctionCallbackInfo object provides a view of the stack area where the
// data is stored and thus it's not supposed to be copyable/movable.
FunctionCallbackInfo(const FunctionCallbackInfo&) = delete;
FunctionCallbackInfo& operator=(const FunctionCallbackInfo&) = delete;
FunctionCallbackInfo(FunctionCallbackInfo&&) = delete;
FunctionCallbackInfo& operator=(FunctionCallbackInfo&&) = delete;
// Declare as mutable to let GC modify the contents of the slots even though
// it's not possible to change values via this class.
// Define the array size as 1 to make it clear that we are going to access
// it out-of-bounds from both sides anyway.
mutable internal::Address values_[1];
};
/**
* The information passed to a property callback about the context
* of the property access.
*/
template <typename T>
class PropertyCallbackInfo {
public:
/**
* \return The isolate of the property access.
*/
V8_INLINE Isolate* GetIsolate() const;
/**
* \return The data set in the configuration, i.e., in
* `NamedPropertyHandlerConfiguration` or
* `IndexedPropertyHandlerConfiguration.`
*/
V8_INLINE Local<Value> Data() const;
/**
* \return The object in the prototype chain of the receiver that has the
* interceptor. Suppose you have `x` and its prototype is `y`, and `y`
* has an interceptor. Then `info.This()` is `x` and `info.Holder()` is `y`.
* In case the property is installed on the global object the Holder()
* would return the global proxy.
*/
V8_INLINE Local<Object> Holder() const;
// TODO(http://crbug.com/333672197): deprecate and remove.
V8_DEPRECATE_SOON("Use Holder().")
V8_INLINE Local<Object> HolderV2() const;
/**
* \return The return value of the callback.
* Can be changed by calling Set().
* \code
* info.GetReturnValue().Set(...)
* \endcode
*
*/
V8_INLINE ReturnValue<T> GetReturnValue() const;
/**
* For [[Set]], [[DefineOwnProperty]] and [[Delete]] operations (i.e.
* for setter/definer/deleter callbacks) indicates whether TypeError
* should be thrown upon operation failure. The callback should throw
* TypeError only if it's necessary to provide more details than a default
* error thrown by V8 contains in this case.
*
* \return True if the intercepted function should throw if an error occurs.
* Usually, `true` corresponds to `'use strict'` execution mode.
*
* \note Always `false` when the operation was initiated by respecive
* `Reflect` call (i.e. `Reflect.set()`, `Reflect.defineProperty()` and
* `Reflect.deleteProperty()`).
*/
V8_INLINE bool ShouldThrowOnError() const;
private:
template <typename U>
friend class PropertyCallbackInfo;
friend class MacroAssembler;
friend class internal::PropertyCallbackArguments;
friend class internal::CustomArguments<PropertyCallbackInfo>;
friend void internal::PrintPropertyCallbackInfo(void*);
using I = internal::Internals;
// ShouldThrowOnError() can return true only for setter/definer/deleter
// callbacks which match [[Set]]/[[DefineOwnProperty]]/[[Delete]]
// operations. We detect these operations by return value type - they
// all return boolean value, even though setter/deleter callbacks are
// still using v8::PropertyCallbackInfo<void>.
// TODO(https://crbug.com/348660658): cleanup this, once the callbacks are
// migrated to a new return type.
static constexpr bool HasShouldThrowOnError() {
return std::is_same_v<T, v8::Boolean> || std::is_same_v<T, void>;
}
// Indicates whether this is a named accessor/interceptor callback call
// or an indexed one.
V8_INLINE bool IsNamed() const;
// Frame block, matches the layout of ApiAccessorExitFrame.
// See ApiAccessorExitFrameConstants.
enum {
// Frame arguments block.
kPropertyKeyIndex,
// Regular ExitFrame structure.
kFrameSPIndex,
kFrameTypeIndex,
kFrameConstantPoolIndex, // Optional, see I::kFrameCPSlotCount.
kFrameFPIndex = kFrameConstantPoolIndex + I::kFrameCPSlotCount,
kFramePCIndex,
// Other arguments block, starts at kFirstArgumentIndex.
kFirstApiArgumentIndex,
kIsolateIndex = kFirstApiArgumentIndex,
kReturnValueIndex,
kCallbackInfoIndex,
kHolderIndex,
//
// Optional part, used only by setter/definer/deleter callbacks.
//
kFirstOptionalArgument,
kShouldThrowOnErrorIndex = kFirstOptionalArgument,
// Used as value handle storage when called via CallApiSetter builtin.
kValueIndex,
kFullArgsLength,
kMandatoryArgsLength = kFirstOptionalArgument,
kOptionalArgsLength = kFullArgsLength - kFirstOptionalArgument,
// Various lengths of just Api arguments part.
kMandatoryApiArgsLength = kMandatoryArgsLength - kFirstApiArgumentIndex,
kFullApiArgsLength = kFullArgsLength - kFirstApiArgumentIndex,
};
// PropertyCallbackInfo object provides a view of the stack area where the
// data is stored and thus it's not supposed to be copyable/movable.
PropertyCallbackInfo(const PropertyCallbackInfo&) = delete;
PropertyCallbackInfo& operator=(const PropertyCallbackInfo&) = delete;
PropertyCallbackInfo(PropertyCallbackInfo&&) = delete;
PropertyCallbackInfo& operator=(PropertyCallbackInfo&&) = delete;
PropertyCallbackInfo() = default;
// Declare as mutable to let GC modify the contents of the slots even though
// it's not possible to change values via this class.
// Define the array size as 1 to make it clear that we are going to access
// it out-of-bounds anyway.
mutable internal::Address args_[1];
};
using FunctionCallback = void (*)(const FunctionCallbackInfo<Value>& info);
// --- Implementation ---
template <typename T>
ReturnValue<T>::ReturnValue(internal::Address* slot) : value_(slot) {}
template <typename T>
void ReturnValue<T>::SetInternal(internal::Address value) {
#if V8_STATIC_ROOTS_BOOL
using I = internal::Internals;
// Ensure that the upper 32-bits are not modified. Compiler should be
// able to optimize this to a store of a lower 32-bits of the value.
// This is fine since the callback can return only JavaScript values which
// are either Smis or heap objects allocated in the main cage.
*value_ = I::DecompressTaggedField(*value_, I::CompressTagged(value));
#else
*value_ = value;
#endif // V8_STATIC_ROOTS_BOOL
}
template <typename T>
template <typename S>
void ReturnValue<T>::Set(const Global<S>& handle) {
static_assert(std::is_base_of_v<T, S>, "type check");
if (V8_UNLIKELY(handle.IsEmpty())) {
SetDefaultValue();
} else {
SetInternal(handle.ptr());
}
}
template <typename T>
template <typename S>
void ReturnValue<T>::SetNonEmpty(const Global<S>& handle) {
static_assert(std::is_base_of_v<T, S>, "type check");
#ifdef V8_ENABLE_CHECKS
internal::VerifyHandleIsNonEmpty(handle.IsEmpty());
#endif // V8_ENABLE_CHECKS
SetInternal(handle.ptr());
}
template <typename T>
template <typename S>
void ReturnValue<T>::Set(const BasicTracedReference<S>& handle) {
static_assert(std::is_base_of_v<T, S>, "type check");
if (V8_UNLIKELY(handle.IsEmpty())) {
SetDefaultValue();
} else {
SetInternal(handle.ptr());
}
}
template <typename T>
template <typename S>
void ReturnValue<T>::SetNonEmpty(const BasicTracedReference<S>& handle) {
static_assert(std::is_base_of_v<T, S>, "type check");
#ifdef V8_ENABLE_CHECKS
internal::VerifyHandleIsNonEmpty(handle.IsEmpty());
#endif // V8_ENABLE_CHECKS
SetInternal(handle.ptr());
}
template <typename T>
template <typename S>
void ReturnValue<T>::Set(const Local<S> handle) {
static_assert(std::is_base_of_v<T, S>, "type check");
if (V8_UNLIKELY(handle.IsEmpty())) {
SetDefaultValue();
} else {
SetInternal(handle.ptr());
}
}
template <typename T>
template <typename S>
void ReturnValue<T>::SetNonEmpty(const Local<S> handle) {
static_assert(std::is_base_of_v<T, S>, "type check");
#ifdef V8_ENABLE_CHECKS
internal::VerifyHandleIsNonEmpty(handle.IsEmpty());
#endif // V8_ENABLE_CHECKS
SetInternal(handle.ptr());
}
template <typename T>
void ReturnValue<T>::Set(double i) {
static_assert(std::is_base_of_v<T, Number>, "type check");
SetNonEmpty(Number::New(GetIsolate(), i));
}
template <typename T>
void ReturnValue<T>::Set(int16_t i) {
static_assert(std::is_base_of_v<T, Integer>, "type check");
using I = internal::Internals;
static_assert(I::IsValidSmi(std::numeric_limits<int16_t>::min()));
static_assert(I::IsValidSmi(std::numeric_limits<int16_t>::max()));
SetInternal(I::IntegralToSmi(i));
}
template <typename T>
void ReturnValue<T>::Set(int32_t i) {
static_assert(std::is_base_of_v<T, Integer>, "type check");
if (const auto result = internal::Internals::TryIntegralToSmi(i)) {
SetInternal(*result);
return;
}
SetNonEmpty(Integer::New(GetIsolate(), i));
}
template <typename T>
void ReturnValue<T>::Set(int64_t i) {
static_assert(std::is_base_of_v<T, Integer>, "type check");
if (const auto result = internal::Internals::TryIntegralToSmi(i)) {
SetInternal(*result);
return;
}
SetNonEmpty(Number::New(GetIsolate(), static_cast<double>(i)));
}
template <typename T>
void ReturnValue<T>::Set(uint16_t i) {
static_assert(std::is_base_of_v<T, Integer>, "type check");
using I = internal::Internals;
static_assert(I::IsValidSmi(std::numeric_limits<uint16_t>::min()));
static_assert(I::IsValidSmi(std::numeric_limits<uint16_t>::max()));
SetInternal(I::IntegralToSmi(i));
}
template <typename T>
void ReturnValue<T>::Set(uint32_t i) {
static_assert(std::is_base_of_v<T, Integer>, "type check");
if (const auto result = internal::Internals::TryIntegralToSmi(i)) {
SetInternal(*result);
return;
}
SetNonEmpty(Integer::NewFromUnsigned(GetIsolate(), i));
}
template <typename T>
void ReturnValue<T>::Set(uint64_t i) {
static_assert(std::is_base_of_v<T, Integer>, "type check");
if (const auto result = internal::Internals::TryIntegralToSmi(i)) {
SetInternal(*result);
return;
}
SetNonEmpty(Number::New(GetIsolate(), static_cast<double>(i)));
}
template <typename T>
void ReturnValue<T>::Set(bool value) {
static_assert(std::is_void_v<T> || std::is_base_of_v<T, Boolean>,
"type check");
using I = internal::Internals;
#if V8_STATIC_ROOTS_BOOL
#ifdef V8_ENABLE_CHECKS
internal::PerformCastCheck(
internal::ValueHelper::SlotAsValue<Value, true>(value_));
#endif // V8_ENABLE_CHECKS
SetInternal(value ? I::StaticReadOnlyRoot::kTrueValue
: I::StaticReadOnlyRoot::kFalseValue);
#else
int root_index;
if (value) {
root_index = I::kTrueValueRootIndex;
} else {
root_index = I::kFalseValueRootIndex;
}
*value_ = I::GetRoot(GetIsolate(), root_index);
#endif // V8_STATIC_ROOTS_BOOL
}
template <typename T>
void ReturnValue<T>::SetDefaultValue() {
using I = internal::Internals;
if constexpr (std::is_same_v<void, T> || std::is_same_v<v8::Boolean, T>) {
Set(true);
} else if constexpr (std::is_same_v<v8::Integer, T>) {
SetInternal(I::IntegralToSmi(0));
} else {
static_assert(std::is_same_v<v8::Value, T> || std::is_same_v<v8::Array, T>);
#if V8_STATIC_ROOTS_BOOL
SetInternal(I::StaticReadOnlyRoot::kUndefinedValue);
#else
*value_ = I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
#endif // V8_STATIC_ROOTS_BOOL
}
}
template <typename T>
void ReturnValue<T>::SetNull() {
static_assert(std::is_base_of_v<T, Primitive>, "type check");
using I = internal::Internals;
#if V8_STATIC_ROOTS_BOOL
#ifdef V8_ENABLE_CHECKS
internal::PerformCastCheck(
internal::ValueHelper::SlotAsValue<Value, true>(value_));
#endif // V8_ENABLE_CHECKS
SetInternal(I::StaticReadOnlyRoot::kNullValue);
#else
*value_ = I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
#endif // V8_STATIC_ROOTS_BOOL
}
template <typename T>
void ReturnValue<T>::SetUndefined() {
static_assert(std::is_base_of_v<T, Primitive>, "type check");
using I = internal::Internals;
#if V8_STATIC_ROOTS_BOOL
#ifdef V8_ENABLE_CHECKS
internal::PerformCastCheck(
internal::ValueHelper::SlotAsValue<Value, true>(value_));
#endif // V8_ENABLE_CHECKS
SetInternal(I::StaticReadOnlyRoot::kUndefinedValue);
#else
*value_ = I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
#endif // V8_STATIC_ROOTS_BOOL
}
template <typename T>
void ReturnValue<T>::SetFalse() {
static_assert(std::is_void_v<T> || std::is_base_of_v<T, Boolean>,
"type check");
using I = internal::Internals;
#if V8_STATIC_ROOTS_BOOL
#ifdef V8_ENABLE_CHECKS
internal::PerformCastCheck(
internal::ValueHelper::SlotAsValue<Value, true>(value_));
#endif // V8_ENABLE_CHECKS
SetInternal(I::StaticReadOnlyRoot::kFalseValue);
#else
*value_ = I::GetRoot(GetIsolate(), I::kFalseValueRootIndex);
#endif // V8_STATIC_ROOTS_BOOL
}
template <typename T>
void ReturnValue<T>::SetEmptyString() {
static_assert(std::is_base_of_v<T, String>, "type check");
using I = internal::Internals;
#if V8_STATIC_ROOTS_BOOL
#ifdef V8_ENABLE_CHECKS
internal::PerformCastCheck(
internal::ValueHelper::SlotAsValue<Value, true>(value_));
#endif // V8_ENABLE_CHECKS
SetInternal(I::StaticReadOnlyRoot::kEmptyString);
#else
*value_ = I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
#endif // V8_STATIC_ROOTS_BOOL
}
template <typename T>
Isolate* ReturnValue<T>::GetIsolate() const {
return *reinterpret_cast<Isolate**>(&value_[kIsolateValueIndex]);
}
template <typename T>
Local<Value> ReturnValue<T>::Get() const {
return Local<Value>::New(GetIsolate(),
internal::ValueHelper::SlotAsValue<Value>(value_));
}
template <typename T>
template <typename S>
void ReturnValue<T>::Set(S* whatever) {
static_assert(sizeof(S) < 0, "incompilable to prevent inadvertent misuse");
}
template <typename T>
Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
if (i < 0 || Length() <= i) return Undefined(GetIsolate());
return Local<Value>::FromSlot(&values_[kFirstJSArgumentIndex + i]);
}
template <typename T>
Local<Object> FunctionCallbackInfo<T>::This() const {
return Local<Object>::FromSlot(&values_[kReceiverIndex]);
}
template <typename T>
Local<Value> FunctionCallbackInfo<T>::NewTarget() const {
if (IsConstructCall()) {
// Can't use &values_[kNewTargetIndex] because of "array index -1 is
// before the beginning of the array" error.
internal::Address* values = &values_[0];
return Local<Value>::FromSlot(values + kNewTargetIndex);
}
return Undefined(GetIsolate());
}
template <typename T>
Local<Value> FunctionCallbackInfo<T>::Data() const {
auto target = Local<v8::Data>::FromSlot(&values_[kTargetIndex]);
return api_internal::GetFunctionTemplateData(GetIsolate(), target);
}
template <typename T>
Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
return reinterpret_cast<Isolate*>(values_[kIsolateIndex]);
}
template <typename T>
ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
return ReturnValue<T>(&values_[kReturnValueIndex]);
}
template <typename T>
bool FunctionCallbackInfo<T>::IsConstructCall() const {
return I::SmiValue(values_[kFrameTypeIndex]) == I::kFrameTypeApiConstructExit;
}
template <typename T>
int FunctionCallbackInfo<T>::Length() const {
return static_cast<int>(values_[kArgcIndex]);
}
template <typename T>
bool PropertyCallbackInfo<T>::IsNamed() const {
return I::SmiValue(args_[kFrameTypeIndex]) ==
I::kFrameTypeApiNamedAccessorExit;
}
template <typename T>
Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
}
template <typename T>
Local<Value> PropertyCallbackInfo<T>::Data() const {
internal::Address callback_info = args_[kCallbackInfoIndex];
internal::Address data =
I::ReadTaggedPointerField(callback_info, I::kCallbackInfoDataOffset);
return Local<Value>::New(GetIsolate(), data);
}
template <typename T>
Local<Object> PropertyCallbackInfo<T>::Holder() const {
return Local<Object>::FromSlot(&args_[kHolderIndex]);
}
template <typename T>
Local<Object> PropertyCallbackInfo<T>::HolderV2() const {
return Holder();
}
template <typename T>
ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
return ReturnValue<T>(&args_[kReturnValueIndex]);
}
template <typename T>
bool PropertyCallbackInfo<T>::ShouldThrowOnError() const {
if constexpr (!HasShouldThrowOnError()) return false;
if (args_[kShouldThrowOnErrorIndex] !=
I::IntegralToSmi(I::kInferShouldThrowMode)) {
return args_[kShouldThrowOnErrorIndex] != I::IntegralToSmi(I::kDontThrow);
}
return v8::internal::ShouldThrowOnError(
reinterpret_cast<v8::internal::Isolate*>(GetIsolate()));
}
} // namespace v8
#endif // INCLUDE_V8_FUNCTION_CALLBACK_H_

View File

@ -0,0 +1,148 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_FUNCTION_H_
#define INCLUDE_V8_FUNCTION_H_
#include <stddef.h>
#include <stdint.h>
#include "v8-function-callback.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-message.h" // NOLINT(build/include_directory)
#include "v8-object.h" // NOLINT(build/include_directory)
#include "v8-template.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
class Location;
class UnboundScript;
/**
* A JavaScript function object (ECMA-262, 15.3).
*/
class V8_EXPORT Function : public Object {
public:
/**
* Create a function in the current execution context
* for a given FunctionCallback.
*/
static MaybeLocal<Function> New(
Local<Context> context, FunctionCallback callback, Local<Data> data = {},
int length = 0,
ConstructorBehavior behavior = ConstructorBehavior::kAllow,
SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
Local<Context> context, int argc, Local<Value> argv[]) const;
V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
Local<Context> context) const {
return NewInstance(context, 0, nullptr);
}
/**
* When side effect checks are enabled, passing kHasNoSideEffect allows the
* constructor to be invoked without throwing. Calls made within the
* constructor are still checked.
*/
V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstanceWithSideEffectType(
Local<Context> context, int argc, Local<Value> argv[],
SideEffectType side_effect_type = SideEffectType::kHasSideEffect) const;
V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(v8::Isolate* isolate,
Local<Context> context,
Local<Value> recv, int argc,
Local<Value> argv[]);
V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
Local<Value> recv, int argc,
Local<Value> argv[]);
void SetName(Local<String> name);
Local<Value> GetName() const;
/**
* Name inferred from variable or property assignment of this function.
* Used to facilitate debugging and profiling of JavaScript code written
* in an OO style, where many functions are anonymous but are assigned
* to object properties.
*/
Local<Value> GetInferredName() const;
/**
* displayName if it is set, otherwise name if it is configured, otherwise
* function name, otherwise inferred name.
*/
Local<Value> GetDebugName() const;
/**
* Returns zero based line number of function body and
* kLineOffsetNotFound if no information available.
*/
int GetScriptLineNumber() const;
/**
* Returns zero based column number of function body and
* kLineOffsetNotFound if no information available.
*/
int GetScriptColumnNumber() const;
/**
* Returns zero based line and column number of function body, else returns
* {-1, -1}.
*/
Location GetScriptLocation() const;
/**
* Returns zero based start position (character offset) of function body and
* kLineOffsetNotFound if no information available.
*/
int GetScriptStartPosition() const;
/**
* Returns scriptId.
*/
int ScriptId() const;
/**
* Returns the original function if this function is bound, else returns
* v8::Undefined.
*/
Local<Value> GetBoundFunction() const;
/**
* Calls builtin Function.prototype.toString on this function.
* This is different from Value::ToString() that may call a user-defined
* toString() function, and different than Object::ObjectProtoToString() which
* always serializes "[object Function]".
*/
V8_WARN_UNUSED_RESULT MaybeLocal<String> FunctionProtoToString(
Local<Context> context);
/**
* Returns true if the function does nothing.
* The function returns false on error.
* Note that this function is experimental. Embedders should not rely on
* this existing. We may remove this function in the future.
*/
V8_WARN_UNUSED_RESULT bool Experimental_IsNopFunction() const;
ScriptOrigin GetScriptOrigin() const;
V8_INLINE static Function* Cast(Value* value) {
#ifdef V8_ENABLE_CHECKS
CheckCast(value);
#endif
return static_cast<Function*>(value);
}
static const int kLineOffsetNotFound;
private:
Function();
static void CheckCast(Value* obj);
};
} // namespace v8
#endif // INCLUDE_V8_FUNCTION_H_

View File

@ -0,0 +1,155 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_HANDLE_BASE_H_
#define INCLUDE_V8_HANDLE_BASE_H_
#include "v8-internal.h" // NOLINT(build/include_directory)
namespace v8::api_internal {
template <bool check_statically_enabled>
class StackAllocated {
public:
V8_INLINE StackAllocated() = default;
protected:
struct no_checking_tag {};
static constexpr no_checking_tag do_not_check{};
V8_INLINE explicit StackAllocated(no_checking_tag) {}
V8_INLINE explicit StackAllocated(const StackAllocated& other,
no_checking_tag) {}
V8_INLINE void VerifyOnStack() const {}
};
template <>
class V8_TRIVIAL_ABI StackAllocated<true> : public StackAllocated<false> {
public:
V8_INLINE StackAllocated() { VerifyOnStack(); }
#if V8_HAS_ATTRIBUTE_TRIVIAL_ABI
// In this case, StackAllocated becomes not trivially copyable.
V8_INLINE StackAllocated(const StackAllocated& other) { VerifyOnStack(); }
StackAllocated& operator=(const StackAllocated&) = default;
#endif
protected:
V8_INLINE explicit StackAllocated(no_checking_tag tag)
: StackAllocated<false>(tag) {}
V8_INLINE explicit StackAllocated(const StackAllocated& other,
no_checking_tag tag)
: StackAllocated<false>(other, tag) {}
#ifdef ENABLE_SLOW_DCHECKS
V8_EXPORT void VerifyOnStack() const;
#else
V8_INLINE V8_EXPORT void VerifyOnStack() const {}
#endif
};
/**
* A base class for abstract handles containing indirect pointers.
* These are useful regardless of whether direct local support is enabled.
*/
class IndirectHandleBase {
public:
// Returns true if the handle is empty.
V8_INLINE bool IsEmpty() const { return location_ == nullptr; }
// Sets the handle to be empty. IsEmpty() will then return true.
V8_INLINE void Clear() { location_ = nullptr; }
protected:
friend class internal::ValueHelper;
friend class internal::HandleHelper;
V8_INLINE IndirectHandleBase() = default;
V8_INLINE IndirectHandleBase(const IndirectHandleBase& other) = default;
V8_INLINE IndirectHandleBase& operator=(const IndirectHandleBase& that) =
default;
V8_INLINE explicit IndirectHandleBase(internal::Address* location)
: location_(location) {}
// Returns the address of the actual heap object (tagged).
// This method must be called only if the handle is not empty, otherwise it
// will crash.
V8_INLINE internal::Address ptr() const { return *location_; }
// Returns a reference to the slot (indirect pointer).
V8_INLINE internal::Address* const& slot() const { return location_; }
V8_INLINE internal::Address*& slot() { return location_; }
// Returns the handler's "value" (direct or indirect pointer, depending on
// whether direct local support is enabled).
template <typename T, bool check_null = false>
V8_INLINE T* value() const {
return internal::ValueHelper::SlotAsValue<T, check_null>(slot());
}
#ifdef V8_ENABLE_DIRECT_HANDLE
V8_INLINE internal::ValueHelper::InternalRepresentationType repr() const {
return location_ ? *location_ : internal::ValueHelper::kEmpty;
}
#else
V8_INLINE internal::ValueHelper::InternalRepresentationType repr() const {
return location_;
}
#endif // V8_ENABLE_DIRECT_HANDLE
private:
internal::Address* location_ = nullptr;
};
#ifdef V8_ENABLE_DIRECT_HANDLE
/**
* A base class for abstract handles containing direct pointers.
* These are only possible when conservative stack scanning is enabled.
*/
class DirectHandleBase {
public:
// Returns true if the handle is empty.
V8_INLINE bool IsEmpty() const {
return ptr_ == internal::ValueHelper::kEmpty;
}
// Sets the handle to be empty. IsEmpty() will then return true.
V8_INLINE void Clear() { ptr_ = internal::ValueHelper::kEmpty; }
protected:
friend class internal::ValueHelper;
friend class internal::HandleHelper;
V8_INLINE DirectHandleBase() = default;
V8_INLINE DirectHandleBase(const DirectHandleBase& other) = default;
V8_INLINE DirectHandleBase& operator=(const DirectHandleBase& that) = default;
V8_INLINE explicit DirectHandleBase(internal::Address ptr) : ptr_(ptr) {}
// Returns the address of the referenced object.
V8_INLINE internal::Address ptr() const { return ptr_; }
// Returns the handler's "value" (direct pointer, as direct local support
// is guaranteed to be enabled here).
template <typename T, bool check_null = false>
V8_INLINE T* value() const {
return reinterpret_cast<T*>(ptr_);
}
V8_INLINE internal::ValueHelper::InternalRepresentationType repr() const {
return ptr_;
}
private:
internal::Address ptr_ = internal::ValueHelper::kEmpty;
};
#endif // V8_ENABLE_DIRECT_HANDLE
} // namespace v8::api_internal
#endif // INCLUDE_V8_HANDLE_BASE_H_

View File

@ -0,0 +1,384 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_INITIALIZATION_H_
#define INCLUDE_V8_INITIALIZATION_H_
#include <stddef.h>
#include <stdint.h>
#include "v8-callbacks.h" // NOLINT(build/include_directory)
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8-isolate.h" // NOLINT(build/include_directory)
#include "v8-platform.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
// We reserve the V8_* prefix for macros defined in V8 public API and
// assume there are no name conflicts with the embedder's code.
/**
* The v8 JavaScript engine.
*/
namespace v8 {
class PageAllocator;
class Platform;
template <class K, class V, class T>
class PersistentValueMapBase;
/**
* EntropySource is used as a callback function when v8 needs a source
* of entropy.
*/
using EntropySource = bool (*)(unsigned char* buffer, size_t length);
/**
* ReturnAddressLocationResolver is used as a callback function when v8 is
* resolving the location of a return address on the stack. Profilers that
* change the return address on the stack can use this to resolve the stack
* location to wherever the profiler stashed the original return address.
*
* \param return_addr_location A location on stack where a machine
* return address resides.
* \returns Either return_addr_location, or else a pointer to the profiler's
* copy of the original return address.
*
* \note The resolver function must not cause garbage collection.
*/
using ReturnAddressLocationResolver =
uintptr_t (*)(uintptr_t return_addr_location);
using DcheckErrorCallback = void (*)(const char* file, int line,
const char* message);
using V8FatalErrorCallback = void (*)(const char* file, int line,
const char* message);
/**
* Container class for static utility functions.
*/
class V8_EXPORT V8 {
public:
/**
* Hand startup data to V8, in case the embedder has chosen to build
* V8 with external startup data.
*
* Note:
* - By default the startup data is linked into the V8 library, in which
* case this function is not meaningful.
* - If this needs to be called, it needs to be called before V8
* tries to make use of its built-ins.
* - To avoid unnecessary copies of data, V8 will point directly into the
* given data blob, so pretty please keep it around until V8 exit.
* - Compression of the startup blob might be useful, but needs to
* handled entirely on the embedders' side.
* - The call will abort if the data is invalid.
*/
static void SetSnapshotDataBlob(StartupData* startup_blob);
/** Set the callback to invoke in case of Dcheck failures. */
static void SetDcheckErrorHandler(DcheckErrorCallback that);
/** Set the callback to invoke in the case of CHECK failures or fatal
* errors. This is distinct from Isolate::SetFatalErrorHandler, which
* is invoked in response to API usage failures.
* */
static void SetFatalErrorHandler(V8FatalErrorCallback that);
/**
* Sets V8 flags from a string.
*/
static void SetFlagsFromString(const char* str);
static void SetFlagsFromString(const char* str, size_t length);
/**
* Sets V8 flags from the command line.
*/
static void SetFlagsFromCommandLine(int* argc, char** argv,
bool remove_flags);
/** Get the version string. */
static const char* GetVersion();
/**
* Initializes V8. This function needs to be called before the first Isolate
* is created. It always returns true.
*/
V8_INLINE static bool Initialize() {
#ifdef V8_TARGET_OS_ANDROID
const bool kV8TargetOsIsAndroid = true;
#else
const bool kV8TargetOsIsAndroid = false;
#endif
#ifdef V8_ENABLE_CHECKS
const bool kV8EnableChecks = true;
#else
const bool kV8EnableChecks = false;
#endif
const int kBuildConfiguration =
(internal::PointerCompressionIsEnabled() ? kPointerCompression : 0) |
(internal::SmiValuesAre31Bits() ? k31BitSmis : 0) |
(internal::SandboxIsEnabled() ? kSandbox : 0) |
(kV8TargetOsIsAndroid ? kTargetOsIsAndroid : 0) |
(kV8EnableChecks ? kEnableChecks : 0);
return Initialize(kBuildConfiguration);
}
/**
* Allows the host application to provide a callback which can be used
* as a source of entropy for random number generators.
*/
static void SetEntropySource(EntropySource source);
/**
* Allows the host application to provide a callback that allows v8 to
* cooperate with a profiler that rewrites return addresses on stack.
*/
static void SetReturnAddressLocationResolver(
ReturnAddressLocationResolver return_address_resolver);
/**
* Releases any resources used by v8 and stops any utility threads
* that may be running. Note that disposing v8 is permanent, it
* cannot be reinitialized.
*
* It should generally not be necessary to dispose v8 before exiting
* a process, this should happen automatically. It is only necessary
* to use if the process needs the resources taken up by v8.
*/
static bool Dispose();
/**
* Initialize the ICU library bundled with V8. The embedder should only
* invoke this method when using the bundled ICU. Returns true on success.
*
* If V8 was compiled with the ICU data in an external file, the location
* of the data file has to be provided.
*/
static bool InitializeICU(const char* icu_data_file = nullptr);
/**
* Initialize the ICU library bundled with V8. The embedder should only
* invoke this method when using the bundled ICU. If V8 was compiled with
* the ICU data in an external file and when the default location of that
* file should be used, a path to the executable must be provided.
* Returns true on success.
*
* The default is a file called icudtl.dat side-by-side with the executable.
*
* Optionally, the location of the data file can be provided to override the
* default.
*/
static bool InitializeICUDefaultLocation(const char* exec_path,
const char* icu_data_file = nullptr);
/**
* Initialize the external startup data. The embedder only needs to
* invoke this method when external startup data was enabled in a build.
*
* If V8 was compiled with the startup data in an external file, then
* V8 needs to be given those external files during startup. There are
* three ways to do this:
* - InitializeExternalStartupData(const char*)
* This will look in the given directory for the file "snapshot_blob.bin".
* - InitializeExternalStartupDataFromFile(const char*)
* As above, but will directly use the given file name.
* - Call SetSnapshotDataBlob.
* This will read the blobs from the given data structure and will
* not perform any file IO.
*/
static void InitializeExternalStartupData(const char* directory_path);
static void InitializeExternalStartupDataFromFile(const char* snapshot_blob);
/**
* Sets the v8::Platform to use. This should be invoked before V8 is
* initialized.
*/
static void InitializePlatform(Platform* platform);
/**
* Clears all references to the v8::Platform. This should be invoked after
* V8 was disposed.
*/
static void DisposePlatform();
#if defined(V8_ENABLE_SANDBOX)
/**
* The mode the V8 sandbox operates in.
*
* These values are persisted to logs. Entries should not be renumbered and
* numeric values should never be reused. If you add new items here, update
* V8SandboxMode in tools/metrics/histograms/metadata/v8/enums.xml in
* Chromium.
*/
enum class SandboxMode : uint8_t {
/**
* The sandbox is configured securely with a full reservation and an
* inaccessible Smi address range.
*/
kSecure = 0,
/**
* The sandbox is configured insecurely without a known reason.
*/
kInsecure = 1,
/**
* The sandbox is partially reserved, but the Smi address range is
* inaccessible.
*/
kInsecurePartialReservationSmiInaccessible = 2,
/**
* The sandbox is fully reserved, but the Smi address range is accessible.
*/
kInsecureFullReservationSmiAccessible = 3,
/**
* The sandbox is partially reserved and the Smi address range is
* accessible.
*/
kInsecurePartialReservationSmiAccessible = 4,
kMaxValue = kInsecurePartialReservationSmiAccessible,
};
/**
* Returns the current state of the sandbox.
*/
static SandboxMode GetSandboxMode();
/**
* Returns true if the sandbox is configured securely.
*
* If V8 cannot create a regular sandbox during initialization, for example
* because not enough virtual address space can be reserved, it will instead
* create a fallback sandbox that still allows it to function normally but
* does not have the same security properties as a regular sandbox. This API
* can be used to determine if such a fallback sandbox is being used, in
* which case it will return false.
*/
static bool IsSandboxConfiguredSecurely();
/**
* Provides access to the virtual address subspace backing the sandbox.
*
* This can be used to allocate pages inside the sandbox, for example to
* obtain virtual memory for ArrayBuffer backing stores, which must be
* located inside the sandbox.
*
* It should be assumed that an attacker can corrupt data inside the sandbox,
* and so in particular the contents of pages allocagted in this virtual
* address space, arbitrarily and concurrently. Due to this, it is
* recommended to to only place pure data buffers in them.
*/
static VirtualAddressSpace* GetSandboxAddressSpace();
/**
* Returns the size of the sandbox in bytes.
*
* This represents the size of the address space that V8 can directly address
* and in which it allocates its objects.
*/
static size_t GetSandboxSizeInBytes();
/**
* Returns the size of the address space reservation backing the sandbox.
*
* This may be larger than the sandbox (i.e. |GetSandboxSizeInBytes()|) due
* to surrounding guard regions, or may be smaller than the sandbox in case a
* fallback sandbox is being used, which will use a smaller virtual address
* space reservation. In the latter case this will also be different from
* |GetSandboxAddressSpace()->size()| as that will cover a larger part of the
* address space than what has actually been reserved.
*/
static size_t GetSandboxReservationSizeInBytes();
#endif // V8_ENABLE_SANDBOX
enum class WasmMemoryType {
kMemory32,
kMemory64,
};
/**
* Returns the virtual address space reservation size (in bytes) needed
* for one WebAssembly memory instance of the given capacity.
*
* \param type Whether this is a memory32 or memory64 instance.
* \param byte_capacity The maximum size, in bytes, of the WebAssembly
* memory. Values exceeding the engine's maximum allocatable memory
* size for the given type (determined by max_mem32_pages or
* max_mem64_pages) are clamped.
*
* When trap-based bounds checking is enabled by
* EnableWebAssemblyTrapHandler(), the amount of virtual address space
* that V8 needs to reserve for each WebAssembly memory instance can
* be much bigger than the requested size. If the process does
* not have enough virtual memory available, WebAssembly memory allocation
* would fail. During the initialization of V8, embedders can use this method
* to estimate whether the process has enough virtual memory for their
* usage of WebAssembly, and decide whether to enable the trap handler
* via EnableWebAssemblyTrapHandler(), or to skip it and reduce the amount of
* virtual memory required to keep the application running.
*/
static size_t GetWasmMemoryReservationSizeInBytes(WasmMemoryType type,
size_t byte_capacity);
/**
* Activate trap-based bounds checking for WebAssembly.
*
* \param use_v8_signal_handler Whether V8 should install its own signal
* handler or rely on the embedder's.
*/
static bool EnableWebAssemblyTrapHandler(bool use_v8_signal_handler);
#if defined(V8_OS_WIN)
/**
* On Win64, by default V8 does not emit unwinding data for jitted code,
* which means the OS cannot walk the stack frames and the system Structured
* Exception Handling (SEH) cannot unwind through V8-generated code:
* https://code.google.com/p/v8/issues/detail?id=3598.
*
* This function allows embedders to register a custom exception handler for
* exceptions in V8-generated code.
*/
static void SetUnhandledExceptionCallback(
UnhandledExceptionCallback callback);
#endif
/**
* Allows the host application to provide a callback that will be called when
* v8 has encountered a fatal failure to allocate memory and is about to
* terminate.
*/
static void SetFatalMemoryErrorCallback(OOMErrorCallback callback);
/**
* Get statistics about the shared memory usage.
*/
static void GetSharedMemoryStatistics(SharedMemoryStatistics* statistics);
private:
V8();
enum BuildConfigurationFeatures {
kPointerCompression = 1 << 0,
k31BitSmis = 1 << 1,
kSandbox = 1 << 2,
kTargetOsIsAndroid = 1 << 3,
kEnableChecks = 1 << 4,
};
/**
* Checks that the embedder build configuration is compatible with
* the V8 binary and if so initializes V8.
*/
static bool Initialize(int build_config);
friend class Context;
template <class K, class V, class T>
friend class PersistentValueMapBase;
};
} // namespace v8
#endif // INCLUDE_V8_INITIALIZATION_H_

View File

@ -0,0 +1,13 @@
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_V8_INSPECTOR_PROTOCOL_H_
#define V8_V8_INSPECTOR_PROTOCOL_H_
#include "inspector/Debugger.h" // NOLINT(build/include_directory)
#include "inspector/Runtime.h" // NOLINT(build/include_directory)
#include "inspector/Schema.h" // NOLINT(build/include_directory)
#include "v8-inspector.h" // NOLINT(build/include_directory)
#endif // V8_V8_INSPECTOR_PROTOCOL_H_

View File

@ -0,0 +1,468 @@
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_V8_INSPECTOR_H_
#define V8_V8_INSPECTOR_H_
#include <stdint.h>
#include <cctype>
#include <memory>
#include "cppgc/garbage-collected.h" // NOLINT(build/include_directory)
#include "v8-isolate.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
class Name;
class Object;
class StackTrace;
class Value;
} // namespace v8
namespace v8_inspector {
namespace internal {
class V8DebuggerId;
} // namespace internal
namespace protocol {
namespace Debugger {
namespace API {
class SearchMatch;
}
} // namespace Debugger
namespace Runtime {
namespace API {
class RemoteObject;
class StackTrace;
class StackTraceId;
} // namespace API
} // namespace Runtime
namespace Schema {
namespace API {
class Domain;
}
} // namespace Schema
} // namespace protocol
class V8_EXPORT StringView {
public:
StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
StringView(const uint8_t* characters, size_t length)
: m_is8Bit(true), m_length(length), m_characters8(characters) {}
StringView(const uint16_t* characters, size_t length)
: m_is8Bit(false), m_length(length), m_characters16(characters) {}
bool is8Bit() const { return m_is8Bit; }
size_t length() const { return m_length; }
// TODO(dgozman): add DCHECK(m_is8Bit) to accessors once platform can be used
// here.
const uint8_t* characters8() const { return m_characters8; }
const uint16_t* characters16() const { return m_characters16; }
private:
bool m_is8Bit;
size_t m_length;
union {
const uint8_t* m_characters8;
const uint16_t* m_characters16;
};
};
class V8_EXPORT StringBuffer {
public:
virtual ~StringBuffer() = default;
virtual StringView string() const = 0;
// This method copies contents.
static std::unique_ptr<StringBuffer> create(StringView);
};
class V8_EXPORT V8ContextInfo {
public:
V8ContextInfo(v8::Local<v8::Context> context, int contextGroupId,
StringView humanReadableName)
: context(context),
contextGroupId(contextGroupId),
humanReadableName(humanReadableName),
hasMemoryOnConsole(false) {}
v8::Local<v8::Context> context;
// Each v8::Context is a part of a group. The group id must be non-zero.
int contextGroupId;
StringView humanReadableName;
StringView origin;
StringView auxData;
bool hasMemoryOnConsole;
static int executionContextId(v8::Local<v8::Context> context);
// Disallow copying and allocating this one.
enum NotNullTagEnum { NotNullLiteral };
void* operator new(size_t) = delete;
void* operator new(size_t, NotNullTagEnum, void*) = delete;
void* operator new(size_t, void*) = delete;
V8ContextInfo(const V8ContextInfo&) = delete;
V8ContextInfo& operator=(const V8ContextInfo&) = delete;
};
// This debugger id tries to be unique by generating two random
// numbers, which should most likely avoid collisions.
// Debugger id has a 1:1 mapping to context group. It is used to
// attribute stack traces to a particular debugging, when doing any
// cross-debugger operations (e.g. async step in).
// See also Runtime.UniqueDebuggerId in the protocol.
class V8_EXPORT V8DebuggerId {
public:
V8DebuggerId() = default;
V8DebuggerId(const V8DebuggerId&) = default;
V8DebuggerId& operator=(const V8DebuggerId&) = default;
std::unique_ptr<StringBuffer> toString() const;
bool isValid() const;
std::pair<int64_t, int64_t> pair() const;
private:
friend class internal::V8DebuggerId;
explicit V8DebuggerId(std::pair<int64_t, int64_t>);
int64_t m_first = 0;
int64_t m_second = 0;
};
struct V8_EXPORT V8StackFrame {
StringView sourceURL;
StringView functionName;
int lineNumber;
int columnNumber;
int scriptId;
};
class V8_EXPORT V8StackTrace {
public:
virtual StringView firstNonEmptySourceURL() const = 0;
virtual bool isEmpty() const = 0;
virtual StringView topSourceURL() const = 0;
virtual int topLineNumber() const = 0;
virtual int topColumnNumber() const = 0;
virtual int topScriptId() const = 0;
virtual StringView topFunctionName() const = 0;
virtual ~V8StackTrace() = default;
virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
buildInspectorObject(int maxAsyncDepth) const = 0;
virtual std::unique_ptr<StringBuffer> toString() const = 0;
// Safe to pass between threads, drops async chain.
virtual std::unique_ptr<V8StackTrace> clone() = 0;
virtual std::vector<V8StackFrame> frames() const = 0;
};
class V8_EXPORT V8InspectorSession {
public:
virtual ~V8InspectorSession() = default;
// Cross-context inspectable values (DOM nodes in different worlds, etc.).
class V8_EXPORT Inspectable {
public:
virtual v8::Local<v8::Value> get(v8::Local<v8::Context>) = 0;
virtual ~Inspectable() = default;
};
virtual void addInspectedObject(std::unique_ptr<Inspectable>) = 0;
// Dispatching protocol messages.
static bool canDispatchMethod(StringView method);
virtual void dispatchProtocolMessage(StringView message) = 0;
virtual std::vector<uint8_t> state() = 0;
virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
supportedDomains() = 0;
// Debugger actions.
virtual void schedulePauseOnNextStatement(StringView breakReason,
StringView breakDetails) = 0;
virtual void cancelPauseOnNextStatement() = 0;
virtual void breakProgram(StringView breakReason,
StringView breakDetails) = 0;
virtual void setSkipAllPauses(bool) = 0;
virtual void resume(bool setTerminateOnResume = false) = 0;
virtual void stepOver() = 0;
virtual std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
searchInTextByLines(StringView text, StringView query, bool caseSensitive,
bool isRegex) = 0;
// Remote objects.
virtual std::unique_ptr<protocol::Runtime::API::RemoteObject> wrapObject(
v8::Local<v8::Context>, v8::Local<v8::Value>, StringView groupName,
bool generatePreview) = 0;
virtual bool unwrapObject(std::unique_ptr<StringBuffer>* error,
StringView objectId, v8::Local<v8::Value>*,
v8::Local<v8::Context>*,
std::unique_ptr<StringBuffer>* objectGroup) = 0;
virtual void releaseObjectGroup(StringView) = 0;
virtual void triggerPreciseCoverageDeltaUpdate(StringView occasion) = 0;
struct V8_EXPORT EvaluateResult {
enum class ResultType {
kNotRun,
kSuccess,
kException,
};
ResultType type;
v8::Local<v8::Value> value;
};
// Evalaute 'expression' in the provided context. Does the same as
// Runtime#evaluate under-the-hood but exposed on the C++ side.
virtual EvaluateResult evaluate(v8::Local<v8::Context> context,
StringView expression,
bool includeCommandLineAPI = false) = 0;
// Prepare for shutdown (disables debugger pausing, etc.).
virtual void stop() = 0;
};
struct V8_EXPORT DeepSerializedValue {
explicit DeepSerializedValue(std::unique_ptr<StringBuffer> type,
v8::MaybeLocal<v8::Value> value = {})
: type(std::move(type)), value(value) {}
std::unique_ptr<StringBuffer> type;
v8::MaybeLocal<v8::Value> value;
};
struct V8_EXPORT DeepSerializationResult {
explicit DeepSerializationResult(
std::unique_ptr<DeepSerializedValue> serializedValue)
: serializedValue(std::move(serializedValue)), isSuccess(true) {}
explicit DeepSerializationResult(std::unique_ptr<StringBuffer> errorMessage)
: errorMessage(std::move(errorMessage)), isSuccess(false) {}
// Use std::variant when available.
std::unique_ptr<DeepSerializedValue> serializedValue;
std::unique_ptr<StringBuffer> errorMessage;
bool isSuccess;
};
class V8_EXPORT V8InspectorClient {
public:
virtual ~V8InspectorClient() = default;
virtual void runMessageLoopOnPause(int contextGroupId) {}
virtual void runMessageLoopOnInstrumentationPause(int contextGroupId) {
runMessageLoopOnPause(contextGroupId);
}
virtual void quitMessageLoopOnPause() {}
virtual void runIfWaitingForDebugger(int contextGroupId) {}
virtual void muteMetrics(int contextGroupId) {}
virtual void unmuteMetrics(int contextGroupId) {}
virtual void beginUserGesture() {}
virtual void endUserGesture() {}
virtual std::unique_ptr<DeepSerializationResult> deepSerialize(
v8::Local<v8::Value> v8Value, int maxDepth,
v8::Local<v8::Object> additionalParameters) {
return nullptr;
}
virtual std::unique_ptr<StringBuffer> valueSubtype(v8::Local<v8::Value>) {
return nullptr;
}
virtual std::unique_ptr<StringBuffer> descriptionForValueSubtype(
v8::Local<v8::Context>, v8::Local<v8::Value>) {
return nullptr;
}
virtual bool isInspectableHeapObject(v8::Local<v8::Object>) { return true; }
virtual v8::Local<v8::Context> ensureDefaultContextInGroup(
int contextGroupId) {
return v8::Local<v8::Context>();
}
virtual void beginEnsureAllContextsInGroup(int contextGroupId) {}
virtual void endEnsureAllContextsInGroup(int contextGroupId) {}
virtual void installAdditionalCommandLineAPI(v8::Local<v8::Context>,
v8::Local<v8::Object>) {}
// Deprecated. Use version with contextId.
virtual void consoleAPIMessage(int contextGroupId,
v8::Isolate::MessageErrorLevel level,
const StringView& message,
const StringView& url, unsigned lineNumber,
unsigned columnNumber, V8StackTrace*) {}
virtual void consoleAPIMessage(int contextGroupId, int contextId,
v8::Isolate::MessageErrorLevel level,
const StringView& message,
const StringView& url, unsigned lineNumber,
unsigned columnNumber,
V8StackTrace* stackTrace) {
consoleAPIMessage(contextGroupId, level, message, url, lineNumber,
columnNumber, stackTrace);
}
virtual v8::MaybeLocal<v8::Value> memoryInfo(v8::Isolate*,
v8::Local<v8::Context>) {
return v8::MaybeLocal<v8::Value>();
}
virtual void consoleTime(v8::Isolate* isolate, v8::Local<v8::String> label) {}
virtual void consoleTimeEnd(v8::Isolate* isolate,
v8::Local<v8::String> label) {}
virtual void consoleTimeStamp(v8::Isolate* isolate,
v8::Local<v8::String> label) {}
virtual void consoleTimeStampWithArgs(
v8::Isolate* isolate, v8::Local<v8::String> label,
const v8::LocalVector<v8::Value>& args) {}
virtual void consoleClear(int contextGroupId) {}
virtual double currentTimeMS() { return 0; }
typedef void (*TimerCallback)(void*);
virtual void startRepeatingTimer(double, TimerCallback, void* data) {}
virtual void cancelTimer(void* data) {}
// TODO(dgozman): this was added to support service worker shadow page. We
// should not connect at all.
virtual bool canExecuteScripts(int contextGroupId) { return true; }
virtual void maxAsyncCallStackDepthChanged(int depth) {}
virtual std::unique_ptr<StringBuffer> resourceNameToUrl(
const StringView& resourceName) {
return nullptr;
}
// The caller would defer to generating a random 64 bit integer if
// this method returns 0.
virtual int64_t generateUniqueId() { return 0; }
virtual void dispatchError(v8::Local<v8::Context>, v8::Local<v8::Message>,
v8::Local<v8::Value>) {}
};
// These stack trace ids are intended to be passed between debuggers and be
// resolved later. This allows to track cross-debugger calls and step between
// them if a single client connects to multiple debuggers.
struct V8_EXPORT V8StackTraceId {
uintptr_t id;
std::pair<int64_t, int64_t> debugger_id;
bool should_pause = false;
V8StackTraceId();
V8StackTraceId(const V8StackTraceId&) = default;
V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id);
V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id,
bool should_pause);
explicit V8StackTraceId(StringView);
V8StackTraceId& operator=(const V8StackTraceId&) = default;
V8StackTraceId& operator=(V8StackTraceId&&) noexcept = default;
~V8StackTraceId() = default;
bool IsInvalid() const;
std::unique_ptr<StringBuffer> ToString();
};
class V8_EXPORT V8Inspector {
public:
static std::unique_ptr<V8Inspector> create(v8::Isolate*, V8InspectorClient*);
virtual ~V8Inspector() = default;
// Contexts instrumentation.
virtual void contextCreated(const V8ContextInfo&) = 0;
virtual void contextDestroyed(v8::Local<v8::Context>) = 0;
virtual void resetContextGroup(int contextGroupId) = 0;
virtual v8::MaybeLocal<v8::Context> contextById(int contextId) = 0;
virtual V8DebuggerId uniqueDebuggerId(int contextId) = 0;
virtual uint64_t isolateId() = 0;
// Various instrumentation.
virtual void idleStarted() = 0;
virtual void idleFinished() = 0;
// Async stack traces instrumentation.
virtual void asyncTaskScheduled(StringView taskName, void* task,
bool recurring) = 0;
virtual void asyncTaskCanceled(void* task) = 0;
virtual void asyncTaskStarted(void* task) = 0;
virtual void asyncTaskFinished(void* task) = 0;
virtual void allAsyncTasksCanceled() = 0;
virtual V8StackTraceId storeCurrentStackTrace(StringView description) = 0;
virtual void externalAsyncTaskStarted(const V8StackTraceId& parent) = 0;
virtual void externalAsyncTaskFinished(const V8StackTraceId& parent) = 0;
// Exceptions instrumentation.
virtual unsigned exceptionThrown(v8::Local<v8::Context>, StringView message,
v8::Local<v8::Value> exception,
StringView detailedMessage, StringView url,
unsigned lineNumber, unsigned columnNumber,
std::unique_ptr<V8StackTrace>,
int scriptId) = 0;
virtual void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
StringView message) = 0;
virtual bool associateExceptionData(v8::Local<v8::Context>,
v8::Local<v8::Value> exception,
v8::Local<v8::Name> key,
v8::Local<v8::Value> value) = 0;
// Connection.
class V8_EXPORT Channel {
public:
virtual ~Channel() = default;
virtual void sendResponse(int callId,
std::unique_ptr<StringBuffer> message) = 0;
virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
virtual void flushProtocolNotifications() = 0;
};
class V8_EXPORT ManagedChannel
: public cppgc::GarbageCollected<ManagedChannel> {
public:
virtual ~ManagedChannel() = default;
virtual void sendResponse(int callId,
std::unique_ptr<StringBuffer> message) = 0;
virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
virtual void flushProtocolNotifications() = 0;
virtual void Trace(cppgc::Visitor* visitor) const {}
};
enum ClientTrustLevel { kUntrusted, kFullyTrusted };
enum SessionPauseState { kWaitingForDebugger, kNotWaitingForDebugger };
// TODO(chromium:1352175): remove default value once downstream change lands.
// Deprecated: Use `connectShared` instead.
// Channel is owned by the embedder. Ensure to keep it alive as long as the
// returned session is alive.
virtual std::unique_ptr<V8InspectorSession> connect(
int contextGroupId, Channel*, StringView state,
ClientTrustLevel client_trust_level,
SessionPauseState = kNotWaitingForDebugger) = 0;
// Same as `connect` but returns a std::shared_ptr instead.
// Embedders should not deconstruct V8 sessions while the nested run loop
// (V8InspectorClient::runMessageLoopOnPause) is running. To partially ensure
// this, we defer session deconstruction until no "dispatchProtocolMessages"
// remains on the stack.
// Channel is owned by the embedder. Ensure to keep it alive as long as the
// returned session is alive.
virtual std::shared_ptr<V8InspectorSession> connectShared(
int contextGroupId, Channel* channel, StringView state,
ClientTrustLevel clientTrustLevel, SessionPauseState pauseState) = 0;
// Same as `connectShared` but takes a `ManagedChannel` instead. The session
// will take a cppgc::Persistent on the ManagedChannel so the embedder doesn't
// have to worry about the life-time of `channel`.
virtual std::shared_ptr<V8InspectorSession> connectShared(
int contextGroupId, ManagedChannel* channel, StringView state,
ClientTrustLevel clientTrustLevel, SessionPauseState pauseState) = 0;
// API methods.
virtual std::unique_ptr<V8StackTrace> createStackTrace(
v8::Local<v8::StackTrace>) = 0;
virtual std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) = 0;
};
} // namespace v8_inspector
#endif // V8_V8_INSPECTOR_H_

1949
vendor/artifacts/v8/include/v8-internal.h vendored Normal file

File diff suppressed because it is too large Load Diff

1929
vendor/artifacts/v8/include/v8-isolate.h vendored Normal file

File diff suppressed because it is too large Load Diff

54
vendor/artifacts/v8/include/v8-json.h vendored Normal file
View File

@ -0,0 +1,54 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_JSON_H_
#define INCLUDE_V8_JSON_H_
#include <optional>
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-message.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
class Value;
class String;
/**
* A JSON Parser and Stringifier.
*/
class V8_EXPORT JSON {
public:
/**
* Tries to parse the string |json_string| and returns it as value if
* successful.
*
* \param the context in which to parse and create the value.
* \param json_string The string to parse.
* \param origin Optional script origin to use for error reporting.
* If not provided, error reporting will use default origin options
* or attempt to infer origin from the current stack.
* \return The corresponding value if successfully parsed.
*/
static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
Local<Context> context, Local<String> json_string,
std::optional<ScriptOrigin> origin = std::nullopt);
/**
* Tries to stringify the JSON-serializable object |json_object| and returns
* it as string if successful.
*
* \param json_object The JSON-serializable object to stringify.
* \return The corresponding string if successfully stringified.
*/
static V8_WARN_UNUSED_RESULT MaybeLocal<String> Stringify(
Local<Context> context, Local<Value> json_object,
Local<String> gap = Local<String>());
};
} // namespace v8
#endif // INCLUDE_V8_JSON_H_

View File

@ -0,0 +1,884 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_LOCAL_HANDLE_H_
#define INCLUDE_V8_LOCAL_HANDLE_H_
#include <stddef.h>
#include <type_traits>
#include <vector>
#include "v8-handle-base.h" // NOLINT(build/include_directory)
#include "v8-internal.h" // NOLINT(build/include_directory)
namespace v8 {
template <class T>
class LocalBase;
template <class T>
class Local;
template <class T>
class LocalVector;
template <class F>
class MaybeLocal;
template <class T>
class Eternal;
template <class T>
class Global;
template <class T>
class NonCopyablePersistentTraits;
template <class T>
class PersistentBase;
template <class T, class M = NonCopyablePersistentTraits<T>>
class Persistent;
class TracedReferenceBase;
template <class T>
class BasicTracedReference;
template <class F>
class TracedReference;
class ArrayBuffer;
class Boolean;
class Context;
class EscapableHandleScope;
template <class F>
class FunctionCallbackInfo;
class Isolate;
class Object;
template <class F1, class F2, class F3>
class PersistentValueMapBase;
class Primitive;
class Private;
template <class F>
class PropertyCallbackInfo;
template <class F>
class ReturnValue;
class String;
template <class F>
class Traced;
class TypecheckWitness;
class Utils;
class Uint32;
class Value;
namespace debug {
class ConsoleCallArguments;
}
namespace internal {
template <typename T>
class CustomArguments;
template <typename T>
class LocalUnchecked;
class SamplingHeapProfiler;
} // namespace internal
namespace api_internal {
// Called when ToLocalChecked is called on an empty Local.
V8_EXPORT void ToLocalEmpty();
#ifdef V8_ENABLE_CHECKS
template <typename T, typename V = Value>
void TypeCheckLocal(V* value) {
// If `T` does not provide a `Cast` method we cannot check anything.
if constexpr (requires { T::Cast(value); }) {
// TODO(419454582): Remove all these exceptions.
if (std::is_same_v<Array, T> && value->IsArgumentsObject()) return;
if (std::is_same_v<ArrayBuffer, T> && value->IsSharedArrayBuffer()) return;
if (std::is_same_v<Object, T> && value->IsNull()) return;
if (std::is_same_v<Object, T> && value->IsString()) return;
if (std::is_same_v<Object, T> && value->IsUndefined()) return;
if (std::is_same_v<Uint32, T> && value->IsInt32()) return;
if (std::is_same_v<Object, T> && value->IsNumber()) return;
// Execute the actual check (part of the cast).
T::Cast(value);
}
}
#endif
} // namespace api_internal
/**
* A stack-allocated class that governs a number of local handles.
* After a handle scope has been created, all local handles will be
* allocated within that handle scope until either the handle scope is
* deleted or another handle scope is created. If there is already a
* handle scope and a new one is created, all allocations will take
* place in the new handle scope until it is deleted. After that,
* new handles will again be allocated in the original handle scope.
*
* After the handle scope of a local handle has been deleted the
* garbage collector will no longer track the object stored in the
* handle and may deallocate it. The behavior of accessing a handle
* for which the handle scope has been deleted is undefined.
*/
class V8_EXPORT V8_NODISCARD HandleScope {
public:
V8_INLINE explicit HandleScope(Isolate* isolate);
V8_INLINE ~HandleScope();
/**
* Counts the number of allocated handles.
*/
static int NumberOfHandles(Isolate* isolate);
V8_INLINE Isolate* GetIsolate() const { return isolate_; }
HandleScope(const HandleScope&) = delete;
void operator=(const HandleScope&) = delete;
static internal::Address* CreateHandleForCurrentIsolate(
internal::Address value);
protected:
V8_INLINE HandleScope() = default;
V8_INLINE void Initialize(Isolate* isolate);
V8_INLINE static internal::Address* CreateHandle(Isolate* i_isolate,
internal::Address value);
private:
// Extend the HandleScope making room for more handles. Not inlined.
static internal::Address* Extend(Isolate* isolate);
// Delete any extensions in HandleScope destructor. Not called unless there
// are extensions. Not inlined.
void DeleteExtensions(Isolate* isolate);
#ifdef V8_ENABLE_CHECKS
// Non-inlined asserts on HandleScope constructor.
void DoInitializeAsserts(Isolate* isolate);
// Non-inlined assert for HandleScope destructor.
void AssertScopeLevelsMatch();
// Non-inlined asserts for HandleScope destructor. Also zaps the slots
// if this is enabled.
void DoCloseScopeAsserts(int before, internal::Address* limit,
internal::HandleScopeData* current);
#endif
// Declaring operator new and delete as deleted is not spec compliant.
// Therefore declare them private instead to disable dynamic alloc
void* operator new(size_t size);
void* operator new[](size_t size);
void operator delete(void*, size_t);
void operator delete[](void*, size_t);
Isolate* isolate_;
internal::Address* prev_next_;
internal::Address* prev_limit_;
#ifdef V8_ENABLE_CHECKS
int scope_level_ = 0;
#endif
// LocalBase<T>::New uses CreateHandle with an Isolate* parameter.
template <typename T>
friend class LocalBase;
// Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
// a HeapObject in their shortcuts.
friend class Object;
friend class Context;
};
HandleScope::HandleScope(Isolate* v8_isolate) { Initialize(v8_isolate); }
void HandleScope::Initialize(Isolate* v8_isolate) {
using I = internal::Internals;
internal::HandleScopeData* current = I::GetHandleScopeData(v8_isolate);
isolate_ = v8_isolate;
prev_next_ = current->next;
prev_limit_ = current->limit;
current->level++;
#ifdef V8_ENABLE_CHECKS
DoInitializeAsserts(v8_isolate);
scope_level_ = current->level;
#endif
}
HandleScope::~HandleScope() {
if (V8_UNLIKELY(isolate_ == nullptr)) return;
#ifdef V8_ENABLE_CHECKS
AssertScopeLevelsMatch();
int handle_count_before = NumberOfHandles(isolate_);
#endif
using I = internal::Internals;
internal::HandleScopeData* current = I::GetHandleScopeData(isolate_);
std::swap(current->next, prev_next_);
current->level--;
internal::Address* limit = prev_next_;
if (V8_UNLIKELY(current->limit != prev_limit_)) {
current->limit = prev_limit_;
limit = prev_limit_;
DeleteExtensions(isolate_);
}
#ifdef V8_ENABLE_CHECKS
DoCloseScopeAsserts(handle_count_before, limit, current);
#else
(void)limit; // Avoid unused variable warning.
#endif
}
internal::Address* HandleScope::CreateHandle(Isolate* v8_isolate,
internal::Address value) {
using I = internal::Internals;
internal::HandleScopeData* data = I::GetHandleScopeData(v8_isolate);
internal::Address* result = data->next;
if (V8_UNLIKELY(result == data->limit)) {
result = Extend(v8_isolate);
}
// Update the current next field, set the value in the created handle,
// and return the result.
data->next = reinterpret_cast<internal::Address*>(
reinterpret_cast<internal::Address>(result) + sizeof(internal::Address));
*result = value;
return result;
}
/**
* A base class for local handles.
* Its implementation depends on whether direct handle support is enabled.
* When it is, a local handle contains a direct pointer to the referenced
* object, otherwise it contains an indirect pointer.
*/
#ifdef V8_ENABLE_DIRECT_HANDLE
template <typename T>
class LocalBase : public api_internal::DirectHandleBase {
protected:
template <class F>
friend class Local;
V8_INLINE LocalBase() = default;
V8_INLINE explicit LocalBase(internal::Address ptr) : DirectHandleBase(ptr) {
#ifdef V8_ENABLE_CHECKS
if (!IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
#endif
}
template <typename S>
V8_INLINE LocalBase(const LocalBase<S>& other) : DirectHandleBase(other) {}
V8_INLINE static LocalBase<T> New(Isolate* isolate, internal::Address value) {
return LocalBase<T>(value);
}
V8_INLINE static LocalBase<T> New(Isolate* isolate, T* that) {
return LocalBase<T>::New(isolate,
internal::ValueHelper::ValueAsAddress(that));
}
V8_INLINE static LocalBase<T> FromSlot(internal::Address* slot) {
if (slot == nullptr) return LocalBase<T>();
return LocalBase<T>(*slot);
}
V8_INLINE static LocalBase<T> FromRepr(
internal::ValueHelper::InternalRepresentationType repr) {
return LocalBase<T>(repr);
}
};
#else // !V8_ENABLE_DIRECT_HANDLE
template <typename T>
class LocalBase : public api_internal::IndirectHandleBase {
protected:
template <class F>
friend class Local;
V8_INLINE LocalBase() = default;
V8_INLINE explicit LocalBase(internal::Address* location)
: IndirectHandleBase(location) {
#ifdef V8_ENABLE_CHECKS
if (!IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
#endif
}
template <typename S>
V8_INLINE LocalBase(const LocalBase<S>& other) : IndirectHandleBase(other) {}
V8_INLINE static LocalBase<T> New(Isolate* isolate, internal::Address value) {
return LocalBase(HandleScope::CreateHandle(isolate, value));
}
V8_INLINE static LocalBase<T> New(Isolate* isolate, T* that) {
if (internal::ValueHelper::IsEmpty(that)) return LocalBase<T>();
return LocalBase<T>::New(isolate,
internal::ValueHelper::ValueAsAddress(that));
}
V8_INLINE static LocalBase<T> FromSlot(internal::Address* slot) {
return LocalBase<T>(slot);
}
V8_INLINE static LocalBase<T> FromRepr(
internal::ValueHelper::InternalRepresentationType repr) {
return LocalBase<T>(repr);
}
};
#endif // V8_ENABLE_DIRECT_HANDLE
/**
* An object reference managed by the v8 garbage collector.
*
* All objects returned from v8 have to be tracked by the garbage collector so
* that it knows that the objects are still alive. Also, because the garbage
* collector may move objects, it is unsafe to point directly to an object.
* Instead, all objects are stored in handles which are known by the garbage
* collector and updated whenever an object moves. Handles should always be
* passed by value (except in cases like out-parameters) and they should never
* be allocated on the heap.
*
* There are two types of handles: local and persistent handles.
*
* Local handles are light-weight and transient and typically used in local
* operations. They are managed by HandleScopes. That means that a HandleScope
* must exist on the stack when they are created and that they are only valid
* inside of the HandleScope active during their creation. For passing a local
* handle to an outer HandleScope, an EscapableHandleScope and its Escape()
* method must be used.
*
* Persistent handles can be used when storing objects across several
* independent operations and have to be explicitly deallocated when they're no
* longer used.
*
* It is safe to extract the object stored in the handle by dereferencing the
* handle (for instance, to extract the Object* from a Local<Object>); the value
* will still be governed by a handle behind the scenes and the same rules apply
* to these values as to their handles.
*/
template <class T>
class V8_TRIVIAL_ABI Local : public LocalBase<T>,
#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
public api_internal::StackAllocated<true>
#else
public api_internal::StackAllocated<false>
#endif
{
public:
/**
* Default constructor: Returns an empty handle.
*/
V8_INLINE Local() = default;
/**
* Constructor for handling automatic up casting.
* Ex. Local<Object> can be passed when Local<Value> is expected but not
* the other way round.
*/
template <class S>
requires std::is_base_of_v<T, S>
V8_INLINE Local(Local<S> that) : LocalBase<T>(that) {}
V8_INLINE T* operator->() const { return this->template value<T>(); }
V8_INLINE T* operator*() const { return this->operator->(); }
/**
* Checks whether two handles are equal or different.
* 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
* non-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 <class S>
V8_INLINE bool operator==(const Local<S>& that) const {
return internal::HandleHelper::EqualHandles(*this, that);
}
template <class S>
V8_INLINE bool operator==(const PersistentBase<S>& that) const {
return internal::HandleHelper::EqualHandles(*this, that);
}
template <class S>
V8_INLINE bool operator!=(const Local<S>& that) const {
return !operator==(that);
}
template <class S>
V8_INLINE bool operator!=(const Persistent<S>& that) const {
return !operator==(that);
}
/**
* Cast a handle to a subclass, e.g. Local<Value> to Local<Object>.
* This is only valid if the handle actually refers to a value of the
* target type or if the handle is empty.
*/
template <class S>
V8_INLINE static Local<T> Cast(Local<S> that) {
#ifdef V8_ENABLE_CHECKS
// If we're going to perform the type check then we have to check
// that the handle isn't empty before doing the checked cast.
if (that.IsEmpty()) return Local<T>();
T::Cast(that.template value<S>());
#endif
return Local<T>(LocalBase<T>(that));
}
/**
* Calling this is equivalent to Local<S>::Cast().
* In particular, this is only valid if the handle actually refers to a value
* of the target type or if the handle is empty.
*/
template <class S>
V8_INLINE Local<S> As() const {
return Local<S>::Cast(*this);
}
/**
* Create a local handle for the content of another handle.
* The referee is kept alive by the local handle even when
* the original handle is destroyed/disposed.
*/
V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that) {
return New(isolate, that.template value<T, true>());
}
V8_INLINE static Local<T> New(Isolate* isolate,
const PersistentBase<T>& that) {
return New(isolate, that.template value<T, true>());
}
V8_INLINE static Local<T> New(Isolate* isolate,
const BasicTracedReference<T>& that) {
return New(isolate, that.template value<T, true>());
}
private:
friend class TracedReferenceBase;
friend class Utils;
template <class F>
friend class Eternal;
template <class F>
friend class Global;
template <class F>
friend class Local;
template <class F>
friend class MaybeLocal;
template <class F, class M>
friend class Persistent;
template <class F>
friend class FunctionCallbackInfo;
template <class F>
friend class PropertyCallbackInfo;
friend class String;
friend class Object;
friend class Context;
friend class Isolate;
friend class Private;
template <class F>
friend class internal::CustomArguments;
friend Local<Primitive> Undefined(Isolate* isolate);
friend Local<Primitive> Null(Isolate* isolate);
friend Local<Boolean> True(Isolate* isolate);
friend Local<Boolean> False(Isolate* isolate);
friend class HandleScope;
friend class EscapableHandleScope;
friend class InternalEscapableScope;
template <class F1, class F2, class F3>
friend class PersistentValueMapBase;
template <class F>
friend class ReturnValue;
template <class F>
friend class Traced;
friend class internal::SamplingHeapProfiler;
friend class internal::HandleHelper;
friend class debug::ConsoleCallArguments;
friend class internal::LocalUnchecked<T>;
explicit Local(no_checking_tag do_not_check)
: LocalBase<T>(), StackAllocated(do_not_check) {}
explicit Local(const Local<T>& other, no_checking_tag do_not_check)
: LocalBase<T>(other), StackAllocated(do_not_check) {}
V8_INLINE explicit Local(const LocalBase<T>& other) : LocalBase<T>(other) {}
V8_INLINE static Local<T> FromRepr(
internal::ValueHelper::InternalRepresentationType repr) {
return Local<T>(LocalBase<T>::FromRepr(repr));
}
V8_INLINE static Local<T> FromSlot(internal::Address* slot) {
return Local<T>(LocalBase<T>::FromSlot(slot));
}
#ifdef V8_ENABLE_DIRECT_HANDLE
friend class TypecheckWitness;
V8_INLINE static Local<T> FromAddress(internal::Address ptr) {
return Local<T>(LocalBase<T>(ptr));
}
#endif // V8_ENABLE_DIRECT_HANDLE
V8_INLINE static Local<T> New(Isolate* isolate, internal::Address value) {
return Local<T>(LocalBase<T>::New(isolate, value));
}
V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
return Local<T>(LocalBase<T>::New(isolate, that));
}
// Unsafe cast, should be avoided.
template <class S>
V8_INLINE Local<S> UnsafeAs() const {
return Local<S>(LocalBase<S>(*this));
}
};
namespace internal {
// A local variant that is suitable for off-stack allocation.
// Used internally by LocalVector<T>. Not to be used directly!
template <typename T>
class V8_TRIVIAL_ABI LocalUnchecked : public Local<T> {
public:
LocalUnchecked() : Local<T>(Local<T>::do_not_check) {}
#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
// In this case, the check is also enforced in the copy constructor and we
// need to suppress it.
LocalUnchecked(
const LocalUnchecked& other) noexcept // NOLINT(runtime/explicit)
: Local<T>(other, Local<T>::do_not_check) {}
LocalUnchecked& operator=(const LocalUnchecked&) noexcept = default;
#endif
// Implicit conversion from Local.
LocalUnchecked(const Local<T>& other) noexcept // NOLINT(runtime/explicit)
: Local<T>(other, Local<T>::do_not_check) {}
};
#ifdef V8_ENABLE_DIRECT_HANDLE
// Off-stack allocated direct locals must be registered as strong roots.
// For off-stack indirect locals, this is not necessary.
template <typename T>
class StrongRootAllocator<LocalUnchecked<T>> : public StrongRootAllocatorBase {
public:
using value_type = LocalUnchecked<T>;
static_assert(std::is_standard_layout_v<value_type>);
static_assert(sizeof(value_type) == sizeof(Address));
template <typename HeapOrIsolateT>
explicit StrongRootAllocator(HeapOrIsolateT* heap_or_isolate)
: StrongRootAllocatorBase(heap_or_isolate) {}
template <typename U>
StrongRootAllocator(const StrongRootAllocator<U>& other) noexcept
: StrongRootAllocatorBase(other) {}
value_type* allocate(size_t n) {
return reinterpret_cast<value_type*>(allocate_impl(n));
}
void deallocate(value_type* p, size_t n) noexcept {
return deallocate_impl(reinterpret_cast<Address*>(p), n);
}
};
#endif // V8_ENABLE_DIRECT_HANDLE
} // namespace internal
template <typename T>
class LocalVector {
private:
using element_type = internal::LocalUnchecked<T>;
#ifdef V8_ENABLE_DIRECT_HANDLE
using allocator_type = internal::StrongRootAllocator<element_type>;
static allocator_type make_allocator(Isolate* isolate) noexcept {
return allocator_type(isolate);
}
#else
using allocator_type = std::allocator<element_type>;
static allocator_type make_allocator(Isolate* isolate) noexcept {
return allocator_type();
}
#endif // V8_ENABLE_DIRECT_HANDLE
using vector_type = std::vector<element_type, allocator_type>;
public:
using value_type = Local<T>;
using reference = value_type&;
using const_reference = const value_type&;
using size_type = size_t;
using difference_type = ptrdiff_t;
using iterator =
internal::WrappedIterator<typename vector_type::iterator, Local<T>>;
using const_iterator =
internal::WrappedIterator<typename vector_type::const_iterator,
const Local<T>>;
explicit LocalVector(Isolate* isolate) : backing_(make_allocator(isolate)) {}
LocalVector(Isolate* isolate, size_t n)
: backing_(n, make_allocator(isolate)) {}
explicit LocalVector(Isolate* isolate, std::initializer_list<Local<T>> init)
: backing_(make_allocator(isolate)) {
if (init.size() == 0) return;
backing_.reserve(init.size());
backing_.insert(backing_.end(), init.begin(), init.end());
}
iterator begin() noexcept { return iterator(backing_.begin()); }
const_iterator begin() const noexcept {
return const_iterator(backing_.begin());
}
iterator end() noexcept { return iterator(backing_.end()); }
const_iterator end() const noexcept { return const_iterator(backing_.end()); }
size_t size() const noexcept { return backing_.size(); }
bool empty() const noexcept { return backing_.empty(); }
void reserve(size_t n) { backing_.reserve(n); }
void shrink_to_fit() { backing_.shrink_to_fit(); }
Local<T>& operator[](size_t n) { return backing_[n]; }
const Local<T>& operator[](size_t n) const { return backing_[n]; }
Local<T>& at(size_t n) { return backing_.at(n); }
const Local<T>& at(size_t n) const { return backing_.at(n); }
Local<T>& front() { return backing_.front(); }
const Local<T>& front() const { return backing_.front(); }
Local<T>& back() { return backing_.back(); }
const Local<T>& back() const { return backing_.back(); }
Local<T>* data() noexcept { return backing_.data(); }
const Local<T>* data() const noexcept { return backing_.data(); }
iterator insert(const_iterator pos, const Local<T>& value) {
return iterator(backing_.insert(pos.base(), value));
}
template <typename InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last) {
return iterator(backing_.insert(pos.base(), first, last));
}
iterator insert(const_iterator pos, std::initializer_list<Local<T>> init) {
return iterator(backing_.insert(pos.base(), init.begin(), init.end()));
}
LocalVector<T>& operator=(std::initializer_list<Local<T>> init) {
backing_.clear();
backing_.reserve(init.size());
backing_.insert(backing_.end(), init.begin(), init.end());
return *this;
}
void push_back(const Local<T>& x) { backing_.push_back(x); }
void pop_back() { backing_.pop_back(); }
template <typename... Args>
void emplace_back(Args&&... args) {
backing_.push_back(value_type{std::forward<Args>(args)...});
}
void clear() noexcept { backing_.clear(); }
void resize(size_t n) { backing_.resize(n); }
void swap(LocalVector<T>& other) { backing_.swap(other.backing_); }
friend bool operator==(const LocalVector<T>& x, const LocalVector<T>& y) {
return x.backing_ == y.backing_;
}
friend bool operator!=(const LocalVector<T>& x, const LocalVector<T>& y) {
return x.backing_ != y.backing_;
}
friend bool operator<(const LocalVector<T>& x, const LocalVector<T>& y) {
return x.backing_ < y.backing_;
}
friend bool operator>(const LocalVector<T>& x, const LocalVector<T>& y) {
return x.backing_ > y.backing_;
}
friend bool operator<=(const LocalVector<T>& x, const LocalVector<T>& y) {
return x.backing_ <= y.backing_;
}
friend bool operator>=(const LocalVector<T>& x, const LocalVector<T>& y) {
return x.backing_ >= y.backing_;
}
private:
vector_type backing_;
};
#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
// Handle is an alias for Local for historical reasons.
template <class T>
using Handle = Local<T>;
#endif
/**
* A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
* the Local<> is empty before it can be used.
*
* If an API method returns a MaybeLocal<>, the API method can potentially fail
* either because an exception is thrown, or because an exception is pending,
* e.g. because a previous API call threw an exception that hasn't been caught
* yet, or because a TerminateExecution exception was thrown. In that case, an
* empty MaybeLocal is returned.
*/
template <class T>
class MaybeLocal {
public:
/**
* Default constructor: Returns an empty handle.
*/
V8_INLINE MaybeLocal() = default;
/**
* Implicitly construct MaybeLocal from Local.
*/
template <class S>
requires std::is_base_of_v<T, S>
V8_INLINE MaybeLocal(Local<S> that) : local_(that) {}
/**
* Implicitly up-cast MaybeLocal<S> to MaybeLocal<T> if T is a base of S.
*/
template <class S>
requires std::is_base_of_v<T, S>
V8_INLINE MaybeLocal(MaybeLocal<S> that) : local_(that.local_) {}
V8_INLINE bool IsEmpty() const { return local_.IsEmpty(); }
/**
* Converts this MaybeLocal<> to a Local<>. If this MaybeLocal<> is empty,
* |false| is returned and |out| is assigned with nullptr.
*/
template <class S>
V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const {
*out = local_;
return !IsEmpty();
}
/**
* Converts this MaybeLocal<> to a Local<>. If this MaybeLocal<> is empty,
* V8 will crash the process.
*/
V8_INLINE Local<T> ToLocalChecked() {
if (V8_UNLIKELY(IsEmpty())) api_internal::ToLocalEmpty();
return local_;
}
/**
* Converts this MaybeLocal<> to a Local<>, using a default value if this
* MaybeLocal<> is empty.
*/
template <class S>
V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
return IsEmpty() ? default_value : Local<S>(local_);
}
/**
* Cast a handle to a subclass, e.g. MaybeLocal<Value> to MaybeLocal<Object>.
* This is only valid if the handle actually refers to a value of the target
* type or if the handle is empty.
*/
template <class S>
V8_INLINE static MaybeLocal<T> Cast(MaybeLocal<S> that) {
return MaybeLocal<T>{Local<T>::Cast(that.local_)};
}
/**
* Calling this is equivalent to MaybeLocal<S>::Cast().
* In particular, this is only valid if the handle actually refers to a value
* of the target type or if the handle is empty.
*/
template <class S>
V8_INLINE MaybeLocal<S> As() const {
return MaybeLocal<S>::Cast(*this);
}
private:
Local<T> local_;
template <typename S>
friend class MaybeLocal;
};
/**
* A HandleScope which first allocates a handle in the current scope
* which will be later filled with the escape value.
*/
class V8_EXPORT V8_NODISCARD EscapableHandleScopeBase : public HandleScope {
public:
explicit EscapableHandleScopeBase(Isolate* isolate);
V8_INLINE ~EscapableHandleScopeBase() = default;
EscapableHandleScopeBase(const EscapableHandleScopeBase&) = delete;
void operator=(const EscapableHandleScopeBase&) = delete;
void* operator new(size_t size) = delete;
void* operator new[](size_t size) = delete;
void operator delete(void*, size_t) = delete;
void operator delete[](void*, size_t) = delete;
protected:
/**
* Pushes the value into the previous scope and returns a handle to it.
* Cannot be called twice.
*/
internal::Address* EscapeSlot(internal::Address* escape_value);
private:
internal::Address* escape_slot_;
};
class V8_EXPORT V8_NODISCARD EscapableHandleScope
: public EscapableHandleScopeBase {
public:
explicit EscapableHandleScope(Isolate* isolate)
: EscapableHandleScopeBase(isolate) {}
V8_INLINE ~EscapableHandleScope() = default;
template <class T>
V8_INLINE Local<T> Escape(Local<T> value) {
#ifdef V8_ENABLE_DIRECT_HANDLE
return value;
#else
if (value.IsEmpty()) return value;
return Local<T>::FromSlot(EscapeSlot(value.slot()));
#endif
}
template <class T>
V8_INLINE MaybeLocal<T> EscapeMaybe(MaybeLocal<T> value) {
return Escape(value.FromMaybe(Local<T>()));
}
};
/**
* A SealHandleScope acts like a handle scope in which no handle allocations
* are allowed. It can be useful for debugging handle leaks.
* Handles can be allocated within inner normal HandleScopes.
*/
class V8_EXPORT V8_NODISCARD SealHandleScope {
public:
explicit SealHandleScope(Isolate* isolate);
~SealHandleScope();
SealHandleScope(const SealHandleScope&) = delete;
void operator=(const SealHandleScope&) = delete;
void* operator new(size_t size) = delete;
void* operator new[](size_t size) = delete;
void operator delete(void*, size_t) = delete;
void operator delete[](void*, size_t) = delete;
private:
internal::Isolate* const i_isolate_;
internal::Address* prev_limit_;
int prev_sealed_level_;
};
} // namespace v8
#endif // INCLUDE_V8_LOCAL_HANDLE_H_

138
vendor/artifacts/v8/include/v8-locker.h vendored Normal file
View File

@ -0,0 +1,138 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_LOCKER_H_
#define INCLUDE_V8_LOCKER_H_
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
namespace internal {
class Isolate;
} // namespace internal
class Isolate;
/**
* Multiple threads in V8 are allowed, but only one thread at a time is allowed
* to use any given V8 isolate, see the comments in the Isolate class. The
* definition of 'using a V8 isolate' includes accessing handles or holding onto
* object pointers obtained from V8 handles while in the particular V8 isolate.
* It is up to the user of V8 to ensure, perhaps with locking, that this
* constraint is not violated. In addition to any other synchronization
* mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
* used to signal thread switches to V8.
*
* v8::Locker is a scoped lock object. While it's active, i.e. between its
* construction and destruction, the current thread is allowed to use the locked
* isolate. V8 guarantees that an isolate can be locked by at most one thread at
* any time. In other words, the scope of a v8::Locker is a critical section.
*
* Sample usage:
* \code
* ...
* {
* v8::Locker locker(isolate);
* v8::Isolate::Scope isolate_scope(isolate);
* ...
* // Code using V8 and isolate goes here.
* ...
* } // Destructor called here
* \endcode
*
* If you wish to stop using V8 in a thread A you can do this either by
* destroying the v8::Locker object as above or by constructing a v8::Unlocker
* object:
*
* \code
* {
* isolate->Exit();
* v8::Unlocker unlocker(isolate);
* ...
* // Code not using V8 goes here while V8 can run in another thread.
* ...
* } // Destructor called here.
* isolate->Enter();
* \endcode
*
* The Unlocker object is intended for use in a long-running callback from V8,
* where you want to release the V8 lock for other threads to use.
*
* The v8::Locker is a recursive lock, i.e. you can lock more than once in a
* given thread. This can be useful if you have code that can be called either
* from code that holds the lock or from code that does not. The Unlocker is
* not recursive so you can not have several Unlockers on the stack at once, and
* you cannot use an Unlocker in a thread that is not inside a Locker's scope.
*
* An unlocker will unlock several lockers if it has to and reinstate the
* correct depth of locking on its destruction, e.g.:
*
* \code
* // V8 not locked.
* {
* v8::Locker locker(isolate);
* Isolate::Scope isolate_scope(isolate);
* // V8 locked.
* {
* v8::Locker another_locker(isolate);
* // V8 still locked (2 levels).
* {
* isolate->Exit();
* v8::Unlocker unlocker(isolate);
* // V8 not locked.
* }
* isolate->Enter();
* // V8 locked again (2 levels).
* }
* // V8 still locked (1 level).
* }
* // V8 Now no longer locked.
* \endcode
*/
class V8_EXPORT Unlocker {
public:
/**
* Initialize Unlocker for a given Isolate.
*/
V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); }
~Unlocker();
private:
void Initialize(Isolate* isolate);
internal::Isolate* isolate_;
};
class V8_EXPORT Locker {
public:
/**
* Initialize Locker for a given Isolate.
*/
V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }
~Locker();
/**
* Returns whether or not the locker for a given isolate, is locked by the
* current thread.
*/
static bool IsLocked(Isolate* isolate);
// Disallow copying and assigning.
Locker(const Locker&) = delete;
void operator=(const Locker&) = delete;
private:
void Initialize(Isolate* isolate);
bool has_lock_;
bool top_level_;
internal::Isolate* isolate_;
};
} // namespace v8
#endif // INCLUDE_V8_LOCKER_H_

185
vendor/artifacts/v8/include/v8-maybe.h vendored Normal file
View File

@ -0,0 +1,185 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_MAYBE_H_
#define INCLUDE_V8_MAYBE_H_
#include <type_traits>
#include <utility>
#include "cppgc/internal/conditional-stack-allocated.h" // NOLINT(build/include_directory)
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
namespace internal {
struct NullMaybeType {};
constexpr NullMaybeType kNullMaybe;
} // namespace internal
namespace api_internal {
// Called when ToChecked is called on an empty Maybe.
V8_EXPORT void FromJustIsNothing();
} // namespace api_internal
/**
* A simple Maybe type, representing an object which may or may not have a
* value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
*
* If an API method returns a Maybe<>, the API method can potentially fail
* either because an exception is thrown, or because an exception is pending,
* e.g. because a previous API call threw an exception that hasn't been caught
* yet, or because a TerminateExecution exception was thrown. In that case, a
* "Nothing" value is returned.
*/
template <class T>
class Maybe : public cppgc::internal::ConditionalStackAllocatedBase<T> {
public:
constexpr Maybe() = default;
V8_INLINE Maybe(internal::NullMaybeType) {}
V8_INLINE bool IsNothing() const { return !has_value_; }
V8_INLINE bool IsJust() const { return has_value_; }
/**
* Same as IsNothing(). It's useful for unified handling of empty states
* with v8::MaybeLocal<T>.
*/
V8_INLINE bool IsEmpty() const { return IsNothing(); }
/**
* An alias for |FromJust|. Will crash if the Maybe<> is nothing.
*/
V8_INLINE T ToChecked() const { return FromJust(); }
/**
* Short-hand for ToChecked(), which doesn't return a value. To be used, where
* the actual value of the Maybe is not needed like Object::Set.
*/
V8_INLINE void Check() const {
if (V8_UNLIKELY(!IsJust())) api_internal::FromJustIsNothing();
}
/**
* Converts this Maybe<> to a value of type T. If this Maybe<> is
* nothing (empty), |false| is returned and |out| is left untouched.
*/
V8_WARN_UNUSED_RESULT V8_INLINE bool To(T* out) const {
if (V8_LIKELY(IsJust())) *out = value_;
return IsJust();
}
/**
* Converts this Maybe<> to a value of type T, moving out of it. If this
* Maybe<> is nothing (empty), |false| is returned and |out| is left
* untouched.
*/
V8_WARN_UNUSED_RESULT V8_INLINE bool MoveTo(T* out) && {
if (V8_LIKELY(IsJust())) *out = std::move(value_);
return IsJust();
}
/**
* Converts this Maybe<> to a value of type T. If this Maybe<> is
* nothing (empty), V8 will crash the process.
*/
V8_INLINE T FromJust() const& {
if (V8_UNLIKELY(!IsJust())) api_internal::FromJustIsNothing();
return value_;
}
/**
* Converts this Maybe<> to a value of type T. If this Maybe<> is
* nothing (empty), V8 will crash the process.
*/
V8_INLINE T FromJust() && {
if (V8_UNLIKELY(!IsJust())) api_internal::FromJustIsNothing();
return std::move(value_);
}
/**
* Converts this Maybe<> to a value of type T, using a default value if this
* Maybe<> is nothing (empty).
*/
V8_INLINE T FromMaybe(const T& default_value) const {
return has_value_ ? value_ : default_value;
}
V8_INLINE bool operator==(const Maybe& other) const {
return (IsJust() == other.IsJust()) &&
(!IsJust() || FromJust() == other.FromJust());
}
V8_INLINE bool operator!=(const Maybe& other) const {
return !operator==(other);
}
private:
explicit Maybe(const T& t) : has_value_(true), value_(t) {}
explicit Maybe(T&& t) : has_value_(true), value_(std::move(t)) {}
bool has_value_ = false;
T value_;
template <class U>
friend Maybe<U> Just(const U& u);
template <class U, std::enable_if_t<!std::is_lvalue_reference_v<U>>*>
friend Maybe<U> Just(U&& u);
};
template <class T>
inline constexpr Maybe<T> Nothing() {
return {};
}
template <class T>
inline Maybe<T> Just(const T& t) {
return Maybe<T>(t);
}
// Don't use forwarding references here but instead use two overloads.
// Forwarding references only work when type deduction takes place, which is not
// the case for callsites such as Just<Type>(t).
template <class T, std::enable_if_t<!std::is_lvalue_reference_v<T>>* = nullptr>
inline Maybe<T> Just(T&& t) {
return Maybe<T>(std::move(t));
}
// A template specialization of Maybe<T> for the case of T = void.
template <>
class Maybe<void> {
public:
constexpr Maybe() = default;
constexpr Maybe(internal::NullMaybeType) {}
V8_INLINE bool IsNothing() const { return !is_valid_; }
V8_INLINE bool IsEmpty() const { return IsNothing(); }
V8_INLINE bool IsJust() const { return is_valid_; }
V8_INLINE bool operator==(const Maybe& other) const {
return IsJust() == other.IsJust();
}
V8_INLINE bool operator!=(const Maybe& other) const {
return !operator==(other);
}
private:
struct JustTag {};
explicit Maybe(JustTag) : is_valid_(true) {}
bool is_valid_ = false;
friend Maybe<void> JustVoid();
};
inline Maybe<void> JustVoid() { return Maybe<void>(Maybe<void>::JustTag()); }
} // namespace v8
#endif // INCLUDE_V8_MAYBE_H_

View File

@ -0,0 +1,48 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_MEMORY_SPAN_H_
#define INCLUDE_V8_MEMORY_SPAN_H_
#include <array>
#include <span>
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
/**
* Points to an unowned contiguous buffer holding a known number of elements.
*
* This is an alias for std::span. In a future version, it will be marked as:
* V8_DEPRECATE_SOON("Use std::span instead.")
*/
template <typename T>
using MemorySpan = std::span<T>;
/**
* Helper function template to create an array of fixed length, initialized by
* the provided initializer list, without explicitly specifying the array size,
* e.g.
*
* auto arr = v8::to_array<Local<String>>({v8_str("one"), v8_str("two")});
*
* This is an alias for std::to_array. In a future version, it will be marked
* as: V8_DEPRECATE_SOON("Use std::to_array instead.")
*/
template <typename T, std::size_t N>
[[nodiscard]] constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) {
return std::to_array(a);
}
template <typename T, std::size_t N>
[[nodiscard]] constexpr std::array<std::remove_cv_t<T>, N> to_array(
T (&&a)[N]) {
return std::to_array(std::move(a));
}
} // namespace v8
#endif // INCLUDE_V8_MEMORY_SPAN_H_

217
vendor/artifacts/v8/include/v8-message.h vendored Normal file
View File

@ -0,0 +1,217 @@
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_V8_MESSAGE_H_
#define INCLUDE_V8_MESSAGE_H_
#include <stdio.h>
#include <iosfwd>
#include "v8-callbacks.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-maybe.h" // NOLINT(build/include_directory)
#include "v8-primitive.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Integer;
class PrimitiveArray;
class StackTrace;
class String;
class Value;
/**
* The optional attributes of ScriptOrigin.
*/
class ScriptOriginOptions {
public:
V8_INLINE ScriptOriginOptions(bool is_shared_cross_origin = false,
bool is_opaque = false, bool is_wasm = false,
bool is_module = false)
: flags_((is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
(is_wasm ? kIsWasm : 0) | (is_opaque ? kIsOpaque : 0) |
(is_module ? kIsModule : 0)) {}
V8_INLINE ScriptOriginOptions(int flags)
: flags_(flags &
(kIsSharedCrossOrigin | kIsOpaque | kIsWasm | kIsModule)) {}
bool IsSharedCrossOrigin() const {
return (flags_ & kIsSharedCrossOrigin) != 0;
}
bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
bool IsWasm() const { return (flags_ & kIsWasm) != 0; }
bool IsModule() const { return (flags_ & kIsModule) != 0; }
int Flags() const { return flags_; }
private:
enum {
kIsSharedCrossOrigin = 1,
kIsOpaque = 1 << 1,
kIsWasm = 1 << 2,
kIsModule = 1 << 3
};
const int flags_;
};
/**
* The origin, within a file, of a script.
*/
class V8_EXPORT ScriptOrigin {
public:
V8_INLINE ScriptOrigin(Local<Value> resource_name,
int resource_line_offset = 0,
int resource_column_offset = 0,
bool resource_is_shared_cross_origin = false,
int script_id = -1,
Local<Value> source_map_url = Local<Value>(),
bool resource_is_opaque = false, bool is_wasm = false,
bool is_module = false,
Local<Data> host_defined_options = Local<Data>())
: resource_name_(resource_name),
resource_line_offset_(resource_line_offset),
resource_column_offset_(resource_column_offset),
options_(resource_is_shared_cross_origin, resource_is_opaque, is_wasm,
is_module),
script_id_(script_id),
source_map_url_(source_map_url),
host_defined_options_(host_defined_options) {
VerifyHostDefinedOptions();
}
V8_INLINE Local<Value> ResourceName() const;
V8_INLINE int LineOffset() const;
V8_INLINE int ColumnOffset() const;
V8_INLINE int ScriptId() const;
V8_INLINE Local<Value> SourceMapUrl() const;
V8_INLINE Local<Data> GetHostDefinedOptions() const;
V8_INLINE ScriptOriginOptions Options() const { return options_; }
private:
void VerifyHostDefinedOptions() const;
Local<Value> resource_name_;
int resource_line_offset_;
int resource_column_offset_;
ScriptOriginOptions options_;
int script_id_;
Local<Value> source_map_url_;
Local<Data> host_defined_options_;
};
/**
* An error message.
*/
class V8_EXPORT Message {
public:
Local<String> Get() const;
V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSource(
Local<Context> context) const;
V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine(
Local<Context> context) const;
/**
* Returns the origin for the script from where the function causing the
* error originates.
*/
ScriptOrigin GetScriptOrigin() const;
/**
* Returns the resource name for the script from where the function causing
* the error originates.
*/
Local<Value> GetScriptResourceName() const;
/**
* Exception stack trace. By default stack traces are not captured for
* uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
* to change this option.
*/
Local<StackTrace> GetStackTrace() const;
/**
* Returns the number, 1-based, of the line where the error occurred.
*/
V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const;
/**
* Returns the index within the script of the first character where
* the error occurred. This is best effort and not guaranteed. It may be -1 or
* even > EndPosition in some cases.
*/
int GetStartPosition() const;
/**
* Returns the index within the script of the last character where
* the error occurred. This is best effort and not guaranteed. It may be -1 or
* even < StartPosition in some cases.
*/
int GetEndPosition() const;
/**
* Returns the Wasm function index where the error occurred. Returns -1 if
* message is not from a Wasm script.
*/
int GetWasmFunctionIndex() const;
/**
* Returns the error level of the message.
*/
int ErrorLevel() const;
/**
* Returns the index within the line of the first character where
* the error occurred.
*/
int GetStartColumn() const;
V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const;
/**
* Returns the index within the line of the last character where
* the error occurred.
*/
int GetEndColumn() const;
V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const;
/**
* Passes on the value set by the embedder when it fed the script from which
* this Message was generated to V8.
*/
bool IsSharedCrossOrigin() const;
bool IsOpaque() const;
/**
* If provided, the callback can be used to selectively include
* or redact frames based on their script names. (true to include a frame)
*/
static void PrintCurrentStackTrace(
Isolate* isolate, std::ostream& out,
PrintCurrentStackTraceFilterCallback should_include_frame_callback =
nullptr);
static const int kNoLineNumberInfo = 0;
static const int kNoColumnInfo = 0;
static const int kNoScriptIdInfo = 0;
static const int kNoWasmFunctionIndexInfo = -1;
};
Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
Local<Data> ScriptOrigin::GetHostDefinedOptions() const {
return host_defined_options_;
}
int ScriptOrigin::LineOffset() const { return resource_line_offset_; }
int ScriptOrigin::ColumnOffset() const { return resource_column_offset_; }
int ScriptOrigin::ScriptId() const { return script_id_; }
Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
} // namespace v8
#endif // INCLUDE_V8_MESSAGE_H_

273
vendor/artifacts/v8/include/v8-metrics.h vendored Normal file
View File

@ -0,0 +1,273 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_METRICS_H_
#define V8_METRICS_H_
#include <stddef.h>
#include <stdint.h>
#include <optional>
#include <vector>
#include "v8-internal.h" // NOLINT(build/include_directory)
#include "v8-isolate.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
namespace v8 {
class Context;
class Isolate;
namespace metrics {
struct GarbageCollectionPhases {
int64_t total_wall_clock_duration_in_us = -1;
int64_t compact_wall_clock_duration_in_us = -1;
int64_t mark_wall_clock_duration_in_us = -1;
int64_t sweep_wall_clock_duration_in_us = -1;
int64_t weak_wall_clock_duration_in_us = -1;
};
struct GarbageCollectionSizes {
int64_t bytes_before = -1;
int64_t bytes_after = -1;
int64_t bytes_freed = -1;
};
struct GarbageCollectionLimits {
int64_t bytes_baseline = -1;
int64_t bytes_limit = -1;
int64_t bytes_current = -1;
int64_t bytes_max = -1;
};
struct GarbageCollectionFullCycle {
int reason = -1;
int incremental_marking_reason = -1;
// The priority of the isolate during the GC cycle. A nullopt value denotes a
// mixed priority cycle, meaning the Isolate's priority was changed while the
// cycle was in progress.
std::optional<v8::Isolate::Priority> priority = std::nullopt;
bool reduce_memory = false;
bool is_loading = false;
bool is_input_handling = false;
GarbageCollectionPhases total;
GarbageCollectionPhases total_cpp;
GarbageCollectionPhases main_thread;
GarbageCollectionPhases main_thread_cpp;
GarbageCollectionPhases main_thread_atomic;
GarbageCollectionPhases main_thread_atomic_cpp;
GarbageCollectionPhases main_thread_incremental;
GarbageCollectionPhases main_thread_incremental_cpp;
GarbageCollectionSizes objects;
GarbageCollectionSizes objects_cpp;
GarbageCollectionSizes memory;
GarbageCollectionSizes memory_cpp;
GarbageCollectionLimits old_generation_consumed;
GarbageCollectionLimits global_consumed;
int64_t external_memory_bytes = -1;
double collection_rate_in_percent = -1.0;
double collection_rate_cpp_in_percent = -1.0;
double efficiency_in_bytes_per_us = -1.0;
double efficiency_cpp_in_bytes_per_us = -1.0;
double main_thread_efficiency_in_bytes_per_us = -1.0;
double main_thread_efficiency_cpp_in_bytes_per_us = -1.0;
double collection_weight_in_percent = -1.0;
double collection_weight_cpp_in_percent = -1.0;
double main_thread_collection_weight_in_percent = -1.0;
double main_thread_collection_weight_cpp_in_percent = -1.0;
int64_t incremental_marking_start_stop_wall_clock_duration_in_us = -1;
int64_t total_duration_since_last_mark_compact = -1;
};
struct GarbageCollectionFullMainThreadIncrementalMark {
int64_t wall_clock_duration_in_us = -1;
int64_t cpp_wall_clock_duration_in_us = -1;
};
struct GarbageCollectionFullMainThreadIncrementalSweep {
int64_t wall_clock_duration_in_us = -1;
int64_t cpp_wall_clock_duration_in_us = -1;
};
template <typename EventType>
struct GarbageCollectionBatchedEvents {
std::vector<EventType> events;
};
using GarbageCollectionFullMainThreadBatchedIncrementalMark =
GarbageCollectionBatchedEvents<
GarbageCollectionFullMainThreadIncrementalMark>;
using GarbageCollectionFullMainThreadBatchedIncrementalSweep =
GarbageCollectionBatchedEvents<
GarbageCollectionFullMainThreadIncrementalSweep>;
struct GarbageCollectionYoungCycle {
int reason = -1;
// The priority of the isolate during the GC cycle. A nullopt value denotes a
// mixed priority cycle, meaning the Isolate's priority was changed while the
// cycle was in progress.
std::optional<v8::Isolate::Priority> priority = std::nullopt;
int64_t total_wall_clock_duration_in_us = -1;
int64_t main_thread_wall_clock_duration_in_us = -1;
double collection_rate_in_percent = -1.0;
double efficiency_in_bytes_per_us = -1.0;
double main_thread_efficiency_in_bytes_per_us = -1.0;
#if defined(CPPGC_YOUNG_GENERATION)
GarbageCollectionPhases total_cpp;
GarbageCollectionSizes objects_cpp;
GarbageCollectionSizes memory_cpp;
double collection_rate_cpp_in_percent = -1.0;
double efficiency_cpp_in_bytes_per_us = -1.0;
double main_thread_efficiency_cpp_in_bytes_per_us = -1.0;
#endif // defined(CPPGC_YOUNG_GENERATION)
};
// Note: These structs do not define any constructor, and declare most fields as
// const, to force initializing them when using aggregate (designated)
// initialization.
// Those structs are meant to be created in V8 and read by embedders.
struct WasmModuleDecoded {
const bool async;
const bool streamed;
const bool success;
const size_t module_size_in_bytes;
const size_t function_count;
// Optional field; only set if a high-resolution clock is available.
int64_t wall_clock_duration_in_us = -1;
};
struct WasmModuleCompiled {
const bool async;
const bool streamed;
const bool cached;
const bool deserialized;
const bool lazy;
const bool success;
const size_t code_size_in_bytes;
const size_t liftoff_bailout_count;
// Optional field; only set if a high-resolution clock is available.
int64_t wall_clock_duration_in_us = -1;
};
struct WasmModuleInstantiated {
const bool async;
const bool success;
const size_t imported_function_count;
// Optional field; only set if a high-resolution clock is available.
int64_t wall_clock_duration_in_us = -1;
};
struct WasmModulesPerIsolate {
const size_t count;
};
/**
* This class serves as a base class for recording event-based metrics in V8.
* There a two kinds of metrics, those which are expected to be thread-safe and
* whose implementation is required to fulfill this requirement and those whose
* implementation does not have that requirement and only needs to be
* executable on the main thread. If such an event is triggered from a
* background thread, it will be delayed and executed by the foreground task
* runner.
*
* The embedder is expected to call v8::Isolate::SetMetricsRecorder()
* providing its implementation and have the virtual methods overwritten
* for the events it cares about.
*/
class V8_EXPORT Recorder {
public:
// A unique identifier for a context in this Isolate.
// It is guaranteed to not be reused throughout the lifetime of the Isolate.
class ContextId {
public:
ContextId() : id_(kEmptyId) {}
bool IsEmpty() const { return id_ == kEmptyId; }
static const ContextId Empty() { return ContextId{kEmptyId}; }
bool operator==(const ContextId& other) const { return id_ == other.id_; }
bool operator!=(const ContextId& other) const { return id_ != other.id_; }
private:
friend class ::v8::Context;
friend class ::v8::internal::Isolate;
explicit ContextId(uintptr_t id) : id_(id) {}
static constexpr uintptr_t kEmptyId = 0;
uintptr_t id_;
};
virtual ~Recorder() = default;
// Main thread events. Those are only triggered on the main thread, and hence
// can access the context.
#define ADD_MAIN_THREAD_EVENT(E) \
virtual void AddMainThreadEvent(const E&, ContextId) {}
ADD_MAIN_THREAD_EVENT(GarbageCollectionFullCycle)
ADD_MAIN_THREAD_EVENT(GarbageCollectionFullMainThreadIncrementalMark)
ADD_MAIN_THREAD_EVENT(GarbageCollectionFullMainThreadBatchedIncrementalMark)
ADD_MAIN_THREAD_EVENT(GarbageCollectionFullMainThreadIncrementalSweep)
ADD_MAIN_THREAD_EVENT(GarbageCollectionFullMainThreadBatchedIncrementalSweep)
ADD_MAIN_THREAD_EVENT(GarbageCollectionYoungCycle)
ADD_MAIN_THREAD_EVENT(WasmModuleDecoded)
ADD_MAIN_THREAD_EVENT(WasmModuleCompiled)
ADD_MAIN_THREAD_EVENT(WasmModuleInstantiated)
#undef ADD_MAIN_THREAD_EVENT
// Thread-safe events are not allowed to access the context and therefore do
// not carry a context ID with them. These IDs can be generated using
// Recorder::GetContextId() and the ID will be valid throughout the lifetime
// of the isolate. It is not guaranteed that the ID will still resolve to
// a valid context using Recorder::GetContext() at the time the metric is
// recorded. In this case, an empty handle will be returned.
#define ADD_THREAD_SAFE_EVENT(E) \
virtual void AddThreadSafeEvent(const E&) {}
ADD_THREAD_SAFE_EVENT(WasmModulesPerIsolate)
#undef ADD_THREAD_SAFE_EVENT
virtual void NotifyIsolateDisposal() {}
// Return the context with the given id or an empty handle if the context
// was already garbage collected.
static MaybeLocal<Context> GetContext(Isolate* isolate, ContextId id);
// Return the unique id corresponding to the given context.
static ContextId GetContextId(Local<Context> context);
};
/**
* Experimental API intended for the LongTasks UKM (crbug.com/1173527).
* The Reset() method should be called at the start of a potential
* long task. The Get() method returns durations of V8 work that
* happened during the task.
*
* This API is experimental and may be removed/changed in the future.
*/
struct V8_EXPORT LongTaskStats {
/**
* Resets durations of V8 work for the new task.
*/
V8_INLINE static void Reset(Isolate* isolate) {
v8::internal::Internals::IncrementLongTasksStatsCounter(isolate);
}
/**
* Returns durations of V8 work that happened since the last Reset().
*/
static LongTaskStats Get(Isolate* isolate);
int64_t gc_full_atomic_wall_clock_duration_us = 0;
int64_t gc_full_incremental_wall_clock_duration_us = 0;
int64_t gc_young_wall_clock_duration_us = 0;
// Only collected with --slow-histograms
int64_t v8_execute_us = 0;
};
} // namespace metrics
} // namespace v8
#endif // V8_METRICS_H_

Some files were not shown because too many files have changed in this diff Show More