GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_realm.h Lines: 2 2 100.0 %
Date: 2022-11-09 04:21:35 Branches: 0 0 - %

Line Branch Exec Source
1
#ifndef SRC_NODE_REALM_H_
2
#define SRC_NODE_REALM_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <v8.h>
7
#include "cleanup_queue.h"
8
#include "env_properties.h"
9
#include "memory_tracker.h"
10
#include "node_snapshotable.h"
11
12
namespace node {
13
14
struct RealmSerializeInfo {
15
  std::vector<PropInfo> persistent_values;
16
  std::vector<PropInfo> native_objects;
17
18
  SnapshotIndex context;
19
  friend std::ostream& operator<<(std::ostream& o, const RealmSerializeInfo& i);
20
};
21
22
/**
23
 * node::Realm is a container for a set of JavaScript objects and functions
24
 * that associated with a particular global environment.
25
 *
26
 * An ECMAScript realm (https://tc39.es/ecma262/#sec-code-realms) representing
27
 * a global environment in which script is run. Each ECMAScript realm comes
28
 * with a global object and a set of intrinsic objects. An ECMAScript realm has
29
 * a [[HostDefined]] field, which contains the node::Realm object.
30
 *
31
 * Realm can be a principal realm or a synthetic realm. A principal realm is
32
 * created with an Environment as its principal global environment to evaluate
33
 * scripts. A synthetic realm is created with JS APIs like ShadowRealm.
34
 *
35
 * Native bindings and builtin modules can be evaluated in either a principal
36
 * realm or a synthetic realm.
37
 */
38
class Realm : public MemoryRetainer {
39
 public:
40
  static inline Realm* GetCurrent(v8::Isolate* isolate);
41
  static inline Realm* GetCurrent(v8::Local<v8::Context> context);
42
  static inline Realm* GetCurrent(
43
      const v8::FunctionCallbackInfo<v8::Value>& info);
44
  template <typename T>
45
  static inline Realm* GetCurrent(const v8::PropertyCallbackInfo<T>& info);
46
47
  Realm(Environment* env,
48
        v8::Local<v8::Context> context,
49
        const RealmSerializeInfo* realm_info);
50
  ~Realm();
51
52
  Realm(const Realm&) = delete;
53
  Realm& operator=(const Realm&) = delete;
54
  Realm(Realm&&) = delete;
55
  Realm& operator=(Realm&&) = delete;
56
57
37
  SET_MEMORY_INFO_NAME(Realm)
58
37
  SET_SELF_SIZE(Realm);
59
  void MemoryInfo(MemoryTracker* tracker) const override;
60
61
  void CreateProperties();
62
  RealmSerializeInfo Serialize(v8::SnapshotCreator* creator);
63
  void DeserializeProperties(const RealmSerializeInfo* info);
64
65
  v8::MaybeLocal<v8::Value> ExecuteBootstrapper(const char* id);
66
  v8::MaybeLocal<v8::Value> BootstrapInternalLoaders();
67
  v8::MaybeLocal<v8::Value> BootstrapNode();
68
  v8::MaybeLocal<v8::Value> RunBootstrapping();
69
70
  inline void AddCleanupHook(CleanupQueue::Callback cb, void* arg);
71
  inline void RemoveCleanupHook(CleanupQueue::Callback cb, void* arg);
72
  inline bool HasCleanupHooks() const;
73
  void RunCleanup();
74
75
  template <typename T>
76
  void ForEachBaseObject(T&& iterator) const;
77
78
  void PrintInfoForSnapshot();
79
  void VerifyNoStrongBaseObjects();
80
81
  inline IsolateData* isolate_data() const;
82
  inline Environment* env() const;
83
  inline v8::Isolate* isolate() const;
84
  inline v8::Local<v8::Context> context() const;
85
  inline bool has_run_bootstrapping_code() const;
86
87
  // The BaseObject count is a debugging helper that makes sure that there are
88
  // no memory leaks caused by BaseObjects staying alive longer than expected
89
  // (in particular, no circular BaseObjectPtr references).
90
  inline void modify_base_object_count(int64_t delta);
91
  inline int64_t base_object_count() const;
92
93
  // Base object count created after the bootstrap of the realm.
94
  inline int64_t base_object_created_after_bootstrap() const;
95
96
#define V(PropertyName, TypeName)                                              \
97
  inline v8::Local<TypeName> PropertyName() const;                             \
98
  inline void set_##PropertyName(v8::Local<TypeName> value);
99
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
100
#undef V
101
102
 private:
103
  void InitializeContext(v8::Local<v8::Context> context,
104
                         const RealmSerializeInfo* realm_info);
105
  void DoneBootstrapping();
106
107
  Environment* env_;
108
  // Shorthand for isolate pointer.
109
  v8::Isolate* isolate_;
110
  v8::Global<v8::Context> context_;
111
  bool has_run_bootstrapping_code_ = false;
112
113
  int64_t base_object_count_ = 0;
114
  int64_t base_object_created_by_bootstrap_ = 0;
115
116
  CleanupQueue cleanup_queue_;
117
118
#define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName##_;
119
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
120
#undef V
121
};
122
123
}  // namespace node
124
125
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
126
127
#endif  // SRC_NODE_REALM_H_