11cb0ef41Sopenharmony_ci#ifndef SRC_NODE_REALM_H_
21cb0ef41Sopenharmony_ci#define SRC_NODE_REALM_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include <v8.h>
71cb0ef41Sopenharmony_ci#include <unordered_map>
81cb0ef41Sopenharmony_ci#include "cleanup_queue.h"
91cb0ef41Sopenharmony_ci#include "env_properties.h"
101cb0ef41Sopenharmony_ci#include "memory_tracker.h"
111cb0ef41Sopenharmony_ci#include "node_snapshotable.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace node {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cistruct RealmSerializeInfo {
161cb0ef41Sopenharmony_ci  std::vector<std::string> builtins;
171cb0ef41Sopenharmony_ci  std::vector<PropInfo> persistent_values;
181cb0ef41Sopenharmony_ci  std::vector<PropInfo> native_objects;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  SnapshotIndex context;
211cb0ef41Sopenharmony_ci  friend std::ostream& operator<<(std::ostream& o, const RealmSerializeInfo& i);
221cb0ef41Sopenharmony_ci};
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciusing BindingDataStore = std::array<BaseObjectPtr<BaseObject>,
251cb0ef41Sopenharmony_ci                     static_cast<size_t>(
261cb0ef41Sopenharmony_ci                         BindingDataType::kBindingDataTypeCount)>;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci/**
291cb0ef41Sopenharmony_ci * node::Realm is a container for a set of JavaScript objects and functions
301cb0ef41Sopenharmony_ci * that associated with a particular global environment.
311cb0ef41Sopenharmony_ci *
321cb0ef41Sopenharmony_ci * An ECMAScript realm (https://tc39.es/ecma262/#sec-code-realms) representing
331cb0ef41Sopenharmony_ci * a global environment in which script is run. Each ECMAScript realm comes
341cb0ef41Sopenharmony_ci * with a global object and a set of intrinsic objects. An ECMAScript realm has
351cb0ef41Sopenharmony_ci * a [[HostDefined]] field, which contains the node::Realm object.
361cb0ef41Sopenharmony_ci *
371cb0ef41Sopenharmony_ci * Realm can be a principal realm or a synthetic realm. A principal realm is
381cb0ef41Sopenharmony_ci * created with an Environment as its principal global environment to evaluate
391cb0ef41Sopenharmony_ci * scripts. A synthetic realm is created with JS APIs like ShadowRealm.
401cb0ef41Sopenharmony_ci *
411cb0ef41Sopenharmony_ci * Native bindings and builtin modules can be evaluated in either a principal
421cb0ef41Sopenharmony_ci * realm or a synthetic realm.
431cb0ef41Sopenharmony_ci */
441cb0ef41Sopenharmony_ciclass Realm : public MemoryRetainer {
451cb0ef41Sopenharmony_ci public:
461cb0ef41Sopenharmony_ci  static inline Realm* GetCurrent(v8::Isolate* isolate);
471cb0ef41Sopenharmony_ci  static inline Realm* GetCurrent(v8::Local<v8::Context> context);
481cb0ef41Sopenharmony_ci  static inline Realm* GetCurrent(
491cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& info);
501cb0ef41Sopenharmony_ci  template <typename T>
511cb0ef41Sopenharmony_ci  static inline Realm* GetCurrent(const v8::PropertyCallbackInfo<T>& info);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  Realm(Environment* env,
541cb0ef41Sopenharmony_ci        v8::Local<v8::Context> context,
551cb0ef41Sopenharmony_ci        const RealmSerializeInfo* realm_info);
561cb0ef41Sopenharmony_ci  ~Realm();
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  Realm(const Realm&) = delete;
591cb0ef41Sopenharmony_ci  Realm& operator=(const Realm&) = delete;
601cb0ef41Sopenharmony_ci  Realm(Realm&&) = delete;
611cb0ef41Sopenharmony_ci  Realm& operator=(Realm&&) = delete;
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  SET_MEMORY_INFO_NAME(Realm)
641cb0ef41Sopenharmony_ci  SET_SELF_SIZE(Realm)
651cb0ef41Sopenharmony_ci  void MemoryInfo(MemoryTracker* tracker) const override;
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  void CreateProperties();
681cb0ef41Sopenharmony_ci  RealmSerializeInfo Serialize(v8::SnapshotCreator* creator);
691cb0ef41Sopenharmony_ci  void DeserializeProperties(const RealmSerializeInfo* info);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> ExecuteBootstrapper(const char* id);
721cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> BootstrapNode();
731cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> RunBootstrapping();
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  inline void AddCleanupHook(CleanupQueue::Callback cb, void* arg);
761cb0ef41Sopenharmony_ci  inline void RemoveCleanupHook(CleanupQueue::Callback cb, void* arg);
771cb0ef41Sopenharmony_ci  inline bool HasCleanupHooks() const;
781cb0ef41Sopenharmony_ci  void RunCleanup();
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  template <typename T>
811cb0ef41Sopenharmony_ci  void ForEachBaseObject(T&& iterator) const;
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  void PrintInfoForSnapshot();
841cb0ef41Sopenharmony_ci  void VerifyNoStrongBaseObjects();
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  inline IsolateData* isolate_data() const;
871cb0ef41Sopenharmony_ci  inline Environment* env() const;
881cb0ef41Sopenharmony_ci  inline v8::Isolate* isolate() const;
891cb0ef41Sopenharmony_ci  inline v8::Local<v8::Context> context() const;
901cb0ef41Sopenharmony_ci  inline bool has_run_bootstrapping_code() const;
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  // Methods created using SetMethod(), SetPrototypeMethod(), etc. inside
931cb0ef41Sopenharmony_ci  // this scope can access the created T* object using
941cb0ef41Sopenharmony_ci  // GetBindingData<T>(args) later.
951cb0ef41Sopenharmony_ci  template <typename T>
961cb0ef41Sopenharmony_ci  T* AddBindingData(v8::Local<v8::Context> context,
971cb0ef41Sopenharmony_ci                    v8::Local<v8::Object> target);
981cb0ef41Sopenharmony_ci  template <typename T, typename U>
991cb0ef41Sopenharmony_ci  static inline T* GetBindingData(const v8::PropertyCallbackInfo<U>& info);
1001cb0ef41Sopenharmony_ci  template <typename T>
1011cb0ef41Sopenharmony_ci  static inline T* GetBindingData(
1021cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& info);
1031cb0ef41Sopenharmony_ci  template <typename T>
1041cb0ef41Sopenharmony_ci  static inline T* GetBindingData(v8::Local<v8::Context> context);
1051cb0ef41Sopenharmony_ci  inline BindingDataStore* binding_data_store();
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  // The BaseObject count is a debugging helper that makes sure that there are
1081cb0ef41Sopenharmony_ci  // no memory leaks caused by BaseObjects staying alive longer than expected
1091cb0ef41Sopenharmony_ci  // (in particular, no circular BaseObjectPtr references).
1101cb0ef41Sopenharmony_ci  inline void modify_base_object_count(int64_t delta);
1111cb0ef41Sopenharmony_ci  inline int64_t base_object_count() const;
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  // Base object count created after the bootstrap of the realm.
1141cb0ef41Sopenharmony_ci  inline int64_t base_object_created_after_bootstrap() const;
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci#define V(PropertyName, TypeName)                                              \
1171cb0ef41Sopenharmony_ci  inline v8::Local<TypeName> PropertyName() const;                             \
1181cb0ef41Sopenharmony_ci  inline void set_##PropertyName(v8::Local<TypeName> value);
1191cb0ef41Sopenharmony_ci  PER_REALM_STRONG_PERSISTENT_VALUES(V)
1201cb0ef41Sopenharmony_ci#undef V
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  std::set<struct node_module*> internal_bindings;
1231cb0ef41Sopenharmony_ci  std::set<std::string> builtins_with_cache;
1241cb0ef41Sopenharmony_ci  std::set<std::string> builtins_without_cache;
1251cb0ef41Sopenharmony_ci  // This is only filled during deserialization. We use a vector since
1261cb0ef41Sopenharmony_ci  // it's only used for tests.
1271cb0ef41Sopenharmony_ci  std::vector<std::string> builtins_in_snapshot;
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci private:
1301cb0ef41Sopenharmony_ci  void InitializeContext(v8::Local<v8::Context> context,
1311cb0ef41Sopenharmony_ci                         const RealmSerializeInfo* realm_info);
1321cb0ef41Sopenharmony_ci  void DoneBootstrapping();
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  Environment* env_;
1351cb0ef41Sopenharmony_ci  // Shorthand for isolate pointer.
1361cb0ef41Sopenharmony_ci  v8::Isolate* isolate_;
1371cb0ef41Sopenharmony_ci  v8::Global<v8::Context> context_;
1381cb0ef41Sopenharmony_ci  bool has_run_bootstrapping_code_ = false;
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  int64_t base_object_count_ = 0;
1411cb0ef41Sopenharmony_ci  int64_t base_object_created_by_bootstrap_ = 0;
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  BindingDataStore binding_data_store_;
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  CleanupQueue cleanup_queue_;
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci#define V(PropertyName, TypeName) v8::Global<TypeName> PropertyName##_;
1481cb0ef41Sopenharmony_ci  PER_REALM_STRONG_PERSISTENT_VALUES(V)
1491cb0ef41Sopenharmony_ci#undef V
1501cb0ef41Sopenharmony_ci};
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci}  // namespace node
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci#endif  // SRC_NODE_REALM_H_
157