GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node.h Lines: 13 14 92.9 %
Date: 2019-02-26 22:23:30 Branches: 2 4 50.0 %

Line Branch Exec Source
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 __attribute__((visibility("default")))
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/nodejs/node-v0.x-archive/issues/1236
44
#if defined(__MINGW32__) || defined(_MSC_VER)
45
#ifndef _WIN32_WINNT
46
# define _WIN32_WINNT   0x0600  // Windows Server 2008
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 "v8-platform.h"  // NOLINT(build/include_order)
65
#include "node_version.h"  // NODE_MODULE_VERSION
66
67
#define NODE_MAKE_VERSION(major, minor, patch)                                \
68
  ((major) * 0x1000 + (minor) * 0x100 + (patch))
69
70
#ifdef __clang__
71
# define NODE_CLANG_AT_LEAST(major, minor, patch)                             \
72
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
73
      NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
74
#else
75
# define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
76
#endif
77
78
#ifdef __GNUC__
79
# define NODE_GNUC_AT_LEAST(major, minor, patch)                              \
80
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
81
      NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
82
#else
83
# define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
84
#endif
85
86
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
87
# define NODE_DEPRECATED(message, declarator) declarator
88
#else  // NODE_WANT_INTERNALS
89
# if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
90
#  define NODE_DEPRECATED(message, declarator)                                 \
91
    __attribute__((deprecated(message))) declarator
92
# elif defined(_MSC_VER)
93
#  define NODE_DEPRECATED(message, declarator)                                 \
94
    __declspec(deprecated) declarator
95
# else
96
#  define NODE_DEPRECATED(message, declarator) declarator
97
# endif
98
#endif
99
100
// Forward-declare libuv loop
101
struct uv_loop_s;
102
103
// Forward-declare these functions now to stop MSVS from becoming
104
// terminally confused when it's done in node_internals.h
105
namespace node {
106
107
namespace tracing {
108
109
class TracingController;
110
111
}
112
113
NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
114
                                                int errorno,
115
                                                const char* syscall = nullptr,
116
                                                const char* message = nullptr,
117
                                                const char* path = nullptr);
118
NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
119
                                             int errorno,
120
                                             const char* syscall = nullptr,
121
                                             const char* message = nullptr,
122
                                             const char* path = nullptr,
123
                                             const char* dest = nullptr);
124
125
NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
126
                inline v8::Local<v8::Value> ErrnoException(
127
      int errorno,
128
      const char* syscall = nullptr,
129
      const char* message = nullptr,
130
      const char* path = nullptr) {
131
  return ErrnoException(v8::Isolate::GetCurrent(),
132
                        errorno,
133
                        syscall,
134
                        message,
135
                        path);
136
})
137
138
NODE_DEPRECATED("Use UVException(isolate, ...)",
139
                inline v8::Local<v8::Value> UVException(int errorno,
140
                                        const char* syscall = nullptr,
141
                                        const char* message = nullptr,
142
                                        const char* path = nullptr) {
143
  return UVException(v8::Isolate::GetCurrent(),
144
                     errorno,
145
                     syscall,
146
                     message,
147
                     path);
148
})
149
150
/*
151
 * These methods need to be called in a HandleScope.
152
 *
153
 * It is preferred that you use the `MakeCallback` overloads taking
154
 * `async_context` arguments.
155
 */
156
157
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
158
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
159
                    v8::Isolate* isolate,
160
                    v8::Local<v8::Object> recv,
161
                    const char* method,
162
                    int argc,
163
                    v8::Local<v8::Value>* argv));
164
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
165
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
166
                    v8::Isolate* isolate,
167
                    v8::Local<v8::Object> recv,
168
                    v8::Local<v8::String> symbol,
169
                    int argc,
170
                    v8::Local<v8::Value>* argv));
171
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
172
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
173
                    v8::Isolate* isolate,
174
                    v8::Local<v8::Object> recv,
175
                    v8::Local<v8::Function> callback,
176
                    int argc,
177
                    v8::Local<v8::Value>* argv));
