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