GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_realm.h Lines: 3 3 100.0 %
Date: 2022-09-25 04:23:55 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 "env_properties.h"
8
#include "memory_tracker.h"
9
#include "node_snapshotable.h"
10
11
namespace node {
12
13
struct RealmSerializeInfo {
14
  std::vector<PropInfo> persistent_values;
15
16
  SnapshotIndex context;
17
  friend std::ostream& operator<<(std::ostream& o, const RealmSerializeInfo& i);
18
};
19
20
/**
21
 * node::Realm is a container for a set of JavaScript objects and functions
22
 * that associated with a particular global environment.
23
 *
24
 * An ECMAScript realm (https://tc39.es/ecma262/#sec-code-realms) representing
25
 * a global environment in which script is run. Each ECMAScript realm comes
26
 * with a global object and a set of intrinsic objects. An ECMAScript realm has
27
 * a [[HostDefined]] field, which contains the node::Realm object.
28
 *
29
 * Realm can be a principal realm or a synthetic realm. A principal realm is
30
 * created with an Environment as its principal global environment to evaluate
31
 * scripts. A synthetic realm is created with JS APIs like ShadowRealm.
32
 *
33
 * Native bindings and builtin modules can be evaluated in either a principal
34
 * realm or a synthetic realm.
35
 */
36
class Realm : public MemoryRetainer {
37
 public:
38
  static inline Realm* GetCurrent(v8::Isolate* isolate);
39
  static inline Realm* GetCurrent(v8::Local<v8::Context> context);
40
  static inline Realm* GetCurrent(
41
      const v8::FunctionCallbackInfo<v8::Value>& info);
42
  template <typename T>
43
  static inline Realm* GetCurrent(const v8::PropertyCallbackInfo<T>& info);
44
45
  Realm(Environment* env,
46
        v8::Local<v8::Context> context,
47
        const RealmSerializeInfo* realm_info);
48
745272
  ~Realm() = default;
49
50
  Realm(const Realm&) = delete;
51
  Realm& operator=(const Realm&) = delete;
52
  Realm(Realm&&) = delete;
53
  Realm& operator=(Realm&&) = delete;
54
55
25
  SET_MEMORY_INFO_NAME(Realm)
56
25
  SET_SELF_SIZE(Realm);
57
  void MemoryInfo(MemoryTracker* tracker) const override;
58
59
  void CreateProperties();
60
  RealmSerializeInfo Serialize(v8::SnapshotCreator* creator);
61
  void DeserializeProperties(const RealmSerializeInfo* info);
62
63
  v8::MaybeLocal<v8::Value> ExecuteBootstrapper(const char* id);
64
  v8::MaybeLocal<v8::Value> BootstrapInternalLoaders();
65
  v8::MaybeLocal<v8::Value> BootstrapNode();
66
  v8::MaybeLocal<v8::Value> RunBootstrapping();
67
68
  inline Environment* env() const;
69
  inline v8::Isolate* isolate() const;
70
  inline v8::Local<v8::Context> context() const;
71
  inline bool has_run_bootstrapping_code() const;
72
73
#define V(PropertyName, TypeName)                                              \
74
  inline v8::Local<TypeName> PropertyName() const;                             \
75
  inline void set_##PropertyName(v8::Local<TypeName> value);
76
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
77
#undef V
78
79
 private:
80
  void InitializeContext(v8::Local<v8::Context> context,
81
                         const RealmSerializeInfo* realm_info);
82
  void DoneBootstrapping();
83
84
  Environment* env_;
85
  // Shorthand for isolate pointer.
86
  v8::Isolate* isolate_;
87
  v8::Global<v8::Context> context_;
88
  bool has_run_bootstrapping_code_ = false;
89
90
#define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName##_;
91
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
92
#undef V
93
};
94
95
}  // namespace node
96
97
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
98
99
#endif  // SRC_NODE_REALM_H_