178
179
}  // namespace node
180
181
#include <assert.h>
182
#include <stdint.h>
183
184
#ifndef NODE_STRINGIFY
185
#define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
186
#define NODE_STRINGIFY_HELPER(n) #n
187
#endif
188
189
#ifdef _WIN32
190
#if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
191
typedef intptr_t ssize_t;
192
# define _SSIZE_T_
193
# define _SSIZE_T_DEFINED
194
#endif
195
#else  // !_WIN32
196
# include <sys/types.h>  // size_t, ssize_t
197
#endif  // _WIN32
198
199
200
namespace node {
201
202
// TODO(addaleax): Officially deprecate this and replace it with something
203
// better suited for a public embedder API.
204
NODE_EXTERN int Start(int argc, char* argv[]);
205
206
// TODO(addaleax): Officially deprecate this and replace it with something
207
// better suited for a public embedder API.
208
NODE_EXTERN void Init(int* argc,
209
                      const char** argv,
210
                      int* exec_argc,
211
                      const char*** exec_argv);
212
213
class ArrayBufferAllocator;
214
215
NODE_EXTERN ArrayBufferAllocator* CreateArrayBufferAllocator();
216
NODE_EXTERN void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator);
217
218
class IsolateData;
219
class Environment;
220
221
4237
class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
222
 public:
223
4232
  ~MultiIsolatePlatform() override { }
224
  // Returns true if work was dispatched or executed. New tasks that are
225
  // posted during flushing of the queue are postponed until the next
226
  // flushing.
227
  virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
228
  virtual void DrainTasks(v8::Isolate* isolate) = 0;
229
  virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
230
231
  // These will be called by the `IsolateData` creation/destruction functions.
232
  virtual void RegisterIsolate(v8::Isolate* isolate,
233
                               struct uv_loop_s* loop) = 0;
234
  virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
235
};
236
237
// Creates a new isolate with Node.js-specific settings.
238
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
239
                                    struct uv_loop_s* event_loop);
240
241
// Creates a new context with Node.js-specific tweaks.
242
NODE_EXTERN v8::Local<v8::Context> NewContext(
243
    v8::Isolate* isolate,
244
    v8::Local<v8::ObjectTemplate> object_template =
245
        v8::Local<v8::ObjectTemplate>());
246
247
// If `platform` is passed, it will be used to register new Worker instances.
248
// It can be `nullptr`, in which case creating new Workers inside of
249
// Environments that use this `IsolateData` will not work.
250
NODE_EXTERN IsolateData* CreateIsolateData(
251
    v8::Isolate* isolate,
252
    struct uv_loop_s* loop,
253
    MultiIsolatePlatform* platform = nullptr,
254
    ArrayBufferAllocator* allocator = nullptr);
255
NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
256
257
// TODO(addaleax): Add an official variant using STL containers, and move
258
// per-Environment options parsing here.
259
NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
260
                                           v8::Local<v8::Context> context,
261
                                           int argc,
262
                                           const char* const* argv,
263
                                           int exec_argc,
264
                                           const char* const* exec_argv);
265
266
NODE_EXTERN void LoadEnvironment(Environment* env);
267
NODE_EXTERN void FreeEnvironment(Environment* env);
268
269
// This may return nullptr if context is not associated with a Node instance.
270
NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
271
272
// This returns the MultiIsolatePlatform used in the main thread of Node.js.
273
// If NODE_USE_V8_PLATFORM haven't been defined when Node.js was built,
274
// it returns nullptr.
275
NODE_EXTERN MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform();
276
277
NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
278
    int thread_pool_size,
279
    node::tracing::TracingController* tracing_controller);
280
MultiIsolatePlatform* InitializeV8Platform(int thread_pool_size);
281
NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform);
282
283
NODE_EXTERN void EmitBeforeExit(Environment* env);
284
NODE_EXTERN int EmitExit(Environment* env);
285
NODE_EXTERN void RunAtExit(Environment* env);
286
287
// This may return nullptr if the current v8::Context is not associated
288
// with a Node instance.
289
NODE_EXTERN struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate);
290
291
/* Converts a unixtime to V8 Date */
292
NODE_DEPRECATED("Use v8::Date::New() directly",
293
                inline v8::Local<v8::Value> NODE_UNIXTIME_V8(double time) {
294
                  return v8::Date::New(
295
                             v8::Isolate::GetCurrent()->GetCurrentContext(),
296
                             1000 * time)
297
                      .ToLocalChecked();
298
                })
