summaryrefslogtreecommitdiff
path: root/src/env-inl.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/env-inl.h')
-rw-r--r--src/env-inl.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/env-inl.h b/src/env-inl.h
index f88e764815..e0345b0daf 100644
--- a/src/env-inl.h
+++ b/src/env-inl.h
@@ -95,6 +95,20 @@ v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
return PersistentToLocal::Strong(native_execution_async_resources_[i]);
}
+inline void AsyncHooks::SetJSPromiseHooks(v8::Local<v8::Function> init,
+ v8::Local<v8::Function> before,
+ v8::Local<v8::Function> after,
+ v8::Local<v8::Function> resolve) {
+ js_promise_hooks_[0].Reset(env()->isolate(), init);
+ js_promise_hooks_[1].Reset(env()->isolate(), before);
+ js_promise_hooks_[2].Reset(env()->isolate(), after);
+ js_promise_hooks_[3].Reset(env()->isolate(), resolve);
+ for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
+ PersistentToLocal::Weak(env()->isolate(), *it)
+ ->SetPromiseHooks(init, before, after, resolve);
+ }
+}
+
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
return env()->isolate_data()->async_wrap_provider(idx);
}
@@ -217,6 +231,41 @@ void AsyncHooks::clear_async_id_stack() {
fields_[kStackLength] = 0;
}
+inline void AsyncHooks::AddContext(v8::Local<v8::Context> ctx) {
+ ctx->SetPromiseHooks(
+ js_promise_hooks_[0].IsEmpty() ?
+ v8::Local<v8::Function>() :
+ PersistentToLocal::Strong(js_promise_hooks_[0]),
+ js_promise_hooks_[1].IsEmpty() ?
+ v8::Local<v8::Function>() :
+ PersistentToLocal::Strong(js_promise_hooks_[1]),
+ js_promise_hooks_[2].IsEmpty() ?
+ v8::Local<v8::Function>() :
+ PersistentToLocal::Strong(js_promise_hooks_[2]),
+ js_promise_hooks_[3].IsEmpty() ?
+ v8::Local<v8::Function>() :
+ PersistentToLocal::Strong(js_promise_hooks_[3]));
+
+ size_t id = contexts_.size();
+ contexts_.resize(id + 1);
+ contexts_[id].Reset(env()->isolate(), ctx);
+ contexts_[id].SetWeak();
+}
+
+inline void AsyncHooks::RemoveContext(v8::Local<v8::Context> ctx) {
+ v8::Isolate* isolate = env()->isolate();
+ v8::HandleScope handle_scope(isolate);
+ for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
+ v8::Local<v8::Context> saved_context =
+ PersistentToLocal::Weak(env()->isolate(), *it);
+ if (saved_context == ctx) {
+ it->Reset();
+ contexts_.erase(it);
+ break;
+ }
+ }
+}
+
// The DefaultTriggerAsyncIdScope(AsyncWrap*) constructor is defined in
// async_wrap-inl.h to avoid a circular dependency.
@@ -304,6 +353,8 @@ inline void Environment::AssignToContext(v8::Local<v8::Context> context,
#if HAVE_INSPECTOR
inspector_agent()->ContextCreated(context, info);
#endif // HAVE_INSPECTOR
+
+ this->async_hooks()->AddContext(context);
}
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {