Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/node_realm-inl.h
1 #ifndef SRC_NODE_REALM_INL_H_ 2 #define SRC_NODE_REALM_INL_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "node_context_data.h" 7 #include "node_realm.h" 8 #include "util-inl.h" 9 10 namespace node { 11 12 inline Realm* Realm::GetCurrent(v8::Isolate* isolate) { 13 if (!isolate->InContext()) [[unlikely]] { 14 return nullptr; 15 } 16 v8::HandleScope handle_scope(isolate); 17 return GetCurrent(isolate->GetCurrentContext()); 18 } 19 20 inline Realm* Realm::GetCurrent(v8::Local<v8::Context> context) { 21 if (!ContextEmbedderTag::IsNodeContext(context)) [[unlikely]] { 22 return nullptr; 23 } 24 return static_cast<Realm*>( 25 context->GetAlignedPointerFromEmbedderData(ContextEmbedderIndex::kRealm)); 26 } 27 28 inline Realm* Realm::GetCurrent( 29 const v8::FunctionCallbackInfo<v8::Value>& info) { 30 return GetCurrent(info.GetIsolate()->GetCurrentContext()); 31 } 32 33 template <typename T> 34 inline Realm* Realm::GetCurrent(const v8::PropertyCallbackInfo<T>& info) { 35 return GetCurrent(info.GetIsolate()->GetCurrentContext()); 36 } 37 38 inline IsolateData* Realm::isolate_data() const { 39 return env_->isolate_data(); 40 } 41 42 inline Environment* Realm::env() const { 43 return env_; 44 } 45 46 inline v8::Isolate* Realm::isolate() const { 47 return isolate_; 48 } 49 50 inline v8::Local<v8::Context> Realm::context() const { 51 return PersistentToLocal::Strong(context_); 52 } 53 54 inline Realm::Kind Realm::kind() const { 55 return kind_; 56 } 57 58 inline bool Realm::has_run_bootstrapping_code() const { 59 return has_run_bootstrapping_code_; 60 } 61 62 // static 63 template <typename T, typename U> 64 inline T* Realm::GetBindingData(const v8::PropertyCallbackInfo<U>& info) { 65 return GetBindingData<T>(info.GetIsolate()->GetCurrentContext()); 66 } 67 68 // static 69 template <typename T> 70 inline T* Realm::GetBindingData( 71 const v8::FunctionCallbackInfo<v8::Value>& info) { 72 return GetBindingData<T>(info.GetIsolate()->GetCurrentContext()); 73 } 74 75 // static 76 template <typename T> 77 inline T* Realm::GetBindingData(v8::Local<v8::Context> context) { 78 Realm* realm = GetCurrent(context); 79 return realm->GetBindingData<T>(); 80 } 81 82 template <typename T> 83 inline T* Realm::GetBindingData() { 84 constexpr size_t binding_index = static_cast<size_t>(T::binding_type_int); 85 static_assert(binding_index < std::tuple_size_v<BindingDataStore>); 86 auto ptr = binding_data_store_[binding_index]; 87 if (!ptr) [[unlikely]] { 88 return nullptr; 89 } 90 T* result = static_cast<T*>(ptr.get()); 91 DCHECK_NOT_NULL(result); 92 return result; 93 } 94 95 template <typename T, typename... Args> 96 inline T* Realm::AddBindingData(v8::Local<v8::Object> target, Args&&... args) { 97 // This won't compile if T is not a BaseObject subclass. 98 static_assert(std::is_base_of_v<BaseObject, T>); 99 // The binding data must be weak so that it won't keep the realm reachable 100 // from strong GC roots indefinitely. The wrapper object of binding data 101 // should be referenced from JavaScript, thus the binding data should be 102 // reachable throughout the lifetime of the realm. 103 BaseObjectWeakPtr<T> item = 104 MakeWeakBaseObject<T>(this, target, std::forward<Args>(args)...); 105 constexpr size_t binding_index = static_cast<size_t>(T::binding_type_int); 106 static_assert(binding_index < std::tuple_size_v<BindingDataStore>); 107 // Each slot is expected to be assigned only once. 108 CHECK(!binding_data_store_[binding_index]); 109 binding_data_store_[binding_index] = item; 110 return item.get(); 111 } 112 113 inline BindingDataStore* Realm::binding_data_store() { 114 return &binding_data_store_; 115 } 116 117 template <typename T> 118 void Realm::ForEachBaseObject(T&& iterator) const { 119 for (auto bo : base_object_list_) { 120 iterator(bo); 121 } 122 } 123 124 int64_t Realm::base_object_created_after_bootstrap() const { 125 return base_object_count_ - base_object_created_by_bootstrap_; 126 } 127 128 int64_t Realm::base_object_count() const { 129 return base_object_count_; 130 } 131 132 void Realm::TrackBaseObject(BaseObject* bo) { 133 DCHECK_EQ(bo->realm(), this); 134 base_object_list_.PushBack(bo); 135 ++base_object_count_; 136 } 137 138 void Realm::UntrackBaseObject(BaseObject* bo) { 139 DCHECK_EQ(bo->realm(), this); 140 --base_object_count_; 141 } 142 143 bool Realm::PendingCleanup() const { 144 return !base_object_list_.IsEmpty(); 145 } 146 147 } // namespace node 148 149 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 150 151 #endif // SRC_NODE_REALM_INL_H_