299
#define NODE_UNIXTIME_V8 node::NODE_UNIXTIME_V8
300
NODE_DEPRECATED("Use v8::Date::ValueOf() directly",
301
                inline double NODE_V8_UNIXTIME(v8::Local<v8::Date> date) {
302
  return date->ValueOf() / 1000;
303
})
304
#define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME
305
306
#define NODE_DEFINE_CONSTANT(target, constant)                                \
307
  do {                                                                        \
308
    v8::Isolate* isolate = target->GetIsolate();                              \
309
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
310
    v8::Local<v8::String> constant_name =                                     \
311
        v8::String::NewFromUtf8(isolate, #constant,                           \
312
            v8::NewStringType::kInternalized).ToLocalChecked();               \
313
    v8::Local<v8::Number> constant_value =                                    \
314
        v8::Number::New(isolate, static_cast<double>(constant));              \
315
    v8::PropertyAttribute constant_attributes =                               \
316
        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
317
    (target)->DefineOwnProperty(context,                                      \
318
                                constant_name,                                \
319
                                constant_value,                               \
320
                                constant_attributes).FromJust();              \
321
  }                                                                           \
322
  while (0)
323
324
#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant)                         \
325
  do {                                                                        \
326
    v8::Isolate* isolate = target->GetIsolate();                              \
327
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
328
    v8::Local<v8::String> constant_name =                                     \
329
        v8::String::NewFromUtf8(isolate, #constant,                           \
330
                                v8::NewStringType::kInternalized)             \
331
                                  .ToLocalChecked();                          \
332
    v8::Local<v8::Number> constant_value =                                    \
333
        v8::Number::New(isolate, static_cast<double>(constant));              \
334
    v8::PropertyAttribute constant_attributes =                               \
335
        static_cast<v8::PropertyAttribute>(v8::ReadOnly |                     \
336
                                           v8::DontDelete |                   \
337
                                           v8::DontEnum);                     \
338
    (target)->DefineOwnProperty(context,                                      \
339
                                constant_name,                                \
340
                                constant_value,                               \
341
                                constant_attributes).FromJust();              \
342
  }                                                                           \
343
  while (0)
344
345
// Used to be a macro, hence the uppercase name.
346
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
347
                            const char* name,
348
                            v8::FunctionCallback callback) {
349
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
350
  v8::HandleScope handle_scope(isolate);
351
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
352
                                                                callback);
353
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
354
      v8::NewStringType::kInternalized).ToLocalChecked();
355
  t->SetClassName(fn_name);
356
  recv->Set(fn_name, t);
357
}
358
359
// Used to be a macro, hence the uppercase name.
360
inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
361
                            const char* name,
362
                            v8::FunctionCallback callback) {
363
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
364
  v8::HandleScope handle_scope(isolate);
365
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
366
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
367
                                                                callback);
368
  v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
369
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
370
      v8::NewStringType::kInternalized).ToLocalChecked();
371
  fn->SetName(fn_name);
372
  recv->Set(context, fn_name, fn).FromJust();
373
}
374
#define NODE_SET_METHOD node::NODE_SET_METHOD
375
376
// Used to be a macro, hence the uppercase name.
377
// Not a template because it only makes sense for FunctionTemplates.
378
inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
379
                                      const char* name,
380
                                      v8::FunctionCallback callback) {
381
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
382
  v8::HandleScope handle_scope(isolate);
383
  v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
384
  v8::Local<v8::FunctionTemplate> t =
385
      v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
386
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
387
      v8::NewStringType::kInternalized).ToLocalChecked();
388
  t->SetClassName(fn_name);
389
  recv->PrototypeTemplate()->Set(fn_name, t);
390
}
391
#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
392
393
// BINARY is a deprecated alias of LATIN1.
394
enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER, LATIN1 = BINARY};
395
396
NODE_EXTERN enum encoding ParseEncoding(
397
    v8::Isolate* isolate,
398
    v8::Local<v8::Value> encoding_v,
399
    enum encoding default_encoding = LATIN1);
400
401
NODE_EXTERN void FatalException(v8::Isolate* isolate,
402
                                const v8::TryCatch& try_catch);
403
404
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
405
                                        const char* buf,
406
                                        size_t len,
407
                                        enum encoding encoding = LATIN1);
408
409
// Warning: This reverses endianness on Big Endian platforms, even though the
410
// signature using uint16_t implies that it should not.
411
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
412
                                        const uint16_t* buf,
413
                                        size_t len);
414
415
// Returns -1 if the handle was not valid for decoding
416
NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
417
                                v8::Local<v8::Value>,
418
                                enum encoding encoding = LATIN1);
419
// returns bytes written.
420
NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
421
                                char* buf,
422
                                size_t buflen,
423
                                v8::Local<v8::Value>,
424
                                enum encoding encoding = LATIN1);
