Coverage Report

Created: 2025-08-28 09:57

/src/node/deps/v8/include/v8-weak-callback-info.h
Line
Count
Source (jump to first uncovered line)
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 "v8config.h"  // NOLINT(build/include_directory)
9
10
namespace v8 {
11
12
class Isolate;
13
14
namespace api_internal {
15
V8_EXPORT void InternalFieldOutOfBounds(int index);
16
}  // namespace api_internal
17
18
static const int kInternalFieldsInWeakCallback = 2;
19
static const int kEmbedderFieldsInWeakCallback = 2;
20
21
template <typename T>
22
class WeakCallbackInfo {
23
 public:
24
  using Callback = void (*)(const WeakCallbackInfo<T>& data);
25
26
  WeakCallbackInfo(Isolate* isolate, T* parameter,
27
                   void* embedder_fields[kEmbedderFieldsInWeakCallback],
28
                   Callback* callback)
29
      : isolate_(isolate), parameter_(parameter), callback_(callback) {
30
    for (int i = 0; i < kEmbedderFieldsInWeakCallback; ++i) {
31
      embedder_fields_[i] = embedder_fields[i];
32
    }
33
  }
34
35
0
  V8_INLINE Isolate* GetIsolate() const { return isolate_; }
36
2
  V8_INLINE T* GetParameter() const { return parameter_; }
Unexecuted instantiation: v8::WeakCallbackInfo<node::DestroyParam>::GetParameter() const
v8::WeakCallbackInfo<node::BaseObject>::GetParameter() const
Line
Count
Source
36
2
  V8_INLINE T* GetParameter() const { return parameter_; }
Unexecuted instantiation: node_api.cc:v8::WeakCallbackInfo<v8impl::(anonymous namespace)::AsyncContext>::GetParameter() const
Unexecuted instantiation: v8::WeakCallbackInfo<node::contextify::ContextifyContext>::GetParameter() const
Unexecuted instantiation: v8::WeakCallbackInfo<node::shadow_realm::ShadowRealm>::GetParameter() const
Unexecuted instantiation: v8::WeakCallbackInfo<v8impl::ExternalWrapper>::GetParameter() const
Unexecuted instantiation: v8::WeakCallbackInfo<v8impl::Reference>::GetParameter() const
37
  V8_INLINE void* GetInternalField(int index) const;
38
39
  // When first called, the embedder MUST Reset() the Global which triggered the
40
  // callback. The Global itself is unusable for anything else. No v8 other api
41
  // calls may be called in the first callback. Should additional work be
42
  // required, the embedder must set a second pass callback, which will be
43
  // called after all the initial callbacks are processed.
44
  // Calling SetSecondPassCallback on the second pass will immediately crash.
45
  void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
46
47
 private:
48
  Isolate* isolate_;
49
  T* parameter_;
50
  Callback* callback_;
51
  void* embedder_fields_[kEmbedderFieldsInWeakCallback];
52
};
53
54
/**
55
 * Weakness type for weak handles.
56
 */
57
enum class WeakCallbackType {
58
  /**
59
   * Passes a user-defined void* parameter back to the callback.
60
   */
61
  kParameter,
62
  /**
63
   * Passes the first two internal fields of the object back to the callback.
64
   */
65
  kInternalFields,
66
};
67
68
template <class T>
69
void* WeakCallbackInfo<T>::GetInternalField(int index) const {
70
#ifdef V8_ENABLE_CHECKS
71
  if (index < 0 || index >= kEmbedderFieldsInWeakCallback) {
72
    api_internal::InternalFieldOutOfBounds(index);
73
  }
74
#endif
75
  return embedder_fields_[index];
76
}
77
78
}  // namespace v8
79
80
#endif  // INCLUDE_V8_WEAK_CALLBACK_INFO_H_