Skip to content

Commit 4a58725

Browse files
committed
src: add iteration over all base objects to Environment
PR-URL: nodejs#21741 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent e75163f commit 4a58725

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/env-inl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,22 @@ bool Environment::CleanupHookCallback::Equal::operator()(
809809
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
810810
}
811811

812+
BaseObject* Environment::CleanupHookCallback::GetBaseObject() const {
813+
if (fn_ == BaseObject::DeleteMe)
814+
return static_cast<BaseObject*>(arg_);
815+
else
816+
return nullptr;
817+
}
818+
819+
template <typename T>
820+
void Environment::ForEachBaseObject(T&& iterator) {
821+
for (const auto& hook : cleanup_hooks_) {
822+
BaseObject* obj = hook.GetBaseObject();
823+
if (obj != nullptr)
824+
iterator(obj);
825+
}
826+
}
827+
812828
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
813829
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
814830
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)

src/env.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,8 @@ class Environment {
981981
inline bool operator()(const CleanupHookCallback& a,
982982
const CleanupHookCallback& b) const;
983983
};
984+
985+
inline BaseObject* GetBaseObject() const;
984986
};
985987

986988
// Use an unordered_set, so that we have efficient insertion and removal.
@@ -993,6 +995,9 @@ class Environment {
993995
v8::Local<v8::Promise> promise,
994996
v8::Local<v8::Value> parent);
995997

998+
template <typename T>
999+
void ForEachBaseObject(T&& iterator);
1000+
9961001
#define V(PropertyName, TypeName) Persistent<TypeName> PropertyName ## _;
9971002
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
9981003
#undef V

0 commit comments

Comments
 (0)