425
#ifdef _WIN32
426
NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
427
    v8::Isolate* isolate,
428
    int errorno,
429
    const char* syscall = nullptr,
430
    const char* msg = "",
431
    const char* path = nullptr);
432
#endif
433
434
const char* signo_string(int errorno);
435
436
437
typedef void (*addon_register_func)(
438
    v8::Local<v8::Object> exports,
439
    v8::Local<v8::Value> module,
440
    void* priv);
441
442
typedef void (*addon_context_register_func)(
443
    v8::Local<v8::Object> exports,
444
    v8::Local<v8::Value> module,
445
    v8::Local<v8::Context> context,
446
    void* priv);
447
448
struct node_module {
449
  int nm_version;
450
  unsigned int nm_flags;
451
  void* nm_dso_handle;
452
  const char* nm_filename;
453
  node::addon_register_func nm_register_func;
454
  node::addon_context_register_func nm_context_register_func;
455
  const char* nm_modname;
456
  void* nm_priv;
457
  struct node_module* nm_link;
458
};
459
460
extern "C" NODE_EXTERN void node_module_register(void* mod);
461
462
#ifdef _WIN32
463
# define NODE_MODULE_EXPORT __declspec(dllexport)
464
#else
465
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
466
#endif
467
468
#ifdef NODE_SHARED_MODE
469
# define NODE_CTOR_PREFIX
470
#else
471
# define NODE_CTOR_PREFIX static
472
#endif
473
474
#if defined(_MSC_VER)
475
#pragma section(".CRT$XCU", read)
476
#define NODE_C_CTOR(fn)                                               \
477
  NODE_CTOR_PREFIX void __cdecl fn(void);                             \
478
  __declspec(dllexport, allocate(".CRT$XCU"))                         \
479
      void (__cdecl*fn ## _)(void) = fn;                              \
480
  NODE_CTOR_PREFIX void __cdecl fn(void)
481
#else
482
#define NODE_C_CTOR(fn)                                               \
483
  NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
484
  NODE_CTOR_PREFIX void fn(void)
485
#endif
486
487
#define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
488
  extern "C" {                                                        \
489
    static node::node_module _module =                                \
490
    {                                                                 \
491
      NODE_MODULE_VERSION,                                            \
492
      flags,                                                          \
493
      NULL,  /* NOLINT (readability/null_usage) */                    \
494
      __FILE__,                                                       \
495
      (node::addon_register_func) (regfunc),                          \
496
      NULL,  /* NOLINT (readability/null_usage) */                    \
497
      NODE_STRINGIFY(modname),                                        \
498
      priv,                                                           \
499
      NULL   /* NOLINT (readability/null_usage) */                    \
500
    };                                                                \
501
    NODE_C_CTOR(_register_ ## modname) {                              \
502
      node_module_register(&_module);                                 \
503
    }                                                                 \
504
  }
505
506
#define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
507
  extern "C" {                                                        \
508
    static node::node_module _module =                                \
509
    {                                                                 \
510
      NODE_MODULE_VERSION,                                            \
511
      flags,                                                          \
512
      NULL,  /* NOLINT (readability/null_usage) */                    \
513
      __FILE__,                                                       \
514
      NULL,  /* NOLINT (readability/null_usage) */                    \
515
      (node::addon_context_register_func) (regfunc),                  \
516
      NODE_STRINGIFY(modname),                                        \
517
      priv,                                                           \
518
      NULL  /* NOLINT (readability/null_usage) */                     \
519
    };                                                                \
520
    NODE_C_CTOR(_register_ ## modname) {                              \
521
      node_module_register(&_module);                                 \
522
    }                                                                 \
523
  }
524
525
// Usage: `NODE_MODULE(NODE_GYP_MODULE_NAME, InitializerFunction)`
526
// If no NODE_MODULE is declared, Node.js looks for the well-known
527
// symbol `node_register_module_v${NODE_MODULE_VERSION}`.
528
#define NODE_MODULE(modname, regfunc)                                 \
529
  NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
530
531
#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
532
  /* NOLINTNEXTLINE (readability/null_usage) */                       \
533
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
534
535
/*
536
 * For backward compatibility in add-on modules.
537
 */
538
#define NODE_MODULE_DECL /* nothing */
539
540
#define NODE_MODULE_INITIALIZER_BASE node_register_module_v
541
542
#define NODE_MODULE_INITIALIZER_X(base, version)                      \
543
    NODE_MODULE_INITIALIZER_X_HELPER(base, version)
544
545
#define NODE_MODULE_INITIALIZER_X_HELPER(base, version) base##version
546
547
#define NODE_MODULE_INITIALIZER                                       \
548
  NODE_MODULE_INITIALIZER_X(NODE_MODULE_INITIALIZER_BASE,             \
549
      NODE_MODULE_VERSION)
550
551
#define NODE_MODULE_INIT()                                            \
552
  extern "C" NODE_MODULE_EXPORT void                                  \
553
  NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,              \
554
                          v8::Local<v8::Value> module,                \
555
                          v8::Local<v8::Context> context);            \
