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