Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/node_realm.h
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 <unordered_map> 8 #include "cleanup_queue.h" 9 #include "env_properties.h" 10 #include "memory_tracker.h" 11 #include "node_snapshotable.h" 12 13 namespace node { 14 15 struct RealmSerializeInfo { 16 std::vector<std::string> builtins; 17 std::vector<PropInfo> persistent_values; 18 std::vector<PropInfo> native_objects; 19 20 SnapshotIndex context; 21 friend std::ostream& operator<<(std::ostream& o, const RealmSerializeInfo& i); 22 }; 23 24 using BindingDataStore = 25 std::array<BaseObjectWeakPtr<BaseObject>, 26 static_cast<size_t>(BindingDataType::kBindingDataTypeCount)>; 27 28 /** 29 * node::Realm is a container for a set of JavaScript objects and functions 30 * that associated with a particular global environment. 31 * 32 * An ECMAScript realm (https://tc39.es/ecma262/#sec-code-realms) representing 33 * a global environment in which script is run. Each ECMAScript realm comes 34 * with a global object and a set of intrinsic objects. An ECMAScript realm has 35 * a [[HostDefined]] field, which contains the node::Realm object. 36 * 37 * Realm can be a principal realm or a synthetic realm. A principal realm is 38 * created with an Environment as its principal global environment to evaluate 39 * scripts. A synthetic realm is created with JS APIs like ShadowRealm. 40 * 41 * Native bindings and builtin modules can be evaluated in either a principal 42 * realm or a synthetic realm. 43 */ 44 class Realm : public MemoryRetainer { 45 public: 46 enum Kind { 47 kPrincipal, 48 kShadowRealm, 49 }; 50 51 static inline Realm* GetCurrent(v8::Isolate* isolate); 52 static inline Realm* GetCurrent(v8::Local<v8::Context> context); 53 static inline Realm* GetCurrent( 54 const v8::FunctionCallbackInfo<v8::Value>& info); 55 template <typename T> 56 static inline Realm* GetCurrent(const v8::PropertyCallbackInfo<T>& info); 57 58 Realm(Environment* env, v8::Local<v8::Context> context, Kind kind); 59 60 Realm(const Realm&) = delete; 61 Realm& operator=(const Realm&) = delete; 62 Realm(Realm&&) = delete; 63 Realm& operator=(Realm&&) = delete; 64 65 void MemoryInfo(MemoryTracker* tracker) const override; 66 67 void CreateProperties(); 68 RealmSerializeInfo Serialize(v8::SnapshotCreator* creator); 69 void DeserializeProperties(const RealmSerializeInfo* info); 70 71 v8::MaybeLocal<v8::Value> ExecuteBootstrapper(const char* id); 72 v8::MaybeLocal<v8::Value> RunBootstrapping(); 73 74 inline void TrackBaseObject(BaseObject* bo); 75 inline void UntrackBaseObject(BaseObject* bo); 76 inline bool PendingCleanup() const; 77 void RunCleanup(); 78 79 template <typename T> 80 void ForEachBaseObject(T&& iterator) const; 81 82 void PrintInfoForSnapshot(); 83 void VerifyNoStrongBaseObjects(); 84 85 inline IsolateData* isolate_data() const; 86 inline Environment* env() const; 87 inline v8::Isolate* isolate() const; 88 inline Kind kind() const; 89 inline virtual v8::Local<v8::Context> context() const; 90 inline bool has_run_bootstrapping_code() const; 91 92 // Methods created using SetMethod(), SetPrototypeMethod(), etc. inside 93 // this scope can access the created T* object using 94 // GetBindingData<T>(args) later. 95 template <typename T, typename... Args> 96 T* AddBindingData(v8::Local<v8::Object> target, Args&&... args); 97 template <typename T, typename U> 98 static inline T* GetBindingData(const v8::PropertyCallbackInfo<U>& info); 99 template <typename T> 100 static inline T* GetBindingData( 101 const v8::FunctionCallbackInfo<v8::Value>& info); 102 template <typename T> 103 static inline T* GetBindingData(v8::Local<v8::Context> context); 104 template <typename T> 105 inline T* GetBindingData(); 106 inline BindingDataStore* binding_data_store(); 107 108 // The BaseObject count is a debugging helper that makes sure that there are 109 // no memory leaks caused by BaseObjects staying alive longer than expected 110 // (in particular, no circular BaseObjectPtr references). 111 inline int64_t base_object_count() const; 112 113 // Base object count created after the bootstrap of the realm. 114 inline int64_t base_object_created_after_bootstrap() const; 115 116 #define V(PropertyName, TypeName) \ 117 virtual v8::Local<TypeName> PropertyName() const = 0; \ 118 virtual void set_##PropertyName(v8::Local<TypeName> value) = 0; 119 PER_REALM_STRONG_PERSISTENT_VALUES(V) 120 #undef V 121 122 std::set<struct node_module*> internal_bindings; 123 std::set<std::string> builtins_with_cache; 124 std::set<std::string> builtins_without_cache; 125 // This is only filled during deserialization. We use a vector since 126 // it's only used for tests. 127 std::vector<std::string> builtins_in_snapshot; 128 129 protected: 130 ~Realm(); 131 132 virtual v8::MaybeLocal<v8::Value> BootstrapRealm() = 0; 133 134 Environment* env_; 135 // Shorthand for isolate pointer. 136 v8::Isolate* isolate_; 137 v8::Global<v8::Context> context_; 138 139 #define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName##_; 140 PER_REALM_STRONG_PERSISTENT_VALUES(V) 141 #undef V 142 143 private: 144 void InitializeContext(v8::Local<v8::Context> context, 145 const RealmSerializeInfo* realm_info); 146 void DoneBootstrapping(); 147 148 Kind kind_; 149 bool has_run_bootstrapping_code_ = false; 150 151 int64_t base_object_count_ = 0; 152 int64_t base_object_created_by_bootstrap_ = 0; 153 154 BindingDataStore binding_data_store_; 155 156 BaseObjectList base_object_list_; 157 }; 158 159 class PrincipalRealm final : public Realm { 160 public: 161 PrincipalRealm(Environment* env, 162 v8::Local<v8::Context> context, 163 const RealmSerializeInfo* realm_info); 164 ~PrincipalRealm(); 165 166 SET_MEMORY_INFO_NAME(PrincipalRealm) 167 SET_SELF_SIZE(PrincipalRealm) 168 169 #define V(PropertyName, TypeName) \ 170 v8::Local<TypeName> PropertyName() const override; \ 171 void set_##PropertyName(v8::Local<TypeName> value) override; 172 PER_REALM_STRONG_PERSISTENT_VALUES(V) 173 #undef V 174 175 protected: 176 v8::MaybeLocal<v8::Value> BootstrapRealm() override; 177 }; 178 179 } // namespace node 180 181 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 182 183 #endif // SRC_NODE_REALM_H_