556
  NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME,                     \
557
                            NODE_MODULE_INITIALIZER)                  \
558
  void NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,         \
559
                               v8::Local<v8::Value> module,           \
560
                               v8::Local<v8::Context> context)
561
562
/* Called after the event loop exits but before the VM is disposed.
563
 * Callbacks are run in reverse order of registration, i.e. newest first.
564
 */
565
NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = nullptr);
566
567
/* Registers a callback with the passed-in Environment instance. The callback
568
 * is called after the event loop exits, but before the VM is disposed.
569
 * Callbacks are run in reverse order of registration, i.e. newest first.
570
 */
571
NODE_EXTERN void AtExit(Environment* env,
572
                        void (*cb)(void* arg),
573
                        void* arg = nullptr);
574
575
typedef void (*promise_hook_func) (v8::PromiseHookType type,
576
                                   v8::Local<v8::Promise> promise,
577
                                   v8::Local<v8::Value> parent,
578
                                   void* arg);
579
580
typedef double async_id;
581
struct async_context {
582
  ::node::async_id async_id;
583
  ::node::async_id trigger_async_id;
584
};
585
586
/* Registers an additional v8::PromiseHook wrapper. This API exists because V8
587
 * itself supports only a single PromiseHook. */
588
NODE_EXTERN void AddPromiseHook(v8::Isolate* isolate,
589
                                promise_hook_func fn,
590
                                void* arg);
591
592
/* This is a lot like node::AtExit, except that the hooks added via this
593
 * function are run before the AtExit ones and will always be registered
594
 * for the current Environment instance.
595
 * These functions are safe to use in an addon supporting multiple
596
 * threads/isolates. */
597
NODE_EXTERN void AddEnvironmentCleanupHook(v8::Isolate* isolate,
598
                                           void (*fun)(void* arg),
599
                                           void* arg);
600
601
NODE_EXTERN void RemoveEnvironmentCleanupHook(v8::Isolate* isolate,
602
                                              void (*fun)(void* arg),
603
                                              void* arg);
604
605
/* Returns the id of the current execution context. If the return value is
606
 * zero then no execution has been set. This will happen if the user handles
607
 * I/O from native code. */
608
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
609
610
/* Return same value as async_hooks.triggerAsyncId(); */
611
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
612
613
/* If the native API doesn't inherit from the helper class then the callbacks
614
 * must be triggered manually. This triggers the init() callback. The return
615
 * value is the async id assigned to the resource.
616
 *
617
 * The `trigger_async_id` parameter should correspond to the resource which is
618
 * creating the new resource, which will usually be the return value of
619
 * `AsyncHooksGetTriggerAsyncId()`. */
620
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
621
                                        v8::Local<v8::Object> resource,
622
                                        const char* name,
623
                                        async_id trigger_async_id = -1);
624
625
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
626
                                        v8::Local<v8::Object> resource,
627
                                        v8::Local<v8::String> name,
628
                                        async_id trigger_async_id = -1);
629
630
/* Emit the destroy() callback. */
631
NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
632
                                  async_context asyncContext);
633
634
class InternalCallbackScope;
635
636
/* This class works like `MakeCallback()` in that it sets up a specific
637
 * asyncContext as the current one and informs the async_hooks and domains
638
 * modules that this context is currently active.
639
 *
640
 * `MakeCallback()` is a wrapper around this class as well as
641
 * `Function::Call()`. Either one of these mechanisms needs to be used for
642
 * top-level calls into JavaScript (i.e. without any existing JS stack).
643
 *
644
 * This object should be stack-allocated to ensure that it is contained in a
645
 * valid HandleScope.
646
 *
647
 * Exceptions happening within this scope will be treated like uncaught
648
 * exceptions. If this behaviour is undesirable, a new `v8::TryCatch` scope
649
 * needs to be created inside of this scope.
650
 */
