• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_ASYNC_HOOKS_WRAPPER_H_
6 #define V8_ASYNC_HOOKS_WRAPPER_H_
7 
8 #include <stack>
9 
10 #include "include/v8.h"
11 #include "src/objects.h"
12 
13 namespace v8 {
14 
15 typedef double async_id_t;
16 
17 struct AsyncContext {
18   async_id_t execution_async_id;
19   async_id_t trigger_async_id;
20 };
21 
22 class AsyncHooksWrap {
23  public:
AsyncHooksWrap(Isolate * isolate)24   explicit AsyncHooksWrap(Isolate* isolate) {
25     enabled_ = false;
26     isolate_ = isolate;
27   }
28   void Enable();
29   void Disable();
IsEnabled()30   bool IsEnabled() const { return enabled_; }
31 
32   inline v8::Local<v8::Function> init_function() const;
33   inline void set_init_function(v8::Local<v8::Function> value);
34   inline v8::Local<v8::Function> before_function() const;
35   inline void set_before_function(v8::Local<v8::Function> value);
36   inline v8::Local<v8::Function> after_function() const;
37   inline void set_after_function(v8::Local<v8::Function> value);
38   inline v8::Local<v8::Function> promiseResolve_function() const;
39   inline void set_promiseResolve_function(v8::Local<v8::Function> value);
40 
41  private:
42   Isolate* isolate_;
43 
44   Persistent<v8::Function> init_function_;
45   Persistent<v8::Function> before_function_;
46   Persistent<v8::Function> after_function_;
47   Persistent<v8::Function> promiseResolve_function_;
48 
49   bool enabled_;
50 };
51 
52 class AsyncHooks {
53  public:
AsyncHooks(Isolate * isolate)54   explicit AsyncHooks(Isolate* isolate) {
55     isolate_ = isolate;
56 
57     AsyncContext ctx;
58     ctx.execution_async_id = 1;
59     ctx.trigger_async_id = 0;
60     asyncContexts.push(ctx);
61     current_async_id = 1;
62 
63     Initialize();
64   }
~AsyncHooks()65   ~AsyncHooks() { Deinitialize(); }
66 
67   async_id_t GetExecutionAsyncId() const;
68   async_id_t GetTriggerAsyncId() const;
69 
70   Local<Object> CreateHook(const v8::FunctionCallbackInfo<v8::Value>& args);
71 
72   Persistent<FunctionTemplate> async_hook_ctor;
73 
74  private:
75   std::vector<AsyncHooksWrap*> async_wraps_;
76   Isolate* isolate_;
77   Persistent<ObjectTemplate> async_hooks_templ;
78   Persistent<Private> async_id_smb;
79   Persistent<Private> trigger_id_smb;
80 
81   void Initialize();
82   void Deinitialize();
83 
84   static void ShellPromiseHook(PromiseHookType type, Local<Promise> promise,
85                                Local<Value> parent);
86   static void PromiseHookDispatch(PromiseHookType type, Local<Promise> promise,
87                                   Local<Value> parent, AsyncHooksWrap* wrap,
88                                   AsyncHooks* hooks);
89 
90   std::stack<AsyncContext> asyncContexts;
91   async_id_t current_async_id;
92 };
93 
94 }  // namespace v8
95 
96 #endif  // V8_ASYNC_HOOKS_WRAPPER_H_
97