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