v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-weak-callback-info.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_WEAK_CALLBACK_INFO_H_
6#define INCLUDE_V8_WEAK_CALLBACK_INFO_H_
7
8#include <cstring>
9
10#include "cppgc/internal/conditional-stack-allocated.h" // NOLINT(build/include_directory)
11#include "v8config.h" // NOLINT(build/include_directory)
12
13namespace v8 {
14
15class Isolate;
16
17namespace api_internal {
19} // namespace api_internal
20
21static constexpr int kInternalFieldsInWeakCallback = 2;
22static constexpr int kEmbedderFieldsInWeakCallback = 2;
23
24template <typename T>
26 : public cppgc::internal::ConditionalStackAllocatedBase<T> {
27 public:
28 using Callback = void (*)(const WeakCallbackInfo<T>& data);
29
30 WeakCallbackInfo(Isolate* isolate, T* parameter,
31 void* embedder_fields[kEmbedderFieldsInWeakCallback],
32 Callback* callback)
33 : isolate_(isolate), parameter_(parameter), callback_(callback) {
34 memcpy(embedder_fields_, embedder_fields,
35 sizeof(embedder_fields[0]) * kEmbedderFieldsInWeakCallback);
36 }
37
38 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
39 V8_INLINE T* GetParameter() const { return parameter_; }
40 V8_INLINE void* GetInternalField(int index) const;
41
42 /**
43 * When a weak callback is first invoked the embedders _must_ Reset() the
44 * handle which triggered the callback. The handle itself is unusable for
45 * anything else. No other V8 API calls may be called in the first callback.
46 * Additional work requires scheduling a second invocation via
47 * `SetSecondPassCallback()` which will be called some time after all the
48 * initial callbacks are processed.
49 *
50 * The second pass callback is prohibited from executing JavaScript. Embedders
51 * should schedule another callback in case this is required.
52 */
53 void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
54
55 private:
56 Isolate* isolate_;
57 T* parameter_;
58 Callback* callback_;
59 void* embedder_fields_[kEmbedderFieldsInWeakCallback];
60};
61
62/**
63 * Weakness type for weak handles.
64 */
65enum class WeakCallbackType {
66 /**
67 * Passes a user-defined void* parameter back to the callback.
68 */
70 /**
71 * Passes the first two internal fields of the object back to the callback.
72 */
74};
75
76template <class T>
77void* WeakCallbackInfo<T>::GetInternalField(int index) const {
78#ifdef V8_ENABLE_CHECKS
79 if (index < 0 || index >= kEmbedderFieldsInWeakCallback) {
80 api_internal::InternalFieldOutOfBounds(index);
81 }
82#endif
83 return embedder_fields_[index];
84}
85
86} // namespace v8
87
88#endif // INCLUDE_V8_WEAK_CALLBACK_INFO_H_
WeakCallbackInfo(Isolate *isolate, T *parameter, void *embedder_fields[kEmbedderFieldsInWeakCallback], Callback *callback)
V8_INLINE T * GetParameter() const
void SetSecondPassCallback(Callback callback) const
V8_INLINE Isolate * GetIsolate() const
V8_INLINE void * GetInternalField(int index) const
V8_EXPORT void InternalFieldOutOfBounds(int index)
#define V8_EXPORT
Definition v8config.h:860
#define V8_INLINE
Definition v8config.h:513