GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node.h Lines: 18 18 100.0 %
Date: 2022-12-07 04:23:16 Branches: 3 4 75.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
// Declarations annotated with NODE_EXTERN_PRIVATE do not form part of
36
// the public API. They are implementation details that can and will
37
// change between releases, even in semver patch releases. Do not use
38
// any such symbol in external code.
39
#ifdef NODE_SHARED_MODE
40
#define NODE_EXTERN_PRIVATE NODE_EXTERN
41
#else
42
#define NODE_EXTERN_PRIVATE
43
#endif
44
45
#ifdef BUILDING_NODE_EXTENSION
46
# undef BUILDING_V8_SHARED
47
# undef BUILDING_UV_SHARED
48
# define USING_V8_SHARED 1
49
# define USING_UV_SHARED 1
50
#endif
51
52
// This should be defined in make system.
53
// See issue https://github.com/nodejs/node-v0.x-archive/issues/1236
54
#if defined(__MINGW32__) || defined(_MSC_VER)
55
#ifndef _WIN32_WINNT
56
# define _WIN32_WINNT 0x0600  // Windows Server 2008
57
#endif
58
59
#ifndef NOMINMAX
60
# define NOMINMAX
61
#endif
62
63
#endif
64
65
#if defined(_MSC_VER)
66
#define PATH_MAX MAX_PATH
67
#endif
68
69
#ifdef _WIN32
70
# define SIGKILL 9
71
#endif
72
73
#include "v8.h"  // NOLINT(build/include_order)
74
75
#include "v8-platform.h"  // NOLINT(build/include_order)
76
#include "node_version.h"  // NODE_MODULE_VERSION
77
78
#include <functional>
79
#include <memory>
80
#include <ostream>
81
82
// We cannot use __POSIX__ in this header because that's only defined when
83
// building Node.js.
84
#ifndef _WIN32
85
#include <signal.h>
86
#endif  // _WIN32
87
88
#define NODE_MAKE_VERSION(major, minor, patch)                                \
89
  ((major) * 0x1000 + (minor) * 0x100 + (patch))
90
91
#ifdef __clang__
92
# define NODE_CLANG_AT_LEAST(major, minor, patch)                             \
93
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
94
      NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
95
#else
96
# define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
97
#endif
98
99
#ifdef __GNUC__
100
# define NODE_GNUC_AT_LEAST(major, minor, patch)                              \
101
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
102
      NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
103
#else
104
# define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
105
#endif
106
107
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
108
# define NODE_DEPRECATED(message, declarator) declarator
109
#else  // NODE_WANT_INTERNALS
110
# if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
111
#  define NODE_DEPRECATED(message, declarator)                                 \
112
    __attribute__((deprecated(message))) declarator
113
# elif defined(_MSC_VER)
114
#  define NODE_DEPRECATED(message, declarator)                                 \
115
    __declspec(deprecated) declarator
116
# else
117
#  define NODE_DEPRECATED(message, declarator) declarator
118
# endif
119
#endif
120
121
// Forward-declare libuv loop
122
struct uv_loop_s;
123
124
struct napi_module;
125
126
// Forward-declare these functions now to stop MSVS from becoming
127
// terminally confused when it's done in node_internals.h
128
namespace node {
129
130
namespace tracing {
131
132
class TracingController;
133
134
}
135
136
NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
137
                                                int errorno,
138
                                                const char* syscall = nullptr,
139
                                                const char* message = nullptr,
140
                                                const char* path = nullptr);
141
NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
142
                                             int errorno,
143
                                             const char* syscall = nullptr,
144
                                             const char* message = nullptr,
145
                                             const char* path = nullptr,
146
                                             const char* dest = nullptr);
147
148
NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
149
                inline v8::Local<v8::Value> ErrnoException(
150
      int errorno,
151
      const char* syscall = nullptr,
152
      const char* message = nullptr,
153
      const char* path = nullptr) {
154
  return ErrnoException(v8::Isolate::GetCurrent(),
155
                        errorno,
156
                        syscall,
157
                        message,
158
                        path);
159
})
160
161
NODE_DEPRECATED("Use UVException(isolate, ...)",
162
                inline v8::Local<v8::Value> UVException(int errorno,
163
                                        const char* syscall = nullptr,
164
                                        const char* message = nullptr,
165
                                        const char* path = nullptr) {
166
  return UVException(v8::Isolate::GetCurrent(),
167
                     errorno,
168
                     syscall,
169
                     message,
170
                     path);
171
})
172
173
/*
174
 * These methods need to be called in a HandleScope.
175
 *
176
 * It is preferred that you use the `MakeCallback` overloads taking
177
 * `async_context` arguments.
178
 */
179
180
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
181
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
182
                    v8::Isolate* isolate,
183
                    v8::Local<v8::Object> recv,
184
                    const char* method,
185
                    int argc,
186
                    v8::Local<v8::Value>* argv));
187
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
188
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
189
                    v8::Isolate* isolate,
190
                    v8::Local<v8::Object> recv,
191
                    v8::Local<v8::String> symbol,
192
                    int argc,
193
                    v8::Local<v8::Value>* argv));
194
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
195
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
196
                    v8::Isolate* isolate,
197
                    v8::Local<v8::Object> recv,
198
                    v8::Local<v8::Function> callback,
199
                    int argc,
200
                    v8::Local<v8::Value>* argv));
