Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node.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_NODE_H_
23 #define SRC_NODE_H_
24 
25 #ifdef _WIN32
26 # ifndef BUILDING_NODE_EXTENSION
27 # define NODE_EXTERN __declspec(dllexport)
28 # else
29 # define NODE_EXTERN __declspec(dllimport)
30 # endif
31 #else
32 # define NODE_EXTERN /* nothing */
33 #endif
34 
35 #ifdef BUILDING_NODE_EXTENSION
36 # undef BUILDING_V8_SHARED
37 # undef BUILDING_UV_SHARED
38 # define USING_V8_SHARED 1
39 # define USING_UV_SHARED 1
40 #endif
41 
42 // This should be defined in make system.
43 // See issue https://github.com/joyent/node/issues/1236
44 #if defined(__MINGW32__) || defined(_MSC_VER)
45 #ifndef _WIN32_WINNT
46 # define _WIN32_WINNT 0x0501
47 #endif
48 
49 #ifndef NOMINMAX
50 # define NOMINMAX
51 #endif
52 
53 #endif
54 
55 #if defined(_MSC_VER)
56 #define PATH_MAX MAX_PATH
57 #endif
58 
59 #ifdef _WIN32
60 # define SIGKILL 9
61 #endif
62 
63 #include "v8.h" // NOLINT(build/include_order)
64 #include "node_version.h" // NODE_MODULE_VERSION
65 
66 #define NODE_MAKE_VERSION(major, minor, patch) \
67  ((major) * 0x1000 + (minor) * 0x100 + (patch))
68 
69 #ifdef __clang__
70 # define NODE_CLANG_AT_LEAST(major, minor, patch) \
71  (NODE_MAKE_VERSION(major, minor, patch) <= \
72  NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
73 #else
74 # define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
75 #endif
76 
77 #ifdef __GNUC__
78 # define NODE_GNUC_AT_LEAST(major, minor, patch) \
79  (NODE_MAKE_VERSION(major, minor, patch) <= \
80  NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
81 #else
82 # define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
83 #endif
84 
85 #if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
86 # define NODE_DEPRECATED(message, declarator) \
87  __attribute__((deprecated(message))) declarator
88 #elif defined(_MSC_VER)
89 # define NODE_DEPRECATED(message, declarator) \
90  __declspec(deprecated) declarator
91 #else
92 # define NODE_DEPRECATED(message, declarator) \
93  declarator
94 #endif
95 
96 // Forward-declare libuv loop
97 struct uv_loop_s;
98 
99 // Forward-declare these functions now to stop MSVS from becoming
100 // terminally confused when it's done in node_internals.h
101 namespace node {
102 
103 NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
104  int errorno,
105  const char* syscall = NULL,
106  const char* message = NULL,
107  const char* path = NULL);
108 NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
109  int errorno,
110  const char* syscall = NULL,
111  const char* message = NULL,
112  const char* path = NULL);
113 NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
114  int errorno,
115  const char* syscall,
116  const char* message,
117  const char* path,
118  const char* dest);
119 
120 NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
121  inline v8::Local<v8::Value> ErrnoException(
122  int errorno,
123  const char* syscall = NULL,
124  const char* message = NULL,
125  const char* path = NULL) {
126  return ErrnoException(v8::Isolate::GetCurrent(),
127  errorno,
128  syscall,
129  message,
130  path);
131 })
132 
133 inline v8::Local<v8::Value> UVException(int errorno,
134  const char* syscall = NULL,
135  const char* message = NULL,
136  const char* path = NULL) {
137  return UVException(v8::Isolate::GetCurrent(),
138  errorno,
139  syscall,
140  message,
141  path);
142 }
143 
144 /*
145  * These methods need to be called in a HandleScope.
146  *
147  * It is preferred that you use the `MakeCallback` overloads taking
148  * `async_id` arguments.
149  */
150 
151 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
152  v8::Isolate* isolate,
153  v8::Local<v8::Object> recv,
154  const char* method,
155  int argc,
156  v8::Local<v8::Value>* argv);
157 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
158  v8::Isolate* isolate,
159  v8::Local<v8::Object> recv,
160  v8::Local<v8::String> symbol,
161  int argc,
162  v8::Local<v8::Value>* argv);
163 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
164  v8::Isolate* isolate,
165  v8::Local<v8::Object> recv,
166  v8::Local<v8::Function> callback,
167  int argc,
168  v8::Local<v8::Value>* argv);
169 
170 } // namespace node
171 
172 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
173 #include "node_internals.h"
174 #endif
175 
176 #include <assert.h>
177 #include <stdint.h>
178 
179 #ifndef NODE_STRINGIFY
180 #define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
181 #define NODE_STRINGIFY_HELPER(n) #n
182 #endif
183 
184 #ifdef _WIN32
185 // TODO(tjfontaine) consider changing the usage of ssize_t to ptrdiff_t
186 #if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
187 typedef intptr_t ssize_t;
188 # define _SSIZE_T_
189 # define _SSIZE_T_DEFINED
190 #endif
191 #else // !_WIN32
192 # include <sys/types.h> // size_t, ssize_t
193 #endif // _WIN32
194 
195 
196 namespace node {
197 
198 NODE_EXTERN extern bool no_deprecation;
199 #if HAVE_OPENSSL
200 NODE_EXTERN extern bool ssl_openssl_cert_store;
201 # if NODE_FIPS_MODE
202 NODE_EXTERN extern bool enable_fips_crypto;
203 NODE_EXTERN extern bool force_fips_crypto;
204 # endif
205 #endif
206 
207 NODE_EXTERN int Start(int argc, char *argv[]);
208 NODE_EXTERN void Init(int* argc,
209  const char** argv,
210  int* exec_argc,
211  const char*** exec_argv);
212 
213 class IsolateData;
214 class Environment;
215 
216 NODE_EXTERN IsolateData* CreateIsolateData(v8::Isolate* isolate,
217  struct uv_loop_s* loop);
218 NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
219 
220 NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
221  v8::Local<v8::Context> context,
222  int argc,
223  const char* const* argv,
224  int exec_argc,
225  const char* const* exec_argv);
226 
227 NODE_EXTERN void LoadEnvironment(Environment* env);
228 NODE_EXTERN void FreeEnvironment(Environment* env);
229 
230 NODE_EXTERN void EmitBeforeExit(Environment* env);
231 NODE_EXTERN int EmitExit(Environment* env);
232 NODE_EXTERN void RunAtExit(Environment* env);
233 
234 /* Converts a unixtime to V8 Date */
235 #define NODE_UNIXTIME_V8(t) v8::Date::New(v8::Isolate::GetCurrent(), \
236  1000 * static_cast<double>(t))
237 #define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->NumberValue())/1000.0);
238 
239 #define NODE_DEFINE_CONSTANT(target, constant) \
240  do { \
241  v8::Isolate* isolate = target->GetIsolate(); \
242  v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
243  v8::Local<v8::String> constant_name = \
244  v8::String::NewFromUtf8(isolate, #constant); \
245  v8::Local<v8::Number> constant_value = \
246  v8::Number::New(isolate, static_cast<double>(constant)); \
247  v8::PropertyAttribute constant_attributes = \
248  static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); \
249  (target)->DefineOwnProperty(context, \
250  constant_name, \
251  constant_value, \
252  constant_attributes).FromJust(); \
253  } \
254  while (0)
255 
256 #define NODE_DEFINE_HIDDEN_CONSTANT(target, constant) \
257  do { \
258  v8::Isolate* isolate = target->GetIsolate(); \
259  v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
260  v8::Local<v8::String> constant_name = \
261  v8::String::NewFromUtf8(isolate, #constant, \
262  v8::NewStringType::kInternalized) \
263  .ToLocalChecked(); \
264  v8::Local<v8::Number> constant_value = \
265  v8::Number::New(isolate, static_cast<double>(constant)); \
266  v8::PropertyAttribute constant_attributes = \
267  static_cast<v8::PropertyAttribute>(v8::ReadOnly | \
268  v8::DontDelete | \
269  v8::DontEnum); \
270  (target)->DefineOwnProperty(context, \
271  constant_name, \
272  constant_value, \
273  constant_attributes).FromJust(); \
274  } \
275  while (0)
276 
277 // Used to be a macro, hence the uppercase name.
278 inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
279  const char* name,
280  v8::FunctionCallback callback) {
281  v8::Isolate* isolate = v8::Isolate::GetCurrent();
282  v8::HandleScope handle_scope(isolate);
283  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
284  callback);
285  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
286  t->SetClassName(fn_name);
287  recv->Set(fn_name, t);
288 }
289 
290 // Used to be a macro, hence the uppercase name.
291 inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
292  const char* name,
293  v8::FunctionCallback callback) {
294  v8::Isolate* isolate = v8::Isolate::GetCurrent();
295  v8::HandleScope handle_scope(isolate);
296  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
297  callback);
298  v8::Local<v8::Function> fn = t->GetFunction();
299  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
300  fn->SetName(fn_name);
301  recv->Set(fn_name, fn);
302 }
303 #define NODE_SET_METHOD node::NODE_SET_METHOD
304 
305 // Used to be a macro, hence the uppercase name.
306 // Not a template because it only makes sense for FunctionTemplates.
307 inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
308  const char* name,
309  v8::FunctionCallback callback) {
310  v8::Isolate* isolate = v8::Isolate::GetCurrent();
311  v8::HandleScope handle_scope(isolate);
312  v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
313  v8::Local<v8::FunctionTemplate> t =
314  v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
315  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
316  t->SetClassName(fn_name);
317  recv->PrototypeTemplate()->Set(fn_name, t);
318 }
319 #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
320 
321 // BINARY is a deprecated alias of LATIN1.
323 
325  v8::Isolate* isolate,
326  v8::Local<v8::Value> encoding_v,
327  enum encoding default_encoding = LATIN1);
328 NODE_DEPRECATED("Use ParseEncoding(isolate, ...)",
329  inline enum encoding ParseEncoding(
330  v8::Local<v8::Value> encoding_v,
331  enum encoding default_encoding = LATIN1) {
332  return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);
333 })
334 
335 NODE_EXTERN void FatalException(v8::Isolate* isolate,
336  const v8::TryCatch& try_catch);
337 
338 NODE_DEPRECATED("Use FatalException(isolate, ...)",
339  inline void FatalException(const v8::TryCatch& try_catch) {
340  return FatalException(v8::Isolate::GetCurrent(), try_catch);
341 })
342 
343 // Don't call with encoding=UCS2.
344 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
345  const char* buf,
346  size_t len,
347  enum encoding encoding = LATIN1);
348 
349 // The input buffer should be in host endianness.
350 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
351  const uint16_t* buf,
352  size_t len);
353 
354 NODE_DEPRECATED("Use Encode(isolate, ...)",
355  inline v8::Local<v8::Value> Encode(
356  const void* buf,
357  size_t len,
358  enum encoding encoding = LATIN1) {
359  v8::Isolate* isolate = v8::Isolate::GetCurrent();
360  if (encoding == UCS2) {
361  assert(reinterpret_cast<uintptr_t>(buf) % sizeof(uint16_t) == 0 &&
362  "UCS2 buffer must be aligned on two-byte boundary.");
363  const uint16_t* that = static_cast<const uint16_t*>(buf);
364  return Encode(isolate, that, len / sizeof(*that));
365  }
366  return Encode(isolate, static_cast<const char*>(buf), len, encoding);
367 })
368 
369 // Returns -1 if the handle was not valid for decoding
370 NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
371  v8::Local<v8::Value>,
372  enum encoding encoding = LATIN1);
373 NODE_DEPRECATED("Use DecodeBytes(isolate, ...)",
374  inline ssize_t DecodeBytes(
375  v8::Local<v8::Value> val,
376  enum encoding encoding = LATIN1) {
377  return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding);
378 })
379 
380 // returns bytes written.
381 NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
382  char* buf,
383  size_t buflen,
384  v8::Local<v8::Value>,
385  enum encoding encoding = LATIN1);
386 NODE_DEPRECATED("Use DecodeWrite(isolate, ...)",
387  inline ssize_t DecodeWrite(char* buf,
388  size_t buflen,
389  v8::Local<v8::Value> val,
390  enum encoding encoding = LATIN1) {
391  return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding);
392 })
393 
394 #ifdef _WIN32
395 NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
396  v8::Isolate* isolate,
397  int errorno,
398  const char *syscall = NULL,
399  const char *msg = "",
400  const char *path = NULL);
401 
402 NODE_DEPRECATED("Use WinapiErrnoException(isolate, ...)",
403  inline v8::Local<v8::Value> WinapiErrnoException(int errorno,
404  const char *syscall = NULL, const char *msg = "",
405  const char *path = NULL) {
406  return WinapiErrnoException(v8::Isolate::GetCurrent(),
407  errorno,
408  syscall,
409  msg,
410  path);
411 })
412 #endif
413 
414 const char *signo_string(int errorno);
415 
416 
417 typedef void (*addon_register_func)(
418  v8::Local<v8::Object> exports,
419  v8::Local<v8::Value> module,
420  void* priv);
421 
423  v8::Local<v8::Object> exports,
424  v8::Local<v8::Value> module,
425  v8::Local<v8::Context> context,
426  void* priv);
427 
428 #define NM_F_BUILTIN 0x01
429 #define NM_F_LINKED 0x02
430 
431 struct node_module {
433  unsigned int nm_flags;
435  const char* nm_filename;
438  const char* nm_modname;
439  void* nm_priv;
441 };
442 
443 node_module* get_builtin_module(const char *name);
444 node_module* get_linked_module(const char *name);
445 
446 extern "C" NODE_EXTERN void node_module_register(void* mod);
447 
448 #ifdef _WIN32
449 # define NODE_MODULE_EXPORT __declspec(dllexport)
450 #else
451 # define NODE_MODULE_EXPORT __attribute__((visibility("default")))
452 #endif
453 
454 #ifdef NODE_SHARED_MODE
455 # define NODE_CTOR_PREFIX
456 #else
457 # define NODE_CTOR_PREFIX static
458 #endif
459 
460 #if defined(_MSC_VER)
461 #pragma section(".CRT$XCU", read)
462 #define NODE_C_CTOR(fn) \
463  NODE_CTOR_PREFIX void __cdecl fn(void); \
464  __declspec(dllexport, allocate(".CRT$XCU")) \
465  void (__cdecl*fn ## _)(void) = fn; \
466  NODE_CTOR_PREFIX void __cdecl fn(void)
467 #else
468 #define NODE_C_CTOR(fn) \
469  NODE_CTOR_PREFIX void fn(void) __attribute__((constructor)); \
470  NODE_CTOR_PREFIX void fn(void)
471 #endif
472 
473 #define NODE_MODULE_X(modname, regfunc, priv, flags) \
474  extern "C" { \
475  static node::node_module _module = \
476  { \
477  NODE_MODULE_VERSION, \
478  flags, \
479  NULL, \
480  __FILE__, \
481  (node::addon_register_func) (regfunc), \
482  NULL, \
483  NODE_STRINGIFY(modname), \
484  priv, \
485  NULL \
486  }; \
487  NODE_C_CTOR(_register_ ## modname) { \
488  node_module_register(&_module); \
489  } \
490  }
491 
492 #define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags) \
493  extern "C" { \
494  static node::node_module _module = \
495  { \
496  NODE_MODULE_VERSION, \
497  flags, \
498  NULL, \
499  __FILE__, \
500  NULL, \
501  (node::addon_context_register_func) (regfunc), \
502  NODE_STRINGIFY(modname), \
503  priv, \
504  NULL \
505  }; \
506  NODE_C_CTOR(_register_ ## modname) { \
507  node_module_register(&_module); \
508  } \
509  }
510 
511 #define NODE_MODULE(modname, regfunc) \
512  NODE_MODULE_X(modname, regfunc, NULL, 0)
513 
514 #define NODE_MODULE_CONTEXT_AWARE(modname, regfunc) \
515  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
516 
517 #define NODE_MODULE_CONTEXT_AWARE_BUILTIN(modname, regfunc) \
518  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, NM_F_BUILTIN) \
519 
520 /*
521  * For backward compatibility in add-on modules.
522  */
523 #define NODE_MODULE_DECL /* nothing */
524 
525 /* Called after the event loop exits but before the VM is disposed.
526  * Callbacks are run in reverse order of registration, i.e. newest first.
527  */
528 NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
529 
530 /* Registers a callback with the passed-in Environment instance. The callback
531  * is called after the event loop exits, but before the VM is disposed.
532  * Callbacks are run in reverse order of registration, i.e. newest first.
533  */
534 NODE_EXTERN void AtExit(Environment* env, void (*cb)(void* arg), void* arg = 0);
535 
536 typedef void (*promise_hook_func) (v8::PromiseHookType type,
537  v8::Local<v8::Promise> promise,
538  v8::Local<v8::Value> parent,
539  void* arg);
540 
541 typedef double async_id;
545 
546  // Legacy, Node 8.x only.
547  NODE_DEPRECATED("Use async_context directly as returned by EmitAsyncInit()",
548  operator ::node::async_id() const {
549  return async_id;
550  });
551 };
552 
553 typedef async_id async_uid; // Legacy, Node 8.x only
554 
555 /* Registers an additional v8::PromiseHook wrapper. This API exists because V8
556  * itself supports only a single PromiseHook. */
557 NODE_EXTERN void AddPromiseHook(v8::Isolate* isolate,
559  void* arg);
560 
561 /* Returns the id of the current execution context. If the return value is
562  * zero then no execution has been set. This will happen if the user handles
563  * I/O from native code. */
564 NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
565 /* legacy alias */
566 NODE_EXTERN NODE_DEPRECATED("Use AsyncHooksGetExecutionAsyncId(isolate)",
567  async_id AsyncHooksGetCurrentId(v8::Isolate* isolate));
568 
569 
570 /* Return same value as async_hooks.triggerAsyncId(); */
571 NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
572 /* legacy alias */
573 NODE_EXTERN NODE_DEPRECATED("Use AsyncHooksGetTriggerAsyncId(isolate)",
574  async_id AsyncHooksGetTriggerId(v8::Isolate* isolate));
575 
576 // This is a legacy overload of EmitAsyncInit that has to remain for ABI
577 // compatibility during Node 8.x. Do not use.
578 NODE_EXTERN async_uid EmitAsyncInit(v8::Isolate* isolate,
579  v8::Local<v8::Object> resource,
580  const char* name,
581  async_id trigger_async_id);
582 
583 // From now on EmitAsyncInit always refers to the proper, new version.
584 #define EmitAsyncInit EmitAsyncInit__New
585 
586 /* If the native API doesn't inherit from the helper class then the callbacks
587  * must be triggered manually. This triggers the init() callback. The return
588  * value is the uid assigned to the resource.
589  *
590  * The `trigger_async_id` parameter should correspond to the resource which is
591  * creating the new resource, which will usually be the return value of
592  * `AsyncHooksGetTriggerAsyncId()`. */
593 NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
594  v8::Local<v8::Object> resource,
595  const char* name,
596  async_id trigger_async_id = -1);
597 
598 /* Emit the destroy() callback. */
599 NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
600  async_context asyncContext);
601 
602 /* An API specific to emit before/after callbacks is unnecessary because
603  * MakeCallback will automatically call them for you.
604  *
605  * These methods may create handles on their own, so run them inside a
606  * HandleScope.
607  *
608  * `asyncId` and `triggerAsyncId` should correspond to the values returned by
609  * `EmitAsyncInit()` and `AsyncHooksGetTriggerAsyncId()`, respectively, when the
610  * invoking resource was created. If these values are unknown, 0 can be passed.
611  * */
613 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
614  v8::Local<v8::Object> recv,
615  v8::Local<v8::Function> callback,
616  int argc,
617  v8::Local<v8::Value>* argv,
618  async_context asyncContext);
620 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
621  v8::Local<v8::Object> recv,
622  const char* method,
623  int argc,
624  v8::Local<v8::Value>* argv,
625  async_context asyncContext);
627 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
628  v8::Local<v8::Object> recv,
629  v8::Local<v8::String> symbol,
630  int argc,
631  v8::Local<v8::Value>* argv,
632  async_context asyncContext);
633 
634 // Legacy, Node 8.x only.
635 
637 NODE_DEPRECATED("Use MakeCallback(..., async_context asyncContext) instead",
638  v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
639  v8::Local<v8::Object> recv,
640  v8::Local<v8::Function> callback,
641  int argc,
642  v8::Local<v8::Value>* argv,
643  async_id asyncId,
644  async_id triggerAsyncId));
646 NODE_DEPRECATED("Use MakeCallback(..., async_context asyncContext) instead",
647  v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
648  v8::Local<v8::Object> recv,
649  const char* method,
650  int argc,
651  v8::Local<v8::Value>* argv,
652  async_id asyncId,
653  async_id triggerAsyncId));
655 NODE_DEPRECATED("Use MakeCallback(..., async_context asyncContext) instead",
656  v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
657  v8::Local<v8::Object> recv,
658  v8::Local<v8::String> symbol,
659  int argc,
660  v8::Local<v8::Value>* argv,
661  async_id asyncId,
662  async_id triggerAsyncId));
663 
664 /* Helper class users can optionally inherit from. If
665  * `AsyncResource::MakeCallback()` is used, then all four callbacks will be
666  * called automatically. */
668  public:
669  AsyncResource(v8::Isolate* isolate,
670  v8::Local<v8::Object> resource,
671  const char* name,
672  async_id trigger_async_id = -1)
673  : isolate_(isolate),
674  resource_(isolate, resource) {
675  async_context_ = EmitAsyncInit(isolate, resource, name,
676  trigger_async_id);
677  }
678 
680  EmitAsyncDestroy(isolate_, async_context_);
681  }
682 
683  v8::MaybeLocal<v8::Value> MakeCallback(
684  v8::Local<v8::Function> callback,
685  int argc,
686  v8::Local<v8::Value>* argv) {
687  return node::MakeCallback(isolate_, get_resource(),
688  callback, argc, argv,
689  async_context_);
690  }
691 
692  v8::MaybeLocal<v8::Value> MakeCallback(
693  const char* method,
694  int argc,
695  v8::Local<v8::Value>* argv) {
696  return node::MakeCallback(isolate_, get_resource(),
697  method, argc, argv,
698  async_context_);
699  }
700 
701  v8::MaybeLocal<v8::Value> MakeCallback(
702  v8::Local<v8::String> symbol,
703  int argc,
704  v8::Local<v8::Value>* argv) {
705  return node::MakeCallback(isolate_, get_resource(),
706  symbol, argc, argv,
707  async_context_);
708  }
709 
710  v8::Local<v8::Object> get_resource() {
711  return resource_.Get(isolate_);
712  }
713 
714  NODE_DEPRECATED("Use AsyncResource::get_async_id()",
715  async_id get_uid() const {
716  return get_async_id();
717  }
718  )
719 
720  async_id get_async_id() const {
721  return async_context_.async_id;
722  }
723 
724  async_id get_trigger_async_id() const {
725  return async_context_.trigger_async_id;
726  }
727  private:
728  v8::Isolate* isolate_;
729  v8::Persistent<v8::Object> resource_;
730  async_context async_context_;
731 };
732 
733 } // namespace node
734 
735 #endif // SRC_NODE_H_
void FreeEnvironment(Environment *env)
Definition: node.cc:4531
unsigned int nm_flags
Definition: node.h:433
void EmitBeforeExit(Environment *env)
Definition: node.cc:4467
void * nm_dso_handle
Definition: node.h:434
unsigned char * buf
Definition: cares_wrap.cc:483
void NODE_SET_METHOD(v8::Local< v8::Template > recv, const char *name, v8::FunctionCallback callback)
Definition: node.h:278
v8::MaybeLocal< v8::Value > MakeCallback(v8::Local< v8::Function > callback, int argc, v8::Local< v8::Value > *argv)
Definition: node.h:683
ssize_t DecodeWrite(Isolate *isolate, char *buf, size_t buflen, Local< Value > val, enum encoding encoding)
Definition: node.cc:1599
async_id AsyncHooksGetTriggerId(Isolate *isolate)
Definition: async-wrap.cc:759
struct node_module * nm_link
Definition: node.h:440
int len
Definition: cares_wrap.cc:485
v8::Local< v8::Object > get_resource()
Definition: node.h:710
int EmitExit(Environment *env)
Definition: node.cc:4482
ssize_t DecodeBytes(Isolate *isolate, Local< Value > val, enum encoding encoding)
Definition: node.cc:1590
void FatalException(Isolate *isolate, Local< Value > error, Local< Message > message)
Definition: node.cc:2623
IsolateData * CreateIsolateData(Isolate *isolate, uv_loop_t *loop)
Definition: node.cc:4506
async_context EmitAsyncInit(Isolate *isolate, Local< Object > resource, const char *name, async_id trigger_async_id)
Definition: async-wrap.cc:764
void node_module_register(void *m)
Definition: node.cc:2465
void AtExit(void(*cb)(void *arg), void *arg)
Definition: node.cc:4455
async_id get_trigger_async_id() const
Definition: node.h:724
NODE_DEPRECATED("Use Encode(isolate, ...)", inline v8::Local< v8::Value > Encode(const void *buf, size_t len, enum encoding encoding=LATIN1) { v8::Isolate *isolate=v8::Isolate::GetCurrent();if(encoding==UCS2) { assert(reinterpret_cast< uintptr_t >(buf) % sizeof(uint16_t)==0 &&"UCS2 buffer must be aligned on two-byte boundary.");const uint16_t *that=static_cast< const uint16_t * >(buf);return Encode(isolate, that, len/sizeof(*that));} return Encode(isolate, static_cast< const char * >(buf), len, encoding);}) NODE_EXTERN ssize_t DecodeBytes(v8 NODE_DEPRECATED("Use DecodeBytes(isolate, ...)", inline ssize_t DecodeBytes(v8::Local< v8::Value > val, enum encoding encoding=LATIN1) { return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding);}) NODE_EXTERN ssize_t DecodeWrite(v8 NODE_DEPRECATED("Use DecodeWrite(isolate, ...)", inline ssize_t DecodeWrite(char *buf, size_t buflen, v8::Local< v8::Value > val, enum encoding encoding=LATIN1) { return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding);}) const char *signo_string(int errorno)
Definition: node.h:386
void AddPromiseHook(v8::Isolate *isolate, promise_hook_func fn, void *arg)
Definition: node.cc:1306
void FreeIsolateData(IsolateData *isolate_data)
Definition: node.cc:4511
async_id AsyncHooksGetExecutionAsyncId(Isolate *isolate)
Definition: async-wrap.cc:746
#define NODE_EXTERN
Definition: node.h:32
const char * nm_modname
Definition: node.h:438
Local< Value > ErrnoException(Isolate *isolate, int errorno, const char *syscall, const char *msg, const char *path)
Definition: node.cc:879
async_id async_uid
Definition: node.h:553
Environment * CreateEnvironment(IsolateData *isolate_data, Local< Context > context, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4516
const char * signo_string(int signo)
Definition: node.cc:718
node::addon_context_register_func nm_context_register_func
Definition: node.h:437
encoding
Definition: node.h:322
void(* addon_register_func)(v8::Local< v8::Object > exports, v8::Local< v8::Value > module, void *priv)
Definition: node.h:417
struct node_module * get_linked_module(const char *name)
Definition: node.cc:2494
v8::MaybeLocal< v8::Value > MakeCallback(v8::Local< v8::String > symbol, int argc, v8::Local< v8::Value > *argv)
Definition: node.h:701
void(* addon_context_register_func)(v8::Local< v8::Object > exports, v8::Local< v8::Value > module, v8::Local< v8::Context > context, void *priv)
Definition: node.h:422
dtrace s
Definition: v8ustack.d:615
dtrace t
Definition: v8ustack.d:582
enum encoding ParseEncoding(const char *encoding, enum encoding default_encoding)
Definition: node.cc:1485
void EmitAsyncDestroy(Isolate *isolate, async_context asyncContext)
Definition: async-wrap.cc:789
::node::async_id trigger_async_id
Definition: node.h:544
double async_id
Definition: node.h:541
::node::async_id async_id
Definition: node.h:543
node::addon_register_func nm_register_func
Definition: node.h:436
async_id AsyncHooksGetCurrentId(Isolate *isolate)
Definition: async-wrap.cc:750
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
bool no_deprecation
Definition: node.cc:200
const char * nm_filename
Definition: node.h:435
void * nm_priv
Definition: node.h:439
void(* promise_hook_func)(v8::PromiseHookType type, v8::Local< v8::Promise > promise, v8::Local< v8::Value > parent, void *arg)
Definition: node.h:536
int nm_version
Definition: node.h:432
method
Definition: node.d:195
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
NODE_DEPRECATED("Use AsyncResource::get_async_id()", async_id get_uid() const { return get_async_id();}) async_id get_async_id() const
Definition: node.h:714
struct node_module * get_builtin_module(const char *name)
Definition: node.cc:2482
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536
v8::MaybeLocal< v8::Value > MakeCallback(const char *method, int argc, v8::Local< v8::Value > *argv)
Definition: node.h:692
void Init(int *argc, const char **argv, int *exec_argc, const char ***exec_argv)
Definition: node.cc:4351
void NODE_SET_PROTOTYPE_METHOD(v8::Local< v8::FunctionTemplate > recv, const char *name, v8::FunctionCallback callback)
Definition: node.h:307
async_id AsyncHooksGetTriggerAsyncId(Isolate *isolate)
Definition: async-wrap.cc:755
NODE_DEPRECATED("Use ParseEncoding(isolate, ...)", inline enum encoding ParseEncoding(v8::Local< v8::Value > encoding_v, enum encoding default_encoding=LATIN1) { return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);}) NODE_EXTERN void FatalException(v8 NODE_DEPRECATED("Use FatalException(isolate, ...)", inline void FatalException(const v8::TryCatch &try_catch) { return FatalException(v8::Isolate::GetCurrent(), try_catch);}) NODE_EXTERN v8 NODE_EXTERN v8::Local< v8::Value > Encode(v8::Isolate *isolate, const uint16_t *buf, size_t len)
Definition: node.h:350
AsyncResource(v8::Isolate *isolate, v8::Local< v8::Object > resource, const char *name, async_id trigger_async_id=-1)
Definition: node.h:669
Local< Value > UVException(Isolate *isolate, int errorno, const char *syscall, const char *msg, const char *path)
Definition: node.cc:940
void RunAtExit(Environment *env)
Definition: node.cc:4447
void LoadEnvironment(Environment *env)
Definition: node.cc:3573