#ifndef SRC_NODE_REALM_INL_H_ #define SRC_NODE_REALM_INL_H_ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include "cleanup_queue-inl.h" #include "node_realm.h" namespace node { inline Realm* Realm::GetCurrent(v8::Isolate* isolate) { if (UNLIKELY(!isolate->InContext())) return nullptr; v8::HandleScope handle_scope(isolate); return GetCurrent(isolate->GetCurrentContext()); } inline Realm* Realm::GetCurrent(v8::Local context) { if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) return nullptr; return static_cast( context->GetAlignedPointerFromEmbedderData(ContextEmbedderIndex::kRealm)); } inline Realm* Realm::GetCurrent( const v8::FunctionCallbackInfo& info) { return GetCurrent(info.GetIsolate()->GetCurrentContext()); } template inline Realm* Realm::GetCurrent(const v8::PropertyCallbackInfo& info) { return GetCurrent(info.GetIsolate()->GetCurrentContext()); } inline IsolateData* Realm::isolate_data() const { return env_->isolate_data(); } inline Environment* Realm::env() const { return env_; } inline v8::Isolate* Realm::isolate() const { return isolate_; } inline Realm::Kind Realm::kind() const { return kind_; } inline bool Realm::has_run_bootstrapping_code() const { return has_run_bootstrapping_code_; } // static template inline T* Realm::GetBindingData(const v8::PropertyCallbackInfo& info) { return GetBindingData(info.GetIsolate()->GetCurrentContext()); } // static template inline T* Realm::GetBindingData( const v8::FunctionCallbackInfo& info) { return GetBindingData(info.GetIsolate()->GetCurrentContext()); } // static template inline T* Realm::GetBindingData(v8::Local context) { BindingDataStore* map = static_cast(context->GetAlignedPointerFromEmbedderData( ContextEmbedderIndex::kBindingDataStoreIndex)); DCHECK_NOT_NULL(map); constexpr size_t binding_index = static_cast(T::binding_type_int); static_assert(binding_index < std::tuple_size_v); auto ptr = (*map)[binding_index]; if (UNLIKELY(!ptr)) return nullptr; T* result = static_cast(ptr.get()); DCHECK_NOT_NULL(result); DCHECK_EQ(result->realm(), GetCurrent(context)); return result; } template inline T* Realm::AddBindingData(v8::Local context, v8::Local target, Args&&... args) { DCHECK_EQ(GetCurrent(context), this); // This won't compile if T is not a BaseObject subclass. BaseObjectPtr item = MakeDetachedBaseObject(this, target, std::forward(args)...); BindingDataStore* map = static_cast(context->GetAlignedPointerFromEmbedderData( ContextEmbedderIndex::kBindingDataStoreIndex)); DCHECK_NOT_NULL(map); constexpr size_t binding_index = static_cast(T::binding_type_int); static_assert(binding_index < std::tuple_size_v); CHECK(!(*map)[binding_index]); // Should not insert the binding twice. (*map)[binding_index] = item; DCHECK_EQ(GetBindingData(context), item.get()); return item.get(); } inline BindingDataStore* Realm::binding_data_store() { return &binding_data_store_; } template void Realm::ForEachBaseObject(T&& iterator) const { cleanup_queue_.ForEachBaseObject(std::forward(iterator)); } void Realm::modify_base_object_count(int64_t delta) { base_object_count_ += delta; } int64_t Realm::base_object_created_after_bootstrap() const { return base_object_count_ - base_object_created_by_bootstrap_; } int64_t Realm::base_object_count() const { return base_object_count_; } void Realm::AddCleanupHook(CleanupQueue::Callback fn, void* arg) { cleanup_queue_.Add(fn, arg); } void Realm::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) { cleanup_queue_.Remove(fn, arg); } bool Realm::HasCleanupHooks() const { return !cleanup_queue_.empty(); } } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #endif // SRC_NODE_REALM_INL_H_