201
202
}  // namespace node
203
204
#include <cassert>
205
#include <cstdint>
206
207
#ifndef NODE_STRINGIFY
208
# define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
209
# define NODE_STRINGIFY_HELPER(n) #n
210
#endif
211
212
#ifdef _WIN32
213
#if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
214
typedef intptr_t ssize_t;
215
# define _SSIZE_T_
216
# define _SSIZE_T_DEFINED
217
#endif
218
#else  // !_WIN32
219
# include <sys/types.h>  // size_t, ssize_t
220
#endif  // _WIN32
221
222
223
namespace node {
224
225
class IsolateData;
226
class Environment;
227
class MultiIsolatePlatform;
228
class InitializationResultImpl;
229
230
namespace ProcessFlags {
231
// TODO(addaleax): Switch to uint32_t to match std::atomic<uint32_t>
232
// init_process_flags in node.cc
233
enum Flags : uint64_t {
234
  kNoFlags = 0,
235
  // Enable stdio inheritance, which is disabled by default.
236
  // This flag is also implied by kNoStdioInitialization.
237
  kEnableStdioInheritance = 1 << 0,
238
  // Disable reading the NODE_OPTIONS environment variable.
239
  kDisableNodeOptionsEnv = 1 << 1,
240
  // Do not parse CLI options.
241
  kDisableCLIOptions = 1 << 2,
242
  // Do not initialize ICU.
243
  kNoICU = 1 << 3,
244
  // Do not modify stdio file descriptor or TTY state.
245
  kNoStdioInitialization = 1 << 4,
246
  // Do not register Node.js-specific signal handlers
247
  // and reset other signal handlers to default state.
248
  kNoDefaultSignalHandling = 1 << 5,
249
  // Do not perform V8 initialization.
250
  kNoInitializeV8 = 1 << 6,
251
  // Do not initialize a default Node.js-provided V8 platform instance.
252
  kNoInitializeNodeV8Platform = 1 << 7,
253
  // Do not initialize OpenSSL config.
254
  kNoInitOpenSSL = 1 << 8,
255
  // Do not initialize Node.js debugging based on environment variables.
256
  kNoParseGlobalDebugVariables = 1 << 9,
257
  // Do not adjust OS resource limits for this process.
258
  kNoAdjustResourceLimits = 1 << 10,
259
  // Do not map code segments into large pages for this process.
260
  kNoUseLargePages = 1 << 11,
261
  // Skip printing output for --help, --version, --v8-options.
262
  kNoPrintHelpOrVersionOutput = 1 << 12,
263
264
  // Emulate the behavior of InitializeNodeWithArgs() when passing
265
  // a flags argument to the InitializeOncePerProcess() replacement
266
  // function.
267
  kLegacyInitializeNodeWithArgsBehavior =
268
      kNoStdioInitialization | kNoDefaultSignalHandling | kNoInitializeV8 |
269
      kNoInitializeNodeV8Platform | kNoInitOpenSSL |
270
      kNoParseGlobalDebugVariables | kNoAdjustResourceLimits |
271
      kNoUseLargePages | kNoPrintHelpOrVersionOutput,
272
};
273
}  // namespace ProcessFlags
274
// TODO(addaleax): Make this the canonical name, as it is more descriptive.
275
namespace ProcessInitializationFlags = ProcessFlags;
276
277
class NODE_EXTERN InitializationResult {
278
 public:
279
  virtual ~InitializationResult();
280
281
  // Returns a suggested process exit code.
282
  virtual int exit_code() const = 0;
283
284
  // Returns 'true' if initialization was aborted early due to errors.
285
  virtual bool early_return() const = 0;
286
287
  // Returns the parsed list of non-Node.js arguments.
288
  virtual const std::vector<std::string>& args() const = 0;
289
290
  // Returns the parsed list of Node.js arguments.
291
  virtual const std::vector<std::string>& exec_args() const = 0;
292
293
  // Returns an array of errors. Note that these may be warnings
294
  // whose existence does not imply a non-zero exit code.
295
  virtual const std::vector<std::string>& errors() const = 0;
296
297
  // If kNoInitializeNodeV8Platform was not specified, the global Node.js
298
  // platform instance.
299
  virtual MultiIsolatePlatform* platform() const = 0;
300
301
 private:
302
5709
  InitializationResult() = default;
303
  friend class InitializationResultImpl;
304
};
305
306
// TODO(addaleax): Officially deprecate this and replace it with something
307
// better suited for a public embedder API.
308
NODE_EXTERN int Start(int argc, char* argv[]);
309
310
// Tear down Node.js while it is running (there are active handles
311
// in the loop and / or actively executing JavaScript code).
312
NODE_EXTERN int Stop(Environment* env);
313
314
// Set up per-process state needed to run Node.js. This will consume arguments
315
// from argv, fill exec_argv, and possibly add errors resulting from parsing
316
// the arguments to `errors`. The return value is a suggested exit code for the
317
// program; If it is 0, then initializing Node.js succeeded.
318
// This runs a subset of the initialization performed by
319
// InitializeOncePerProcess(), which supersedes this function.
320
// The subset is roughly equivalent to the one given by
321
// `ProcessInitializationFlags::kLegacyInitializeNodeWithArgsBehavior`.
322
NODE_DEPRECATED("Use InitializeOncePerProcess() instead",
323
                NODE_EXTERN int InitializeNodeWithArgs(
324
                    std::vector<std::string>* argv,
325
                    std::vector<std::string>* exec_argv,
326
                    std::vector<std::string>* errors,
327
                    ProcessInitializationFlags::Flags flags =
328
                        ProcessInitializationFlags::kNoFlags));
329
330
// Set up per-process state needed to run Node.js. This will consume arguments
331
// from args, and return information about the initialization success,
332
// including the arguments split into argv/exec_argv, a list of potential
333
// errors encountered during initialization, and a potential suggested
334
// exit code.
335
NODE_EXTERN std::unique_ptr<InitializationResult> InitializeOncePerProcess(
336
    const std::vector<std::string>& args,
337
    ProcessInitializationFlags::Flags flags =
338
        ProcessInitializationFlags::kNoFlags);
339
// Undoes the initialization performed by InitializeOncePerProcess(),
340
// where cleanup is necessary.
341
NODE_EXTERN void TearDownOncePerProcess();
342
// Convenience overload for specifying multiple flags without having
343
// to worry about casts.
344
7
inline std::unique_ptr<InitializationResult> InitializeOncePerProcess(
345
    const std::vector<std::string>& args,
346
    std::initializer_list<ProcessInitializationFlags::Flags> list) {
347
7
  uint64_t flags_accum = ProcessInitializationFlags::kNoFlags;
348
21
  for (const auto flag : list) flags_accum |= static_cast<uint64_t>(flag);
349
  return InitializeOncePerProcess(
350
7
      args, static_cast<ProcessInitializationFlags::Flags>(flags_accum));
351
}
352
353
enum OptionEnvvarSettings {
354
  // Allow the options to be set via the environment variable, like
355
  // `NODE_OPTIONS`.
356
  kAllowedInEnvvar = 0,
357
  // Disallow the options to be set via the environment variable, like
358
  // `NODE_OPTIONS`.
359
  kDisallowedInEnvvar = 1,
360
  // Deprecated, use kAllowedInEnvvar instead.
361
  kAllowedInEnvironment = kAllowedInEnvvar,
362
  // Deprecated, use kDisallowedInEnvvar instead.
363
  kDisallowedInEnvironment = kDisallowedInEnvvar,
364
};
365
366
// Process the arguments and set up the per-process options.
367
// If the `settings` is set as OptionEnvvarSettings::kAllowedInEnvvar, the
368
// options that are allowed in the environment variable are processed. Options
369
// that are disallowed to be set via environment variable are processed as
370
// errors.
371
// Otherwise all the options that are disallowed (and those are allowed) to be
372
// set via environment variable are processed.
373
NODE_EXTERN int ProcessGlobalArgs(std::vector<std::string>* args,
374
                      std::vector<std::string>* exec_args,
375
                      std::vector<std::string>* errors,
376
                      OptionEnvvarSettings settings);
377
378
class NodeArrayBufferAllocator;
379
380
// An ArrayBuffer::Allocator class with some Node.js-specific tweaks. If you do
381
// not have to use another allocator, using this class is recommended:
382
// - It supports Buffer.allocUnsafe() and Buffer.allocUnsafeSlow() with
383
//   uninitialized memory.
384
// - It supports transferring, rather than copying, ArrayBuffers when using
385
//   MessagePorts.
386
class NODE_EXTERN ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
387
 public:
388
  // If `always_debug` is true, create an ArrayBuffer::Allocator instance
389
  // that performs additional integrity checks (e.g. make sure that only memory
390
  // that was allocated by the it is also freed by it).
391
  // This can also be set using the --debug-arraybuffer-allocations flag.
392
  static std::unique_ptr<ArrayBufferAllocator> Create(
393
      bool always_debug = false);
394
395
 private:
396
  virtual NodeArrayBufferAllocator* GetImpl() = 0;
397
398
  friend class IsolateData;
399
};
400
401
// Legacy equivalents for ArrayBufferAllocator::Create().
402
NODE_EXTERN ArrayBufferAllocator* CreateArrayBufferAllocator();
403
NODE_EXTERN void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator);
404
405
class NODE_EXTERN IsolatePlatformDelegate {
406
 public:
407
  virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() = 0;
408
  virtual bool IdleTasksEnabled() = 0;
409
};
410
411
class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
412
 public:
413
11288
  ~MultiIsolatePlatform() override = default;
414
  // Returns true if work was dispatched or executed. New tasks that are
415
  // posted during flushing of the queue are postponed until the next
416
  // flushing.
417
  virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
418
  virtual void DrainTasks(v8::Isolate* isolate) = 0;
419
420
  // This needs to be called between the calls to `Isolate::Allocate()` and
421
  // `Isolate::Initialize()`, so that initialization can already start
422
  // using the platform.
423
  // When using `NewIsolate()`, this is taken care of by that function.
424
  // This function may only be called once per `Isolate`.
425
  virtual void RegisterIsolate(v8::Isolate* isolate,
426
                               struct uv_loop_s* loop) = 0;
427
  // This method can be used when an application handles task scheduling on its
428
  // own through `IsolatePlatformDelegate`. Upon registering an isolate with
429
  // this overload any other method in this class with the exception of
430
  // `UnregisterIsolate` *must not* be used on that isolate.
431
  virtual void RegisterIsolate(v8::Isolate* isolate,
432
                               IsolatePlatformDelegate* delegate) = 0;
433
434
  // This function may only be called once per `Isolate`, and discard any
435
  // pending delayed tasks scheduled for that isolate.
436
  // This needs to be called right before calling `Isolate::Dispose()`.
437
  virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
438
439
  // The platform should call the passed function once all state associated
440
  // with the given isolate has been cleaned up. This can, but does not have to,
441
  // happen asynchronously.
442
  virtual void AddIsolateFinishedCallback(v8::Isolate* isolate,
443
                                          void (*callback)(void*),
444
                                          void* data) = 0;
445
446
  static std::unique_ptr<MultiIsolatePlatform> Create(
447
      int thread_pool_size,
448
      v8::TracingController* tracing_controller = nullptr,
449
      v8::PageAllocator* page_allocator = nullptr);
450
};
451
452
enum IsolateSettingsFlags {
453
  MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
454
  DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1,
455
  SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK = 1 << 2,
456
  SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK = 1 << 3
457
};
458
459
struct IsolateSettings {
460
  uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
461
      DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
462
  v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
463
464
  // Error handling callbacks
465
  v8::Isolate::AbortOnUncaughtExceptionCallback
466
      should_abort_on_uncaught_exception_callback = nullptr;
467
  v8::FatalErrorCallback fatal_error_callback = nullptr;
468
  v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
469
470
  // Miscellaneous callbacks
471
  v8::PromiseRejectCallback promise_reject_callback = nullptr;
472
  v8::AllowWasmCodeGenerationCallback
473
      allow_wasm_code_generation_callback = nullptr;
474
};
475
476
// Overriding IsolateSettings may produce unexpected behavior
477
// in Node.js core functionality, so proceed at your own risk.
478
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
479
                                     const IsolateSettings& settings);
480
481
// Set a number of callbacks for the `isolate`, in particular the Node.js
482
// uncaught exception listener.
483
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
484
485
// Creates a new isolate with Node.js-specific settings.
486
// This is a convenience method equivalent to using SetIsolateCreateParams(),
487
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
488
// Isolate::Initialize(), and SetIsolateUpForNode().
489
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
490
                                    struct uv_loop_s* event_loop,
491
                                    MultiIsolatePlatform* platform = nullptr);
492
NODE_EXTERN v8::Isolate* NewIsolate(
493
    std::shared_ptr<ArrayBufferAllocator> allocator,
494
    struct uv_loop_s* event_loop,
495
    MultiIsolatePlatform* platform);
496
497
// Creates a new context with Node.js-specific tweaks.
498
NODE_EXTERN v8::Local<v8::Context> NewContext(
499
    v8::Isolate* isolate,
500
    v8::Local<v8::ObjectTemplate> object_template =
501
        v8::Local<v8::ObjectTemplate>());
502
503
// Runs Node.js-specific tweaks on an already constructed context
504
// Return value indicates success of operation
505
NODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context);
506
507
// If `platform` is passed, it will be used to register new Worker instances.
508
// It can be `nullptr`, in which case creating new Workers inside of
509
// Environments that use this `IsolateData` will not work.
510
NODE_EXTERN IsolateData* CreateIsolateData(
511
    v8::Isolate* isolate,
512
    struct uv_loop_s* loop,
513
    MultiIsolatePlatform* platform = nullptr,
514
    ArrayBufferAllocator* allocator = nullptr);