651
class NODE_EXTERN CallbackScope {
652
 public:
653
  CallbackScope(v8::Isolate* isolate,
654
                v8::Local<v8::Object> resource,
655
                async_context asyncContext);
656
  ~CallbackScope();
657
658
  void operator=(const CallbackScope&) = delete;
659
  void operator=(CallbackScope&&) = delete;
660
  CallbackScope(const CallbackScope&) = delete;
661
  CallbackScope(CallbackScope&&) = delete;
662
663
 private:
664
  InternalCallbackScope* private_;
665
  v8::TryCatch try_catch_;
666
};
667
668
/* An API specific to emit before/after callbacks is unnecessary because
669
 * MakeCallback will automatically call them for you.
670
 *
671
 * These methods may create handles on their own, so run them inside a
672
 * HandleScope.
673
 *
674
 * `asyncId` and `triggerAsyncId` should correspond to the values returned by
675
 * `EmitAsyncInit()` and `AsyncHooksGetTriggerAsyncId()`, respectively, when the
676
 * invoking resource was created. If these values are unknown, 0 can be passed.
677
 * */
678
NODE_EXTERN
679
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
680
                                       v8::Local<v8::Object> recv,
681
                                       v8::Local<v8::Function> callback,
682
                                       int argc,
683
                                       v8::Local<v8::Value>* argv,
684
                                       async_context asyncContext);
685
NODE_EXTERN
686
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
687
                                       v8::Local<v8::Object> recv,
688
                                       const char* method,
689
                                       int argc,
690
                                       v8::Local<v8::Value>* argv,
691
                                       async_context asyncContext);
692
NODE_EXTERN
693
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
694
                                       v8::Local<v8::Object> recv,
695
                                       v8::Local<v8::String> symbol,
696
                                       int argc,
697
                                       v8::Local<v8::Value>* argv,
698
                                       async_context asyncContext);
699
700
/* Helper class users can optionally inherit from. If
701
 * `AsyncResource::MakeCallback()` is used, then all four callbacks will be
702
 * called automatically. */
703
class AsyncResource {
704
 public:
705
524
  AsyncResource(v8::Isolate* isolate,
706
                v8::Local<v8::Object> resource,
707
                const char* name,
708
                async_id trigger_async_id = -1)
709
      : isolate_(isolate),
710
524
        resource_(isolate, resource) {
711
    async_context_ = EmitAsyncInit(isolate, resource, name,
712
524
                                   trigger_async_id);
713
524
  }
714
715
1062
  virtual ~AsyncResource() {
716
528
    EmitAsyncDestroy(isolate_, async_context_);
717
528
    resource_.Reset();
718
534
  }
719
720
  v8::MaybeLocal<v8::Value> MakeCallback(
721
      v8::Local<v8::Function> callback,
722
      int argc,
723
      v8::Local<v8::Value>* argv) {
724
    return node::MakeCallback(isolate_, get_resource(),
725
                              callback, argc, argv,
726
                              async_context_);
727
  }
728
729
  v8::MaybeLocal<v8::Value> MakeCallback(
730
      const char* method,
731
      int argc,
732
      v8::Local<v8::Value>* argv) {
733
    return node::MakeCallback(isolate_, get_resource(),
734
                              method, argc, argv,
735
                              async_context_);
736
  }
737
738
  v8::MaybeLocal<v8::Value> MakeCallback(
739
      v8::Local<v8::String> symbol,
740
      int argc,
741
      v8::Local<v8::Value>* argv) {
742
    return node::MakeCallback(isolate_, get_resource(),
743
                              symbol, argc, argv,
744
                              async_context_);
745
  }
746
747
  v8::Local<v8::Object> get_resource() {
748
    return resource_.Get(isolate_);
749
  }
750
751
  async_id get_async_id() const {
752
    return async_context_.async_id;
753
  }
754
755
  async_id get_trigger_async_id() const {
756
    return async_context_.trigger_async_id;
757
  }
758
759
 protected:
760
621
  class CallbackScope : public node::CallbackScope {
761
   public:
762
622
    explicit CallbackScope(AsyncResource* res)
763
      : node::CallbackScope(res->isolate_,
764
                            res->resource_.Get(res->isolate_),
765
1244
                            res->async_context_) {}
766
  };
767
768
 private:
769
  v8::Isolate* isolate_;
770
  v8::Persistent<v8::Object> resource_;
771
  async_context async_context_;
772
};
773
774
}  // namespace node
775
776
#endif  // SRC_NODE_H_