mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
async_wrap: expose enable/disablePromiseHook API
Allow node::PromiseHook (src/async-wrap.cc) to be enabled/disabled from the JavaScript API. PR-URL: https://github.com/nodejs/node/pull/13509 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
b9379095e1
commit
cc2dd93ea5
@ -422,6 +422,18 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
|
||||
|
||||
static void EnablePromiseHook(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
env->AddPromiseHook(PromiseHook, nullptr);
|
||||
}
|
||||
|
||||
|
||||
static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
env->RemovePromiseHook(PromiseHook, nullptr);
|
||||
}
|
||||
|
||||
|
||||
void AsyncWrap::GetAsyncId(const FunctionCallbackInfo<Value>& args) {
|
||||
AsyncWrap* wrap;
|
||||
args.GetReturnValue().Set(-1);
|
||||
@ -478,6 +490,8 @@ void AsyncWrap::Initialize(Local<Object> target,
|
||||
env->SetMethod(target, "popAsyncIds", PopAsyncIds);
|
||||
env->SetMethod(target, "clearIdStack", ClearIdStack);
|
||||
env->SetMethod(target, "addIdToDestroyList", QueueDestroyId);
|
||||
env->SetMethod(target, "enablePromiseHook", EnablePromiseHook);
|
||||
env->SetMethod(target, "disablePromiseHook", DisablePromiseHook);
|
||||
|
||||
v8::PropertyAttribute ReadOnlyDontDelete =
|
||||
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
|
||||
|
25
src/env.cc
25
src/env.cc
@ -11,6 +11,7 @@
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace node {
|
||||
|
||||
@ -178,12 +179,36 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
|
||||
}
|
||||
|
||||
void Environment::AddPromiseHook(promise_hook_func fn, void* arg) {
|
||||
auto it = std::find_if(
|
||||
promise_hooks_.begin(), promise_hooks_.end(),
|
||||
[&](const PromiseHookCallback& hook) {
|
||||
return hook.cb_ == fn && hook.arg_ == arg;
|
||||
});
|
||||
CHECK_EQ(it, promise_hooks_.end());
|
||||
promise_hooks_.push_back(PromiseHookCallback{fn, arg});
|
||||
|
||||
if (promise_hooks_.size() == 1) {
|
||||
isolate_->SetPromiseHook(EnvPromiseHook);
|
||||
}
|
||||
}
|
||||
|
||||
bool Environment::RemovePromiseHook(promise_hook_func fn, void* arg) {
|
||||
auto it = std::find_if(
|
||||
promise_hooks_.begin(), promise_hooks_.end(),
|
||||
[&](const PromiseHookCallback& hook) {
|
||||
return hook.cb_ == fn && hook.arg_ == arg;
|
||||
});
|
||||
|
||||
if (it == promise_hooks_.end()) return false;
|
||||
|
||||
promise_hooks_.erase(it);
|
||||
if (promise_hooks_.empty()) {
|
||||
isolate_->SetPromiseHook(nullptr);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Environment::EnvPromiseHook(v8::PromiseHookType type,
|
||||
v8::Local<v8::Promise> promise,
|
||||
v8::Local<v8::Value> parent) {
|
||||
|
@ -667,6 +667,7 @@ class Environment {
|
||||
static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
|
||||
|
||||
void AddPromiseHook(promise_hook_func fn, void* arg);
|
||||
bool RemovePromiseHook(promise_hook_func fn, void* arg);
|
||||
|
||||
private:
|
||||
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
|
||||
|
Loading…
Reference in New Issue
Block a user