515
NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
516
517
struct ThreadId {
518
  uint64_t id = static_cast<uint64_t>(-1);
519
};
520
NODE_EXTERN ThreadId AllocateEnvironmentThreadId();
521
522
namespace EnvironmentFlags {
523
enum Flags : uint64_t {
524
  kNoFlags = 0,
525
  // Use the default behaviour for Node.js instances.
526
  kDefaultFlags = 1 << 0,
527
  // Controls whether this Environment is allowed to affect per-process state
528
  // (e.g. cwd, process title, uid, etc.).
529
  // This is set when using kDefaultFlags.
530
  kOwnsProcessState = 1 << 1,
531
  // Set if this Environment instance is associated with the global inspector
532
  // handling code (i.e. listening on SIGUSR1).
533
  // This is set when using kDefaultFlags.
534
  kOwnsInspector = 1 << 2,
535
  // Set if Node.js should not run its own esm loader. This is needed by some
536
  // embedders, because it's possible for the Node.js esm loader to conflict
537
  // with another one in an embedder environment, e.g. Blink's in Chromium.
538
  kNoRegisterESMLoader = 1 << 3,
539
  // Set this flag to make Node.js track "raw" file descriptors, i.e. managed
540
  // by fs.open() and fs.close(), and close them during FreeEnvironment().
541
  kTrackUnmanagedFds = 1 << 4,
542
  // Set this flag to force hiding console windows when spawning child
543
  // processes. This is usually used when embedding Node.js in GUI programs on
544
  // Windows.
545
  kHideConsoleWindows = 1 << 5,
546
  // Set this flag to disable loading native addons via `process.dlopen`.
547
  // This environment flag is especially important for worker threads
548
  // so that a worker thread can't load a native addon even if `execArgv`
549
  // is overwritten and `--no-addons` is not specified but was specified
550
  // for this Environment instance.
551
  kNoNativeAddons = 1 << 6,
552
  // Set this flag to disable searching modules from global paths like
553
  // $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
554
  // do not expect to have their behaviors changed because of globally
555
  // installed modules.
556
  kNoGlobalSearchPaths = 1 << 7,
557
  // Do not export browser globals like setTimeout, console, etc.
558
  kNoBrowserGlobals = 1 << 8,
559
  // Controls whether or not the Environment should call V8Inspector::create().
560
  // This control is needed by embedders who may not want to initialize the V8
561
  // inspector in situations where one has already been created,
562
  // e.g. Blink's in Chromium.
563
  kNoCreateInspector = 1 << 9
564
};
565
}  // namespace EnvironmentFlags
566
567
struct InspectorParentHandle {
568
  virtual ~InspectorParentHandle();
569
};
570
571
// TODO(addaleax): Maybe move per-Environment options parsing here.
572
// Returns nullptr when the Environment cannot be created e.g. there are
573
// pending JavaScript exceptions.
574
NODE_EXTERN Environment* CreateEnvironment(
575
    IsolateData* isolate_data,
576
    v8::Local<v8::Context> context,
577
    const std::vector<std::string>& args,
578
    const std::vector<std::string>& exec_args,
579
    EnvironmentFlags::Flags flags = EnvironmentFlags::kDefaultFlags,
580
    ThreadId thread_id = {} /* allocates a thread id automatically */,
581
    std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
582
583
// Returns a handle that can be passed to `LoadEnvironment()`, making the
584
// child Environment accessible to the inspector as if it were a Node.js Worker.
585
// `child_thread_id` can be created using `AllocateEnvironmentThreadId()`
586
// and then later passed on to `CreateEnvironment()` to create the child
587
// Environment, together with the inspector handle.
588
// This method should not be called while the parent Environment is active
589
// on another thread.
590
NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
591
    Environment* parent_env,
592
    ThreadId child_thread_id,
593
    const char* child_url);
594
595
struct StartExecutionCallbackInfo {
596
  v8::Local<v8::Object> process_object;
597
  v8::Local<v8::Function> native_require;
598
};
599
600
using StartExecutionCallback =
601
    std::function<v8::MaybeLocal<v8::Value>(const StartExecutionCallbackInfo&)>;
602
603
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
604
    Environment* env,
605
    StartExecutionCallback cb);
606
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
607
    Environment* env,
608
    const char* main_script_source_utf8);
609
NODE_EXTERN void FreeEnvironment(Environment* env);
610
611
// Set a callback that is called when process.exit() is called from JS,
612
// overriding the default handler.
613
// It receives the Environment* instance and the exit code as arguments.
614
// This could e.g. call Stop(env); in order to terminate execution and stop
615
// the event loop.
616
// The default handler disposes of the global V8 platform instance, if one is
617
// being used, and calls exit().
618
NODE_EXTERN void SetProcessExitHandler(
619
    Environment* env,
620
    std::function<void(Environment*, int)>&& handler);
621
NODE_EXTERN void DefaultProcessExitHandler(Environment* env, int exit_code);
622
623
// This may return nullptr if context is not associated with a Node instance.
624
NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
625
NODE_EXTERN IsolateData* GetEnvironmentIsolateData(Environment* env);
626
NODE_EXTERN ArrayBufferAllocator* GetArrayBufferAllocator(IsolateData* data);
627
628
NODE_EXTERN void OnFatalError(const char* location, const char* message);
629
NODE_EXTERN void PromiseRejectCallback(v8::PromiseRejectMessage message);
630
NODE_EXTERN bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
631
                                            v8::Local<v8::String>);
632
NODE_EXTERN bool ShouldAbortOnUncaughtException(v8::Isolate* isolate);
633
NODE_EXTERN v8::MaybeLocal<v8::Value> PrepareStackTraceCallback(
634
    v8::Local<v8::Context> context,
635
    v8::Local<v8::Value> exception,
636
    v8::Local<v8::Array> trace);
637
638
// Writes a diagnostic report to a file. If filename is not provided, the
639
// default filename includes the date, time, PID, and a sequence number.
640
// The report's JavaScript stack trace is taken from err, if present.
641
// If isolate is nullptr, no information about the JavaScript environment
642
// is included in the report.
643
// Returns the filename of the written report.
644
NODE_EXTERN std::string TriggerNodeReport(v8::Isolate* isolate,
645
                                          const char* message,
646
                                          const char* trigger,
647
                                          const std::string& filename,
648
                                          v8::Local<v8::Value> error);
649
NODE_EXTERN std::string TriggerNodeReport(Environment* env,
650
                                          const char* message,
651
                                          const char* trigger,
652
                                          const std::string& filename,
653
                                          v8::Local<v8::Value> error);
654
NODE_EXTERN void GetNodeReport(v8::Isolate* isolate,
655
                               const char* message,
656
                               const char* trigger,
657
                               v8::Local<v8::Value> error,
658
                               std::ostream& out);
659
NODE_EXTERN void GetNodeReport(Environment* env,
660
                               const char* message,
661
                               const char* trigger,
662
                               v8::Local<v8::Value> error,
663
                               std::ostream& out);
664
665
// This returns the MultiIsolatePlatform used for an Environment or IsolateData
666
// instance, if one exists.
667
NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env);
668
NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env);
669
670
NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
671
    NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
672
        int thread_pool_size,
673
        v8::TracingController* tracing_controller));
674
NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
675
    NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform));
