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