Coverage Report

Created: 2025-10-31 09:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/node/deps/v8/include/v8-weak-callback-info.h
Line
Count
Source
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
13
namespace v8 {
14
15
class Isolate;
16
17
namespace api_internal {
18
V8_EXPORT void InternalFieldOutOfBounds(int index);
19
}  // namespace api_internal
20
21
static constexpr int kInternalFieldsInWeakCallback = 2;
22
static constexpr int kEmbedderFieldsInWeakCallback = 2;
23
24
template <typename T>
25
class WeakCallbackInfo
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
0
  V8_INLINE Isolate* GetIsolate() const { return isolate_; }
39
0
  V8_INLINE T* GetParameter() const { return parameter_; }
Unexecuted instantiation: v8::WeakCallbackInfo<node::DestroyParam>::GetParameter() const
Unexecuted instantiation: v8::WeakCallbackInfo<node::BaseObject>::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
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
 */
65
enum class WeakCallbackType {
66
  /**
67
   * Passes a user-defined void* parameter back to the callback.
68
   */
69
  kParameter,
70
  /**
71
   * Passes the first two internal fields of the object back to the callback.
72
   */
73
  kInternalFields,
74
};
75
76
template <class T>
77
void* 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_