Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
async-wrap.h
Go to the documentation of this file.
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 #include "node.h"
30 
31 #include <stdint.h>
32 
33 namespace node {
34 
35 #define NODE_ASYNC_ID_OFFSET 0xA1C
36 
37 #define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
38  V(NONE) \
39  V(DNSCHANNEL) \
40  V(FSEVENTWRAP) \
41  V(FSREQWRAP) \
42  V(GETADDRINFOREQWRAP) \
43  V(GETNAMEINFOREQWRAP) \
44  V(HTTP2SESSION) \
45  V(HTTP2SESSIONSHUTDOWNWRAP) \
46  V(HTTPPARSER) \
47  V(JSSTREAM) \
48  V(PIPECONNECTWRAP) \
49  V(PIPEWRAP) \
50  V(PROCESSWRAP) \
51  V(PROMISE) \
52  V(QUERYWRAP) \
53  V(SHUTDOWNWRAP) \
54  V(SIGNALWRAP) \
55  V(STATWATCHER) \
56  V(TCPCONNECTWRAP) \
57  V(TCPWRAP) \
58  V(TIMERWRAP) \
59  V(TTYWRAP) \
60  V(UDPSENDWRAP) \
61  V(UDPWRAP) \
62  V(WRITEWRAP) \
63  V(ZLIB)
64 
65 #if HAVE_OPENSSL
66 #define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \
67  V(SSLCONNECTION) \
68  V(PBKDF2REQUEST) \
69  V(RANDOMBYTESREQUEST) \
70  V(TLSWRAP)
71 #else
72 #define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
73 #endif // HAVE_OPENSSL
74 
75 #define NODE_ASYNC_PROVIDER_TYPES(V) \
76  NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
77  NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
78 
79 class Environment;
80 
81 class AsyncWrap : public BaseObject {
82  public:
83  enum ProviderType {
84 #define V(PROVIDER) \
85  PROVIDER_ ## PROVIDER,
86  NODE_ASYNC_PROVIDER_TYPES(V)
87 #undef V
88  PROVIDERS_LENGTH,
89  };
90 
91  enum Flags {
92  kFlagNone = 0x0,
93  kFlagHasReset = 0x1
94  };
95 
96  AsyncWrap(Environment* env,
97  v8::Local<v8::Object> object,
98  ProviderType provider,
99  bool silent = false);
100 
101  virtual ~AsyncWrap();
102 
103  static void AddWrapMethods(Environment* env,
104  v8::Local<v8::FunctionTemplate> constructor,
105  int flags = kFlagNone);
106 
107  static void Initialize(v8::Local<v8::Object> target,
108  v8::Local<v8::Value> unused,
109  v8::Local<v8::Context> context);
110 
111  static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
112  static void PushAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
113  static void PopAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
114  static void AsyncIdStackSize(const v8::FunctionCallbackInfo<v8::Value>& args);
115  static void ClearIdStack(const v8::FunctionCallbackInfo<v8::Value>& args);
116  static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
117  static void QueueDestroyId(const v8::FunctionCallbackInfo<v8::Value>& args);
118 
119  static void EmitAsyncInit(Environment* env,
120  v8::Local<v8::Object> object,
121  v8::Local<v8::String> type,
122  double id,
123  double trigger_id);
124 
125  static void EmitBefore(Environment* env, double id);
126  static void EmitAfter(Environment* env, double id);
127 
128  inline ProviderType provider_type() const;
129 
130  inline double get_id() const;
131 
132  inline double get_trigger_id() const;
133 
134  void AsyncReset(bool silent = false);
135 
136  // Only call these within a valid HandleScope.
137  v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
138  int argc,
139  v8::Local<v8::Value>* argv);
140  inline v8::MaybeLocal<v8::Value> MakeCallback(
141  const v8::Local<v8::String> symbol,
142  int argc,
143  v8::Local<v8::Value>* argv);
144  inline v8::MaybeLocal<v8::Value> MakeCallback(uint32_t index,
145  int argc,
146  v8::Local<v8::Value>* argv);
147 
148  virtual size_t self_size() const = 0;
149 
150  private:
151  inline AsyncWrap();
152  const ProviderType provider_type_;
153  // Because the values may be Reset(), cannot be made const.
154  double async_id_;
155  double trigger_id_;
156 };
157 
158 void LoadAsyncWrapperInfo(Environment* env);
159 
160 // Return value is an indicator whether the domain was disposed.
161 bool DomainEnter(Environment* env, v8::Local<v8::Object> object);
162 bool DomainExit(Environment* env, v8::Local<v8::Object> object);
163 
164 } // namespace node
165 
166 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
167 
168 #endif // SRC_ASYNC_WRAP_H_
async_context EmitAsyncInit(Isolate *isolate, Local< Object > resource, const char *name, async_id trigger_async_id)
Definition: async-wrap.cc:764
bool DomainEnter(Environment *env, Local< Object > object)
Definition: async-wrap.cc:186
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
#define V(PROVIDER)
MaybeLocal< Value > MakeCallback(Isolate *isolate, Local< Object > recv, Local< Function > callback, int argc, Local< Value > *argv, async_id asyncId, async_id triggerAsyncId)
Definition: async-wrap.cc:802
void LoadAsyncWrapperInfo(Environment *env)
Definition: async-wrap.cc:587
bool DomainExit(Environment *env, v8::Local< v8::Object > object)
Definition: async-wrap.cc:204