676
677
// Get/set the currently active tracing controller. Using CreatePlatform()
678
// will implicitly set this by default. This is global and should be initialized
679
// along with the v8::Platform instance that is being used. `controller`
680
// is allowed to be `nullptr`.
681
// This is used for tracing events from Node.js itself. V8 uses the tracing
682
// controller returned from the active `v8::Platform` instance.
683
NODE_EXTERN v8::TracingController* GetTracingController();
684
NODE_EXTERN void SetTracingController(v8::TracingController* controller);
685
686
// Run `process.emit('beforeExit')` as it would usually happen when Node.js is
687
// run in standalone mode.
688
NODE_EXTERN v8::Maybe<bool> EmitProcessBeforeExit(Environment* env);
689
NODE_DEPRECATED("Use Maybe version (EmitProcessBeforeExit) instead",
690
    NODE_EXTERN void EmitBeforeExit(Environment* env));
691
// Run `process.emit('exit')` as it would usually happen when Node.js is run
692
// in standalone mode. The return value corresponds to the exit code.
693
NODE_EXTERN v8::Maybe<int> EmitProcessExit(Environment* env);
694
NODE_DEPRECATED("Use Maybe version (EmitProcessExit) instead",
695
    NODE_EXTERN int EmitExit(Environment* env));
696
697
// Runs hooks added through `AtExit()`. This is part of `FreeEnvironment()`,
698
// so calling it manually is typically not necessary.
699
NODE_EXTERN void RunAtExit(Environment* env);
700
701
// This may return nullptr if the current v8::Context is not associated
702
// with a Node instance.
703
NODE_EXTERN struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate);
704
705
// Runs the main loop for a given Environment. This roughly performs the
706
// following steps:
707
// 1. Call uv_run() on the event loop until it is drained.
708
// 2. Call platform->DrainTasks() on the associated platform/isolate.
709
//   3. If the event loop is alive again, go to Step 1.
710
// 4. Call EmitProcessBeforeExit().
711
//   5. If the event loop is alive again, go to Step 1.
712
// 6. Call EmitProcessExit() and forward the return value.
713
// If at any point node::Stop() is called, the function will attempt to return
714
// as soon as possible, returning an empty `Maybe`.
715
// This function only works if `env` has an associated `MultiIsolatePlatform`.
716
NODE_EXTERN v8::Maybe<int> SpinEventLoop(Environment* env);
717
718
class NODE_EXTERN CommonEnvironmentSetup {
719
 public:
720
  ~CommonEnvironmentSetup();
721
722
  // Create a new CommonEnvironmentSetup, that is, a group of objects that
723
  // together form the typical setup for a single Node.js Environment instance.
724
  // If any error occurs, `*errors` will be populated and the returned pointer
725
  // will be empty.
726
  // env_args will be passed through as arguments to CreateEnvironment(), after
727
  // `isolate_data` and `context`.
728
  template <typename... EnvironmentArgs>
729
  static std::unique_ptr<CommonEnvironmentSetup> Create(
730
      MultiIsolatePlatform* platform,
731
      std::vector<std::string>* errors,
732
      EnvironmentArgs&&... env_args);
733
734
  struct uv_loop_s* event_loop() const;
735
  std::shared_ptr<ArrayBufferAllocator> array_buffer_allocator() const;
736
  v8::Isolate* isolate() const;
737
  IsolateData* isolate_data() const;
738
  Environment* env() const;
739
  v8::Local<v8::Context> context() const;
740
741
  CommonEnvironmentSetup(const CommonEnvironmentSetup&) = delete;
742
  CommonEnvironmentSetup& operator=(const CommonEnvironmentSetup&) = delete;
743
  CommonEnvironmentSetup(CommonEnvironmentSetup&&) = delete;
744
  CommonEnvironmentSetup& operator=(CommonEnvironmentSetup&&) = delete;
745
746
 private:
747
  struct Impl;
748
  Impl* impl_;
749
  CommonEnvironmentSetup(
750
      MultiIsolatePlatform*,
751
      std::vector<std::string>*,
752
      std::function<Environment*(const CommonEnvironmentSetup*)>);
753
};
754
755
// Implementation for CommonEnvironmentSetup::Create
756
template <typename... EnvironmentArgs>
757
7
std::unique_ptr<CommonEnvironmentSetup> CommonEnvironmentSetup::Create(
758
    MultiIsolatePlatform* platform,
759
    std::vector<std::string>* errors,
760
    EnvironmentArgs&&... env_args) {
761
7
  auto ret = std::unique_ptr<CommonEnvironmentSetup>(new CommonEnvironmentSetup(
762
      platform, errors,
763
7
      [&](const CommonEnvironmentSetup* setup) -> Environment* {
764
21
        return CreateEnvironment(
765
            setup->isolate_data(), setup->context(),
766
14
            std::forward<EnvironmentArgs>(env_args)...);
767
      }));
768
7
  if (!errors->empty()) ret.reset();
769
7
  return ret;
770
}
771
772
/* Converts a unixtime to V8 Date */
773
NODE_DEPRECATED("Use v8::Date::New() directly",
774
                inline v8::Local<v8::Value> NODE_UNIXTIME_V8(double time) {
775
                  return v8::Date::New(
776
                             v8::Isolate::GetCurrent()->GetCurrentContext(),
777
                             1000 * time)
778
                      .ToLocalChecked();
779
                })
780
#define NODE_UNIXTIME_V8 node::NODE_UNIXTIME_V8
781
NODE_DEPRECATED("Use v8::Date::ValueOf() directly",
782
                inline double NODE_V8_UNIXTIME(v8::Local<v8::Date> date) {
783
  return date->ValueOf() / 1000;
784
})
785
#define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME
786
787
#define NODE_DEFINE_CONSTANT(target, constant)                                \
788
  do {                                                                        \
789
    v8::Isolate* isolate = target->GetIsolate();                              \
790
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
791
    v8::Local<v8::String> constant_name =                                     \
792
        v8::String::NewFromUtf8(isolate, #constant,                           \
793
            v8::NewStringType::kInternalized).ToLocalChecked();               \
794
    v8::Local<v8::Number> constant_value =                                    \
795
        v8::Number::New(isolate, static_cast<double>(constant));              \
796
    v8::PropertyAttribute constant_attributes =                               \
797
        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
798
    (target)->DefineOwnProperty(context,                                      \
799
                                constant_name,                                \
800
                                constant_value,                               \
801
                                constant_attributes).Check();                 \
802
  }                                                                           \
803
  while (0)
804
805
#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant)                         \
806
  do {                                                                        \
807
    v8::Isolate* isolate = target->GetIsolate();                              \
808
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
809
    v8::Local<v8::String> constant_name =                                     \
810
        v8::String::NewFromUtf8(isolate, #constant,                           \
811
                                v8::NewStringType::kInternalized)             \
812
                                  .ToLocalChecked();                          \
813
    v8::Local<v8::Number> constant_value =                                    \
814
        v8::Number::New(isolate, static_cast<double>(constant));              \
815
    v8::PropertyAttribute constant_attributes =                               \
816
        static_cast<v8::PropertyAttribute>(v8::ReadOnly |                     \
817
                                           v8::DontDelete |                   \
818
                                           v8::DontEnum);                     \
819
    (target)->DefineOwnProperty(context,                                      \
820
                                constant_name,                                \
821
                                constant_value,                               \
822
                                constant_attributes).Check();                 \
823
  }                                                                           \
