Skip to content

Commit

Permalink
src: add iteration over all base objects to Environment
Browse files Browse the repository at this point in the history
PR-URL: #21741
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
addaleax authored and targos committed Jul 16, 2018
1 parent 0298299 commit d42dbde
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseObject*>(arg_);
else
return nullptr;
}

template <typename T>
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)
Expand Down
5 changes: 5 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -988,6 +990,9 @@ class Environment {
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent);

template <typename T>
void ForEachBaseObject(T&& iterator);

#define V(PropertyName, TypeName) Persistent<TypeName> PropertyName ## _;
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
#undef V
Expand Down

0 comments on commit d42dbde

Please sign in to comment.