File tree 4 files changed +46
-0
lines changed
4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -188,4 +188,20 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
188
188
at_exit_functions_.push_back (AtExitCallback{cb, arg});
189
189
}
190
190
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
+
191
207
} // namespace node
Original file line number Diff line number Diff line change 35
35
#include " util.h"
36
36
#include " uv.h"
37
37
#include " v8.h"
38
+ #include " node.h"
38
39
39
40
#include < list>
40
41
#include < stdint.h>
@@ -572,6 +573,8 @@ class Environment {
572
573
573
574
static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
574
575
576
+ void AddPromiseHook (promise_hook_func fn, void * arg);
577
+
575
578
private:
576
579
inline void ThrowError (v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
577
580
const char* errmsg);
@@ -620,6 +623,16 @@ class Environment {
620
623
};
621
624
std::list<AtExitCallback> at_exit_functions_;
622
625
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
+
623
636
#define V (PropertyName, TypeName ) \
624
637
v8::Persistent<TypeName> PropertyName ## _;
625
638
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES (V)
Original file line number Diff line number Diff line change @@ -1233,6 +1233,12 @@ void SetupPromises(const FunctionCallbackInfo<Value>& args) {
1233
1233
} // anonymous namespace
1234
1234
1235
1235
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
+
1236
1242
Local<Value> MakeCallback (Environment* env,
1237
1243
Local<Value> recv,
1238
1244
const Local<Function> callback,
Original file line number Diff line number Diff line change @@ -516,6 +516,17 @@ NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
516
516
*/
517
517
NODE_EXTERN void AtExit (Environment* env, void (*cb)(void * arg), void* arg = 0);
518
518
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
+
519
530
} // namespace node
520
531
521
532
#endif // SRC_NODE_H_
You can’t perform that action at this time.
0 commit comments