11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifndef SRC_NODE_H_
231cb0ef41Sopenharmony_ci#define SRC_NODE_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#ifdef _WIN32
261cb0ef41Sopenharmony_ci# ifndef BUILDING_NODE_EXTENSION
271cb0ef41Sopenharmony_ci#  define NODE_EXTERN __declspec(dllexport)
281cb0ef41Sopenharmony_ci# else
291cb0ef41Sopenharmony_ci#  define NODE_EXTERN __declspec(dllimport)
301cb0ef41Sopenharmony_ci# endif
311cb0ef41Sopenharmony_ci#else
321cb0ef41Sopenharmony_ci# define NODE_EXTERN __attribute__((visibility("default")))
331cb0ef41Sopenharmony_ci#endif
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Declarations annotated with NODE_EXTERN_PRIVATE do not form part of
361cb0ef41Sopenharmony_ci// the public API. They are implementation details that can and will
371cb0ef41Sopenharmony_ci// change between releases, even in semver patch releases. Do not use
381cb0ef41Sopenharmony_ci// any such symbol in external code.
391cb0ef41Sopenharmony_ci#ifdef NODE_SHARED_MODE
401cb0ef41Sopenharmony_ci#define NODE_EXTERN_PRIVATE NODE_EXTERN
411cb0ef41Sopenharmony_ci#else
421cb0ef41Sopenharmony_ci#define NODE_EXTERN_PRIVATE
431cb0ef41Sopenharmony_ci#endif
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci#ifdef BUILDING_NODE_EXTENSION
461cb0ef41Sopenharmony_ci# undef BUILDING_V8_SHARED
471cb0ef41Sopenharmony_ci# undef BUILDING_UV_SHARED
481cb0ef41Sopenharmony_ci# define USING_V8_SHARED 1
491cb0ef41Sopenharmony_ci# define USING_UV_SHARED 1
501cb0ef41Sopenharmony_ci#endif
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// This should be defined in make system.
531cb0ef41Sopenharmony_ci// See issue https://github.com/nodejs/node-v0.x-archive/issues/1236
541cb0ef41Sopenharmony_ci#if defined(__MINGW32__) || defined(_MSC_VER)
551cb0ef41Sopenharmony_ci#ifndef _WIN32_WINNT
561cb0ef41Sopenharmony_ci# define _WIN32_WINNT 0x0600  // Windows Server 2008
571cb0ef41Sopenharmony_ci#endif
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci#ifndef NOMINMAX
601cb0ef41Sopenharmony_ci# define NOMINMAX
611cb0ef41Sopenharmony_ci#endif
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci#endif
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci#if defined(_MSC_VER)
661cb0ef41Sopenharmony_ci#define PATH_MAX MAX_PATH
671cb0ef41Sopenharmony_ci#endif
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci#ifdef _WIN32
701cb0ef41Sopenharmony_ci# define SIGKILL 9
711cb0ef41Sopenharmony_ci#endif
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci#include "v8.h"  // NOLINT(build/include_order)
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci#include "v8-platform.h"  // NOLINT(build/include_order)
761cb0ef41Sopenharmony_ci#include "node_version.h"  // NODE_MODULE_VERSION
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci#define JSVM_EXPERIMENTAL
791cb0ef41Sopenharmony_ci#include "jsvm_node_api.h"
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci#include <functional>
821cb0ef41Sopenharmony_ci#include <memory>
831cb0ef41Sopenharmony_ci#include <ostream>
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci// We cannot use __POSIX__ in this header because that's only defined when
861cb0ef41Sopenharmony_ci// building Node.js.
871cb0ef41Sopenharmony_ci#ifndef _WIN32
881cb0ef41Sopenharmony_ci#include <signal.h>
891cb0ef41Sopenharmony_ci#endif  // _WIN32
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci#define NODE_MAKE_VERSION(major, minor, patch)                                \
921cb0ef41Sopenharmony_ci  ((major) * 0x1000 + (minor) * 0x100 + (patch))
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci#ifdef __clang__
951cb0ef41Sopenharmony_ci# define NODE_CLANG_AT_LEAST(major, minor, patch)                             \
961cb0ef41Sopenharmony_ci  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
971cb0ef41Sopenharmony_ci      NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
981cb0ef41Sopenharmony_ci#else
991cb0ef41Sopenharmony_ci# define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
1001cb0ef41Sopenharmony_ci#endif
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci#ifdef __GNUC__
1031cb0ef41Sopenharmony_ci# define NODE_GNUC_AT_LEAST(major, minor, patch)                              \
1041cb0ef41Sopenharmony_ci  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
1051cb0ef41Sopenharmony_ci      NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
1061cb0ef41Sopenharmony_ci#else
1071cb0ef41Sopenharmony_ci# define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
1081cb0ef41Sopenharmony_ci#endif
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1111cb0ef41Sopenharmony_ci# define NODE_DEPRECATED(message, declarator) declarator
1121cb0ef41Sopenharmony_ci#else  // NODE_WANT_INTERNALS
1131cb0ef41Sopenharmony_ci# if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
1141cb0ef41Sopenharmony_ci#  define NODE_DEPRECATED(message, declarator)                                 \
1151cb0ef41Sopenharmony_ci    __attribute__((deprecated(message))) declarator
1161cb0ef41Sopenharmony_ci# elif defined(_MSC_VER)
1171cb0ef41Sopenharmony_ci#  define NODE_DEPRECATED(message, declarator)                                 \
1181cb0ef41Sopenharmony_ci    __declspec(deprecated) declarator
1191cb0ef41Sopenharmony_ci# else
1201cb0ef41Sopenharmony_ci#  define NODE_DEPRECATED(message, declarator) declarator
1211cb0ef41Sopenharmony_ci# endif
1221cb0ef41Sopenharmony_ci#endif
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci// Forward-declare libuv loop
1251cb0ef41Sopenharmony_cistruct uv_loop_s;
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci// Forward-declare these functions now to stop MSVS from becoming
1281cb0ef41Sopenharmony_ci// terminally confused when it's done in node_internals.h
1291cb0ef41Sopenharmony_cinamespace node {
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_cinamespace tracing {
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ciclass TracingController;
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci}
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ciNODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
1381cb0ef41Sopenharmony_ci                                                int errorno,
1391cb0ef41Sopenharmony_ci                                                const char* syscall = nullptr,
1401cb0ef41Sopenharmony_ci                                                const char* message = nullptr,
1411cb0ef41Sopenharmony_ci                                                const char* path = nullptr);
1421cb0ef41Sopenharmony_ciNODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
1431cb0ef41Sopenharmony_ci                                             int errorno,
1441cb0ef41Sopenharmony_ci                                             const char* syscall = nullptr,
1451cb0ef41Sopenharmony_ci                                             const char* message = nullptr,
1461cb0ef41Sopenharmony_ci                                             const char* path = nullptr,
1471cb0ef41Sopenharmony_ci                                             const char* dest = nullptr);
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use ErrnoException(isolate, ...)",
1501cb0ef41Sopenharmony_ci                inline v8::Local<v8::Value> ErrnoException(
1511cb0ef41Sopenharmony_ci      int errorno,
1521cb0ef41Sopenharmony_ci      const char* syscall = nullptr,
1531cb0ef41Sopenharmony_ci      const char* message = nullptr,
1541cb0ef41Sopenharmony_ci      const char* path = nullptr) {
1551cb0ef41Sopenharmony_ci  return ErrnoException(v8::Isolate::GetCurrent(),
1561cb0ef41Sopenharmony_ci                        errorno,
1571cb0ef41Sopenharmony_ci                        syscall,
1581cb0ef41Sopenharmony_ci                        message,
1591cb0ef41Sopenharmony_ci                        path);
1601cb0ef41Sopenharmony_ci})
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use UVException(isolate, ...)",
1631cb0ef41Sopenharmony_ci                inline v8::Local<v8::Value> UVException(int errorno,
1641cb0ef41Sopenharmony_ci                                        const char* syscall = nullptr,
1651cb0ef41Sopenharmony_ci                                        const char* message = nullptr,
1661cb0ef41Sopenharmony_ci                                        const char* path = nullptr) {
1671cb0ef41Sopenharmony_ci  return UVException(v8::Isolate::GetCurrent(),
1681cb0ef41Sopenharmony_ci                     errorno,
1691cb0ef41Sopenharmony_ci                     syscall,
1701cb0ef41Sopenharmony_ci                     message,
1711cb0ef41Sopenharmony_ci                     path);
1721cb0ef41Sopenharmony_ci})
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci/*
1751cb0ef41Sopenharmony_ci * These methods need to be called in a HandleScope.
1761cb0ef41Sopenharmony_ci *
1771cb0ef41Sopenharmony_ci * It is preferred that you use the `MakeCallback` overloads taking
1781cb0ef41Sopenharmony_ci * `async_context` arguments.
1791cb0ef41Sopenharmony_ci */
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use MakeCallback(..., async_context)",
1821cb0ef41Sopenharmony_ci                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
1831cb0ef41Sopenharmony_ci                    v8::Isolate* isolate,
1841cb0ef41Sopenharmony_ci                    v8::Local<v8::Object> recv,
1851cb0ef41Sopenharmony_ci                    const char* method,
1861cb0ef41Sopenharmony_ci                    int argc,
1871cb0ef41Sopenharmony_ci                    v8::Local<v8::Value>* argv));
1881cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use MakeCallback(..., async_context)",
1891cb0ef41Sopenharmony_ci                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
1901cb0ef41Sopenharmony_ci                    v8::Isolate* isolate,
1911cb0ef41Sopenharmony_ci                    v8::Local<v8::Object> recv,
1921cb0ef41Sopenharmony_ci                    v8::Local<v8::String> symbol,
1931cb0ef41Sopenharmony_ci                    int argc,
1941cb0ef41Sopenharmony_ci                    v8::Local<v8::Value>* argv));
1951cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use MakeCallback(..., async_context)",
1961cb0ef41Sopenharmony_ci                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
1971cb0ef41Sopenharmony_ci                    v8::Isolate* isolate,
1981cb0ef41Sopenharmony_ci                    v8::Local<v8::Object> recv,
1991cb0ef41Sopenharmony_ci                    v8::Local<v8::Function> callback,
2001cb0ef41Sopenharmony_ci                    int argc,
2011cb0ef41Sopenharmony_ci                    v8::Local<v8::Value>* argv));
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci}  // namespace node
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci#include <cassert>
2061cb0ef41Sopenharmony_ci#include <cstdint>
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci#ifndef NODE_STRINGIFY
2091cb0ef41Sopenharmony_ci# define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
2101cb0ef41Sopenharmony_ci# define NODE_STRINGIFY_HELPER(n) #n
2111cb0ef41Sopenharmony_ci#endif
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ci#ifdef _WIN32
2141cb0ef41Sopenharmony_ci#if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
2151cb0ef41Sopenharmony_citypedef intptr_t ssize_t;
2161cb0ef41Sopenharmony_ci# define _SSIZE_T_
2171cb0ef41Sopenharmony_ci# define _SSIZE_T_DEFINED
2181cb0ef41Sopenharmony_ci#endif
2191cb0ef41Sopenharmony_ci#else  // !_WIN32
2201cb0ef41Sopenharmony_ci# include <sys/types.h>  // size_t, ssize_t
2211cb0ef41Sopenharmony_ci#endif  // _WIN32
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_cinamespace node {
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ciclass IsolateData;
2271cb0ef41Sopenharmony_ciclass Environment;
2281cb0ef41Sopenharmony_ciclass MultiIsolatePlatform;
2291cb0ef41Sopenharmony_ciclass InitializationResultImpl;
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_cinamespace ProcessFlags {
2321cb0ef41Sopenharmony_ci// TODO(addaleax): Switch to uint32_t to match std::atomic<uint32_t>
2331cb0ef41Sopenharmony_ci// init_process_flags in node.cc
2341cb0ef41Sopenharmony_cienum Flags : uint64_t {
2351cb0ef41Sopenharmony_ci  kNoFlags = 0,
2361cb0ef41Sopenharmony_ci  // Enable stdio inheritance, which is disabled by default.
2371cb0ef41Sopenharmony_ci  // This flag is also implied by kNoStdioInitialization.
2381cb0ef41Sopenharmony_ci  kEnableStdioInheritance = 1 << 0,
2391cb0ef41Sopenharmony_ci  // Disable reading the NODE_OPTIONS environment variable.
2401cb0ef41Sopenharmony_ci  kDisableNodeOptionsEnv = 1 << 1,
2411cb0ef41Sopenharmony_ci  // Do not parse CLI options.
2421cb0ef41Sopenharmony_ci  kDisableCLIOptions = 1 << 2,
2431cb0ef41Sopenharmony_ci  // Do not initialize ICU.
2441cb0ef41Sopenharmony_ci  kNoICU = 1 << 3,
2451cb0ef41Sopenharmony_ci  // Do not modify stdio file descriptor or TTY state.
2461cb0ef41Sopenharmony_ci  kNoStdioInitialization = 1 << 4,
2471cb0ef41Sopenharmony_ci  // Do not register Node.js-specific signal handlers
2481cb0ef41Sopenharmony_ci  // and reset other signal handlers to default state.
2491cb0ef41Sopenharmony_ci  kNoDefaultSignalHandling = 1 << 5,
2501cb0ef41Sopenharmony_ci  // Do not perform V8 initialization.
2511cb0ef41Sopenharmony_ci  kNoInitializeV8 = 1 << 6,
2521cb0ef41Sopenharmony_ci  // Do not initialize a default Node.js-provided V8 platform instance.
2531cb0ef41Sopenharmony_ci  kNoInitializeNodeV8Platform = 1 << 7,
2541cb0ef41Sopenharmony_ci  // Do not initialize OpenSSL config.
2551cb0ef41Sopenharmony_ci  kNoInitOpenSSL = 1 << 8,
2561cb0ef41Sopenharmony_ci  // Do not initialize Node.js debugging based on environment variables.
2571cb0ef41Sopenharmony_ci  kNoParseGlobalDebugVariables = 1 << 9,
2581cb0ef41Sopenharmony_ci  // Do not adjust OS resource limits for this process.
2591cb0ef41Sopenharmony_ci  kNoAdjustResourceLimits = 1 << 10,
2601cb0ef41Sopenharmony_ci  // Do not map code segments into large pages for this process.
2611cb0ef41Sopenharmony_ci  kNoUseLargePages = 1 << 11,
2621cb0ef41Sopenharmony_ci  // Skip printing output for --help, --version, --v8-options.
2631cb0ef41Sopenharmony_ci  kNoPrintHelpOrVersionOutput = 1 << 12,
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci  // Emulate the behavior of InitializeNodeWithArgs() when passing
2661cb0ef41Sopenharmony_ci  // a flags argument to the InitializeOncePerProcess() replacement
2671cb0ef41Sopenharmony_ci  // function.
2681cb0ef41Sopenharmony_ci  kLegacyInitializeNodeWithArgsBehavior =
2691cb0ef41Sopenharmony_ci      kNoStdioInitialization | kNoDefaultSignalHandling | kNoInitializeV8 |
2701cb0ef41Sopenharmony_ci      kNoInitializeNodeV8Platform | kNoInitOpenSSL |
2711cb0ef41Sopenharmony_ci      kNoParseGlobalDebugVariables | kNoAdjustResourceLimits |
2721cb0ef41Sopenharmony_ci      kNoUseLargePages | kNoPrintHelpOrVersionOutput,
2731cb0ef41Sopenharmony_ci};
2741cb0ef41Sopenharmony_ci}  // namespace ProcessFlags
2751cb0ef41Sopenharmony_ci// TODO(addaleax): Make this the canonical name, as it is more descriptive.
2761cb0ef41Sopenharmony_cinamespace ProcessInitializationFlags = ProcessFlags;
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_cinamespace StopFlags {
2791cb0ef41Sopenharmony_cienum Flags : uint32_t {
2801cb0ef41Sopenharmony_ci  kNoFlags = 0,
2811cb0ef41Sopenharmony_ci  // Do not explicitly terminate the Isolate
2821cb0ef41Sopenharmony_ci  // when exiting the Environment.
2831cb0ef41Sopenharmony_ci  kDoNotTerminateIsolate = 1 << 0,
2841cb0ef41Sopenharmony_ci};
2851cb0ef41Sopenharmony_ci}  // namespace StopFlags
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ciclass NODE_EXTERN InitializationResult {
2881cb0ef41Sopenharmony_ci public:
2891cb0ef41Sopenharmony_ci  virtual ~InitializationResult();
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ci  // Returns a suggested process exit code.
2921cb0ef41Sopenharmony_ci  virtual int exit_code() const = 0;
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci  // Returns 'true' if initialization was aborted early due to errors.
2951cb0ef41Sopenharmony_ci  virtual bool early_return() const = 0;
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci  // Returns the parsed list of non-Node.js arguments.
2981cb0ef41Sopenharmony_ci  virtual const std::vector<std::string>& args() const = 0;
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci  // Returns the parsed list of Node.js arguments.
3011cb0ef41Sopenharmony_ci  virtual const std::vector<std::string>& exec_args() const = 0;
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci  // Returns an array of errors. Note that these may be warnings
3041cb0ef41Sopenharmony_ci  // whose existence does not imply a non-zero exit code.
3051cb0ef41Sopenharmony_ci  virtual const std::vector<std::string>& errors() const = 0;
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_ci  // If kNoInitializeNodeV8Platform was not specified, the global Node.js
3081cb0ef41Sopenharmony_ci  // platform instance.
3091cb0ef41Sopenharmony_ci  virtual MultiIsolatePlatform* platform() const = 0;
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci private:
3121cb0ef41Sopenharmony_ci  InitializationResult() = default;
3131cb0ef41Sopenharmony_ci  friend class InitializationResultImpl;
3141cb0ef41Sopenharmony_ci};
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ci// TODO(addaleax): Officially deprecate this and replace it with something
3171cb0ef41Sopenharmony_ci// better suited for a public embedder API.
3181cb0ef41Sopenharmony_ciNODE_EXTERN int Start(int argc, char* argv[]);
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci// Tear down Node.js while it is running (there are active handles
3211cb0ef41Sopenharmony_ci// in the loop and / or actively executing JavaScript code).
3221cb0ef41Sopenharmony_ciNODE_EXTERN int Stop(Environment* env);
3231cb0ef41Sopenharmony_ciNODE_EXTERN int Stop(Environment* env, StopFlags::Flags flags);
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci// This runs a subset of the initialization performed by
3261cb0ef41Sopenharmony_ci// InitializeOncePerProcess(), which supersedes this function.
3271cb0ef41Sopenharmony_ci// The subset is roughly equivalent to the one given by
3281cb0ef41Sopenharmony_ci// `ProcessInitializationFlags::kLegacyInitializeNodeWithArgsBehavior`.
3291cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use InitializeOncePerProcess() instead",
3301cb0ef41Sopenharmony_ci                NODE_EXTERN int InitializeNodeWithArgs(
3311cb0ef41Sopenharmony_ci                    std::vector<std::string>* argv,
3321cb0ef41Sopenharmony_ci                    std::vector<std::string>* exec_argv,
3331cb0ef41Sopenharmony_ci                    std::vector<std::string>* errors,
3341cb0ef41Sopenharmony_ci                    ProcessInitializationFlags::Flags flags));
3351cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use InitializeOncePerProcess() instead",
3361cb0ef41Sopenharmony_ci                NODE_EXTERN int InitializeNodeWithArgs(
3371cb0ef41Sopenharmony_ci                    std::vector<std::string>* argv,
3381cb0ef41Sopenharmony_ci                    std::vector<std::string>* exec_argv,
3391cb0ef41Sopenharmony_ci                    std::vector<std::string>* errors));
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ci// Set up per-process state needed to run Node.js. This will consume arguments
3421cb0ef41Sopenharmony_ci// from args, and return information about the initialization success,
3431cb0ef41Sopenharmony_ci// including the arguments split into argv/exec_argv, a list of potential
3441cb0ef41Sopenharmony_ci// errors encountered during initialization, and a potential suggested
3451cb0ef41Sopenharmony_ci// exit code.
3461cb0ef41Sopenharmony_ciNODE_EXTERN std::unique_ptr<InitializationResult> InitializeOncePerProcess(
3471cb0ef41Sopenharmony_ci    const std::vector<std::string>& args,
3481cb0ef41Sopenharmony_ci    ProcessInitializationFlags::Flags flags =
3491cb0ef41Sopenharmony_ci        ProcessInitializationFlags::kNoFlags);
3501cb0ef41Sopenharmony_ci// Undoes the initialization performed by InitializeOncePerProcess(),
3511cb0ef41Sopenharmony_ci// where cleanup is necessary.
3521cb0ef41Sopenharmony_ciNODE_EXTERN void TearDownOncePerProcess();
3531cb0ef41Sopenharmony_ci// Convenience overload for specifying multiple flags without having
3541cb0ef41Sopenharmony_ci// to worry about casts.
3551cb0ef41Sopenharmony_ciinline std::unique_ptr<InitializationResult> InitializeOncePerProcess(
3561cb0ef41Sopenharmony_ci    const std::vector<std::string>& args,
3571cb0ef41Sopenharmony_ci    std::initializer_list<ProcessInitializationFlags::Flags> list) {
3581cb0ef41Sopenharmony_ci  uint64_t flags_accum = ProcessInitializationFlags::kNoFlags;
3591cb0ef41Sopenharmony_ci  for (const auto flag : list) flags_accum |= static_cast<uint64_t>(flag);
3601cb0ef41Sopenharmony_ci  return InitializeOncePerProcess(
3611cb0ef41Sopenharmony_ci      args, static_cast<ProcessInitializationFlags::Flags>(flags_accum));
3621cb0ef41Sopenharmony_ci}
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_cienum OptionEnvvarSettings {
3651cb0ef41Sopenharmony_ci  // Allow the options to be set via the environment variable, like
3661cb0ef41Sopenharmony_ci  // `NODE_OPTIONS`.
3671cb0ef41Sopenharmony_ci  kAllowedInEnvvar = 0,
3681cb0ef41Sopenharmony_ci  // Disallow the options to be set via the environment variable, like
3691cb0ef41Sopenharmony_ci  // `NODE_OPTIONS`.
3701cb0ef41Sopenharmony_ci  kDisallowedInEnvvar = 1,
3711cb0ef41Sopenharmony_ci  // Deprecated, use kAllowedInEnvvar instead.
3721cb0ef41Sopenharmony_ci  kAllowedInEnvironment = kAllowedInEnvvar,
3731cb0ef41Sopenharmony_ci  // Deprecated, use kDisallowedInEnvvar instead.
3741cb0ef41Sopenharmony_ci  kDisallowedInEnvironment = kDisallowedInEnvvar,
3751cb0ef41Sopenharmony_ci};
3761cb0ef41Sopenharmony_ci
3771cb0ef41Sopenharmony_ci// Process the arguments and set up the per-process options.
3781cb0ef41Sopenharmony_ci// If the `settings` is set as OptionEnvvarSettings::kAllowedInEnvvar, the
3791cb0ef41Sopenharmony_ci// options that are allowed in the environment variable are processed. Options
3801cb0ef41Sopenharmony_ci// that are disallowed to be set via environment variable are processed as
3811cb0ef41Sopenharmony_ci// errors.
3821cb0ef41Sopenharmony_ci// Otherwise all the options that are disallowed (and those are allowed) to be
3831cb0ef41Sopenharmony_ci// set via environment variable are processed.
3841cb0ef41Sopenharmony_ciNODE_EXTERN int ProcessGlobalArgs(std::vector<std::string>* args,
3851cb0ef41Sopenharmony_ci                      std::vector<std::string>* exec_args,
3861cb0ef41Sopenharmony_ci                      std::vector<std::string>* errors,
3871cb0ef41Sopenharmony_ci                      OptionEnvvarSettings settings);
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ciclass NodeArrayBufferAllocator;
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ci// An ArrayBuffer::Allocator class with some Node.js-specific tweaks. If you do
3921cb0ef41Sopenharmony_ci// not have to use another allocator, using this class is recommended:
3931cb0ef41Sopenharmony_ci// - It supports Buffer.allocUnsafe() and Buffer.allocUnsafeSlow() with
3941cb0ef41Sopenharmony_ci//   uninitialized memory.
3951cb0ef41Sopenharmony_ci// - It supports transferring, rather than copying, ArrayBuffers when using
3961cb0ef41Sopenharmony_ci//   MessagePorts.
3971cb0ef41Sopenharmony_ciclass NODE_EXTERN ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
3981cb0ef41Sopenharmony_ci public:
3991cb0ef41Sopenharmony_ci  // If `always_debug` is true, create an ArrayBuffer::Allocator instance
4001cb0ef41Sopenharmony_ci  // that performs additional integrity checks (e.g. make sure that only memory
4011cb0ef41Sopenharmony_ci  // that was allocated by the it is also freed by it).
4021cb0ef41Sopenharmony_ci  // This can also be set using the --debug-arraybuffer-allocations flag.
4031cb0ef41Sopenharmony_ci  static std::unique_ptr<ArrayBufferAllocator> Create(
4041cb0ef41Sopenharmony_ci      bool always_debug = false);
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci private:
4071cb0ef41Sopenharmony_ci  virtual NodeArrayBufferAllocator* GetImpl() = 0;
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci  friend class IsolateData;
4101cb0ef41Sopenharmony_ci};
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci// Legacy equivalents for ArrayBufferAllocator::Create().
4131cb0ef41Sopenharmony_ciNODE_EXTERN ArrayBufferAllocator* CreateArrayBufferAllocator();
4141cb0ef41Sopenharmony_ciNODE_EXTERN void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator);
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ciclass NODE_EXTERN IsolatePlatformDelegate {
4171cb0ef41Sopenharmony_ci public:
4181cb0ef41Sopenharmony_ci  virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() = 0;
4191cb0ef41Sopenharmony_ci  virtual bool IdleTasksEnabled() = 0;
4201cb0ef41Sopenharmony_ci};
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ciclass NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
4231cb0ef41Sopenharmony_ci public:
4241cb0ef41Sopenharmony_ci  ~MultiIsolatePlatform() override = default;
4251cb0ef41Sopenharmony_ci  // Returns true if work was dispatched or executed. New tasks that are
4261cb0ef41Sopenharmony_ci  // posted during flushing of the queue are postponed until the next
4271cb0ef41Sopenharmony_ci  // flushing.
4281cb0ef41Sopenharmony_ci  virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
4291cb0ef41Sopenharmony_ci  virtual void DrainTasks(v8::Isolate* isolate) = 0;
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci  // This needs to be called between the calls to `Isolate::Allocate()` and
4321cb0ef41Sopenharmony_ci  // `Isolate::Initialize()`, so that initialization can already start
4331cb0ef41Sopenharmony_ci  // using the platform.
4341cb0ef41Sopenharmony_ci  // When using `NewIsolate()`, this is taken care of by that function.
4351cb0ef41Sopenharmony_ci  // This function may only be called once per `Isolate`.
4361cb0ef41Sopenharmony_ci  virtual void RegisterIsolate(v8::Isolate* isolate,
4371cb0ef41Sopenharmony_ci                               struct uv_loop_s* loop) = 0;
4381cb0ef41Sopenharmony_ci  // This method can be used when an application handles task scheduling on its
4391cb0ef41Sopenharmony_ci  // own through `IsolatePlatformDelegate`. Upon registering an isolate with
4401cb0ef41Sopenharmony_ci  // this overload any other method in this class with the exception of
4411cb0ef41Sopenharmony_ci  // `UnregisterIsolate` *must not* be used on that isolate.
4421cb0ef41Sopenharmony_ci  virtual void RegisterIsolate(v8::Isolate* isolate,
4431cb0ef41Sopenharmony_ci                               IsolatePlatformDelegate* delegate) = 0;
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci  // This function may only be called once per `Isolate`, and discard any
4461cb0ef41Sopenharmony_ci  // pending delayed tasks scheduled for that isolate.
4471cb0ef41Sopenharmony_ci  // This needs to be called right before calling `Isolate::Dispose()`.
4481cb0ef41Sopenharmony_ci  virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ci  // The platform should call the passed function once all state associated
4511cb0ef41Sopenharmony_ci  // with the given isolate has been cleaned up. This can, but does not have to,
4521cb0ef41Sopenharmony_ci  // happen asynchronously.
4531cb0ef41Sopenharmony_ci  virtual void AddIsolateFinishedCallback(v8::Isolate* isolate,
4541cb0ef41Sopenharmony_ci                                          void (*callback)(void*),
4551cb0ef41Sopenharmony_ci                                          void* data) = 0;
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_ci  static std::unique_ptr<MultiIsolatePlatform> Create(
4581cb0ef41Sopenharmony_ci      int thread_pool_size,
4591cb0ef41Sopenharmony_ci      v8::TracingController* tracing_controller = nullptr,
4601cb0ef41Sopenharmony_ci      v8::PageAllocator* page_allocator = nullptr);
4611cb0ef41Sopenharmony_ci};
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_cienum IsolateSettingsFlags {
4641cb0ef41Sopenharmony_ci  MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
4651cb0ef41Sopenharmony_ci  DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1,
4661cb0ef41Sopenharmony_ci  SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK = 1 << 2,
4671cb0ef41Sopenharmony_ci  SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK = 1 << 3,
4681cb0ef41Sopenharmony_ci  ALLOW_MODIFY_CODE_GENERATION_FROM_STRINGS_CALLBACK = 1 << 4,
4691cb0ef41Sopenharmony_ci};
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_cistruct IsolateSettings {
4721cb0ef41Sopenharmony_ci  uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
4731cb0ef41Sopenharmony_ci      DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
4741cb0ef41Sopenharmony_ci  v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci  // Error handling callbacks
4771cb0ef41Sopenharmony_ci  v8::Isolate::AbortOnUncaughtExceptionCallback
4781cb0ef41Sopenharmony_ci      should_abort_on_uncaught_exception_callback = nullptr;
4791cb0ef41Sopenharmony_ci  v8::FatalErrorCallback fatal_error_callback = nullptr;
4801cb0ef41Sopenharmony_ci  v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
4811cb0ef41Sopenharmony_ci
4821cb0ef41Sopenharmony_ci  // Miscellaneous callbacks
4831cb0ef41Sopenharmony_ci  v8::PromiseRejectCallback promise_reject_callback = nullptr;
4841cb0ef41Sopenharmony_ci  v8::AllowWasmCodeGenerationCallback
4851cb0ef41Sopenharmony_ci      allow_wasm_code_generation_callback = nullptr;
4861cb0ef41Sopenharmony_ci  v8::ModifyCodeGenerationFromStringsCallback2
4871cb0ef41Sopenharmony_ci      modify_code_generation_from_strings_callback = nullptr;
4881cb0ef41Sopenharmony_ci};
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci// Overriding IsolateSettings may produce unexpected behavior
4911cb0ef41Sopenharmony_ci// in Node.js core functionality, so proceed at your own risk.
4921cb0ef41Sopenharmony_ciNODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
4931cb0ef41Sopenharmony_ci                                     const IsolateSettings& settings);
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci// Set a number of callbacks for the `isolate`, in particular the Node.js
4961cb0ef41Sopenharmony_ci// uncaught exception listener.
4971cb0ef41Sopenharmony_ciNODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci// Creates a new isolate with Node.js-specific settings.
5001cb0ef41Sopenharmony_ci// This is a convenience method equivalent to using SetIsolateCreateParams(),
5011cb0ef41Sopenharmony_ci// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
5021cb0ef41Sopenharmony_ci// Isolate::Initialize(), and SetIsolateUpForNode().
5031cb0ef41Sopenharmony_ciNODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
5041cb0ef41Sopenharmony_ci                                    struct uv_loop_s* event_loop,
5051cb0ef41Sopenharmony_ci                                    MultiIsolatePlatform* platform = nullptr);
5061cb0ef41Sopenharmony_ciNODE_EXTERN v8::Isolate* NewIsolate(
5071cb0ef41Sopenharmony_ci    std::shared_ptr<ArrayBufferAllocator> allocator,
5081cb0ef41Sopenharmony_ci    struct uv_loop_s* event_loop,
5091cb0ef41Sopenharmony_ci    MultiIsolatePlatform* platform);
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci// Creates a new context with Node.js-specific tweaks.
5121cb0ef41Sopenharmony_ciNODE_EXTERN v8::Local<v8::Context> NewContext(
5131cb0ef41Sopenharmony_ci    v8::Isolate* isolate,
5141cb0ef41Sopenharmony_ci    v8::Local<v8::ObjectTemplate> object_template =
5151cb0ef41Sopenharmony_ci        v8::Local<v8::ObjectTemplate>());
5161cb0ef41Sopenharmony_ci
5171cb0ef41Sopenharmony_ci// Runs Node.js-specific tweaks on an already constructed context
5181cb0ef41Sopenharmony_ci// Return value indicates success of operation
5191cb0ef41Sopenharmony_ciNODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context);
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci// If `platform` is passed, it will be used to register new Worker instances.
5221cb0ef41Sopenharmony_ci// It can be `nullptr`, in which case creating new Workers inside of
5231cb0ef41Sopenharmony_ci// Environments that use this `IsolateData` will not work.
5241cb0ef41Sopenharmony_ciNODE_EXTERN IsolateData* CreateIsolateData(
5251cb0ef41Sopenharmony_ci    v8::Isolate* isolate,
5261cb0ef41Sopenharmony_ci    struct uv_loop_s* loop,
5271cb0ef41Sopenharmony_ci    MultiIsolatePlatform* platform = nullptr,
5281cb0ef41Sopenharmony_ci    ArrayBufferAllocator* allocator = nullptr);
5291cb0ef41Sopenharmony_ciNODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_cistruct ThreadId {
5321cb0ef41Sopenharmony_ci  uint64_t id = static_cast<uint64_t>(-1);
5331cb0ef41Sopenharmony_ci};
5341cb0ef41Sopenharmony_ciNODE_EXTERN ThreadId AllocateEnvironmentThreadId();
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_cinamespace EnvironmentFlags {
5371cb0ef41Sopenharmony_cienum Flags : uint64_t {
5381cb0ef41Sopenharmony_ci  kNoFlags = 0,
5391cb0ef41Sopenharmony_ci  // Use the default behaviour for Node.js instances.
5401cb0ef41Sopenharmony_ci  kDefaultFlags = 1 << 0,
5411cb0ef41Sopenharmony_ci  // Controls whether this Environment is allowed to affect per-process state
5421cb0ef41Sopenharmony_ci  // (e.g. cwd, process title, uid, etc.).
5431cb0ef41Sopenharmony_ci  // This is set when using kDefaultFlags.
5441cb0ef41Sopenharmony_ci  kOwnsProcessState = 1 << 1,
5451cb0ef41Sopenharmony_ci  // Set if this Environment instance is associated with the global inspector
5461cb0ef41Sopenharmony_ci  // handling code (i.e. listening on SIGUSR1).
5471cb0ef41Sopenharmony_ci  // This is set when using kDefaultFlags.
5481cb0ef41Sopenharmony_ci  kOwnsInspector = 1 << 2,
5491cb0ef41Sopenharmony_ci  // Set if Node.js should not run its own esm loader. This is needed by some
5501cb0ef41Sopenharmony_ci  // embedders, because it's possible for the Node.js esm loader to conflict
5511cb0ef41Sopenharmony_ci  // with another one in an embedder environment, e.g. Blink's in Chromium.
5521cb0ef41Sopenharmony_ci  kNoRegisterESMLoader = 1 << 3,
5531cb0ef41Sopenharmony_ci  // Set this flag to make Node.js track "raw" file descriptors, i.e. managed
5541cb0ef41Sopenharmony_ci  // by fs.open() and fs.close(), and close them during FreeEnvironment().
5551cb0ef41Sopenharmony_ci  kTrackUnmanagedFds = 1 << 4,
5561cb0ef41Sopenharmony_ci  // Set this flag to force hiding console windows when spawning child
5571cb0ef41Sopenharmony_ci  // processes. This is usually used when embedding Node.js in GUI programs on
5581cb0ef41Sopenharmony_ci  // Windows.
5591cb0ef41Sopenharmony_ci  kHideConsoleWindows = 1 << 5,
5601cb0ef41Sopenharmony_ci  // Set this flag to disable loading native addons via `process.dlopen`.
5611cb0ef41Sopenharmony_ci  // This environment flag is especially important for worker threads
5621cb0ef41Sopenharmony_ci  // so that a worker thread can't load a native addon even if `execArgv`
5631cb0ef41Sopenharmony_ci  // is overwritten and `--no-addons` is not specified but was specified
5641cb0ef41Sopenharmony_ci  // for this Environment instance.
5651cb0ef41Sopenharmony_ci  kNoNativeAddons = 1 << 6,
5661cb0ef41Sopenharmony_ci  // Set this flag to disable searching modules from global paths like
5671cb0ef41Sopenharmony_ci  // $HOME/.node_modules and $NODE_PATH. This is used by standalone apps that
5681cb0ef41Sopenharmony_ci  // do not expect to have their behaviors changed because of globally
5691cb0ef41Sopenharmony_ci  // installed modules.
5701cb0ef41Sopenharmony_ci  kNoGlobalSearchPaths = 1 << 7,
5711cb0ef41Sopenharmony_ci  // Do not export browser globals like setTimeout, console, etc.
5721cb0ef41Sopenharmony_ci  kNoBrowserGlobals = 1 << 8,
5731cb0ef41Sopenharmony_ci  // Controls whether or not the Environment should call V8Inspector::create().
5741cb0ef41Sopenharmony_ci  // This control is needed by embedders who may not want to initialize the V8
5751cb0ef41Sopenharmony_ci  // inspector in situations where one has already been created,
5761cb0ef41Sopenharmony_ci  // e.g. Blink's in Chromium.
5771cb0ef41Sopenharmony_ci  kNoCreateInspector = 1 << 9
5781cb0ef41Sopenharmony_ci};
5791cb0ef41Sopenharmony_ci}  // namespace EnvironmentFlags
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_cistruct InspectorParentHandle {
5821cb0ef41Sopenharmony_ci  virtual ~InspectorParentHandle();
5831cb0ef41Sopenharmony_ci};
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_ci// TODO(addaleax): Maybe move per-Environment options parsing here.
5861cb0ef41Sopenharmony_ci// Returns nullptr when the Environment cannot be created e.g. there are
5871cb0ef41Sopenharmony_ci// pending JavaScript exceptions.
5881cb0ef41Sopenharmony_ciNODE_EXTERN Environment* CreateEnvironment(
5891cb0ef41Sopenharmony_ci    IsolateData* isolate_data,
5901cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context,
5911cb0ef41Sopenharmony_ci    const std::vector<std::string>& args,
5921cb0ef41Sopenharmony_ci    const std::vector<std::string>& exec_args,
5931cb0ef41Sopenharmony_ci    EnvironmentFlags::Flags flags = EnvironmentFlags::kDefaultFlags,
5941cb0ef41Sopenharmony_ci    ThreadId thread_id = {} /* allocates a thread id automatically */,
5951cb0ef41Sopenharmony_ci    std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_ci// Returns a handle that can be passed to `LoadEnvironment()`, making the
5981cb0ef41Sopenharmony_ci// child Environment accessible to the inspector as if it were a Node.js Worker.
5991cb0ef41Sopenharmony_ci// `child_thread_id` can be created using `AllocateEnvironmentThreadId()`
6001cb0ef41Sopenharmony_ci// and then later passed on to `CreateEnvironment()` to create the child
6011cb0ef41Sopenharmony_ci// Environment, together with the inspector handle.
6021cb0ef41Sopenharmony_ci// This method should not be called while the parent Environment is active
6031cb0ef41Sopenharmony_ci// on another thread.
6041cb0ef41Sopenharmony_ciNODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
6051cb0ef41Sopenharmony_ci    Environment* parent_env,
6061cb0ef41Sopenharmony_ci    ThreadId child_thread_id,
6071cb0ef41Sopenharmony_ci    const char* child_url);
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ciNODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
6101cb0ef41Sopenharmony_ci    Environment* parent_env,
6111cb0ef41Sopenharmony_ci    ThreadId child_thread_id,
6121cb0ef41Sopenharmony_ci    const char* child_url,
6131cb0ef41Sopenharmony_ci    const char* name);
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_cistruct StartExecutionCallbackInfo {
6161cb0ef41Sopenharmony_ci  v8::Local<v8::Object> process_object;
6171cb0ef41Sopenharmony_ci  v8::Local<v8::Function> native_require;
6181cb0ef41Sopenharmony_ci};
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_ciusing StartExecutionCallback =
6211cb0ef41Sopenharmony_ci    std::function<v8::MaybeLocal<v8::Value>(const StartExecutionCallbackInfo&)>;
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
6241cb0ef41Sopenharmony_ci    Environment* env,
6251cb0ef41Sopenharmony_ci    StartExecutionCallback cb);
6261cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
6271cb0ef41Sopenharmony_ci    Environment* env,
6281cb0ef41Sopenharmony_ci    const char* main_script_source_utf8);
6291cb0ef41Sopenharmony_ciNODE_EXTERN void FreeEnvironment(Environment* env);
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci// Set a callback that is called when process.exit() is called from JS,
6321cb0ef41Sopenharmony_ci// overriding the default handler.
6331cb0ef41Sopenharmony_ci// It receives the Environment* instance and the exit code as arguments.
6341cb0ef41Sopenharmony_ci// This could e.g. call Stop(env); in order to terminate execution and stop
6351cb0ef41Sopenharmony_ci// the event loop.
6361cb0ef41Sopenharmony_ci// The default handler disposes of the global V8 platform instance, if one is
6371cb0ef41Sopenharmony_ci// being used, and calls exit().
6381cb0ef41Sopenharmony_ciNODE_EXTERN void SetProcessExitHandler(
6391cb0ef41Sopenharmony_ci    Environment* env,
6401cb0ef41Sopenharmony_ci    std::function<void(Environment*, int)>&& handler);
6411cb0ef41Sopenharmony_ciNODE_EXTERN void DefaultProcessExitHandler(Environment* env, int exit_code);
6421cb0ef41Sopenharmony_ci
6431cb0ef41Sopenharmony_ci// This may return nullptr if context is not associated with a Node instance.
6441cb0ef41Sopenharmony_ciNODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
6451cb0ef41Sopenharmony_ciNODE_EXTERN IsolateData* GetEnvironmentIsolateData(Environment* env);
6461cb0ef41Sopenharmony_ciNODE_EXTERN ArrayBufferAllocator* GetArrayBufferAllocator(IsolateData* data);
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci[[noreturn]] NODE_EXTERN void OnFatalError(const char* location,
6491cb0ef41Sopenharmony_ci                                           const char* message);
6501cb0ef41Sopenharmony_ciNODE_EXTERN void PromiseRejectCallback(v8::PromiseRejectMessage message);
6511cb0ef41Sopenharmony_ciNODE_EXTERN bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
6521cb0ef41Sopenharmony_ci                                            v8::Local<v8::String>);
6531cb0ef41Sopenharmony_ciNODE_EXTERN bool ShouldAbortOnUncaughtException(v8::Isolate* isolate);
6541cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Value> PrepareStackTraceCallback(
6551cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context,
6561cb0ef41Sopenharmony_ci    v8::Local<v8::Value> exception,
6571cb0ef41Sopenharmony_ci    v8::Local<v8::Array> trace);
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci// Writes a diagnostic report to a file. If filename is not provided, the
6601cb0ef41Sopenharmony_ci// default filename includes the date, time, PID, and a sequence number.
6611cb0ef41Sopenharmony_ci// The report's JavaScript stack trace is taken from err, if present.
6621cb0ef41Sopenharmony_ci// If isolate is nullptr, no information about the JavaScript environment
6631cb0ef41Sopenharmony_ci// is included in the report.
6641cb0ef41Sopenharmony_ci// Returns the filename of the written report.
6651cb0ef41Sopenharmony_ciNODE_EXTERN std::string TriggerNodeReport(v8::Isolate* isolate,
6661cb0ef41Sopenharmony_ci                                          const char* message,
6671cb0ef41Sopenharmony_ci                                          const char* trigger,
6681cb0ef41Sopenharmony_ci                                          const std::string& filename,
6691cb0ef41Sopenharmony_ci                                          v8::Local<v8::Value> error);
6701cb0ef41Sopenharmony_ciNODE_EXTERN std::string TriggerNodeReport(Environment* env,
6711cb0ef41Sopenharmony_ci                                          const char* message,
6721cb0ef41Sopenharmony_ci                                          const char* trigger,
6731cb0ef41Sopenharmony_ci                                          const std::string& filename,
6741cb0ef41Sopenharmony_ci                                          v8::Local<v8::Value> error);
6751cb0ef41Sopenharmony_ciNODE_EXTERN void GetNodeReport(v8::Isolate* isolate,
6761cb0ef41Sopenharmony_ci                               const char* message,
6771cb0ef41Sopenharmony_ci                               const char* trigger,
6781cb0ef41Sopenharmony_ci                               v8::Local<v8::Value> error,
6791cb0ef41Sopenharmony_ci                               std::ostream& out);
6801cb0ef41Sopenharmony_ciNODE_EXTERN void GetNodeReport(Environment* env,
6811cb0ef41Sopenharmony_ci                               const char* message,
6821cb0ef41Sopenharmony_ci                               const char* trigger,
6831cb0ef41Sopenharmony_ci                               v8::Local<v8::Value> error,
6841cb0ef41Sopenharmony_ci                               std::ostream& out);
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci// This returns the MultiIsolatePlatform used for an Environment or IsolateData
6871cb0ef41Sopenharmony_ci// instance, if one exists.
6881cb0ef41Sopenharmony_ciNODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env);
6891cb0ef41Sopenharmony_ciNODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env);
6901cb0ef41Sopenharmony_ci
6911cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
6921cb0ef41Sopenharmony_ci    NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
6931cb0ef41Sopenharmony_ci        int thread_pool_size,
6941cb0ef41Sopenharmony_ci        v8::TracingController* tracing_controller));
6951cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
6961cb0ef41Sopenharmony_ci    NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform));
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ci// Get/set the currently active tracing controller. Using CreatePlatform()
6991cb0ef41Sopenharmony_ci// will implicitly set this by default. This is global and should be initialized
7001cb0ef41Sopenharmony_ci// along with the v8::Platform instance that is being used. `controller`
7011cb0ef41Sopenharmony_ci// is allowed to be `nullptr`.
7021cb0ef41Sopenharmony_ci// This is used for tracing events from Node.js itself. V8 uses the tracing
7031cb0ef41Sopenharmony_ci// controller returned from the active `v8::Platform` instance.
7041cb0ef41Sopenharmony_ciNODE_EXTERN v8::TracingController* GetTracingController();
7051cb0ef41Sopenharmony_ciNODE_EXTERN void SetTracingController(v8::TracingController* controller);
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci// Run `process.emit('beforeExit')` as it would usually happen when Node.js is
7081cb0ef41Sopenharmony_ci// run in standalone mode.
7091cb0ef41Sopenharmony_ciNODE_EXTERN v8::Maybe<bool> EmitProcessBeforeExit(Environment* env);
7101cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use Maybe version (EmitProcessBeforeExit) instead",
7111cb0ef41Sopenharmony_ci    NODE_EXTERN void EmitBeforeExit(Environment* env));
7121cb0ef41Sopenharmony_ci// Run `process.emit('exit')` as it would usually happen when Node.js is run
7131cb0ef41Sopenharmony_ci// in standalone mode. The return value corresponds to the exit code.
7141cb0ef41Sopenharmony_ciNODE_EXTERN v8::Maybe<int> EmitProcessExit(Environment* env);
7151cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use Maybe version (EmitProcessExit) instead",
7161cb0ef41Sopenharmony_ci    NODE_EXTERN int EmitExit(Environment* env));
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_ci// Runs hooks added through `AtExit()`. This is part of `FreeEnvironment()`,
7191cb0ef41Sopenharmony_ci// so calling it manually is typically not necessary.
7201cb0ef41Sopenharmony_ciNODE_EXTERN void RunAtExit(Environment* env);
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci// This may return nullptr if the current v8::Context is not associated
7231cb0ef41Sopenharmony_ci// with a Node instance.
7241cb0ef41Sopenharmony_ciNODE_EXTERN struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate);
7251cb0ef41Sopenharmony_ci
7261cb0ef41Sopenharmony_ci// Runs the main loop for a given Environment. This roughly performs the
7271cb0ef41Sopenharmony_ci// following steps:
7281cb0ef41Sopenharmony_ci// 1. Call uv_run() on the event loop until it is drained.
7291cb0ef41Sopenharmony_ci// 2. Call platform->DrainTasks() on the associated platform/isolate.
7301cb0ef41Sopenharmony_ci//   3. If the event loop is alive again, go to Step 1.
7311cb0ef41Sopenharmony_ci// 4. Call EmitProcessBeforeExit().
7321cb0ef41Sopenharmony_ci//   5. If the event loop is alive again, go to Step 1.
7331cb0ef41Sopenharmony_ci// 6. Call EmitProcessExit() and forward the return value.
7341cb0ef41Sopenharmony_ci// If at any point node::Stop() is called, the function will attempt to return
7351cb0ef41Sopenharmony_ci// as soon as possible, returning an empty `Maybe`.
7361cb0ef41Sopenharmony_ci// This function only works if `env` has an associated `MultiIsolatePlatform`.
7371cb0ef41Sopenharmony_ciNODE_EXTERN v8::Maybe<int> SpinEventLoop(Environment* env);
7381cb0ef41Sopenharmony_ci
7391cb0ef41Sopenharmony_ciclass NODE_EXTERN CommonEnvironmentSetup {
7401cb0ef41Sopenharmony_ci public:
7411cb0ef41Sopenharmony_ci  ~CommonEnvironmentSetup();
7421cb0ef41Sopenharmony_ci
7431cb0ef41Sopenharmony_ci  // Create a new CommonEnvironmentSetup, that is, a group of objects that
7441cb0ef41Sopenharmony_ci  // together form the typical setup for a single Node.js Environment instance.
7451cb0ef41Sopenharmony_ci  // If any error occurs, `*errors` will be populated and the returned pointer
7461cb0ef41Sopenharmony_ci  // will be empty.
7471cb0ef41Sopenharmony_ci  // env_args will be passed through as arguments to CreateEnvironment(), after
7481cb0ef41Sopenharmony_ci  // `isolate_data` and `context`.
7491cb0ef41Sopenharmony_ci  template <typename... EnvironmentArgs>
7501cb0ef41Sopenharmony_ci  static std::unique_ptr<CommonEnvironmentSetup> Create(
7511cb0ef41Sopenharmony_ci      MultiIsolatePlatform* platform,
7521cb0ef41Sopenharmony_ci      std::vector<std::string>* errors,
7531cb0ef41Sopenharmony_ci      EnvironmentArgs&&... env_args);
7541cb0ef41Sopenharmony_ci
7551cb0ef41Sopenharmony_ci  struct uv_loop_s* event_loop() const;
7561cb0ef41Sopenharmony_ci  std::shared_ptr<ArrayBufferAllocator> array_buffer_allocator() const;
7571cb0ef41Sopenharmony_ci  v8::Isolate* isolate() const;
7581cb0ef41Sopenharmony_ci  IsolateData* isolate_data() const;
7591cb0ef41Sopenharmony_ci  Environment* env() const;
7601cb0ef41Sopenharmony_ci  v8::Local<v8::Context> context() const;
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ci  CommonEnvironmentSetup(const CommonEnvironmentSetup&) = delete;
7631cb0ef41Sopenharmony_ci  CommonEnvironmentSetup& operator=(const CommonEnvironmentSetup&) = delete;
7641cb0ef41Sopenharmony_ci  CommonEnvironmentSetup(CommonEnvironmentSetup&&) = delete;
7651cb0ef41Sopenharmony_ci  CommonEnvironmentSetup& operator=(CommonEnvironmentSetup&&) = delete;
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_ci private:
7681cb0ef41Sopenharmony_ci  struct Impl;
7691cb0ef41Sopenharmony_ci  Impl* impl_;
7701cb0ef41Sopenharmony_ci  CommonEnvironmentSetup(
7711cb0ef41Sopenharmony_ci      MultiIsolatePlatform*,
7721cb0ef41Sopenharmony_ci      std::vector<std::string>*,
7731cb0ef41Sopenharmony_ci      std::function<Environment*(const CommonEnvironmentSetup*)>);
7741cb0ef41Sopenharmony_ci};
7751cb0ef41Sopenharmony_ci
7761cb0ef41Sopenharmony_ci// Implementation for CommonEnvironmentSetup::Create
7771cb0ef41Sopenharmony_citemplate <typename... EnvironmentArgs>
7781cb0ef41Sopenharmony_cistd::unique_ptr<CommonEnvironmentSetup> CommonEnvironmentSetup::Create(
7791cb0ef41Sopenharmony_ci    MultiIsolatePlatform* platform,
7801cb0ef41Sopenharmony_ci    std::vector<std::string>* errors,
7811cb0ef41Sopenharmony_ci    EnvironmentArgs&&... env_args) {
7821cb0ef41Sopenharmony_ci  auto ret = std::unique_ptr<CommonEnvironmentSetup>(new CommonEnvironmentSetup(
7831cb0ef41Sopenharmony_ci      platform, errors,
7841cb0ef41Sopenharmony_ci      [&](const CommonEnvironmentSetup* setup) -> Environment* {
7851cb0ef41Sopenharmony_ci        return CreateEnvironment(
7861cb0ef41Sopenharmony_ci            setup->isolate_data(), setup->context(),
7871cb0ef41Sopenharmony_ci            std::forward<EnvironmentArgs>(env_args)...);
7881cb0ef41Sopenharmony_ci      }));
7891cb0ef41Sopenharmony_ci  if (!errors->empty()) ret.reset();
7901cb0ef41Sopenharmony_ci  return ret;
7911cb0ef41Sopenharmony_ci}
7921cb0ef41Sopenharmony_ci
7931cb0ef41Sopenharmony_ci/* Converts a unixtime to V8 Date */
7941cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use v8::Date::New() directly",
7951cb0ef41Sopenharmony_ci                inline v8::Local<v8::Value> NODE_UNIXTIME_V8(double time) {
7961cb0ef41Sopenharmony_ci                  return v8::Date::New(
7971cb0ef41Sopenharmony_ci                             v8::Isolate::GetCurrent()->GetCurrentContext(),
7981cb0ef41Sopenharmony_ci                             1000 * time)
7991cb0ef41Sopenharmony_ci                      .ToLocalChecked();
8001cb0ef41Sopenharmony_ci                })
8011cb0ef41Sopenharmony_ci#define NODE_UNIXTIME_V8 node::NODE_UNIXTIME_V8
8021cb0ef41Sopenharmony_ciNODE_DEPRECATED("Use v8::Date::ValueOf() directly",
8031cb0ef41Sopenharmony_ci                inline double NODE_V8_UNIXTIME(v8::Local<v8::Date> date) {
8041cb0ef41Sopenharmony_ci  return date->ValueOf() / 1000;
8051cb0ef41Sopenharmony_ci})
8061cb0ef41Sopenharmony_ci#define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME
8071cb0ef41Sopenharmony_ci
8081cb0ef41Sopenharmony_ci#define NODE_DEFINE_CONSTANT(target, constant)                                \
8091cb0ef41Sopenharmony_ci  do {                                                                        \
8101cb0ef41Sopenharmony_ci    v8::Isolate* isolate = target->GetIsolate();                              \
8111cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
8121cb0ef41Sopenharmony_ci    v8::Local<v8::String> constant_name =                                     \
8131cb0ef41Sopenharmony_ci        v8::String::NewFromUtf8(isolate, #constant,                           \
8141cb0ef41Sopenharmony_ci            v8::NewStringType::kInternalized).ToLocalChecked();               \
8151cb0ef41Sopenharmony_ci    v8::Local<v8::Number> constant_value =                                    \
8161cb0ef41Sopenharmony_ci        v8::Number::New(isolate, static_cast<double>(constant));              \
8171cb0ef41Sopenharmony_ci    v8::PropertyAttribute constant_attributes =                               \
8181cb0ef41Sopenharmony_ci        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
8191cb0ef41Sopenharmony_ci    (target)->DefineOwnProperty(context,                                      \
8201cb0ef41Sopenharmony_ci                                constant_name,                                \
8211cb0ef41Sopenharmony_ci                                constant_value,                               \
8221cb0ef41Sopenharmony_ci                                constant_attributes).Check();                 \
8231cb0ef41Sopenharmony_ci  }                                                                           \
8241cb0ef41Sopenharmony_ci  while (0)
8251cb0ef41Sopenharmony_ci
8261cb0ef41Sopenharmony_ci#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant)                         \
8271cb0ef41Sopenharmony_ci  do {                                                                        \
8281cb0ef41Sopenharmony_ci    v8::Isolate* isolate = target->GetIsolate();                              \
8291cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
8301cb0ef41Sopenharmony_ci    v8::Local<v8::String> constant_name =                                     \
8311cb0ef41Sopenharmony_ci        v8::String::NewFromUtf8(isolate, #constant,                           \
8321cb0ef41Sopenharmony_ci                                v8::NewStringType::kInternalized)             \
8331cb0ef41Sopenharmony_ci                                  .ToLocalChecked();                          \
8341cb0ef41Sopenharmony_ci    v8::Local<v8::Number> constant_value =                                    \
8351cb0ef41Sopenharmony_ci        v8::Number::New(isolate, static_cast<double>(constant));              \
8361cb0ef41Sopenharmony_ci    v8::PropertyAttribute constant_attributes =                               \
8371cb0ef41Sopenharmony_ci        static_cast<v8::PropertyAttribute>(v8::ReadOnly |                     \
8381cb0ef41Sopenharmony_ci                                           v8::DontDelete |                   \
8391cb0ef41Sopenharmony_ci                                           v8::DontEnum);                     \
8401cb0ef41Sopenharmony_ci    (target)->DefineOwnProperty(context,                                      \
8411cb0ef41Sopenharmony_ci                                constant_name,                                \
8421cb0ef41Sopenharmony_ci                                constant_value,                               \
8431cb0ef41Sopenharmony_ci                                constant_attributes).Check();                 \
8441cb0ef41Sopenharmony_ci  }                                                                           \
8451cb0ef41Sopenharmony_ci  while (0)
8461cb0ef41Sopenharmony_ci
8471cb0ef41Sopenharmony_ci// Used to be a macro, hence the uppercase name.
8481cb0ef41Sopenharmony_ciinline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
8491cb0ef41Sopenharmony_ci                            const char* name,
8501cb0ef41Sopenharmony_ci                            v8::FunctionCallback callback) {
8511cb0ef41Sopenharmony_ci  v8::Isolate* isolate = v8::Isolate::GetCurrent();
8521cb0ef41Sopenharmony_ci  v8::HandleScope handle_scope(isolate);
8531cb0ef41Sopenharmony_ci  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
8541cb0ef41Sopenharmony_ci                                                                callback);
8551cb0ef41Sopenharmony_ci  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
8561cb0ef41Sopenharmony_ci      v8::NewStringType::kInternalized).ToLocalChecked();
8571cb0ef41Sopenharmony_ci  t->SetClassName(fn_name);
8581cb0ef41Sopenharmony_ci  recv->Set(fn_name, t);
8591cb0ef41Sopenharmony_ci}
8601cb0ef41Sopenharmony_ci
8611cb0ef41Sopenharmony_ci// Used to be a macro, hence the uppercase name.
8621cb0ef41Sopenharmony_ciinline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
8631cb0ef41Sopenharmony_ci                            const char* name,
8641cb0ef41Sopenharmony_ci                            v8::FunctionCallback callback) {
8651cb0ef41Sopenharmony_ci  v8::Isolate* isolate = v8::Isolate::GetCurrent();
8661cb0ef41Sopenharmony_ci  v8::HandleScope handle_scope(isolate);
8671cb0ef41Sopenharmony_ci  v8::Local<v8::Context> context = isolate->GetCurrentContext();
8681cb0ef41Sopenharmony_ci  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
8691cb0ef41Sopenharmony_ci                                                                callback);
8701cb0ef41Sopenharmony_ci  v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
8711cb0ef41Sopenharmony_ci  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
8721cb0ef41Sopenharmony_ci      v8::NewStringType::kInternalized).ToLocalChecked();
8731cb0ef41Sopenharmony_ci  fn->SetName(fn_name);
8741cb0ef41Sopenharmony_ci  recv->Set(context, fn_name, fn).Check();
8751cb0ef41Sopenharmony_ci}
8761cb0ef41Sopenharmony_ci#define NODE_SET_METHOD node::NODE_SET_METHOD
8771cb0ef41Sopenharmony_ci
8781cb0ef41Sopenharmony_ci// Used to be a macro, hence the uppercase name.
8791cb0ef41Sopenharmony_ci// Not a template because it only makes sense for FunctionTemplates.
8801cb0ef41Sopenharmony_ciinline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
8811cb0ef41Sopenharmony_ci                                      const char* name,
8821cb0ef41Sopenharmony_ci                                      v8::FunctionCallback callback) {
8831cb0ef41Sopenharmony_ci  v8::Isolate* isolate = v8::Isolate::GetCurrent();
8841cb0ef41Sopenharmony_ci  v8::HandleScope handle_scope(isolate);
8851cb0ef41Sopenharmony_ci  v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
8861cb0ef41Sopenharmony_ci  v8::Local<v8::FunctionTemplate> t =
8871cb0ef41Sopenharmony_ci      v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
8881cb0ef41Sopenharmony_ci  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
8891cb0ef41Sopenharmony_ci      v8::NewStringType::kInternalized).ToLocalChecked();
8901cb0ef41Sopenharmony_ci  t->SetClassName(fn_name);
8911cb0ef41Sopenharmony_ci  recv->PrototypeTemplate()->Set(fn_name, t);
8921cb0ef41Sopenharmony_ci}
8931cb0ef41Sopenharmony_ci#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_ci// BINARY is a deprecated alias of LATIN1.
8961cb0ef41Sopenharmony_ci// BASE64URL is not currently exposed to the JavaScript side.
8971cb0ef41Sopenharmony_cienum encoding {
8981cb0ef41Sopenharmony_ci  ASCII,
8991cb0ef41Sopenharmony_ci  UTF8,
9001cb0ef41Sopenharmony_ci  BASE64,
9011cb0ef41Sopenharmony_ci  UCS2,
9021cb0ef41Sopenharmony_ci  BINARY,
9031cb0ef41Sopenharmony_ci  HEX,
9041cb0ef41Sopenharmony_ci  BUFFER,
9051cb0ef41Sopenharmony_ci  BASE64URL,
9061cb0ef41Sopenharmony_ci  LATIN1 = BINARY
9071cb0ef41Sopenharmony_ci};
9081cb0ef41Sopenharmony_ci
9091cb0ef41Sopenharmony_ciNODE_EXTERN enum encoding ParseEncoding(
9101cb0ef41Sopenharmony_ci    v8::Isolate* isolate,
9111cb0ef41Sopenharmony_ci    v8::Local<v8::Value> encoding_v,
9121cb0ef41Sopenharmony_ci    enum encoding default_encoding = LATIN1);
9131cb0ef41Sopenharmony_ci
9141cb0ef41Sopenharmony_ciNODE_EXTERN void FatalException(v8::Isolate* isolate,
9151cb0ef41Sopenharmony_ci                                const v8::TryCatch& try_catch);
9161cb0ef41Sopenharmony_ci
9171cb0ef41Sopenharmony_ciNODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
9181cb0ef41Sopenharmony_ci                                        const char* buf,
9191cb0ef41Sopenharmony_ci                                        size_t len,
9201cb0ef41Sopenharmony_ci                                        enum encoding encoding = LATIN1);
9211cb0ef41Sopenharmony_ci
9221cb0ef41Sopenharmony_ci// Warning: This reverses endianness on Big Endian platforms, even though the
9231cb0ef41Sopenharmony_ci// signature using uint16_t implies that it should not.
9241cb0ef41Sopenharmony_ciNODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
9251cb0ef41Sopenharmony_ci                                        const uint16_t* buf,
9261cb0ef41Sopenharmony_ci                                        size_t len);
9271cb0ef41Sopenharmony_ci
9281cb0ef41Sopenharmony_ci// Returns -1 if the handle was not valid for decoding
9291cb0ef41Sopenharmony_ciNODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
9301cb0ef41Sopenharmony_ci                                v8::Local<v8::Value>,
9311cb0ef41Sopenharmony_ci                                enum encoding encoding = LATIN1);
9321cb0ef41Sopenharmony_ci// returns bytes written.
9331cb0ef41Sopenharmony_ciNODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
9341cb0ef41Sopenharmony_ci                                char* buf,
9351cb0ef41Sopenharmony_ci                                size_t buflen,
9361cb0ef41Sopenharmony_ci                                v8::Local<v8::Value>,
9371cb0ef41Sopenharmony_ci                                enum encoding encoding = LATIN1);
9381cb0ef41Sopenharmony_ci#ifdef _WIN32
9391cb0ef41Sopenharmony_ciNODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
9401cb0ef41Sopenharmony_ci    v8::Isolate* isolate,
9411cb0ef41Sopenharmony_ci    int errorno,
9421cb0ef41Sopenharmony_ci    const char* syscall = nullptr,
9431cb0ef41Sopenharmony_ci    const char* msg = "",
9441cb0ef41Sopenharmony_ci    const char* path = nullptr);
9451cb0ef41Sopenharmony_ci#endif
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_ciconst char* signo_string(int errorno);
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ci
9501cb0ef41Sopenharmony_citypedef void (*addon_register_func)(
9511cb0ef41Sopenharmony_ci    v8::Local<v8::Object> exports,
9521cb0ef41Sopenharmony_ci    v8::Local<v8::Value> module,
9531cb0ef41Sopenharmony_ci    void* priv);
9541cb0ef41Sopenharmony_ci
9551cb0ef41Sopenharmony_citypedef void (*addon_context_register_func)(
9561cb0ef41Sopenharmony_ci    v8::Local<v8::Object> exports,
9571cb0ef41Sopenharmony_ci    v8::Local<v8::Value> module,
9581cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context,
9591cb0ef41Sopenharmony_ci    void* priv);
9601cb0ef41Sopenharmony_ci
9611cb0ef41Sopenharmony_cienum ModuleFlags {
9621cb0ef41Sopenharmony_ci  kLinked = 0x02
9631cb0ef41Sopenharmony_ci};
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_cistruct node_module {
9661cb0ef41Sopenharmony_ci  int nm_version;
9671cb0ef41Sopenharmony_ci  unsigned int nm_flags;
9681cb0ef41Sopenharmony_ci  void* nm_dso_handle;
9691cb0ef41Sopenharmony_ci  const char* nm_filename;
9701cb0ef41Sopenharmony_ci  node::addon_register_func nm_register_func;
9711cb0ef41Sopenharmony_ci  node::addon_context_register_func nm_context_register_func;
9721cb0ef41Sopenharmony_ci  const char* nm_modname;
9731cb0ef41Sopenharmony_ci  void* nm_priv;
9741cb0ef41Sopenharmony_ci  struct node_module* nm_link;
9751cb0ef41Sopenharmony_ci};
9761cb0ef41Sopenharmony_ci
9771cb0ef41Sopenharmony_ciextern "C" NODE_EXTERN void node_module_register(void* mod);
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ci#ifdef _WIN32
9801cb0ef41Sopenharmony_ci# define NODE_MODULE_EXPORT __declspec(dllexport)
9811cb0ef41Sopenharmony_ci#else
9821cb0ef41Sopenharmony_ci# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
9831cb0ef41Sopenharmony_ci#endif
9841cb0ef41Sopenharmony_ci
9851cb0ef41Sopenharmony_ci#ifdef NODE_SHARED_MODE
9861cb0ef41Sopenharmony_ci# define NODE_CTOR_PREFIX
9871cb0ef41Sopenharmony_ci#else
9881cb0ef41Sopenharmony_ci# define NODE_CTOR_PREFIX static
9891cb0ef41Sopenharmony_ci#endif
9901cb0ef41Sopenharmony_ci
9911cb0ef41Sopenharmony_ci#if defined(_MSC_VER)
9921cb0ef41Sopenharmony_ci#define NODE_C_CTOR(fn)                                               \
9931cb0ef41Sopenharmony_ci  NODE_CTOR_PREFIX void __cdecl fn(void);                             \
9941cb0ef41Sopenharmony_ci  namespace {                                                         \
9951cb0ef41Sopenharmony_ci  struct fn##_ {                                                      \
9961cb0ef41Sopenharmony_ci    fn##_() { fn(); };                                                \
9971cb0ef41Sopenharmony_ci  } fn##_v_;                                                          \
9981cb0ef41Sopenharmony_ci  }                                                                   \
9991cb0ef41Sopenharmony_ci  NODE_CTOR_PREFIX void __cdecl fn(void)
10001cb0ef41Sopenharmony_ci#else
10011cb0ef41Sopenharmony_ci#define NODE_C_CTOR(fn)                                               \
10021cb0ef41Sopenharmony_ci  NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
10031cb0ef41Sopenharmony_ci  NODE_CTOR_PREFIX void fn(void)
10041cb0ef41Sopenharmony_ci#endif
10051cb0ef41Sopenharmony_ci
10061cb0ef41Sopenharmony_ci#define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
10071cb0ef41Sopenharmony_ci  extern "C" {                                                        \
10081cb0ef41Sopenharmony_ci    static node::node_module _module =                                \
10091cb0ef41Sopenharmony_ci    {                                                                 \
10101cb0ef41Sopenharmony_ci      NODE_MODULE_VERSION,                                            \
10111cb0ef41Sopenharmony_ci      flags,                                                          \
10121cb0ef41Sopenharmony_ci      NULL,  /* NOLINT (readability/null_usage) */                    \
10131cb0ef41Sopenharmony_ci      __FILE__,                                                       \
10141cb0ef41Sopenharmony_ci      (node::addon_register_func) (regfunc),                          \
10151cb0ef41Sopenharmony_ci      NULL,  /* NOLINT (readability/null_usage) */                    \
10161cb0ef41Sopenharmony_ci      NODE_STRINGIFY(modname),                                        \
10171cb0ef41Sopenharmony_ci      priv,                                                           \
10181cb0ef41Sopenharmony_ci      NULL   /* NOLINT (readability/null_usage) */                    \
10191cb0ef41Sopenharmony_ci    };                                                                \
10201cb0ef41Sopenharmony_ci    NODE_C_CTOR(_register_ ## modname) {                              \
10211cb0ef41Sopenharmony_ci      node_module_register(&_module);                                 \
10221cb0ef41Sopenharmony_ci    }                                                                 \
10231cb0ef41Sopenharmony_ci  }
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci#define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
10261cb0ef41Sopenharmony_ci  extern "C" {                                                        \
10271cb0ef41Sopenharmony_ci    static node::node_module _module =                                \
10281cb0ef41Sopenharmony_ci    {                                                                 \
10291cb0ef41Sopenharmony_ci      NODE_MODULE_VERSION,                                            \
10301cb0ef41Sopenharmony_ci      flags,                                                          \
10311cb0ef41Sopenharmony_ci      NULL,  /* NOLINT (readability/null_usage) */                    \
10321cb0ef41Sopenharmony_ci      __FILE__,                                                       \
10331cb0ef41Sopenharmony_ci      NULL,  /* NOLINT (readability/null_usage) */                    \
10341cb0ef41Sopenharmony_ci      (node::addon_context_register_func) (regfunc),                  \
10351cb0ef41Sopenharmony_ci      NODE_STRINGIFY(modname),                                        \
10361cb0ef41Sopenharmony_ci      priv,                                                           \
10371cb0ef41Sopenharmony_ci      NULL  /* NOLINT (readability/null_usage) */                     \
10381cb0ef41Sopenharmony_ci    };                                                                \
10391cb0ef41Sopenharmony_ci    NODE_C_CTOR(_register_ ## modname) {                              \
10401cb0ef41Sopenharmony_ci      node_module_register(&_module);                                 \
10411cb0ef41Sopenharmony_ci    }                                                                 \
10421cb0ef41Sopenharmony_ci  }
10431cb0ef41Sopenharmony_ci
10441cb0ef41Sopenharmony_ci// Usage: `NODE_MODULE(NODE_GYP_MODULE_NAME, InitializerFunction)`
10451cb0ef41Sopenharmony_ci// If no NODE_MODULE is declared, Node.js looks for the well-known
10461cb0ef41Sopenharmony_ci// symbol `node_register_module_v${NODE_MODULE_VERSION}`.
10471cb0ef41Sopenharmony_ci#define NODE_MODULE(modname, regfunc)                                 \
10481cb0ef41Sopenharmony_ci  NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
10491cb0ef41Sopenharmony_ci
10501cb0ef41Sopenharmony_ci#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
10511cb0ef41Sopenharmony_ci  /* NOLINTNEXTLINE (readability/null_usage) */                       \
10521cb0ef41Sopenharmony_ci  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci// Embedders can use this type of binding for statically linked native bindings.
10551cb0ef41Sopenharmony_ci// It is used the same way addon bindings are used, except that linked bindings
10561cb0ef41Sopenharmony_ci// can be accessed through `process._linkedBinding(modname)`.
10571cb0ef41Sopenharmony_ci#define NODE_MODULE_LINKED(modname, regfunc)                               \
10581cb0ef41Sopenharmony_ci  /* NOLINTNEXTLINE (readability/null_usage) */                            \
10591cb0ef41Sopenharmony_ci  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL,                      \
10601cb0ef41Sopenharmony_ci                              node::ModuleFlags::kLinked)
10611cb0ef41Sopenharmony_ci
10621cb0ef41Sopenharmony_ci/*
10631cb0ef41Sopenharmony_ci * For backward compatibility in add-on modules.
10641cb0ef41Sopenharmony_ci */
10651cb0ef41Sopenharmony_ci#define NODE_MODULE_DECL /* nothing */
10661cb0ef41Sopenharmony_ci
10671cb0ef41Sopenharmony_ci#define NODE_MODULE_INITIALIZER_BASE node_register_module_v
10681cb0ef41Sopenharmony_ci
10691cb0ef41Sopenharmony_ci#define NODE_MODULE_INITIALIZER_X(base, version)                      \
10701cb0ef41Sopenharmony_ci    NODE_MODULE_INITIALIZER_X_HELPER(base, version)
10711cb0ef41Sopenharmony_ci
10721cb0ef41Sopenharmony_ci#define NODE_MODULE_INITIALIZER_X_HELPER(base, version) base##version
10731cb0ef41Sopenharmony_ci
10741cb0ef41Sopenharmony_ci#define NODE_MODULE_INITIALIZER                                       \
10751cb0ef41Sopenharmony_ci  NODE_MODULE_INITIALIZER_X(NODE_MODULE_INITIALIZER_BASE,             \
10761cb0ef41Sopenharmony_ci      NODE_MODULE_VERSION)
10771cb0ef41Sopenharmony_ci
10781cb0ef41Sopenharmony_ci#define NODE_MODULE_INIT()                                            \
10791cb0ef41Sopenharmony_ci  extern "C" NODE_MODULE_EXPORT void                                  \
10801cb0ef41Sopenharmony_ci  NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,              \
10811cb0ef41Sopenharmony_ci                          v8::Local<v8::Value> module,                \
10821cb0ef41Sopenharmony_ci                          v8::Local<v8::Context> context);            \
10831cb0ef41Sopenharmony_ci  NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME,                     \
10841cb0ef41Sopenharmony_ci                            NODE_MODULE_INITIALIZER)                  \
10851cb0ef41Sopenharmony_ci  void NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,         \
10861cb0ef41Sopenharmony_ci                               v8::Local<v8::Value> module,           \
10871cb0ef41Sopenharmony_ci                               v8::Local<v8::Context> context)
10881cb0ef41Sopenharmony_ci
10891cb0ef41Sopenharmony_ci// Allows embedders to add a binding to the current Environment* that can be
10901cb0ef41Sopenharmony_ci// accessed through process._linkedBinding() in the target Environment and all
10911cb0ef41Sopenharmony_ci// Worker threads that it creates.
10921cb0ef41Sopenharmony_ci// In each variant, the registration function needs to be usable at least for
10931cb0ef41Sopenharmony_ci// the time during which the Environment exists.
10941cb0ef41Sopenharmony_ciNODE_EXTERN void AddLinkedBinding(Environment* env, const node_module& mod);
10951cb0ef41Sopenharmony_ciNODE_EXTERN void AddLinkedBinding(Environment* env,
10961cb0ef41Sopenharmony_ci                                  const struct jsvm_module& mod);
10971cb0ef41Sopenharmony_ciNODE_EXTERN void AddLinkedBinding(Environment* env,
10981cb0ef41Sopenharmony_ci                                  const char* name,
10991cb0ef41Sopenharmony_ci                                  addon_context_register_func fn,
11001cb0ef41Sopenharmony_ci                                  void* priv);
11011cb0ef41Sopenharmony_ciNODE_EXTERN void AddLinkedBinding(
11021cb0ef41Sopenharmony_ci    Environment* env,
11031cb0ef41Sopenharmony_ci    const char* name,
11041cb0ef41Sopenharmony_ci    jsvm_addon_register_func fn,
11051cb0ef41Sopenharmony_ci    int32_t module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION);
11061cb0ef41Sopenharmony_ci
11071cb0ef41Sopenharmony_ci/* Registers a callback with the passed-in Environment instance. The callback
11081cb0ef41Sopenharmony_ci * is called after the event loop exits, but before the VM is disposed.
11091cb0ef41Sopenharmony_ci * Callbacks are run in reverse order of registration, i.e. newest first.
11101cb0ef41Sopenharmony_ci */
11111cb0ef41Sopenharmony_ciNODE_EXTERN void AtExit(Environment* env,
11121cb0ef41Sopenharmony_ci                        void (*cb)(void* arg),
11131cb0ef41Sopenharmony_ci                        void* arg);
11141cb0ef41Sopenharmony_ci
11151cb0ef41Sopenharmony_citypedef double async_id;
11161cb0ef41Sopenharmony_cistruct async_context {
11171cb0ef41Sopenharmony_ci  ::node::async_id async_id;
11181cb0ef41Sopenharmony_ci  ::node::async_id trigger_async_id;
11191cb0ef41Sopenharmony_ci};
11201cb0ef41Sopenharmony_ci
11211cb0ef41Sopenharmony_ci/* This is a lot like node::AtExit, except that the hooks added via this
11221cb0ef41Sopenharmony_ci * function are run before the AtExit ones and will always be registered
11231cb0ef41Sopenharmony_ci * for the current Environment instance.
11241cb0ef41Sopenharmony_ci * These functions are safe to use in an addon supporting multiple
11251cb0ef41Sopenharmony_ci * threads/isolates. */
11261cb0ef41Sopenharmony_ciNODE_EXTERN void AddEnvironmentCleanupHook(v8::Isolate* isolate,
11271cb0ef41Sopenharmony_ci                                           void (*fun)(void* arg),
11281cb0ef41Sopenharmony_ci                                           void* arg);
11291cb0ef41Sopenharmony_ci
11301cb0ef41Sopenharmony_ciNODE_EXTERN void RemoveEnvironmentCleanupHook(v8::Isolate* isolate,
11311cb0ef41Sopenharmony_ci                                              void (*fun)(void* arg),
11321cb0ef41Sopenharmony_ci                                              void* arg);
11331cb0ef41Sopenharmony_ci
11341cb0ef41Sopenharmony_ci/* These are async equivalents of the above. After the cleanup hook is invoked,
11351cb0ef41Sopenharmony_ci * `cb(cbarg)` *must* be called, and attempting to remove the cleanup hook will
11361cb0ef41Sopenharmony_ci * have no effect. */
11371cb0ef41Sopenharmony_cistruct ACHHandle;
11381cb0ef41Sopenharmony_cistruct NODE_EXTERN DeleteACHHandle { void operator()(ACHHandle*) const; };
11391cb0ef41Sopenharmony_citypedef std::unique_ptr<ACHHandle, DeleteACHHandle> AsyncCleanupHookHandle;
11401cb0ef41Sopenharmony_ci
11411cb0ef41Sopenharmony_ci/* This function is not intended to be used externally, it exists to aid in
11421cb0ef41Sopenharmony_ci * keeping ABI compatibility between Node and Electron. */
11431cb0ef41Sopenharmony_ciNODE_EXTERN ACHHandle* AddEnvironmentCleanupHookInternal(
11441cb0ef41Sopenharmony_ci    v8::Isolate* isolate,
11451cb0ef41Sopenharmony_ci    void (*fun)(void* arg, void (*cb)(void*), void* cbarg),
11461cb0ef41Sopenharmony_ci    void* arg);
11471cb0ef41Sopenharmony_ciinline AsyncCleanupHookHandle AddEnvironmentCleanupHook(
11481cb0ef41Sopenharmony_ci    v8::Isolate* isolate,
11491cb0ef41Sopenharmony_ci    void (*fun)(void* arg, void (*cb)(void*), void* cbarg),
11501cb0ef41Sopenharmony_ci    void* arg) {
11511cb0ef41Sopenharmony_ci  return AsyncCleanupHookHandle(AddEnvironmentCleanupHookInternal(isolate, fun,
11521cb0ef41Sopenharmony_ci      arg));
11531cb0ef41Sopenharmony_ci}
11541cb0ef41Sopenharmony_ci
11551cb0ef41Sopenharmony_ci/* This function is not intended to be used externally, it exists to aid in
11561cb0ef41Sopenharmony_ci * keeping ABI compatibility between Node and Electron. */
11571cb0ef41Sopenharmony_ciNODE_EXTERN void RemoveEnvironmentCleanupHookInternal(ACHHandle* holder);
11581cb0ef41Sopenharmony_ciinline void RemoveEnvironmentCleanupHook(AsyncCleanupHookHandle holder) {
11591cb0ef41Sopenharmony_ci  RemoveEnvironmentCleanupHookInternal(holder.get());
11601cb0ef41Sopenharmony_ci}
11611cb0ef41Sopenharmony_ci
11621cb0ef41Sopenharmony_ci// This behaves like V8's Isolate::RequestInterrupt(), but also wakes up
11631cb0ef41Sopenharmony_ci// the event loop if it is currently idle. Interrupt requests are drained
11641cb0ef41Sopenharmony_ci// in `FreeEnvironment()`. The passed callback can not call back into
11651cb0ef41Sopenharmony_ci// JavaScript.
11661cb0ef41Sopenharmony_ci// This function can be called from any thread.
11671cb0ef41Sopenharmony_ciNODE_EXTERN void RequestInterrupt(Environment* env,
11681cb0ef41Sopenharmony_ci                                  void (*fun)(void* arg),
11691cb0ef41Sopenharmony_ci                                  void* arg);
11701cb0ef41Sopenharmony_ci
11711cb0ef41Sopenharmony_ci/* Returns the id of the current execution context. If the return value is
11721cb0ef41Sopenharmony_ci * zero then no execution has been set. This will happen if the user handles
11731cb0ef41Sopenharmony_ci * I/O from native code. */
11741cb0ef41Sopenharmony_ciNODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci/* Return same value as async_hooks.triggerAsyncId(); */
11771cb0ef41Sopenharmony_ciNODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
11781cb0ef41Sopenharmony_ci
11791cb0ef41Sopenharmony_ci/* If the native API doesn't inherit from the helper class then the callbacks
11801cb0ef41Sopenharmony_ci * must be triggered manually. This triggers the init() callback. The return
11811cb0ef41Sopenharmony_ci * value is the async id assigned to the resource.
11821cb0ef41Sopenharmony_ci *
11831cb0ef41Sopenharmony_ci * The `trigger_async_id` parameter should correspond to the resource which is
11841cb0ef41Sopenharmony_ci * creating the new resource, which will usually be the return value of
11851cb0ef41Sopenharmony_ci * `AsyncHooksGetTriggerAsyncId()`. */
11861cb0ef41Sopenharmony_ciNODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
11871cb0ef41Sopenharmony_ci                                        v8::Local<v8::Object> resource,
11881cb0ef41Sopenharmony_ci                                        const char* name,
11891cb0ef41Sopenharmony_ci                                        async_id trigger_async_id = -1);
11901cb0ef41Sopenharmony_ci
11911cb0ef41Sopenharmony_ciNODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
11921cb0ef41Sopenharmony_ci                                        v8::Local<v8::Object> resource,
11931cb0ef41Sopenharmony_ci                                        v8::Local<v8::String> name,
11941cb0ef41Sopenharmony_ci                                        async_id trigger_async_id = -1);
11951cb0ef41Sopenharmony_ci
11961cb0ef41Sopenharmony_ci/* Emit the destroy() callback. The overload taking an `Environment*` argument
11971cb0ef41Sopenharmony_ci * should be used when the Isolate’s current Context is not associated with
11981cb0ef41Sopenharmony_ci * a Node.js Environment, or when there is no current Context, for example
11991cb0ef41Sopenharmony_ci * when calling this function during garbage collection. In that case, the
12001cb0ef41Sopenharmony_ci * `Environment*` value should have been acquired previously, e.g. through
12011cb0ef41Sopenharmony_ci * `GetCurrentEnvironment()`. */
12021cb0ef41Sopenharmony_ciNODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
12031cb0ef41Sopenharmony_ci                                  async_context asyncContext);
12041cb0ef41Sopenharmony_ciNODE_EXTERN void EmitAsyncDestroy(Environment* env,
12051cb0ef41Sopenharmony_ci                                  async_context asyncContext);
12061cb0ef41Sopenharmony_ci
12071cb0ef41Sopenharmony_ciclass InternalCallbackScope;
12081cb0ef41Sopenharmony_ci
12091cb0ef41Sopenharmony_ci/* This class works like `MakeCallback()` in that it sets up a specific
12101cb0ef41Sopenharmony_ci * asyncContext as the current one and informs the async_hooks and domains
12111cb0ef41Sopenharmony_ci * modules that this context is currently active.
12121cb0ef41Sopenharmony_ci *
12131cb0ef41Sopenharmony_ci * `MakeCallback()` is a wrapper around this class as well as
12141cb0ef41Sopenharmony_ci * `Function::Call()`. Either one of these mechanisms needs to be used for
12151cb0ef41Sopenharmony_ci * top-level calls into JavaScript (i.e. without any existing JS stack).
12161cb0ef41Sopenharmony_ci *
12171cb0ef41Sopenharmony_ci * This object should be stack-allocated to ensure that it is contained in a
12181cb0ef41Sopenharmony_ci * valid HandleScope.
12191cb0ef41Sopenharmony_ci *
12201cb0ef41Sopenharmony_ci * Exceptions happening within this scope will be treated like uncaught
12211cb0ef41Sopenharmony_ci * exceptions. If this behaviour is undesirable, a new `v8::TryCatch` scope
12221cb0ef41Sopenharmony_ci * needs to be created inside of this scope.
12231cb0ef41Sopenharmony_ci */
12241cb0ef41Sopenharmony_ciclass NODE_EXTERN CallbackScope {
12251cb0ef41Sopenharmony_ci public:
12261cb0ef41Sopenharmony_ci  CallbackScope(v8::Isolate* isolate,
12271cb0ef41Sopenharmony_ci                v8::Local<v8::Object> resource,
12281cb0ef41Sopenharmony_ci                async_context asyncContext);
12291cb0ef41Sopenharmony_ci  CallbackScope(Environment* env,
12301cb0ef41Sopenharmony_ci                v8::Local<v8::Object> resource,
12311cb0ef41Sopenharmony_ci                async_context asyncContext);
12321cb0ef41Sopenharmony_ci  ~CallbackScope();
12331cb0ef41Sopenharmony_ci
12341cb0ef41Sopenharmony_ci  void operator=(const CallbackScope&) = delete;
12351cb0ef41Sopenharmony_ci  void operator=(CallbackScope&&) = delete;
12361cb0ef41Sopenharmony_ci  CallbackScope(const CallbackScope&) = delete;
12371cb0ef41Sopenharmony_ci  CallbackScope(CallbackScope&&) = delete;
12381cb0ef41Sopenharmony_ci
12391cb0ef41Sopenharmony_ci private:
12401cb0ef41Sopenharmony_ci  InternalCallbackScope* private_;
12411cb0ef41Sopenharmony_ci  v8::TryCatch try_catch_;
12421cb0ef41Sopenharmony_ci};
12431cb0ef41Sopenharmony_ci
12441cb0ef41Sopenharmony_ci/* An API specific to emit before/after callbacks is unnecessary because
12451cb0ef41Sopenharmony_ci * MakeCallback will automatically call them for you.
12461cb0ef41Sopenharmony_ci *
12471cb0ef41Sopenharmony_ci * These methods may create handles on their own, so run them inside a
12481cb0ef41Sopenharmony_ci * HandleScope.
12491cb0ef41Sopenharmony_ci *
12501cb0ef41Sopenharmony_ci * `asyncId` and `triggerAsyncId` should correspond to the values returned by
12511cb0ef41Sopenharmony_ci * `EmitAsyncInit()` and `AsyncHooksGetTriggerAsyncId()`, respectively, when the
12521cb0ef41Sopenharmony_ci * invoking resource was created. If these values are unknown, 0 can be passed.
12531cb0ef41Sopenharmony_ci * */
12541cb0ef41Sopenharmony_ciNODE_EXTERN
12551cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
12561cb0ef41Sopenharmony_ci                                       v8::Local<v8::Object> recv,
12571cb0ef41Sopenharmony_ci                                       v8::Local<v8::Function> callback,
12581cb0ef41Sopenharmony_ci                                       int argc,
12591cb0ef41Sopenharmony_ci                                       v8::Local<v8::Value>* argv,
12601cb0ef41Sopenharmony_ci                                       async_context asyncContext);
12611cb0ef41Sopenharmony_ciNODE_EXTERN
12621cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
12631cb0ef41Sopenharmony_ci                                       v8::Local<v8::Object> recv,
12641cb0ef41Sopenharmony_ci                                       const char* method,
12651cb0ef41Sopenharmony_ci                                       int argc,
12661cb0ef41Sopenharmony_ci                                       v8::Local<v8::Value>* argv,
12671cb0ef41Sopenharmony_ci                                       async_context asyncContext);
12681cb0ef41Sopenharmony_ciNODE_EXTERN
12691cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
12701cb0ef41Sopenharmony_ci                                       v8::Local<v8::Object> recv,
12711cb0ef41Sopenharmony_ci                                       v8::Local<v8::String> symbol,
12721cb0ef41Sopenharmony_ci                                       int argc,
12731cb0ef41Sopenharmony_ci                                       v8::Local<v8::Value>* argv,
12741cb0ef41Sopenharmony_ci                                       async_context asyncContext);
12751cb0ef41Sopenharmony_ci
12761cb0ef41Sopenharmony_ci/* Helper class users can optionally inherit from. If
12771cb0ef41Sopenharmony_ci * `AsyncResource::MakeCallback()` is used, then all four callbacks will be
12781cb0ef41Sopenharmony_ci * called automatically. */
12791cb0ef41Sopenharmony_ciclass NODE_EXTERN AsyncResource {
12801cb0ef41Sopenharmony_ci public:
12811cb0ef41Sopenharmony_ci  AsyncResource(v8::Isolate* isolate,
12821cb0ef41Sopenharmony_ci                v8::Local<v8::Object> resource,
12831cb0ef41Sopenharmony_ci                const char* name,
12841cb0ef41Sopenharmony_ci                async_id trigger_async_id = -1);
12851cb0ef41Sopenharmony_ci
12861cb0ef41Sopenharmony_ci  virtual ~AsyncResource();
12871cb0ef41Sopenharmony_ci
12881cb0ef41Sopenharmony_ci  AsyncResource(const AsyncResource&) = delete;
12891cb0ef41Sopenharmony_ci  void operator=(const AsyncResource&) = delete;
12901cb0ef41Sopenharmony_ci
12911cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> MakeCallback(
12921cb0ef41Sopenharmony_ci      v8::Local<v8::Function> callback,
12931cb0ef41Sopenharmony_ci      int argc,
12941cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* argv);
12951cb0ef41Sopenharmony_ci
12961cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> MakeCallback(
12971cb0ef41Sopenharmony_ci      const char* method,
12981cb0ef41Sopenharmony_ci      int argc,
12991cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* argv);
13001cb0ef41Sopenharmony_ci
13011cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> MakeCallback(
13021cb0ef41Sopenharmony_ci      v8::Local<v8::String> symbol,
13031cb0ef41Sopenharmony_ci      int argc,
13041cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* argv);
13051cb0ef41Sopenharmony_ci
13061cb0ef41Sopenharmony_ci  v8::Local<v8::Object> get_resource();
13071cb0ef41Sopenharmony_ci  async_id get_async_id() const;
13081cb0ef41Sopenharmony_ci  async_id get_trigger_async_id() const;
13091cb0ef41Sopenharmony_ci
13101cb0ef41Sopenharmony_ci protected:
13111cb0ef41Sopenharmony_ci  class NODE_EXTERN CallbackScope : public node::CallbackScope {
13121cb0ef41Sopenharmony_ci   public:
13131cb0ef41Sopenharmony_ci    explicit CallbackScope(AsyncResource* res);
13141cb0ef41Sopenharmony_ci  };
13151cb0ef41Sopenharmony_ci
13161cb0ef41Sopenharmony_ci private:
13171cb0ef41Sopenharmony_ci  Environment* env_;
13181cb0ef41Sopenharmony_ci  v8::Global<v8::Object> resource_;
13191cb0ef41Sopenharmony_ci  async_context async_context_;
13201cb0ef41Sopenharmony_ci};
13211cb0ef41Sopenharmony_ci
13221cb0ef41Sopenharmony_ci#ifndef _WIN32
13231cb0ef41Sopenharmony_ci// Register a signal handler without interrupting any handlers that node
13241cb0ef41Sopenharmony_ci// itself needs. This does override handlers registered through
13251cb0ef41Sopenharmony_ci// process.on('SIG...', function() { ... }). The `reset_handler` flag indicates
13261cb0ef41Sopenharmony_ci// whether the signal handler for the given signal should be reset to its
13271cb0ef41Sopenharmony_ci// default value before executing the handler (i.e. it works like SA_RESETHAND).
13281cb0ef41Sopenharmony_ci// The `reset_handler` flag is invalid when `signal` is SIGSEGV.
13291cb0ef41Sopenharmony_ciNODE_EXTERN
13301cb0ef41Sopenharmony_civoid RegisterSignalHandler(int signal,
13311cb0ef41Sopenharmony_ci                           void (*handler)(int signal,
13321cb0ef41Sopenharmony_ci                                           siginfo_t* info,
13331cb0ef41Sopenharmony_ci                                           void* ucontext),
13341cb0ef41Sopenharmony_ci                           bool reset_handler = false);
13351cb0ef41Sopenharmony_ci#endif  // _WIN32
13361cb0ef41Sopenharmony_ci
13371cb0ef41Sopenharmony_ci}  // namespace node
13381cb0ef41Sopenharmony_ci
13391cb0ef41Sopenharmony_ci#endif  // SRC_NODE_H_
1340