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(QUIC_LOGSTREAM) \ 64 V(QUIC_PACKET) \ 65 V(SHUTDOWNWRAP) \ 66 V(SIGNALWRAP) \ 67 V(STATWATCHER) \ 68 V(STREAMPIPE) \ 69 V(TCPCONNECTWRAP) \ 70 V(TCPSERVERWRAP) \ 71 V(TCPWRAP) \ 72 V(TTYWRAP) \ 73 V(UDPSENDWRAP) \ 74 V(UDPWRAP) \ 75 V(SIGINTWATCHDOG) \ 76 V(WORKER) \ 77 V(WORKERHEAPSNAPSHOT) \ 78 V(WRITEWRAP) \ 79 V(ZLIB) 80 81 #if HAVE_OPENSSL 82 #define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \ 83 V(CHECKPRIMEREQUEST) \ 84 V(PBKDF2REQUEST) \ 85 V(KEYPAIRGENREQUEST) \ 86 V(KEYGENREQUEST) \ 87 V(KEYEXPORTREQUEST) \ 88 V(CIPHERREQUEST) \ 89 V(DERIVEBITSREQUEST) \ 90 V(HASHREQUEST) \ 91 V(RANDOMBYTESREQUEST) \ 92 V(RANDOMPRIMEREQUEST) \ 93 V(SCRYPTREQUEST) \ 94 V(SIGNREQUEST) \ 95 V(TLSWRAP) \ 96 V(VERIFYREQUEST) 97 #else 98 #define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) 99 #endif // HAVE_OPENSSL 100 101 #if HAVE_INSPECTOR 102 #define NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V) \ 103 V(INSPECTORJSBINDING) 104 #else 105 #define NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V) 106 #endif // HAVE_INSPECTOR 107 108 #define NODE_ASYNC_PROVIDER_TYPES(V) \ 109 NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \ 110 NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \ 111 NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V) 112 113 class Environment; 114 class DestroyParam; 115 class ExternalReferenceRegistry; 116 117 class AsyncWrap : public BaseObject { 118 public: 119 enum ProviderType { 120 #define V(PROVIDER) \ 121 PROVIDER_ ## PROVIDER, 122 NODE_ASYNC_PROVIDER_TYPES(V) 123 #undef V 124 PROVIDERS_LENGTH, 125 }; 126 127 AsyncWrap(Environment* env, 128 v8::Local<v8::Object> object, 129 ProviderType provider, 130 double execution_async_id = kInvalidAsyncId); 131 132 // This constructor creates a reusable instance where user is responsible 133 // to call set_provider_type() and AsyncReset() before use. 134 AsyncWrap(Environment* env, v8::Local<v8::Object> object); 135 136 ~AsyncWrap() override; 137 138 AsyncWrap() = delete; 139 140 static constexpr double kInvalidAsyncId = -1; 141 142 static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( 143 Environment* env); 144 145 static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 146 static void Initialize(v8::Local<v8::Object> target, 147 v8::Local<v8::Value> unused, 148 v8::Local<v8::Context> context, 149 void* priv); 150 151 static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args); 152 static void PushAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args); 153 static void PopAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args); 154 static void ExecutionAsyncResource( 155 const v8::FunctionCallbackInfo<v8::Value>& args); 156 static void ClearAsyncIdStack( 157 const v8::FunctionCallbackInfo<v8::Value>& args); 158 static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args); 159 static void GetProviderType(const v8::FunctionCallbackInfo<v8::Value>& args); 160 static void QueueDestroyAsyncId( 161 const v8::FunctionCallbackInfo<v8::Value>& args); 162 static void SetCallbackTrampoline( 163 const v8::FunctionCallbackInfo<v8::Value>& args); 164 165 static void EmitAsyncInit(Environment* env, 166 v8::Local<v8::Object> object, 167 v8::Local<v8::String> type, 168 double async_id, 169 double trigger_async_id); 170 171 static void EmitDestroy(Environment* env, double async_id); 172 static void EmitBefore(Environment* env, double async_id); 173 static void EmitAfter(Environment* env, double async_id); 174 static void EmitPromiseResolve(Environment* env, double async_id); 175 176 void EmitDestroy(bool from_gc = false); 177 178 void EmitTraceEventBefore(); 179 static void EmitTraceEventAfter(ProviderType type, double async_id); 180 void EmitTraceEventDestroy(); 181 182 static void DestroyAsyncIdsCallback(Environment* env); 183 184 inline ProviderType provider_type() const; 185 inline ProviderType set_provider_type(ProviderType provider); 186 187 inline double get_async_id() const; 188 inline double get_trigger_async_id() const; 189 190 void AsyncReset(v8::Local<v8::Object> resource, 191 double execution_async_id = kInvalidAsyncId, 192 bool silent = false); 193 194 // Only call these within a valid HandleScope. 195 v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb, 196 int argc, 197 v8::Local<v8::Value>* argv); 198 inline v8::MaybeLocal<v8::Value> MakeCallback( 199 const v8::Local<v8::Symbol> symbol, 200 int argc, 201 v8::Local<v8::Value>* argv); 202 inline v8::MaybeLocal<v8::Value> MakeCallback( 203 const v8::Local<v8::String> symbol, 204 int argc, 205 v8::Local<v8::Value>* argv); 206 inline v8::MaybeLocal<v8::Value> MakeCallback( 207 const v8::Local<v8::Name> symbol, 208 int argc, 209 v8::Local<v8::Value>* argv); 210 211 virtual std::string diagnostic_name() const; 212 const char* MemoryInfoName() const override; 213 214 static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info); 215 216 // Returns the object that 'owns' an async wrap. For example, for a 217 // TCP connection handle, this is the corresponding net.Socket. 218 v8::Local<v8::Object> GetOwner(); 219 static v8::Local<v8::Object> GetOwner(Environment* env, 220 v8::Local<v8::Object> obj); 221 222 bool IsDoneInitializing() const override; 223 224 private: 225 friend class PromiseWrap; 226 227 AsyncWrap(Environment* env, 228 v8::Local<v8::Object> promise, 229 ProviderType provider, 230 double execution_async_id, 231 bool silent); 232 AsyncWrap(Environment* env, 233 v8::Local<v8::Object> promise, 234 ProviderType provider, 235 double execution_async_id, 236 double trigger_async_id); 237 ProviderType provider_type_ = PROVIDER_NONE; 238 bool init_hook_ran_ = false; 239 // Because the values may be Reset(), cannot be made const. 240 double async_id_ = kInvalidAsyncId; 241 double trigger_async_id_ = kInvalidAsyncId; 242 }; 243 244 } // namespace node 245 246 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 247 248 #endif // SRC_ASYNC_WRAP_H_ 249