• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef SRC_ASYNC_WRAP_H_
23 #define SRC_ASYNC_WRAP_H_
24 
25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26 
27 #include "base_object.h"
28 #include "v8.h"
29 
30 #include <cstdint>
31 
32 namespace node {
33 
34 #define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V)                               \
35   V(NONE)                                                                     \
36   V(DIRHANDLE)                                                                \
37   V(DNSCHANNEL)                                                               \
38   V(ELDHISTOGRAM)                                                             \
39   V(FILEHANDLE)                                                               \
40   V(FILEHANDLECLOSEREQ)                                                       \
41   V(FIXEDSIZEBLOBCOPY)                                                        \
42   V(FSEVENTWRAP)                                                              \
43   V(FSREQCALLBACK)                                                            \
44   V(FSREQPROMISE)                                                             \
45   V(GETADDRINFOREQWRAP)                                                       \
46   V(GETNAMEINFOREQWRAP)                                                       \
47   V(HEAPSNAPSHOT)                                                             \
48   V(HTTP2SESSION)                                                             \
49   V(HTTP2STREAM)                                                              \
50   V(HTTP2PING)                                                                \
51   V(HTTP2SETTINGS)                                                            \
52   V(HTTPINCOMINGMESSAGE)                                                      \
53   V(HTTPCLIENTREQUEST)                                                        \
54   V(JSSTREAM)                                                                 \
55   V(JSUDPWRAP)                                                                \
56   V(MESSAGEPORT)                                                              \
57   V(PIPECONNECTWRAP)                                                          \
58   V(PIPESERVERWRAP)                                                           \
59   V(PIPEWRAP)                                                                 \
60   V(PROCESSWRAP)                                                              \
61   V(PROMISE)                                                                  \
62   V(QUERYWRAP)                                                                \
63   V(SHUTDOWNWRAP)                                                             \
64   V(SIGNALWRAP)                                                               \
65   V(STATWATCHER)                                                              \
66   V(STREAMPIPE)                                                               \
67   V(TCPCONNECTWRAP)                                                           \
68   V(TCPSERVERWRAP)                                                            \
69   V(TCPWRAP)                                                                  \
70   V(TTYWRAP)                                                                  \
71   V(UDPSENDWRAP)                                                              \
72   V(UDPWRAP)                                                                  \
73   V(SIGINTWATCHDOG)                                                           \
74   V(WORKER)                                                                   \
75   V(WORKERHEAPSNAPSHOT)                                                       \
76   V(WRITEWRAP)                                                                \
77   V(ZLIB)
78 
79 #if HAVE_OPENSSL
80 #define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)                                   \
81   V(PBKDF2REQUEST)                                                            \
82   V(KEYPAIRGENREQUEST)                                                        \
83   V(RANDOMBYTESREQUEST)                                                       \
84   V(SCRYPTREQUEST)                                                            \
85   V(TLSWRAP)
86 #else
87 #define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
88 #endif  // HAVE_OPENSSL
89 
90 #if HAVE_INSPECTOR
91 #define NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)                                \
92   V(INSPECTORJSBINDING)
93 #else
94 #define NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)
95 #endif  // HAVE_INSPECTOR
96 
97 #define NODE_ASYNC_PROVIDER_TYPES(V)                                          \
98   NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V)                                     \
99   NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)                                         \
100   NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)
101 
102 class Environment;
103 class DestroyParam;
104 
105 class AsyncWrap : public BaseObject {
106  public:
107   enum ProviderType {
108 #define V(PROVIDER)                                                           \
109     PROVIDER_ ## PROVIDER,
110     NODE_ASYNC_PROVIDER_TYPES(V)
111 #undef V
112     PROVIDERS_LENGTH,
113   };
114 
115   AsyncWrap(Environment* env,
116             v8::Local<v8::Object> object,
117             ProviderType provider,
118             double execution_async_id = kInvalidAsyncId);
119 
120   // This constructor creates a reusable instance where user is responsible
121   // to call set_provider_type() and AsyncReset() before use.
122   AsyncWrap(Environment* env, v8::Local<v8::Object> object);
123 
124   ~AsyncWrap() override;
125 
126   AsyncWrap() = delete;
127 
128   static constexpr double kInvalidAsyncId = -1;
129 
130   static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
131       Environment* env);
132 
133   static void Initialize(v8::Local<v8::Object> target,
134                          v8::Local<v8::Value> unused,
135                          v8::Local<v8::Context> context,
136                          void* priv);
137 
138   static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
139   static void PushAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args);
140   static void PopAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args);
141   static void ExecutionAsyncResource(
142       const v8::FunctionCallbackInfo<v8::Value>& args);
143   static void ClearAsyncIdStack(
144       const v8::FunctionCallbackInfo<v8::Value>& args);
145   static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
146   static void GetProviderType(const v8::FunctionCallbackInfo<v8::Value>& args);
147   static void QueueDestroyAsyncId(
148     const v8::FunctionCallbackInfo<v8::Value>& args);
149   static void SetCallbackTrampoline(
150     const v8::FunctionCallbackInfo<v8::Value>& args);
151 
152   static void EmitAsyncInit(Environment* env,
153                             v8::Local<v8::Object> object,
154                             v8::Local<v8::String> type,
155                             double async_id,
156                             double trigger_async_id);
157 
158   static void EmitDestroy(Environment* env, double async_id);
159   static void EmitBefore(Environment* env, double async_id);
160   static void EmitAfter(Environment* env, double async_id);
161   static void EmitPromiseResolve(Environment* env, double async_id);
162 
163   void EmitDestroy(bool from_gc = false);
164 
165   void EmitTraceEventBefore();
166   static void EmitTraceEventAfter(ProviderType type, double async_id);
167   void EmitTraceEventDestroy();
168 
169   static void DestroyAsyncIdsCallback(Environment* env);
170 
171   inline ProviderType provider_type() const;
172   inline ProviderType set_provider_type(ProviderType provider);
173 
174   inline double get_async_id() const;
175   inline double get_trigger_async_id() const;
176 
177   void AsyncReset(v8::Local<v8::Object> resource,
178                   double execution_async_id = kInvalidAsyncId,
179                   bool silent = false);
180 
181   // Only call these within a valid HandleScope.
182   v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
183                                          int argc,
184                                          v8::Local<v8::Value>* argv);
185   inline v8::MaybeLocal<v8::Value> MakeCallback(
186       const v8::Local<v8::Symbol> symbol,
187       int argc,
188       v8::Local<v8::Value>* argv);
189   inline v8::MaybeLocal<v8::Value> MakeCallback(
190       const v8::Local<v8::String> symbol,
191       int argc,
192       v8::Local<v8::Value>* argv);
193   inline v8::MaybeLocal<v8::Value> MakeCallback(
194       const v8::Local<v8::Name> symbol,
195       int argc,
196       v8::Local<v8::Value>* argv);
197 
198   virtual std::string diagnostic_name() const;
199   std::string MemoryInfoName() const override;
200 
201   static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);
202 
203   // Returns the object that 'owns' an async wrap. For example, for a
204   // TCP connection handle, this is the corresponding net.Socket.
205   v8::Local<v8::Object> GetOwner();
206   static v8::Local<v8::Object> GetOwner(Environment* env,
207                                         v8::Local<v8::Object> obj);
208 
209   bool IsDoneInitializing() const override;
210 
211  private:
212   friend class PromiseWrap;
213 
214   AsyncWrap(Environment* env,
215             v8::Local<v8::Object> promise,
216             ProviderType provider,
217             double execution_async_id,
218             bool silent);
219   AsyncWrap(Environment* env,
220             v8::Local<v8::Object> promise,
221             ProviderType provider,
222             double execution_async_id,
223             double trigger_async_id);
224   ProviderType provider_type_ = PROVIDER_NONE;
225   bool init_hook_ran_ = false;
226   // Because the values may be Reset(), cannot be made const.
227   double async_id_ = kInvalidAsyncId;
228   double trigger_async_id_;
229 };
230 
231 }  // namespace node
232 
233 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
234 
235 #endif  // SRC_ASYNC_WRAP_H_
236