824
  while (0)
825
826
// Used to be a macro, hence the uppercase name.
827
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
828
                            const char* name,
829
                            v8::FunctionCallback callback) {
830
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
831
  v8::HandleScope handle_scope(isolate);
832
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
833
                                                                callback);
834
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
835
      v8::NewStringType::kInternalized).ToLocalChecked();
836
  t->SetClassName(fn_name);
837
  recv->Set(fn_name, t);
838
}
839
840
// Used to be a macro, hence the uppercase name.
841
inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
842
                            const char* name,
843
                            v8::FunctionCallback callback) {
844
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
845
  v8::HandleScope handle_scope(isolate);
846
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
847
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
848
                                                                callback);
849
  v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
850
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
851
      v8::NewStringType::kInternalized).ToLocalChecked();
852
  fn->SetName(fn_name);
853
  recv->Set(context, fn_name, fn).Check();
854
}
855
#define NODE_SET_METHOD node::NODE_SET_METHOD
856
857
// Used to be a macro, hence the uppercase name.
858
// Not a template because it only makes sense for FunctionTemplates.
859
inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
860
                                      const char* name,
861
                                      v8::FunctionCallback callback) {
862
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
863
  v8::HandleScope handle_scope(isolate);
864
  v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
865
  v8::Local<v8::FunctionTemplate> t =
866
      v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
867
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
868
      v8::NewStringType::kInternalized).ToLocalChecked();
869
  t->SetClassName(fn_name);
870
  recv->PrototypeTemplate()->Set(fn_name, t);
871
}
872
#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
873
874
// BINARY is a deprecated alias of LATIN1.
875
// BASE64URL is not currently exposed to the JavaScript side.
876
enum encoding {
877
  ASCII,
878
  UTF8,
879
  BASE64,
880
  UCS2,
881
  BINARY,
882
  HEX,
883
  BUFFER,
884
  BASE64URL,
885
  LATIN1 = BINARY
886
};
887
888
NODE_EXTERN enum encoding ParseEncoding(
889
    v8::Isolate* isolate,
890
    v8::Local<v8::Value> encoding_v,
891
    enum encoding default_encoding = LATIN1);
892
893
NODE_EXTERN void FatalException(v8::Isolate* isolate,
894
                                const v8::TryCatch& try_catch);
895
896
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
897
                                        const char* buf,
898
                                        size_t len,
899
                                        enum encoding encoding = LATIN1);
900
901
// Warning: This reverses endianness on Big Endian platforms, even though the
902
// signature using uint16_t implies that it should not.
903
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
904
                                        const uint16_t* buf,
905
                                        size_t len);
906
907
// Returns -1 if the handle was not valid for decoding
908
NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
909
                                v8::Local<v8::Value>,
910
                                enum encoding encoding = LATIN1);
911
// returns bytes written.
912
NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
913
                                char* buf,
914
                                size_t buflen,
915
                                v8::Local<v8::Value>,
916
                                enum encoding encoding = LATIN1);
917
#ifdef _WIN32
918
NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
919
    v8::Isolate* isolate,
920
    int errorno,
921
    const char* syscall = nullptr,
922
    const char* msg = "",
923
    const char* path = nullptr);
924
#endif
925
926
const char* signo_string(int errorno);
927
928
929
typedef void (*addon_register_func)(
930
    v8::Local<v8::Object> exports,
931
    v8::Local<v8::Value> module,
932
    void* priv);
933
934
typedef void (*addon_context_register_func)(
935
    v8::Local<v8::Object> exports,
936
    v8::Local<v8::Value> module,
937
    v8::Local<v8::Context> context,
938
    void* priv);
939
940
enum ModuleFlags {
941
  kLinked = 0x02
942
};
943
944
struct node_module {
945
  int nm_version;
946
  unsigned int nm_flags;
947
  void* nm_dso_handle;
948
  const char* nm_filename;
949
  node::addon_register_func nm_register_func;
950
  node::addon_context_register_func nm_context_register_func;
951
  const char* nm_modname;
952
  void* nm_priv;
953
  struct node_module* nm_link;
954
};
955
956
extern "C" NODE_EXTERN void node_module_register(void* mod);
957
958
#ifdef _WIN32
959
# define NODE_MODULE_EXPORT __declspec(dllexport)
960
#else
961
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
962
#endif
963
964
#ifdef NODE_SHARED_MODE
965
# define NODE_CTOR_PREFIX
966
#else
967
# define NODE_CTOR_PREFIX static
968
#endif
969
970
#if defined(_MSC_VER)
971
#define NODE_C_CTOR(fn)                                               \
972
  NODE_CTOR_PREFIX void __cdecl fn(void);                             \
973
  namespace {                                                         \
974
  struct fn##_ {                                                      \
975
    fn##_() { fn(); };                                                \
976
  } fn##_v_;                                                          \
977
  }                                                                   \
978
  NODE_CTOR_PREFIX void __cdecl fn(void)
979
#else
980
#define NODE_C_CTOR(fn)                                               \
981
  NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
982
  NODE_CTOR_PREFIX void fn(void)
983
#endif
984
985
#define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
986
  extern "C" {                                                        \
987
    static node::node_module _module =                                \
988
    {                                                                 \
989
      NODE_MODULE_VERSION,                                            \
990
      flags,                                                          \
991
      NULL,  /* NOLINT (readability/null_usage) */                    \
992
      __FILE__,                                                       \
993
      (node::addon_register_func) (regfunc),                          \
994
      NULL,  /* NOLINT (readability/null_usage) */                    \
995
      NODE_STRINGIFY(modname),                                        \
996
      priv,                                                           \
997
      NULL   /* NOLINT (readability/null_usage) */                    \
998
    };                                                                \
999
    NODE_C_CTOR(_register_ ## modname) {                              \
1000
      node_module_register(&_module);                                 \
1001
    }                                                                 \
1002
  }
1003
1004
#define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
1005
  extern "C" {                                                        \
1006
    static node::node_module _module =                                \
1007
    {                                                                 \
1008
      NODE_MODULE_VERSION,                                            \
1009
      flags,                                                          \
1010
      NULL,  /* NOLINT (readability/null_usage) */                    \
1011
      __FILE__,                                                       \
1012
      NULL,  /* NOLINT (readability/null_usage) */                    \
1013
      (node::addon_context_register_func) (regfunc),                  \
1014
      NODE_STRINGIFY(modname),                                        \
1015
      priv,                                                           \
1016
      NULL  /* NOLINT (readability/null_usage) */                     \
1017
    };                                                                \
1018
    NODE_C_CTOR(_register_ ## modname) {                              \
1019
      node_module_register(&_module);                                 \
1020
    }                                                                 \
1021
  }
