Skip to content

Commit e5a25cb

Browse files
committed
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 <[email protected]> Reviewed-By: Julien Gilli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dca0815 commit e5a25cb

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/env.cc

+16
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,20 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
188188
at_exit_functions_.push_back(AtExitCallback{cb, arg});
189189
}
190190

191+
void Environment::AddPromiseHook(promise_hook_func fn, void* arg) {
192+
promise_hooks_.push_back(PromiseHookCallback{fn, arg});
193+
if (promise_hooks_.size() == 1) {
194+
isolate_->SetPromiseHook(EnvPromiseHook);
195+
}
196+
}
197+
198+
void Environment::EnvPromiseHook(v8::PromiseHookType type,
199+
v8::Local<v8::Promise> promise,
200+
v8::Local<v8::Value> parent) {
201+
Environment* env = Environment::GetCurrent(promise->CreationContext());
202+
for (const PromiseHookCallback& hook : env->promise_hooks_) {
203+
hook.cb_(type, promise, parent, hook.arg_);
204+
}
205+
}
206+
191207
} // namespace node

src/env.h

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "util.h"
3636
#include "uv.h"
3737
#include "v8.h"
38+
#include "node.h"
3839

3940
#include <list>
4041
#include <stdint.h>
@@ -572,6 +573,8 @@ class Environment {
572573

573574
static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
574575

576+
void AddPromiseHook(promise_hook_func fn, void* arg);
577+
575578
private:
576579
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
577580
const char* errmsg);
@@ -620,6 +623,16 @@ class Environment {
620623
};
621624
std::list<AtExitCallback> at_exit_functions_;
622625

626+
struct PromiseHookCallback {
627+
promise_hook_func cb_;
628+
void* arg_;
629+
};
630+
std::vector<PromiseHookCallback> promise_hooks_;
631+
632+
static void EnvPromiseHook(v8::PromiseHookType type,
633+
v8::Local<v8::Promise> promise,
634+
v8::Local<v8::Value> parent);
635+
623636
#define V(PropertyName, TypeName) \
624637
v8::Persistent<TypeName> PropertyName ## _;
625638
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)

src/node.cc

+6
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,12 @@ void SetupPromises(const FunctionCallbackInfo<Value>& args) {
12331233
} // anonymous namespace
12341234

12351235

1236+
void AddPromiseHook(v8::Isolate* isolate, promise_hook_func fn, void* arg) {
1237+
Environment* env = Environment::GetCurrent(isolate);
1238+
env->AddPromiseHook(fn, arg);
1239+
}
1240+
1241+
12361242
Local<Value> MakeCallback(Environment* env,
12371243
Local<Value> recv,
12381244
const Local<Function> callback,

src/node.h

+11
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,17 @@ NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
516516
*/
517517
NODE_EXTERN void AtExit(Environment* env, void (*cb)(void* arg), void* arg = 0);
518518

519+
typedef void (*promise_hook_func) (v8::PromiseHookType type,
520+
v8::Local<v8::Promise> promise,
521+
v8::Local<v8::Value> parent,
522+
void* arg);
523+
524+
/* Registers an additional v8::PromiseHook wrapper. This API exists because V8
525+
* itself supports only a single PromiseHook. */
526+
NODE_EXTERN void AddPromiseHook(v8::Isolate* isolate,
527+
promise_hook_func fn,
528+
void* arg);
529+
519530
} // namespace node
520531

521532
#endif // SRC_NODE_H_

0 commit comments

Comments
 (0)