Skip to content

Commit

Permalink
src: expose node::AddPromiseHook
Browse files Browse the repository at this point in the history
Expose `node::AddPromiseHook`, which wraps V8’s `SetPromiseHook` in
a way that allows multiple hooks to be set up.

PR-URL: #12489
Reviewed-By: Matthew Loring <mattloring@google.com>
Reviewed-By: Julien Gilli <jgilli@nodejs.org>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Apr 27, 2017
1 parent dca0815 commit e5a25cb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,20 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
at_exit_functions_.push_back(AtExitCallback{cb, arg});
}

void Environment::AddPromiseHook(promise_hook_func fn, void* arg) {
promise_hooks_.push_back(PromiseHookCallback{fn, arg});
if (promise_hooks_.size() == 1) {
isolate_->SetPromiseHook(EnvPromiseHook);
}
}

void Environment::EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent) {
Environment* env = Environment::GetCurrent(promise->CreationContext());
for (const PromiseHookCallback& hook : env->promise_hooks_) {
hook.cb_(type, promise, parent, hook.arg_);
}
}

} // namespace node
13 changes: 13 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "util.h"
#include "uv.h"
#include "v8.h"
#include "node.h"

#include <list>
#include <stdint.h>
Expand Down Expand Up @@ -572,6 +573,8 @@ class Environment {

static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;

void AddPromiseHook(promise_hook_func fn, void* arg);

private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);
Expand Down Expand Up @@ -620,6 +623,16 @@ class Environment {
};
std::list<AtExitCallback> at_exit_functions_;

struct PromiseHookCallback {
promise_hook_func cb_;
void* arg_;
};
std::vector<PromiseHookCallback> promise_hooks_;

static void EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent);

#define V(PropertyName, TypeName) \
v8::Persistent<TypeName> PropertyName ## _;
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
Expand Down
6 changes: 6 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,12 @@ void SetupPromises(const FunctionCallbackInfo<Value>& args) {
} // anonymous namespace


void AddPromiseHook(v8::Isolate* isolate, promise_hook_func fn, void* arg) {
Environment* env = Environment::GetCurrent(isolate);
env->AddPromiseHook(fn, arg);
}


Local<Value> MakeCallback(Environment* env,
Local<Value> recv,
const Local<Function> callback,
Expand Down
11 changes: 11 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@ NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
*/
NODE_EXTERN void AtExit(Environment* env, void (*cb)(void* arg), void* arg = 0);

typedef void (*promise_hook_func) (v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent,
void* arg);

/* Registers an additional v8::PromiseHook wrapper. This API exists because V8
* itself supports only a single PromiseHook. */
NODE_EXTERN void AddPromiseHook(v8::Isolate* isolate,
promise_hook_func fn,
void* arg);

} // namespace node

#endif // SRC_NODE_H_

0 comments on commit e5a25cb

Please sign in to comment.