1022
1023
// Usage: `NODE_MODULE(NODE_GYP_MODULE_NAME, InitializerFunction)`
1024
// If no NODE_MODULE is declared, Node.js looks for the well-known
1025
// symbol `node_register_module_v${NODE_MODULE_VERSION}`.
1026
#define NODE_MODULE(modname, regfunc)                                 \
1027
  NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
1028
1029
#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
1030
  /* NOLINTNEXTLINE (readability/null_usage) */                       \
1031
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
1032
1033
// Embedders can use this type of binding for statically linked native bindings.
1034
// It is used the same way addon bindings are used, except that linked bindings
1035
// can be accessed through `process._linkedBinding(modname)`.
1036
#define NODE_MODULE_LINKED(modname, regfunc)                               \
1037
  /* NOLINTNEXTLINE (readability/null_usage) */                            \
1038
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL,                      \
1039
                              node::ModuleFlags::kLinked)
1040
1041
/*
1042
 * For backward compatibility in add-on modules.
1043
 */
1044
#define NODE_MODULE_DECL /* nothing */
1045
1046
#define NODE_MODULE_INITIALIZER_BASE node_register_module_v
1047
1048
#define NODE_MODULE_INITIALIZER_X(base, version)                      \
1049
    NODE_MODULE_INITIALIZER_X_HELPER(base, version)
1050
1051
#define NODE_MODULE_INITIALIZER_X_HELPER(base, version) base##version
1052
1053
#define NODE_MODULE_INITIALIZER                                       \
1054
  NODE_MODULE_INITIALIZER_X(NODE_MODULE_INITIALIZER_BASE,             \
1055
      NODE_MODULE_VERSION)
1056
1057
#define NODE_MODULE_INIT()                                            \
1058
  extern "C" NODE_MODULE_EXPORT void                                  \
1059
  NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,              \
1060
                          v8::Local<v8::Value> module,                \
1061
                          v8::Local<v8::Context> context);            \
1062
  NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME,                     \
1063
                            NODE_MODULE_INITIALIZER)                  \
1064
  void NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,         \
1065
                               v8::Local<v8::Value> module,           \
1066
                               v8::Local<v8::Context> context)
1067
1068
// Allows embedders to add a binding to the current Environment* that can be
1069
// accessed through process._linkedBinding() in the target Environment and all
1070
// Worker threads that it creates.
1071
// In each variant, the registration function needs to be usable at least for
1072
// the time during which the Environment exists.
1073
NODE_EXTERN void AddLinkedBinding(Environment* env, const node_module& mod);
1074
NODE_EXTERN void AddLinkedBinding(Environment* env,
1075
                                  const struct napi_module& mod);
1076
NODE_EXTERN void AddLinkedBinding(Environment* env,
1077
                                  const char* name,
1078
                                  addon_context_register_func fn,
1079
                                  void* priv);
1080
1081
/* Registers a callback with the passed-in Environment instance. The callback
1082
 * is called after the event loop exits, but before the VM is disposed.
1083
 * Callbacks are run in reverse order of registration, i.e. newest first.
1084
 */
1085
NODE_EXTERN void AtExit(Environment* env,
1086
                        void (*cb)(void* arg),
1087
                        void* arg);
1088
1089
typedef double async_id;
1090
struct async_context {
1091
  ::node::async_id async_id;
1092
  ::node::async_id trigger_async_id;
1093
};
1094
1095
/* This is a lot like node::AtExit, except that the hooks added via this
1096
 * function are run before the AtExit ones and will always be registered
1097
 * for the current Environment instance.
1098
 * These functions are safe to use in an addon supporting multiple
1099
 * threads/isolates. */
1100
NODE_EXTERN void AddEnvironmentCleanupHook(v8::Isolate* isolate,
1101
                                           void (*fun)(void* arg),
1102
                                           void* arg);
1103
1104
NODE_EXTERN void RemoveEnvironmentCleanupHook(v8::Isolate* isolate,
1105
                                              void (*fun)(void* arg),
1106
                                              void* arg);
1107
1108
/* These are async equivalents of the above. After the cleanup hook is invoked,
1109
 * `cb(cbarg)` *must* be called, and attempting to remove the cleanup hook will
1110
 * have no effect. */
1111
struct ACHHandle;
1112
struct NODE_EXTERN DeleteACHHandle { void operator()(ACHHandle*) const; };
1113
typedef std::unique_ptr<ACHHandle, DeleteACHHandle> AsyncCleanupHookHandle;
1114
1115
/* This function is not intended to be used externally, it exists to aid in
1116
 * keeping ABI compatibility between Node and Electron. */
1117
NODE_EXTERN ACHHandle* AddEnvironmentCleanupHookInternal(
1118
    v8::Isolate* isolate,
1119
    void (*fun)(void* arg, void (*cb)(void*), void* cbarg),
1120
    void* arg);
1121
6
inline AsyncCleanupHookHandle AddEnvironmentCleanupHook(
1122
    v8::Isolate* isolate,
1123
    void (*fun)(void* arg, void (*cb)(void*), void* cbarg),
1124
    void* arg) {
1125
  return AsyncCleanupHookHandle(AddEnvironmentCleanupHookInternal(isolate, fun,
1126
6
      arg));
1127
}
1128
1129
/* This function is not intended to be used externally, it exists to aid in
1130
 * keeping ABI compatibility between Node and Electron. */
1131
NODE_EXTERN void RemoveEnvironmentCleanupHookInternal(ACHHandle* holder);
1132
6
inline void RemoveEnvironmentCleanupHook(AsyncCleanupHookHandle holder) {
1133
6
  RemoveEnvironmentCleanupHookInternal(holder.get());
1134
6
}
1135
1136
// This behaves like V8's Isolate::RequestInterrupt(), but also wakes up
1137
// the event loop if it is currently idle. Interrupt requests are drained
1138
// in `FreeEnvironment()`. The passed callback can not call back into
1139
// JavaScript.
1140
// This function can be called from any thread.
1141
NODE_EXTERN void RequestInterrupt(Environment* env,
1142
                                  void (*fun)(void* arg),
1143
                                  void* arg);
1144
1145
/* Returns the id of the current execution context. If the return value is
1146
 * zero then no execution has been set. This will happen if the user handles
1147
 * I/O from native code. */
