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