Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:05:22

0001 // Copyright Joyent, Inc. and other Node contributors.
0002 //
0003 // Permission is hereby granted, free of charge, to any person obtaining a
0004 // copy of this software and associated documentation files (the
0005 // "Software"), to deal in the Software without restriction, including
0006 // without limitation the rights to use, copy, modify, merge, publish,
0007 // distribute, sublicense, and/or sell copies of the Software, and to permit
0008 // persons to whom the Software is furnished to do so, subject to the
0009 // following conditions:
0010 //
0011 // The above copyright notice and this permission notice shall be included
0012 // in all copies or substantial portions of the Software.
0013 //
0014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0015 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0016 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
0017 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
0018 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0019 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0020 // USE OR OTHER DEALINGS IN THE SOFTWARE.
0021 
0022 #ifndef SRC_NODE_H_
0023 #define SRC_NODE_H_
0024 
0025 #ifdef _WIN32
0026 # ifndef BUILDING_NODE_EXTENSION
0027 #  define NODE_EXTERN __declspec(dllexport)
0028 # else
0029 #  define NODE_EXTERN __declspec(dllimport)
0030 # endif
0031 #else
0032 # define NODE_EXTERN __attribute__((visibility("default")))
0033 #endif
0034 
0035 // Declarations annotated with NODE_EXTERN_PRIVATE do not form part of
0036 // the public API. They are implementation details that can and will
0037 // change between releases, even in semver patch releases. Do not use
0038 // any such symbol in external code.
0039 #ifdef NODE_SHARED_MODE
0040 #define NODE_EXTERN_PRIVATE NODE_EXTERN
0041 #else
0042 #define NODE_EXTERN_PRIVATE
0043 #endif
0044 
0045 #ifdef BUILDING_NODE_EXTENSION
0046 # undef BUILDING_V8_SHARED
0047 # undef BUILDING_UV_SHARED
0048 # define USING_V8_SHARED 1
0049 # define USING_UV_SHARED 1
0050 #endif
0051 
0052 // This should be defined in make system.
0053 // See issue https://github.com/nodejs/node-v0.x-archive/issues/1236
0054 #if defined(__MINGW32__) || defined(_MSC_VER)
0055 #ifndef _WIN32_WINNT
0056 # define _WIN32_WINNT 0x0600  // Windows Server 2008
0057 #endif
0058 
0059 #ifndef NOMINMAX
0060 # define NOMINMAX
0061 #endif
0062 
0063 #endif
0064 
0065 #if defined(_MSC_VER)
0066 #define PATH_MAX MAX_PATH
0067 #endif
0068 
0069 #ifdef _WIN32
0070 # define SIGKILL 9
0071 #endif
0072 
0073 #include "v8.h"  // NOLINT(build/include_order)
0074 
0075 #include "v8-platform.h"  // NOLINT(build/include_order)
0076 #include "node_version.h"  // NODE_MODULE_VERSION
0077 
0078 #define NAPI_EXPERIMENTAL
0079 #include "node_api.h"
0080 
0081 #include <functional>
0082 #include <memory>
0083 #include <optional>
0084 #include <ostream>
0085 
0086 // We cannot use __POSIX__ in this header because that's only defined when
0087 // building Node.js.
0088 #ifndef _WIN32
0089 #include <signal.h>
0090 #endif  // _WIN32
0091 
0092 #define NODE_MAKE_VERSION(major, minor, patch)                                \
0093   ((major) * 0x1000 + (minor) * 0x100 + (patch))
0094 
0095 #ifdef __clang__
0096 # define NODE_CLANG_AT_LEAST(major, minor, patch)                             \
0097   (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
0098       NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
0099 #else
0100 # define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
0101 #endif
0102 
0103 #ifdef __GNUC__
0104 # define NODE_GNUC_AT_LEAST(major, minor, patch)                              \
0105   (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
0106       NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
0107 #else
0108 # define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
0109 #endif
0110 
0111 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
0112 # define NODE_DEPRECATED(message, declarator) declarator
0113 #else  // NODE_WANT_INTERNALS
0114 # if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
0115 #  define NODE_DEPRECATED(message, declarator)                                 \
0116     __attribute__((deprecated(message))) declarator
0117 # elif defined(_MSC_VER)
0118 #  define NODE_DEPRECATED(message, declarator)                                 \
0119     __declspec(deprecated) declarator
0120 # else
0121 #  define NODE_DEPRECATED(message, declarator) declarator
0122 # endif
0123 #endif
0124 
0125 // Forward-declare libuv loop
0126 struct uv_loop_s;
0127 
0128 // Forward-declare these functions now to stop MSVS from becoming
0129 // terminally confused when it's done in node_internals.h
0130 namespace node {
0131 
0132 struct SnapshotData;
0133 
0134 namespace tracing {
0135 
0136 class TracingController;
0137 
0138 }
0139 
0140 NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
0141                                                 int errorno,
0142                                                 const char* syscall = nullptr,
0143                                                 const char* message = nullptr,
0144                                                 const char* path = nullptr);
0145 NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
0146                                              int errorno,
0147                                              const char* syscall = nullptr,
0148                                              const char* message = nullptr,
0149                                              const char* path = nullptr,
0150                                              const char* dest = nullptr);
0151 
0152 NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
0153                 inline v8::Local<v8::Value> ErrnoException(
0154       int errorno,
0155       const char* syscall = nullptr,
0156       const char* message = nullptr,
0157       const char* path = nullptr) {
0158   return ErrnoException(v8::Isolate::GetCurrent(),
0159                         errorno,
0160                         syscall,
0161                         message,
0162                         path);
0163 })
0164 
0165 NODE_DEPRECATED("Use UVException(isolate, ...)",
0166                 inline v8::Local<v8::Value> UVException(int errorno,
0167                                         const char* syscall = nullptr,
0168                                         const char* message = nullptr,
0169                                         const char* path = nullptr) {
0170   return UVException(v8::Isolate::GetCurrent(),
0171                      errorno,
0172                      syscall,
0173                      message,
0174                      path);
0175 })
0176 
0177 /*
0178  * These methods need to be called in a HandleScope.
0179  *
0180  * It is preferred that you use the `MakeCallback` overloads taking
0181  * `async_context` arguments.
0182  */
0183 
0184 NODE_DEPRECATED("Use MakeCallback(..., async_context)",
0185                 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
0186                     v8::Isolate* isolate,
0187                     v8::Local<v8::Object> recv,
0188                     const char* method,
0189                     int argc,
0190                     v8::Local<v8::Value>* argv));
0191 NODE_DEPRECATED("Use MakeCallback(..., async_context)",
0192                 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
0193                     v8::Isolate* isolate,
0194                     v8::Local<v8::Object> recv,
0195                     v8::Local<v8::String> symbol,
0196                     int argc,
0197                     v8::Local<v8::Value>* argv));
0198 NODE_DEPRECATED("Use MakeCallback(..., async_context)",
0199                 NODE_EXTERN v8::Local<v8::Value> MakeCallback(
0200                     v8::Isolate* isolate,
0201                     v8::Local<v8::Object> recv,
0202                     v8::Local<v8::Function> callback,
0203                     int argc,
0204                     v8::Local<v8::Value>* argv));
0205 
0206 }  // namespace node
0207 
0208 #include <cassert>
0209 #include <cstdint>
0210 
0211 #ifndef NODE_STRINGIFY
0212 # define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
0213 # define NODE_STRINGIFY_HELPER(n) #n
0214 #endif
0215 
0216 #ifdef _WIN32
0217 #if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
0218 typedef intptr_t ssize_t;
0219 # define _SSIZE_T_
0220 # define _SSIZE_T_DEFINED
0221 #endif
0222 #else  // !_WIN32
0223 # include <sys/types.h>  // size_t, ssize_t
0224 #endif  // _WIN32
0225 
0226 
0227 namespace node {
0228 
0229 class IsolateData;
0230 class Environment;
0231 class MultiIsolatePlatform;
0232 class InitializationResultImpl;
0233 
0234 namespace ProcessInitializationFlags {
0235 enum Flags : uint32_t {
0236   kNoFlags = 0,
0237   // Enable stdio inheritance, which is disabled by default.
0238   // This flag is also implied by kNoStdioInitialization.
0239   kEnableStdioInheritance = 1 << 0,
0240   // Disable reading the NODE_OPTIONS environment variable.
0241   kDisableNodeOptionsEnv = 1 << 1,
0242   // Do not parse CLI options.
0243   kDisableCLIOptions = 1 << 2,
0244   // Do not initialize ICU.
0245   kNoICU = 1 << 3,
0246   // Do not modify stdio file descriptor or TTY state.
0247   kNoStdioInitialization = 1 << 4,
0248   // Do not register Node.js-specific signal handlers
0249   // and reset other signal handlers to default state.
0250   kNoDefaultSignalHandling = 1 << 5,
0251   // Do not perform V8 initialization.
0252   kNoInitializeV8 = 1 << 6,
0253   // Do not initialize a default Node.js-provided V8 platform instance.
0254   kNoInitializeNodeV8Platform = 1 << 7,
0255   // Do not initialize OpenSSL config.
0256   kNoInitOpenSSL = 1 << 8,
0257   // Do not initialize Node.js debugging based on environment variables.
0258   kNoParseGlobalDebugVariables = 1 << 9,
0259   // Do not adjust OS resource limits for this process.
0260   kNoAdjustResourceLimits = 1 << 10,
0261   // Do not map code segments into large pages for this process.
0262   kNoUseLargePages = 1 << 11,
0263   // Skip printing output for --help, --version, --v8-options.
0264   kNoPrintHelpOrVersionOutput = 1 << 12,
0265   // Do not perform cppgc initialization. If set, the embedder must call
0266   // cppgc::InitializeProcess() before creating a Node.js environment
0267   // and call cppgc::ShutdownProcess() before process shutdown.
0268   kNoInitializeCppgc = 1 << 13,
0269   // Initialize the process for predictable snapshot generation.
0270   kGeneratePredictableSnapshot = 1 << 14,
0271 
0272   // Emulate the behavior of InitializeNodeWithArgs() when passing
0273   // a flags argument to the InitializeOncePerProcess() replacement
0274   // function.
0275   kLegacyInitializeNodeWithArgsBehavior =
0276       kNoStdioInitialization | kNoDefaultSignalHandling | kNoInitializeV8 |
0277       kNoInitializeNodeV8Platform | kNoInitOpenSSL |
0278       kNoParseGlobalDebugVariables | kNoAdjustResourceLimits |
0279       kNoUseLargePages | kNoPrintHelpOrVersionOutput | kNoInitializeCppgc,
0280 };
0281 }  // namespace ProcessInitializationFlags
0282 namespace ProcessFlags = ProcessInitializationFlags;  // Legacy alias.
0283 
0284 namespace StopFlags {
0285 enum Flags : uint32_t {
0286   kNoFlags = 0,
0287   // Do not explicitly terminate the Isolate
0288   // when exiting the Environment.
0289   kDoNotTerminateIsolate = 1 << 0,
0290 };
0291 }  // namespace StopFlags
0292 
0293 class NODE_EXTERN InitializationResult {
0294  public:
0295   virtual ~InitializationResult() = default;
0296 
0297   // Returns a suggested process exit code.
0298   virtual int exit_code() const = 0;
0299 
0300   // Returns 'true' if initialization was aborted early due to errors.
0301   virtual bool early_return() const = 0;
0302 
0303   // Returns the parsed list of non-Node.js arguments.
0304   virtual const std::vector<std::string>& args() const = 0;
0305 
0306   // Returns the parsed list of Node.js arguments.
0307   virtual const std::vector<std::string>& exec_args() const = 0;
0308 
0309   // Returns an array of errors. Note that these may be warnings
0310   // whose existence does not imply a non-zero exit code.
0311   virtual const std::vector<std::string>& errors() const = 0;
0312 
0313   // If kNoInitializeNodeV8Platform was not specified, the global Node.js
0314   // platform instance.
0315   virtual MultiIsolatePlatform* platform() const = 0;
0316 
0317  private:
0318   InitializationResult() = default;
0319   friend class InitializationResultImpl;
0320 };
0321 
0322 // TODO(addaleax): Officially deprecate this and replace it with something
0323 // better suited for a public embedder API.
0324 NODE_EXTERN int Start(int argc, char* argv[]);
0325 
0326 // Tear down Node.js while it is running (there are active handles
0327 // in the loop and / or actively executing JavaScript code).
0328 NODE_EXTERN int Stop(Environment* env,
0329                      StopFlags::Flags flags = StopFlags::kNoFlags);
0330 
0331 // Set up per-process state needed to run Node.js. This will consume arguments
0332 // from argv, fill exec_argv, and possibly add errors resulting from parsing
0333 // the arguments to `errors`. The return value is a suggested exit code for the
0334 // program; If it is 0, then initializing Node.js succeeded.
0335 // This runs a subset of the initialization performed by
0336 // InitializeOncePerProcess(), which supersedes this function.
0337 // The subset is roughly equivalent to the one given by
0338 // `ProcessInitializationFlags::kLegacyInitializeNodeWithArgsBehavior`.
0339 NODE_DEPRECATED("Use InitializeOncePerProcess() instead",
0340                 NODE_EXTERN int InitializeNodeWithArgs(
0341                     std::vector<std::string>* argv,
0342                     std::vector<std::string>* exec_argv,
0343                     std::vector<std::string>* errors,
0344                     ProcessInitializationFlags::Flags flags =
0345                         ProcessInitializationFlags::kNoFlags));
0346 
0347 // Set up per-process state needed to run Node.js. This will consume arguments
0348 // from args, and return information about the initialization success,
0349 // including the arguments split into argv/exec_argv, a list of potential
0350 // errors encountered during initialization, and a potential suggested
0351 // exit code.
0352 NODE_EXTERN std::shared_ptr<InitializationResult> InitializeOncePerProcess(
0353     const std::vector<std::string>& args,
0354     ProcessInitializationFlags::Flags flags =
0355         ProcessInitializationFlags::kNoFlags);
0356 // Undoes the initialization performed by InitializeOncePerProcess(),
0357 // where cleanup is necessary.
0358 NODE_EXTERN void TearDownOncePerProcess();
0359 // Convenience overload for specifying multiple flags without having
0360 // to worry about casts.
0361 inline std::shared_ptr<InitializationResult> InitializeOncePerProcess(
0362     const std::vector<std::string>& args,
0363     std::initializer_list<ProcessInitializationFlags::Flags> list) {
0364   uint64_t flags_accum = ProcessInitializationFlags::kNoFlags;
0365   for (const auto flag : list) flags_accum |= static_cast<uint64_t>(flag);
0366   return InitializeOncePerProcess(
0367       args, static_cast<ProcessInitializationFlags::Flags>(flags_accum));
0368 }
0369 
0370 enum OptionEnvvarSettings {
0371   // Allow the options to be set via the environment variable, like
0372   // `NODE_OPTIONS`.
0373   kAllowedInEnvvar = 0,
0374   // Disallow the options to be set via the environment variable, like
0375   // `NODE_OPTIONS`.
0376   kDisallowedInEnvvar = 1,
0377   // Deprecated, use kAllowedInEnvvar instead.
0378   kAllowedInEnvironment = kAllowedInEnvvar,
0379   // Deprecated, use kDisallowedInEnvvar instead.
0380   kDisallowedInEnvironment = kDisallowedInEnvvar,
0381 };
0382 
0383 // Process the arguments and set up the per-process options.
0384 // If the `settings` is set as OptionEnvvarSettings::kAllowedInEnvvar, the
0385 // options that are allowed in the environment variable are processed. Options
0386 // that are disallowed to be set via environment variable are processed as
0387 // errors.
0388 // Otherwise all the options that are disallowed (and those are allowed) to be
0389 // set via environment variable are processed.
0390 NODE_EXTERN int ProcessGlobalArgs(std::vector<std::string>* args,
0391                       std::vector<std::string>* exec_args,
0392                       std::vector<std::string>* errors,
0393                       OptionEnvvarSettings settings);
0394 
0395 class NodeArrayBufferAllocator;
0396 
0397 // An ArrayBuffer::Allocator class with some Node.js-specific tweaks. If you do
0398 // not have to use another allocator, using this class is recommended:
0399 // - It supports Buffer.allocUnsafe() and Buffer.allocUnsafeSlow() with
0400 //   uninitialized memory.
0401 // - It supports transferring, rather than copying, ArrayBuffers when using
0402 //   MessagePorts.
0403 class NODE_EXTERN ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
0404  public:
0405   // If `always_debug` is true, create an ArrayBuffer::Allocator instance
0406   // that performs additional integrity checks (e.g. make sure that only memory
0407   // that was allocated by the it is also freed by it).
0408   // This can also be set using the --debug-arraybuffer-allocations flag.
0409   static std::unique_ptr<ArrayBufferAllocator> Create(
0410       bool always_debug = false);
0411 
0412  private:
0413   virtual NodeArrayBufferAllocator* GetImpl() = 0;
0414 
0415   friend class IsolateData;
0416 };
0417 
0418 // Legacy equivalents for ArrayBufferAllocator::Create().
0419 NODE_EXTERN ArrayBufferAllocator* CreateArrayBufferAllocator();
0420 NODE_EXTERN void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator);
0421 
0422 class NODE_EXTERN IsolatePlatformDelegate {
0423  public:
0424   virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() = 0;
0425   virtual bool IdleTasksEnabled() = 0;
0426 };
0427 
0428 class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
0429  public:
0430   ~MultiIsolatePlatform() override = default;
0431   // Returns true if work was dispatched or executed. New tasks that are
0432   // posted during flushing of the queue are postponed until the next
0433   // flushing.
0434   virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
0435   virtual void DrainTasks(v8::Isolate* isolate) = 0;
0436 
0437   // This needs to be called between the calls to `Isolate::Allocate()` and
0438   // `Isolate::Initialize()`, so that initialization can already start
0439   // using the platform.
0440   // When using `NewIsolate()`, this is taken care of by that function.
0441   // This function may only be called once per `Isolate`.
0442   virtual void RegisterIsolate(v8::Isolate* isolate,
0443                                struct uv_loop_s* loop) = 0;
0444   // This method can be used when an application handles task scheduling on its
0445   // own through `IsolatePlatformDelegate`. Upon registering an isolate with
0446   // this overload any other method in this class with the exception of
0447   // `UnregisterIsolate` *must not* be used on that isolate.
0448   virtual void RegisterIsolate(v8::Isolate* isolate,
0449                                IsolatePlatformDelegate* delegate) = 0;
0450 
0451   // This function may only be called once per `Isolate`, and discard any
0452   // pending delayed tasks scheduled for that isolate.
0453   // This needs to be called right before calling `Isolate::Dispose()`.
0454   virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
0455 
0456   // The platform should call the passed function once all state associated
0457   // with the given isolate has been cleaned up. This can, but does not have to,
0458   // happen asynchronously.
0459   virtual void AddIsolateFinishedCallback(v8::Isolate* isolate,
0460                                           void (*callback)(void*),
0461                                           void* data) = 0;
0462 
0463   static std::unique_ptr<MultiIsolatePlatform> Create(
0464       int thread_pool_size,
0465       v8::TracingController* tracing_controller = nullptr,
0466       v8::PageAllocator* page_allocator = nullptr);
0467 };
0468 
0469 enum IsolateSettingsFlags {
0470   MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
0471   DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1,
0472   SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK = 1 << 2,
0473   SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK = 1 << 3,
0474   ALLOW_MODIFY_CODE_GENERATION_FROM_STRINGS_CALLBACK = 0, /* legacy no-op */
0475 };
0476 
0477 struct IsolateSettings {
0478   uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
0479       DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
0480   v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
0481 
0482   // Error handling callbacks
0483   v8::Isolate::AbortOnUncaughtExceptionCallback
0484       should_abort_on_uncaught_exception_callback = nullptr;
0485   v8::FatalErrorCallback fatal_error_callback = nullptr;
0486   v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
0487 
0488   // Miscellaneous callbacks
0489   v8::PromiseRejectCallback promise_reject_callback = nullptr;
0490   v8::AllowWasmCodeGenerationCallback
0491       allow_wasm_code_generation_callback = nullptr;
0492   v8::ModifyCodeGenerationFromStringsCallback2
0493       modify_code_generation_from_strings_callback = nullptr;
0494 };
0495 
0496 // Represents a startup snapshot blob, e.g. created by passing
0497 // --node-snapshot-main=entry.js to the configure script at build time,
0498 // or by running Node.js with the --build-snapshot option.
0499 //
0500 // If used, the snapshot *must* have been built with the same Node.js
0501 // version and V8 flags as the version that is currently running, and will
0502 // be rejected otherwise.
0503 // The same EmbedderSnapshotData instance *must* be passed to both
0504 // `NewIsolate()` and `CreateIsolateData()`. The first `Environment` instance
0505 // should be created with an empty `context` argument and will then
0506 // use the main context included in the snapshot blob. It can be retrieved
0507 // using `GetMainContext()`. `LoadEnvironment` can receive an empty
0508 // `StartExecutionCallback` in this case.
0509 // If V8 was configured with the shared-readonly-heap option, it requires
0510 // all snapshots used to create `Isolate` instances to be identical.
0511 // This option *must* be unset by embedders who wish to use the startup
0512 // feature during the build step by passing the --disable-shared-readonly-heap
0513 // flag to the configure script.
0514 //
0515 // The snapshot *must* be kept alive during the execution of the Isolate
0516 // that was created using it.
0517 //
0518 // Snapshots are an *experimental* feature. In particular, the embedder API
0519 // exposed through this class is subject to change or removal between Node.js
0520 // versions, including possible API and ABI breakage.
0521 class EmbedderSnapshotData {
0522  public:
0523   struct DeleteSnapshotData {
0524     void operator()(const EmbedderSnapshotData*) const;
0525   };
0526   using Pointer =
0527       std::unique_ptr<const EmbedderSnapshotData, DeleteSnapshotData>;
0528 
0529   // Return an EmbedderSnapshotData object that refers to the built-in
0530   // snapshot of Node.js. This can have been configured through e.g.
0531   // --node-snapshot-main=entry.js.
0532   static Pointer BuiltinSnapshotData();
0533 
0534   // Return an EmbedderSnapshotData object that is based on an input file.
0535   // Calling this method will consume but not close the FILE* handle.
0536   // The FILE* handle can be closed immediately following this call.
0537   // If the snapshot is invalid, this returns an empty pointer.
0538   static Pointer FromFile(FILE* in);
0539   static Pointer FromBlob(const std::vector<char>& in);
0540   static Pointer FromBlob(std::string_view in);
0541 
0542   // Write this EmbedderSnapshotData object to an output file.
0543   // Calling this method will not close the FILE* handle.
0544   // The FILE* handle can be closed immediately following this call.
0545   void ToFile(FILE* out) const;
0546   std::vector<char> ToBlob() const;
0547 
0548   // Returns whether custom snapshots can be used. Currently, this means
0549   // that V8 was configured without the shared-readonly-heap feature.
0550   static bool CanUseCustomSnapshotPerIsolate();
0551 
0552   EmbedderSnapshotData(const EmbedderSnapshotData&) = delete;
0553   EmbedderSnapshotData& operator=(const EmbedderSnapshotData&) = delete;
0554   EmbedderSnapshotData(EmbedderSnapshotData&&) = delete;
0555   EmbedderSnapshotData& operator=(EmbedderSnapshotData&&) = delete;
0556 
0557  protected:
0558   EmbedderSnapshotData(const SnapshotData* impl, bool owns_impl);
0559 
0560  private:
0561   const SnapshotData* impl_;
0562   bool owns_impl_;
0563   friend struct SnapshotData;
0564   friend class CommonEnvironmentSetup;
0565 };
0566 
0567 // Overriding IsolateSettings may produce unexpected behavior
0568 // in Node.js core functionality, so proceed at your own risk.
0569 NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
0570                                      const IsolateSettings& settings);
0571 
0572 // Set a number of callbacks for the `isolate`, in particular the Node.js
0573 // uncaught exception listener.
0574 NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
0575 
0576 // Creates a new isolate with Node.js-specific settings.
0577 // This is a convenience method equivalent to using SetIsolateCreateParams(),
0578 // Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
0579 // Isolate::Initialize(), and SetIsolateUpForNode().
0580 NODE_EXTERN v8::Isolate* NewIsolate(
0581     ArrayBufferAllocator* allocator,
0582     struct uv_loop_s* event_loop,
0583     MultiIsolatePlatform* platform,
0584     const EmbedderSnapshotData* snapshot_data = nullptr,
0585     const IsolateSettings& settings = {});
0586 NODE_EXTERN v8::Isolate* NewIsolate(
0587     std::shared_ptr<ArrayBufferAllocator> allocator,
0588     struct uv_loop_s* event_loop,
0589     MultiIsolatePlatform* platform,
0590     const EmbedderSnapshotData* snapshot_data = nullptr,
0591     const IsolateSettings& settings = {});
0592 
0593 // Creates a new context with Node.js-specific tweaks.
0594 NODE_EXTERN v8::Local<v8::Context> NewContext(
0595     v8::Isolate* isolate,
0596     v8::Local<v8::ObjectTemplate> object_template =
0597         v8::Local<v8::ObjectTemplate>());
0598 
0599 // Runs Node.js-specific tweaks on an already constructed context
0600 // Return value indicates success of operation
0601 NODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context);
0602 
0603 // If `platform` is passed, it will be used to register new Worker instances.
0604 // It can be `nullptr`, in which case creating new Workers inside of
0605 // Environments that use this `IsolateData` will not work.
0606 NODE_EXTERN IsolateData* CreateIsolateData(
0607     v8::Isolate* isolate,
0608     struct uv_loop_s* loop,
0609     MultiIsolatePlatform* platform = nullptr,
0610     ArrayBufferAllocator* allocator = nullptr,
0611     const EmbedderSnapshotData* snapshot_data = nullptr);
0612 NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
0613 
0614 struct ThreadId {
0615   uint64_t id = static_cast<uint64_t>(-1);
0616 };
0617 NODE_EXTERN ThreadId AllocateEnvironmentThreadId();
0618 
0619 namespace EnvironmentFlags {
0620 enum Flags : uint64_t {
0621   kNoFlags = 0,
0622   // Use the default behaviour for Node.js instances.
0623   kDefaultFlags = 1 << 0,
0624   // Controls whether this Environment is allowed to affect per-process state
0625   // (e.g. cwd, process title, uid, etc.).
0626   // This is set when using kDefaultFlags.
0627   kOwnsProcessState = 1 << 1,
0628   // Set if this Environment instance is associated with the global inspector
0629   // handling code (i.e. listening on SIGUSR1).
0630   // This is set when using kDefaultFlags.
0631   kOwnsInspector = 1 << 2,
0632   // Set if Node.js should not run its own esm loader. This is needed by some
0633   // embedders, because it's possible for the Node.js esm loader to conflict
0634   // with another one in an embedder environment, e.g. Blink's in Chromium.
0635   kNoRegisterESMLoader = 1 << 3,
0636   // Set this flag to make Node.js track "raw" file descriptors, i.e. managed
0637   // by fs.open() and fs.close(), and close them during FreeEnvironment().
0638   kTrackUnmanagedFds = 1 << 4,
0639   // Set this flag to force hiding console windows when spawning child
0640   // processes. This is usually used when embedding Node.js in GUI programs on
0641   // Windows.
0642   kHideConsoleWindows = 1 << 5,
0643   // Set this flag to disable loading native addons via `process.dlopen`.
0644   // This environment flag is especially important for worker threads
0645   // so that a worker thread can't load a native addon even if `execArgv`
0646   // is overwritten and `--no-addons` is not specified but was specified
0647   // for this Environment instance.
0648   kNoNativeAddons = 1 << 6,
0649   // Set this flag to disable searching modules from global paths like
0650   // $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
0651   // do not expect to have their behaviors changed because of globally
0652   // installed modules.
0653   kNoGlobalSearchPaths = 1 << 7,
0654   // Do not export browser globals like setTimeout, console, etc.
0655   kNoBrowserGlobals = 1 << 8,
0656   // Controls whether or not the Environment should call V8Inspector::create().
0657   // This control is needed by embedders who may not want to initialize the V8
0658   // inspector in situations where one has already been created,
0659   // e.g. Blink's in Chromium.
0660   kNoCreateInspector = 1 << 9,
0661   // Controls where or not the InspectorAgent for this Environment should
0662   // call StartDebugSignalHandler.  This control is needed by embedders who may
0663   // not want to allow other processes to start the V8 inspector.
0664   kNoStartDebugSignalHandler = 1 << 10
0665 };
0666 }  // namespace EnvironmentFlags
0667 
0668 enum class SnapshotFlags : uint32_t {
0669   kDefault = 0,
0670   // Whether code cache should be generated as part of the snapshot.
0671   // Code cache reduces the time spent on compiling functions included
0672   // in the snapshot at the expense of a bigger snapshot size and
0673   // potentially breaking portability of the snapshot.
0674   kWithoutCodeCache = 1 << 0,
0675 };
0676 
0677 struct SnapshotConfig {
0678   SnapshotFlags flags = SnapshotFlags::kDefault;
0679 
0680   // When builder_script_path is std::nullopt, the snapshot is generated as a
0681   // built-in snapshot instead of a custom one, and it's expected that the
0682   // built-in snapshot only contains states that reproduce in every run of the
0683   // application. The event loop won't be run when generating a built-in
0684   // snapshot, so asynchronous operations should be avoided.
0685   //
0686   // When builder_script_path is an std::string, it should match args[1]
0687   // passed to CreateForSnapshotting(). The embedder is also expected to use
0688   // LoadEnvironment() to run a script matching this path. In that case the
0689   // snapshot is generated as a custom snapshot and the event loop is run, so
0690   // the snapshot builder can execute asynchronous operations as long as they
0691   // are run to completion when the snapshot is taken.
0692   std::optional<std::string> builder_script_path;
0693 };
0694 
0695 struct InspectorParentHandle {
0696   virtual ~InspectorParentHandle() = default;
0697 };
0698 
0699 // TODO(addaleax): Maybe move per-Environment options parsing here.
0700 // Returns nullptr when the Environment cannot be created e.g. there are
0701 // pending JavaScript exceptions.
0702 // `context` may be empty if an `EmbedderSnapshotData` instance was provided
0703 // to `NewIsolate()` and `CreateIsolateData()`.
0704 NODE_EXTERN Environment* CreateEnvironment(
0705     IsolateData* isolate_data,
0706     v8::Local<v8::Context> context,
0707     const std::vector<std::string>& args,
0708     const std::vector<std::string>& exec_args,
0709     EnvironmentFlags::Flags flags = EnvironmentFlags::kDefaultFlags,
0710     ThreadId thread_id = {} /* allocates a thread id automatically */,
0711     std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
0712 
0713 // Returns a handle that can be passed to `LoadEnvironment()`, making the
0714 // child Environment accessible to the inspector as if it were a Node.js Worker.
0715 // `child_thread_id` can be created using `AllocateEnvironmentThreadId()`
0716 // and then later passed on to `CreateEnvironment()` to create the child
0717 // Environment, together with the inspector handle.
0718 // This method should not be called while the parent Environment is active
0719 // on another thread.
0720 NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
0721     Environment* parent_env,
0722     ThreadId child_thread_id,
0723     const char* child_url);
0724 
0725 NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
0726     Environment* parent_env,
0727     ThreadId child_thread_id,
0728     const char* child_url,
0729     const char* name);
0730 
0731 struct StartExecutionCallbackInfo {
0732   v8::Local<v8::Object> process_object;
0733   v8::Local<v8::Function> native_require;
0734   v8::Local<v8::Function> run_cjs;
0735 };
0736 
0737 using StartExecutionCallback =
0738     std::function<v8::MaybeLocal<v8::Value>(const StartExecutionCallbackInfo&)>;
0739 using EmbedderPreloadCallback =
0740     std::function<void(Environment* env,
0741                        v8::Local<v8::Value> process,
0742                        v8::Local<v8::Value> require)>;
0743 
0744 // Run initialization for the environment.
0745 //
0746 // The |preload| function, usually used by embedders to inject scripts,
0747 // will be run by Node.js before Node.js executes the entry point.
0748 // The function is guaranteed to run before the user land module loader running
0749 // any user code, so it is safe to assume that at this point, no user code has
0750 // been run yet.
0751 // The function will be executed with preload(process, require), and the passed
0752 // require function has access to internal Node.js modules. There is no
0753 // stability guarantee about the internals exposed to the internal require
0754 // function. Expect breakages when updating Node.js versions if the embedder
0755 // imports internal modules with the internal require function.
0756 // Worker threads created in the environment will also respect The |preload|
0757 // function, so make sure the function is thread-safe.
0758 NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
0759     Environment* env,
0760     StartExecutionCallback cb,
0761     EmbedderPreloadCallback preload = nullptr);
0762 NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
0763     Environment* env,
0764     std::string_view main_script_source_utf8,
0765     EmbedderPreloadCallback preload = nullptr);
0766 NODE_EXTERN void FreeEnvironment(Environment* env);
0767 
0768 // Set a callback that is called when process.exit() is called from JS,
0769 // overriding the default handler.
0770 // It receives the Environment* instance and the exit code as arguments.
0771 // This could e.g. call Stop(env); in order to terminate execution and stop
0772 // the event loop.
0773 // The default handler disposes of the global V8 platform instance, if one is
0774 // being used, and calls exit().
0775 NODE_EXTERN void SetProcessExitHandler(
0776     Environment* env,
0777     std::function<void(Environment*, int)>&& handler);
0778 NODE_EXTERN void DefaultProcessExitHandler(Environment* env, int exit_code);
0779 
0780 // This may return nullptr if context is not associated with a Node instance.
0781 NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
0782 NODE_EXTERN IsolateData* GetEnvironmentIsolateData(Environment* env);
0783 NODE_EXTERN ArrayBufferAllocator* GetArrayBufferAllocator(IsolateData* data);
0784 // This is mostly useful for Environment* instances that were created through
0785 // a snapshot and have a main context that was read from that snapshot.
0786 NODE_EXTERN v8::Local<v8::Context> GetMainContext(Environment* env);
0787 
0788 [[noreturn]] NODE_EXTERN void OnFatalError(const char* location,
0789                                            const char* message);
0790 NODE_EXTERN void PromiseRejectCallback(v8::PromiseRejectMessage message);
0791 NODE_EXTERN bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
0792                                             v8::Local<v8::String>);
0793 NODE_EXTERN bool ShouldAbortOnUncaughtException(v8::Isolate* isolate);
0794 NODE_EXTERN v8::MaybeLocal<v8::Value> PrepareStackTraceCallback(
0795     v8::Local<v8::Context> context,
0796     v8::Local<v8::Value> exception,
0797     v8::Local<v8::Array> trace);
0798 
0799 // Writes a diagnostic report to a file. If filename is not provided, the
0800 // default filename includes the date, time, PID, and a sequence number.
0801 // The report's JavaScript stack trace is taken from err, if present.
0802 // If isolate is nullptr, no information about the JavaScript environment
0803 // is included in the report.
0804 // Returns the filename of the written report.
0805 NODE_EXTERN std::string TriggerNodeReport(v8::Isolate* isolate,
0806                                           const char* message,
0807                                           const char* trigger,
0808                                           const std::string& filename,
0809                                           v8::Local<v8::Value> error);
0810 NODE_EXTERN std::string TriggerNodeReport(Environment* env,
0811                                           const char* message,
0812                                           const char* trigger,
0813                                           const std::string& filename,
0814                                           v8::Local<v8::Value> error);
0815 NODE_EXTERN void GetNodeReport(v8::Isolate* isolate,
0816                                const char* message,
0817                                const char* trigger,
0818                                v8::Local<v8::Value> error,
0819                                std::ostream& out);
0820 NODE_EXTERN void GetNodeReport(Environment* env,
0821                                const char* message,
0822                                const char* trigger,
0823                                v8::Local<v8::Value> error,
0824                                std::ostream& out);
0825 
0826 // This returns the MultiIsolatePlatform used for an Environment or IsolateData
0827 // instance, if one exists.
0828 NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env);
0829 NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env);
0830 
0831 NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
0832     NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
0833         int thread_pool_size,
0834         v8::TracingController* tracing_controller));
0835 NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
0836     NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform));
0837 
0838 // Get/set the currently active tracing controller. Using CreatePlatform()
0839 // will implicitly set this by default. This is global and should be initialized
0840 // along with the v8::Platform instance that is being used. `controller`
0841 // is allowed to be `nullptr`.
0842 // This is used for tracing events from Node.js itself. V8 uses the tracing
0843 // controller returned from the active `v8::Platform` instance.
0844 NODE_EXTERN v8::TracingController* GetTracingController();
0845 NODE_EXTERN void SetTracingController(v8::TracingController* controller);
0846 
0847 // Run `process.emit('beforeExit')` as it would usually happen when Node.js is
0848 // run in standalone mode.
0849 NODE_EXTERN v8::Maybe<bool> EmitProcessBeforeExit(Environment* env);
0850 NODE_DEPRECATED("Use Maybe version (EmitProcessBeforeExit) instead",
0851     NODE_EXTERN void EmitBeforeExit(Environment* env));
0852 // Run `process.emit('exit')` as it would usually happen when Node.js is run
0853 // in standalone mode. The return value corresponds to the exit code.
0854 NODE_EXTERN v8::Maybe<int> EmitProcessExit(Environment* env);
0855 NODE_DEPRECATED("Use Maybe version (EmitProcessExit) instead",
0856     NODE_EXTERN int EmitExit(Environment* env));
0857 
0858 // Runs hooks added through `AtExit()`. This is part of `FreeEnvironment()`,
0859 // so calling it manually is typically not necessary.
0860 NODE_EXTERN void RunAtExit(Environment* env);
0861 
0862 // This may return nullptr if the current v8::Context is not associated
0863 // with a Node instance.
0864 NODE_EXTERN struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate);
0865 
0866 // Runs the main loop for a given Environment. This roughly performs the
0867 // following steps:
0868 // 1. Call uv_run() on the event loop until it is drained.
0869 // 2. Call platform->DrainTasks() on the associated platform/isolate.
0870 //   3. If the event loop is alive again, go to Step 1.
0871 // 4. Call EmitProcessBeforeExit().
0872 //   5. If the event loop is alive again, go to Step 1.
0873 // 6. Call EmitProcessExit() and forward the return value.
0874 // If at any point node::Stop() is called, the function will attempt to return
0875 // as soon as possible, returning an empty `Maybe`.
0876 // This function only works if `env` has an associated `MultiIsolatePlatform`.
0877 NODE_EXTERN v8::Maybe<int> SpinEventLoop(Environment* env);
0878 
0879 NODE_EXTERN std::string GetAnonymousMainPath();
0880 
0881 class NODE_EXTERN CommonEnvironmentSetup {
0882  public:
0883   ~CommonEnvironmentSetup();
0884 
0885   // Create a new CommonEnvironmentSetup, that is, a group of objects that
0886   // together form the typical setup for a single Node.js Environment instance.
0887   // If any error occurs, `*errors` will be populated and the returned pointer
0888   // will be empty.
0889   // env_args will be passed through as arguments to CreateEnvironment(), after
0890   // `isolate_data` and `context`.
0891   template <typename... EnvironmentArgs>
0892   static std::unique_ptr<CommonEnvironmentSetup> Create(
0893       MultiIsolatePlatform* platform,
0894       std::vector<std::string>* errors,
0895       EnvironmentArgs&&... env_args);
0896   template <typename... EnvironmentArgs>
0897   static std::unique_ptr<CommonEnvironmentSetup> CreateFromSnapshot(
0898       MultiIsolatePlatform* platform,
0899       std::vector<std::string>* errors,
0900       const EmbedderSnapshotData* snapshot_data,
0901       EnvironmentArgs&&... env_args);
0902 
0903   // Create an embedding setup which will be used for creating a snapshot
0904   // using CreateSnapshot().
0905   //
0906   // This will create and attach a v8::SnapshotCreator to this instance,
0907   // and the same restrictions apply to this instance that also apply to
0908   // other V8 snapshotting environments.
0909   // Not all Node.js APIs are supported in this case. Currently, there is
0910   // no support for native/host objects other than Node.js builtins
0911   // in the snapshot.
0912   //
0913   // If the embedder wants to use LoadEnvironment() later to run a snapshot
0914   // builder script they should make sure args[1] contains the path of the
0915   // snapshot script, which will be used to create __filename and __dirname
0916   // in the context where the builder script is run. If they do not want to
0917   // include the build-time paths into the snapshot, use the string returned
0918   // by GetAnonymousMainPath() as args[1] to anonymize the script.
0919   //
0920   // Snapshots are an *experimental* feature. In particular, the embedder API
0921   // exposed through this class is subject to change or removal between Node.js
0922   // versions, including possible API and ABI breakage.
0923   static std::unique_ptr<CommonEnvironmentSetup> CreateForSnapshotting(
0924       MultiIsolatePlatform* platform,
0925       std::vector<std::string>* errors,
0926       const std::vector<std::string>& args = {},
0927       const std::vector<std::string>& exec_args = {},
0928       const SnapshotConfig& snapshot_config = {});
0929   EmbedderSnapshotData::Pointer CreateSnapshot();
0930 
0931   struct uv_loop_s* event_loop() const;
0932   v8::SnapshotCreator* snapshot_creator();
0933   // Empty for snapshotting environments.
0934   std::shared_ptr<ArrayBufferAllocator> array_buffer_allocator() const;
0935   v8::Isolate* isolate() const;
0936   IsolateData* isolate_data() const;
0937   Environment* env() const;
0938   v8::Local<v8::Context> context() const;
0939 
0940   CommonEnvironmentSetup(const CommonEnvironmentSetup&) = delete;
0941   CommonEnvironmentSetup& operator=(const CommonEnvironmentSetup&) = delete;
0942   CommonEnvironmentSetup(CommonEnvironmentSetup&&) = delete;
0943   CommonEnvironmentSetup& operator=(CommonEnvironmentSetup&&) = delete;
0944 
0945  private:
0946   enum Flags : uint32_t {
0947     kNoFlags = 0,
0948     kIsForSnapshotting = 1,
0949   };
0950 
0951   struct Impl;
0952   Impl* impl_;
0953 
0954   CommonEnvironmentSetup(
0955       MultiIsolatePlatform*,
0956       std::vector<std::string>*,
0957       std::function<Environment*(const CommonEnvironmentSetup*)>);
0958   CommonEnvironmentSetup(
0959       MultiIsolatePlatform*,
0960       std::vector<std::string>*,
0961       const EmbedderSnapshotData*,
0962       uint32_t flags,
0963       std::function<Environment*(const CommonEnvironmentSetup*)>,
0964       const SnapshotConfig* config = nullptr);
0965 };
0966 
0967 // Implementation for CommonEnvironmentSetup::Create
0968 template <typename... EnvironmentArgs>
0969 std::unique_ptr<CommonEnvironmentSetup> CommonEnvironmentSetup::Create(
0970     MultiIsolatePlatform* platform,
0971     std::vector<std::string>* errors,
0972     EnvironmentArgs&&... env_args) {
0973   auto ret = std::unique_ptr<CommonEnvironmentSetup>(new CommonEnvironmentSetup(
0974       platform, errors,
0975       [&](const CommonEnvironmentSetup* setup) -> Environment* {
0976         return CreateEnvironment(
0977             setup->isolate_data(), setup->context(),
0978             std::forward<EnvironmentArgs>(env_args)...);
0979       }));
0980   if (!errors->empty()) ret.reset();
0981   return ret;
0982 }
0983 
0984 // Implementation for ::CreateFromSnapshot -- the ::Create() method
0985 // could call this with a nullptr snapshot_data in a major version.
0986 template <typename... EnvironmentArgs>
0987 std::unique_ptr<CommonEnvironmentSetup>
0988 CommonEnvironmentSetup::CreateFromSnapshot(
0989     MultiIsolatePlatform* platform,
0990     std::vector<std::string>* errors,
0991     const EmbedderSnapshotData* snapshot_data,
0992     EnvironmentArgs&&... env_args) {
0993   auto ret = std::unique_ptr<CommonEnvironmentSetup>(new CommonEnvironmentSetup(
0994       platform,
0995       errors,
0996       snapshot_data,
0997       Flags::kNoFlags,
0998       [&](const CommonEnvironmentSetup* setup) -> Environment* {
0999         return CreateEnvironment(setup->isolate_data(),
1000                                  setup->context(),
1001                                  std::forward<EnvironmentArgs>(env_args)...);
1002       }));
1003   if (!errors->empty()) ret.reset();
1004   return ret;
1005 }
1006 
1007 /* Converts a unixtime to V8 Date */
1008 NODE_DEPRECATED("Use v8::Date::New() directly",
1009                 inline v8::Local<v8::Value> NODE_UNIXTIME_V8(double time) {
1010                   return v8::Date::New(
1011                              v8::Isolate::GetCurrent()->GetCurrentContext(),
1012                              1000 * time)
1013                       .ToLocalChecked();
1014                 })
1015 #define NODE_UNIXTIME_V8 node::NODE_UNIXTIME_V8
1016 NODE_DEPRECATED("Use v8::Date::ValueOf() directly",
1017                 inline double NODE_V8_UNIXTIME(v8::Local<v8::Date> date) {
1018   return date->ValueOf() / 1000;
1019 })
1020 #define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME
1021 
1022 #define NODE_DEFINE_CONSTANT(target, constant)                                \
1023   do {                                                                        \
1024     v8::Isolate* isolate = target->GetIsolate();                              \
1025     v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
1026     v8::Local<v8::String> constant_name =                                     \
1027         v8::String::NewFromUtf8(isolate, #constant,                           \
1028             v8::NewStringType::kInternalized).ToLocalChecked();               \
1029     v8::Local<v8::Number> constant_value =                                    \
1030         v8::Number::New(isolate, static_cast<double>(constant));              \
1031     v8::PropertyAttribute constant_attributes =                               \
1032         static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
1033     (target)->DefineOwnProperty(context,                                      \
1034                                 constant_name,                                \
1035                                 constant_value,                               \
1036                                 constant_attributes).Check();                 \
1037   }                                                                           \
1038   while (0)
1039 
1040 #define NODE_DEFINE_HIDDEN_CONSTANT(target, constant)                         \
1041   do {                                                                        \
1042     v8::Isolate* isolate = target->GetIsolate();                              \
1043     v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
1044     v8::Local<v8::String> constant_name =                                     \
1045         v8::String::NewFromUtf8(isolate, #constant,                           \
1046                                 v8::NewStringType::kInternalized)             \
1047                                   .ToLocalChecked();                          \
1048     v8::Local<v8::Number> constant_value =                                    \
1049         v8::Number::New(isolate, static_cast<double>(constant));              \
1050     v8::PropertyAttribute constant_attributes =                               \
1051         static_cast<v8::PropertyAttribute>(v8::ReadOnly |                     \
1052                                            v8::DontDelete |                   \
1053                                            v8::DontEnum);                     \
1054     (target)->DefineOwnProperty(context,                                      \
1055                                 constant_name,                                \
1056                                 constant_value,                               \
1057                                 constant_attributes).Check();                 \
1058   }                                                                           \
1059   while (0)
1060 
1061 // Used to be a macro, hence the uppercase name.
1062 inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
1063                             const char* name,
1064                             v8::FunctionCallback callback) {
1065   v8::Isolate* isolate = v8::Isolate::GetCurrent();
1066   v8::HandleScope handle_scope(isolate);
1067   v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
1068                                                                 callback);
1069   v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
1070       v8::NewStringType::kInternalized).ToLocalChecked();
1071   t->SetClassName(fn_name);
1072   recv->Set(fn_name, t);
1073 }
1074 
1075 // Used to be a macro, hence the uppercase name.
1076 inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
1077                             const char* name,
1078                             v8::FunctionCallback callback) {
1079   v8::Isolate* isolate = v8::Isolate::GetCurrent();
1080   v8::HandleScope handle_scope(isolate);
1081   v8::Local<v8::Context> context = isolate->GetCurrentContext();
1082   v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
1083                                                                 callback);
1084   v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
1085   v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
1086       v8::NewStringType::kInternalized).ToLocalChecked();
1087   fn->SetName(fn_name);
1088   recv->Set(context, fn_name, fn).Check();
1089 }
1090 #define NODE_SET_METHOD node::NODE_SET_METHOD
1091 
1092 // Used to be a macro, hence the uppercase name.
1093 // Not a template because it only makes sense for FunctionTemplates.
1094 inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
1095                                       const char* name,
1096                                       v8::FunctionCallback callback) {
1097   v8::Isolate* isolate = v8::Isolate::GetCurrent();
1098   v8::HandleScope handle_scope(isolate);
1099   v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
1100   v8::Local<v8::FunctionTemplate> t =
1101       v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
1102   v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
1103       v8::NewStringType::kInternalized).ToLocalChecked();
1104   t->SetClassName(fn_name);
1105   recv->PrototypeTemplate()->Set(fn_name, t);
1106 }
1107 #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
1108 
1109 // BINARY is a deprecated alias of LATIN1.
1110 // BASE64URL is not currently exposed to the JavaScript side.
1111 enum encoding {
1112   ASCII,
1113   UTF8,
1114   BASE64,
1115   UCS2,
1116   BINARY,
1117   HEX,
1118   BUFFER,
1119   BASE64URL,
1120   LATIN1 = BINARY
1121 };
1122 
1123 NODE_EXTERN enum encoding ParseEncoding(
1124     v8::Isolate* isolate,
1125     v8::Local<v8::Value> encoding_v,
1126     enum encoding default_encoding = LATIN1);
1127 
1128 NODE_EXTERN void FatalException(v8::Isolate* isolate,
1129                                 const v8::TryCatch& try_catch);
1130 
1131 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
1132                                         const char* buf,
1133                                         size_t len,
1134                                         enum encoding encoding = LATIN1);
1135 
1136 // Warning: This reverses endianness on Big Endian platforms, even though the
1137 // signature using uint16_t implies that it should not.
1138 NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
1139                                         const uint16_t* buf,
1140                                         size_t len);
1141 
1142 // Returns -1 if the handle was not valid for decoding
1143 NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
1144                                 v8::Local<v8::Value>,
1145                                 enum encoding encoding = LATIN1);
1146 // returns bytes written.
1147 NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
1148                                 char* buf,
1149                                 size_t buflen,
1150                                 v8::Local<v8::Value>,
1151                                 enum encoding encoding = LATIN1);
1152 #ifdef _WIN32
1153 NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
1154     v8::Isolate* isolate,
1155     int errorno,
1156     const char* syscall = nullptr,
1157     const char* msg = "",
1158     const char* path = nullptr);
1159 #endif
1160 
1161 const char* signo_string(int errorno);
1162 
1163 
1164 typedef void (*addon_register_func)(
1165     v8::Local<v8::Object> exports,
1166     v8::Local<v8::Value> module,
1167     void* priv);
1168 
1169 typedef void (*addon_context_register_func)(
1170     v8::Local<v8::Object> exports,
1171     v8::Local<v8::Value> module,
1172     v8::Local<v8::Context> context,
1173     void* priv);
1174 
1175 enum ModuleFlags {
1176   kLinked = 0x02
1177 };
1178 
1179 struct node_module {
1180   int nm_version;
1181   unsigned int nm_flags;
1182   void* nm_dso_handle;
1183   const char* nm_filename;
1184   node::addon_register_func nm_register_func;
1185   node::addon_context_register_func nm_context_register_func;
1186   const char* nm_modname;
1187   void* nm_priv;
1188   struct node_module* nm_link;
1189 };
1190 
1191 extern "C" NODE_EXTERN void node_module_register(void* mod);
1192 
1193 #ifdef _WIN32
1194 # define NODE_MODULE_EXPORT __declspec(dllexport)
1195 #else
1196 # define NODE_MODULE_EXPORT __attribute__((visibility("default")))
1197 #endif
1198 
1199 #ifdef NODE_SHARED_MODE
1200 # define NODE_CTOR_PREFIX
1201 #else
1202 # define NODE_CTOR_PREFIX static
1203 #endif
1204 
1205 #if defined(_MSC_VER)
1206 #define NODE_C_CTOR(fn)                                               \
1207   NODE_CTOR_PREFIX void __cdecl fn(void);                             \
1208   namespace {                                                         \
1209   struct fn##_ {                                                      \
1210     fn##_() { fn(); };                                                \
1211   } fn##_v_;                                                          \
1212   }                                                                   \
1213   NODE_CTOR_PREFIX void __cdecl fn(void)
1214 #else
1215 #define NODE_C_CTOR(fn)                                               \
1216   NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
1217   NODE_CTOR_PREFIX void fn(void)
1218 #endif
1219 
1220 #define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
1221   extern "C" {                                                        \
1222     static node::node_module _module =                                \
1223     {                                                                 \
1224       NODE_MODULE_VERSION,                                            \
1225       flags,                                                          \
1226       NULL,  /* NOLINT (readability/null_usage) */                    \
1227       __FILE__,                                                       \
1228       (node::addon_register_func) (regfunc),                          \
1229       NULL,  /* NOLINT (readability/null_usage) */                    \
1230       NODE_STRINGIFY(modname),                                        \
1231       priv,                                                           \
1232       NULL   /* NOLINT (readability/null_usage) */                    \
1233     };                                                                \
1234     NODE_C_CTOR(_register_ ## modname) {                              \
1235       node_module_register(&_module);                                 \
1236     }                                                                 \
1237   }
1238 
1239 #define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
1240   extern "C" {                                                        \
1241     static node::node_module _module =                                \
1242     {                                                                 \
1243       NODE_MODULE_VERSION,                                            \
1244       flags,                                                          \
1245       NULL,  /* NOLINT (readability/null_usage) */                    \
1246       __FILE__,                                                       \
1247       NULL,  /* NOLINT (readability/null_usage) */                    \
1248       (node::addon_context_register_func) (regfunc),                  \
1249       NODE_STRINGIFY(modname),                                        \
1250       priv,                                                           \
1251       NULL  /* NOLINT (readability/null_usage) */                     \
1252     };                                                                \
1253     NODE_C_CTOR(_register_ ## modname) {                              \
1254       node_module_register(&_module);                                 \
1255     }                                                                 \
1256   }
1257 
1258 // Usage: `NODE_MODULE(NODE_GYP_MODULE_NAME, InitializerFunction)`
1259 // If no NODE_MODULE is declared, Node.js looks for the well-known
1260 // symbol `node_register_module_v${NODE_MODULE_VERSION}`.
1261 #define NODE_MODULE(modname, regfunc)                                 \
1262   NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
1263 
1264 #define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
1265   /* NOLINTNEXTLINE (readability/null_usage) */                       \
1266   NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
1267 
1268 // Embedders can use this type of binding for statically linked native bindings.
1269 // It is used the same way addon bindings are used, except that linked bindings
1270 // can be accessed through `process._linkedBinding(modname)`.
1271 #define NODE_MODULE_LINKED(modname, regfunc)                               \
1272   /* NOLINTNEXTLINE (readability/null_usage) */                            \
1273   NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL,                      \
1274                               node::ModuleFlags::kLinked)
1275 
1276 /*
1277  * For backward compatibility in add-on modules.
1278  */
1279 #define NODE_MODULE_DECL /* nothing */
1280 
1281 #define NODE_MODULE_INITIALIZER_BASE node_register_module_v
1282 
1283 #define NODE_MODULE_INITIALIZER_X(base, version)                      \
1284     NODE_MODULE_INITIALIZER_X_HELPER(base, version)
1285 
1286 #define NODE_MODULE_INITIALIZER_X_HELPER(base, version) base##version
1287 
1288 #define NODE_MODULE_INITIALIZER                                       \
1289   NODE_MODULE_INITIALIZER_X(NODE_MODULE_INITIALIZER_BASE,             \
1290       NODE_MODULE_VERSION)
1291 
1292 #define NODE_MODULE_INIT()                                            \
1293   extern "C" NODE_MODULE_EXPORT void                                  \
1294   NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,              \
1295                           v8::Local<v8::Value> module,                \
1296                           v8::Local<v8::Context> context);            \
1297   NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME,                     \
1298                             NODE_MODULE_INITIALIZER)                  \
1299   void NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,         \
1300                                v8::Local<v8::Value> module,           \
1301                                v8::Local<v8::Context> context)
1302 
1303 // Allows embedders to add a binding to the current Environment* that can be
1304 // accessed through process._linkedBinding() in the target Environment and all
1305 // Worker threads that it creates.
1306 // In each variant, the registration function needs to be usable at least for
1307 // the time during which the Environment exists.
1308 NODE_EXTERN void AddLinkedBinding(Environment* env, const node_module& mod);
1309 NODE_EXTERN void AddLinkedBinding(Environment* env,
1310                                   const struct napi_module& mod);
1311 NODE_EXTERN void AddLinkedBinding(Environment* env,
1312                                   const char* name,
1313                                   addon_context_register_func fn,
1314                                   void* priv);
1315 NODE_EXTERN void AddLinkedBinding(
1316     Environment* env,
1317     const char* name,
1318     napi_addon_register_func fn,
1319     int32_t module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION);
1320 
1321 /* Registers a callback with the passed-in Environment instance. The callback
1322  * is called after the event loop exits, but before the VM is disposed.
1323  * Callbacks are run in reverse order of registration, i.e. newest first.
1324  */
1325 NODE_EXTERN void AtExit(Environment* env,
1326                         void (*cb)(void* arg),
1327                         void* arg);
1328 
1329 typedef double async_id;
1330 struct async_context {
1331   ::node::async_id async_id;
1332   ::node::async_id trigger_async_id;
1333 };
1334 
1335 /* This is a lot like node::AtExit, except that the hooks added via this
1336  * function are run before the AtExit ones and will always be registered
1337  * for the current Environment instance.
1338  * These functions are safe to use in an addon supporting multiple
1339  * threads/isolates. */
1340 NODE_EXTERN void AddEnvironmentCleanupHook(v8::Isolate* isolate,
1341                                            void (*fun)(void* arg),
1342                                            void* arg);
1343 
1344 NODE_EXTERN void RemoveEnvironmentCleanupHook(v8::Isolate* isolate,
1345                                               void (*fun)(void* arg),
1346                                               void* arg);
1347 
1348 /* These are async equivalents of the above. After the cleanup hook is invoked,
1349  * `cb(cbarg)` *must* be called, and attempting to remove the cleanup hook will
1350  * have no effect. */
1351 struct ACHHandle;
1352 struct NODE_EXTERN DeleteACHHandle { void operator()(ACHHandle*) const; };
1353 typedef std::unique_ptr<ACHHandle, DeleteACHHandle> AsyncCleanupHookHandle;
1354 
1355 /* This function is not intended to be used externally, it exists to aid in
1356  * keeping ABI compatibility between Node and Electron. */
1357 NODE_EXTERN ACHHandle* AddEnvironmentCleanupHookInternal(
1358     v8::Isolate* isolate,
1359     void (*fun)(void* arg, void (*cb)(void*), void* cbarg),
1360     void* arg);
1361 inline AsyncCleanupHookHandle AddEnvironmentCleanupHook(
1362     v8::Isolate* isolate,
1363     void (*fun)(void* arg, void (*cb)(void*), void* cbarg),
1364     void* arg) {
1365   return AsyncCleanupHookHandle(AddEnvironmentCleanupHookInternal(isolate, fun,
1366       arg));
1367 }
1368 
1369 /* This function is not intended to be used externally, it exists to aid in
1370  * keeping ABI compatibility between Node and Electron. */
1371 NODE_EXTERN void RemoveEnvironmentCleanupHookInternal(ACHHandle* holder);
1372 inline void RemoveEnvironmentCleanupHook(AsyncCleanupHookHandle holder) {
1373   RemoveEnvironmentCleanupHookInternal(holder.get());
1374 }
1375 
1376 // This behaves like V8's Isolate::RequestInterrupt(), but also wakes up
1377 // the event loop if it is currently idle. Interrupt requests are drained
1378 // in `FreeEnvironment()`. The passed callback can not call back into
1379 // JavaScript.
1380 // This function can be called from any thread.
1381 NODE_EXTERN void RequestInterrupt(Environment* env,
1382                                   void (*fun)(void* arg),
1383                                   void* arg);
1384 
1385 /* Returns the id of the current execution context. If the return value is
1386  * zero then no execution has been set. This will happen if the user handles
1387  * I/O from native code. */
1388 NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
1389 
1390 /* Return same value as async_hooks.triggerAsyncId(); */
1391 NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
1392 
1393 /* If the native API doesn't inherit from the helper class then the callbacks
1394  * must be triggered manually. This triggers the init() callback. The return
1395  * value is the async id assigned to the resource.
1396  *
1397  * The `trigger_async_id` parameter should correspond to the resource which is
1398  * creating the new resource, which will usually be the return value of
1399  * `AsyncHooksGetTriggerAsyncId()`. */
1400 NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
1401                                         v8::Local<v8::Object> resource,
1402                                         const char* name,
1403                                         async_id trigger_async_id = -1);
1404 
1405 NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
1406                                         v8::Local<v8::Object> resource,
1407                                         v8::Local<v8::String> name,
1408                                         async_id trigger_async_id = -1);
1409 
1410 /* Emit the destroy() callback. The overload taking an `Environment*` argument
1411  * should be used when the Isolate’s current Context is not associated with
1412  * a Node.js Environment, or when there is no current Context, for example
1413  * when calling this function during garbage collection. In that case, the
1414  * `Environment*` value should have been acquired previously, e.g. through
1415  * `GetCurrentEnvironment()`. */
1416 NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
1417                                   async_context asyncContext);
1418 NODE_EXTERN void EmitAsyncDestroy(Environment* env,
1419                                   async_context asyncContext);
1420 
1421 class InternalCallbackScope;
1422 
1423 /* This class works like `MakeCallback()` in that it sets up a specific
1424  * asyncContext as the current one and informs the async_hooks and domains
1425  * modules that this context is currently active.
1426  *
1427  * `MakeCallback()` is a wrapper around this class as well as
1428  * `Function::Call()`. Either one of these mechanisms needs to be used for
1429  * top-level calls into JavaScript (i.e. without any existing JS stack).
1430  *
1431  * This object should be stack-allocated to ensure that it is contained in a
1432  * valid HandleScope.
1433  *
1434  * Exceptions happening within this scope will be treated like uncaught
1435  * exceptions. If this behaviour is undesirable, a new `v8::TryCatch` scope
1436  * needs to be created inside of this scope.
1437  */
1438 class NODE_EXTERN CallbackScope {
1439  public:
1440   CallbackScope(v8::Isolate* isolate,
1441                 v8::Local<v8::Object> resource,
1442                 async_context asyncContext);
1443   CallbackScope(Environment* env,
1444                 v8::Local<v8::Object> resource,
1445                 async_context asyncContext);
1446   ~CallbackScope();
1447 
1448   void operator=(const CallbackScope&) = delete;
1449   void operator=(CallbackScope&&) = delete;
1450   CallbackScope(const CallbackScope&) = delete;
1451   CallbackScope(CallbackScope&&) = delete;
1452 
1453  private:
1454   InternalCallbackScope* private_;
1455   v8::TryCatch try_catch_;
1456 };
1457 
1458 /* An API specific to emit before/after callbacks is unnecessary because
1459  * MakeCallback will automatically call them for you.
1460  *
1461  * These methods may create handles on their own, so run them inside a
1462  * HandleScope.
1463  *
1464  * `asyncId` and `triggerAsyncId` should correspond to the values returned by
1465  * `EmitAsyncInit()` and `AsyncHooksGetTriggerAsyncId()`, respectively, when the
1466  * invoking resource was created. If these values are unknown, 0 can be passed.
1467  * */
1468 NODE_EXTERN
1469 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
1470                                        v8::Local<v8::Object> recv,
1471                                        v8::Local<v8::Function> callback,
1472                                        int argc,
1473                                        v8::Local<v8::Value>* argv,
1474                                        async_context asyncContext);
1475 NODE_EXTERN
1476 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
1477                                        v8::Local<v8::Object> recv,
1478                                        const char* method,
1479                                        int argc,
1480                                        v8::Local<v8::Value>* argv,
1481                                        async_context asyncContext);
1482 NODE_EXTERN
1483 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
1484                                        v8::Local<v8::Object> recv,
1485                                        v8::Local<v8::String> symbol,
1486                                        int argc,
1487                                        v8::Local<v8::Value>* argv,
1488                                        async_context asyncContext);
1489 
1490 /* Helper class users can optionally inherit from. If
1491  * `AsyncResource::MakeCallback()` is used, then all four callbacks will be
1492  * called automatically. */
1493 class NODE_EXTERN AsyncResource {
1494  public:
1495   AsyncResource(v8::Isolate* isolate,
1496                 v8::Local<v8::Object> resource,
1497                 const char* name,
1498                 async_id trigger_async_id = -1);
1499 
1500   virtual ~AsyncResource();
1501 
1502   AsyncResource(const AsyncResource&) = delete;
1503   void operator=(const AsyncResource&) = delete;
1504 
1505   v8::MaybeLocal<v8::Value> MakeCallback(
1506       v8::Local<v8::Function> callback,
1507       int argc,
1508       v8::Local<v8::Value>* argv);
1509 
1510   v8::MaybeLocal<v8::Value> MakeCallback(
1511       const char* method,
1512       int argc,
1513       v8::Local<v8::Value>* argv);
1514 
1515   v8::MaybeLocal<v8::Value> MakeCallback(
1516       v8::Local<v8::String> symbol,
1517       int argc,
1518       v8::Local<v8::Value>* argv);
1519 
1520   v8::Local<v8::Object> get_resource();
1521   async_id get_async_id() const;
1522   async_id get_trigger_async_id() const;
1523 
1524  protected:
1525   class NODE_EXTERN CallbackScope : public node::CallbackScope {
1526    public:
1527     explicit CallbackScope(AsyncResource* res);
1528   };
1529 
1530  private:
1531   Environment* env_;
1532   v8::Global<v8::Object> resource_;
1533   async_context async_context_;
1534 };
1535 
1536 #ifndef _WIN32
1537 // Register a signal handler without interrupting any handlers that node
1538 // itself needs. This does override handlers registered through
1539 // process.on('SIG...', function() { ... }). The `reset_handler` flag indicates
1540 // whether the signal handler for the given signal should be reset to its
1541 // default value before executing the handler (i.e. it works like SA_RESETHAND).
1542 // The `reset_handler` flag is invalid when `signal` is SIGSEGV.
1543 NODE_EXTERN
1544 void RegisterSignalHandler(int signal,
1545                            void (*handler)(int signal,
1546                                            siginfo_t* info,
1547                                            void* ucontext),
1548                            bool reset_handler = false);
1549 #endif  // _WIN32
1550 
1551 // Configure the layout of the JavaScript object with a cppgc::GarbageCollected
1552 // instance so that when the JavaScript object is reachable, the garbage
1553 // collected instance would have its Trace() method invoked per the cppgc
1554 // contract. To make it work, the process must have called
1555 // cppgc::InitializeProcess() before, which is usually the case for addons
1556 // loaded by the stand-alone Node.js executable. Embedders of Node.js can use
1557 // either need to call it themselves or make sure that
1558 // ProcessInitializationFlags::kNoInitializeCppgc is *not* set for cppgc to
1559 // work.
1560 // If the CppHeap is owned by Node.js, which is usually the case for addon,
1561 // the object must be created with at least two internal fields available,
1562 // and the first two internal fields would be configured by Node.js.
1563 // This may be superseded by a V8 API in the future, see
1564 // https://bugs.chromium.org/p/v8/issues/detail?id=13960. Until then this
1565 // serves as a helper for Node.js isolates.
1566 NODE_EXTERN void SetCppgcReference(v8::Isolate* isolate,
1567                                    v8::Local<v8::Object> object,
1568                                    void* wrappable);
1569 
1570 }  // namespace node
1571 
1572 #endif  // SRC_NODE_H_