1148
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
1149
1150
/* Return same value as async_hooks.triggerAsyncId(); */
1151
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
1152
1153
/* If the native API doesn't inherit from the helper class then the callbacks
1154
 * must be triggered manually. This triggers the init() callback. The return
1155
 * value is the async id assigned to the resource.
1156
 *
1157
 * The `trigger_async_id` parameter should correspond to the resource which is
1158
 * creating the new resource, which will usually be the return value of
1159
 * `AsyncHooksGetTriggerAsyncId()`. */
1160
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
1161
                                        v8::Local<v8::Object> resource,
1162
                                        const char* name,
1163
                                        async_id trigger_async_id = -1);
1164
1165
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
1166
                                        v8::Local<v8::Object> resource,
1167
                                        v8::Local<v8::String> name,
1168
                                        async_id trigger_async_id = -1);
1169
1170
/* Emit the destroy() callback. The overload taking an `Environment*` argument
1171
 * should be used when the Isolate’s current Context is not associated with
1172
 * a Node.js Environment, or when there is no current Context, for example
1173
 * when calling this function during garbage collection. In that case, the
1174
 * `Environment*` value should have been acquired previously, e.g. through
1175
 * `GetCurrentEnvironment()`. */
1176
NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
1177
                                  async_context asyncContext);
1178
NODE_EXTERN void EmitAsyncDestroy(Environment* env,
1179
                                  async_context asyncContext);
1180
1181
class InternalCallbackScope;
1182
1183
/* This class works like `MakeCallback()` in that it sets up a specific
1184
 * asyncContext as the current one and informs the async_hooks and domains
1185
 * modules that this context is currently active.
1186
 *
1187
 * `MakeCallback()` is a wrapper around this class as well as
1188
 * `Function::Call()`. Either one of these mechanisms needs to be used for
1189
 * top-level calls into JavaScript (i.e. without any existing JS stack).
1190
 *
1191
 * This object should be stack-allocated to ensure that it is contained in a
1192
 * valid HandleScope.
1193
 *
1194
 * Exceptions happening within this scope will be treated like uncaught
1195
 * exceptions. If this behaviour is undesirable, a new `v8::TryCatch` scope
1196
 * needs to be created inside of this scope.
1197
 */
1198
class NODE_EXTERN CallbackScope {
1199
 public:
1200
  CallbackScope(v8::Isolate* isolate,
1201
                v8::Local<v8::Object> resource,
1202
                async_context asyncContext);
1203
  CallbackScope(Environment* env,
1204
                v8::Local<v8::Object> resource,
1205
                async_context asyncContext);
1206
  ~CallbackScope();
1207
1208
  void operator=(const CallbackScope&) = delete;
1209
  void operator=(CallbackScope&&) = delete;
1210
  CallbackScope(const CallbackScope&) = delete;
1211
  CallbackScope(CallbackScope&&) = delete;
1212
1213
 private:
1214
  InternalCallbackScope* private_;
1215
  v8::TryCatch try_catch_;
1216
};
1217
1218
/* An API specific to emit before/after callbacks is unnecessary because
1219
 * MakeCallback will automatically call them for you.
1220
 *
1221
 * These methods may create handles on their own, so run them inside a
1222
 * HandleScope.
1223
 *
1224
 * `asyncId` and `triggerAsyncId` should correspond to the values returned by
1225
 * `EmitAsyncInit()` and `AsyncHooksGetTriggerAsyncId()`, respectively, when the
1226
 * invoking resource was created. If these values are unknown, 0 can be passed.
1227
 * */
1228
NODE_EXTERN
1229
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
1230
                                       v8::Local<v8::Object> recv,
1231
                                       v8::Local<v8::Function> callback,
1232
                                       int argc,
1233
                                       v8::Local<v8::Value>* argv,
1234
                                       async_context asyncContext);
1235
NODE_EXTERN
1236
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
1237
                                       v8::Local<v8::Object> recv,
1238
                                       const char* method,
1239
                                       int argc,
1240
                                       v8::Local<v8::Value>* argv,
1241
                                       async_context asyncContext);
1242
NODE_EXTERN
1243
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
1244
                                       v8::Local<v8::Object> recv,
1245
                                       v8::Local<v8::String> symbol,
1246
                                       int argc,
1247
                                       v8::Local<v8::Value>* argv,
1248
                                       async_context asyncContext);
1249
1250
/* Helper class users can optionally inherit from. If
1251
 * `AsyncResource::MakeCallback()` is used, then all four callbacks will be
1252
 * called automatically. */
1253
class NODE_EXTERN AsyncResource {
1254
 public:
1255
  AsyncResource(v8::Isolate* isolate,
1256
                v8::Local<v8::Object> resource,
1257
                const char* name,
1258
                async_id trigger_async_id = -1);
1259
1260
  virtual ~AsyncResource();
1261
1262
  AsyncResource(const AsyncResource&) = delete;
1263
  void operator=(const AsyncResource&) = delete;
1264
1265
  v8::MaybeLocal<v8::Value> MakeCallback(
1266
      v8::Local<v8::Function> callback,
1267
      int argc,
1268
      v8::Local<v8::Value>* argv);
1269
1270
  v8::MaybeLocal<v8::Value> MakeCallback(
1271
      const char* method,
1272
      int argc,
1273
      v8::Local<v8::Value>* argv);
1274
1275
  v8::MaybeLocal<v8::Value> MakeCallback(
1276
      v8::Local<v8::String> symbol,
1277
      int argc,
1278
      v8::Local<v8::Value>* argv);
1279
1280
  v8::Local<v8::Object> get_resource();
1281
  async_id get_async_id() const;
1282
  async_id get_trigger_async_id() const;
1283
1284
 protected:
1285
  class NODE_EXTERN CallbackScope : public node::CallbackScope {
1286
   public:
1287
    explicit CallbackScope(AsyncResource* res);
1288
  };
1289
1290
 private:
1291
  Environment* env_;
1292
  v8::Global<v8::Object> resource_;
1293
  async_context async_context_;
1294
};
1295
1296
#ifndef _WIN32
1297
// Register a signal handler without interrupting any handlers that node
1298
// itself needs. This does override handlers registered through
1299
// process.on('SIG...', function() { ... }). The `reset_handler` flag indicates
1300
// whether the signal handler for the given signal should be reset to its
1301
// default value before executing the handler (i.e. it works like SA_RESETHAND).
1302
// The `reset_handler` flag is invalid when `signal` is SIGSEGV.
1303
NODE_EXTERN
1304
void RegisterSignalHandler(int signal,
1305
                           void (*handler)(int signal,
1306
                                           siginfo_t* info,
1307
                                           void* ucontext),
1308
                           bool reset_handler = false);
1309
#endif  // _WIN32
1310
1311
}  // namespace node
1312
1313
#endif  // SRC_NODE_H_