Permalink
Browse files

src: expose `node::AddPromiseHook`

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 21, 2017
1 parent dca0815 commit e5a25cbc8524f0d57c66de85eeb4b1b4eacd6da8
Showing with 46 additions and 0 deletions.
  1. +16 −0 src/env.cc
  2. +13 −0 src/env.h
  3. +6 −0 src/node.cc
  4. +11 −0 src/node.h
View
@@ -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
View
@@ -35,6 +35,7 @@
#include "util.h"
#include "uv.h"
#include "v8.h"
#include "node.h"
#include <list>
#include <stdint.h>
@@ -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);
@@ -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)
View
@@ -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,
View
@@ -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.