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