From d42dbde1a8f1e373ea9ed24cf7e0addadac58af3 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 10 Jun 2018 15:57:46 +0200 Subject: [PATCH] src: add iteration over all base objects to Environment PR-URL: https://github.com/nodejs/node/pull/21741 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Refael Ackermann --- src/env-inl.h | 16 ++++++++++++++++ src/env.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/src/env-inl.h b/src/env-inl.h index b436b23b678015..4ef9665cbddfed 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -809,6 +809,22 @@ bool Environment::CleanupHookCallback::Equal::operator()( return a.fn_ == b.fn_ && a.arg_ == b.arg_; } +BaseObject* Environment::CleanupHookCallback::GetBaseObject() const { + if (fn_ == BaseObject::DeleteMe) + return static_cast(arg_); + else + return nullptr; +} + +template +void Environment::ForEachBaseObject(T&& iterator) { + for (const auto& hook : cleanup_hooks_) { + BaseObject* obj = hook.GetBaseObject(); + if (obj != nullptr) + iterator(obj); + } +} + #define VP(PropertyName, StringValue) V(v8::Private, PropertyName) #define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName) #define VS(PropertyName, StringValue) V(v8::String, PropertyName) diff --git a/src/env.h b/src/env.h index ea50d019c2327f..3f4ea83b5611fd 100644 --- a/src/env.h +++ b/src/env.h @@ -976,6 +976,8 @@ class Environment { inline bool operator()(const CleanupHookCallback& a, const CleanupHookCallback& b) const; }; + + inline BaseObject* GetBaseObject() const; }; // Use an unordered_set, so that we have efficient insertion and removal. @@ -988,6 +990,9 @@ class Environment { v8::Local promise, v8::Local parent); + template + void ForEachBaseObject(T&& iterator); + #define V(PropertyName, TypeName) Persistent PropertyName ## _; ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) #undef V