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_INTERNALS_H_
231cb0ef41Sopenharmony_ci#define SRC_NODE_INTERNALS_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include "env.h"
281cb0ef41Sopenharmony_ci#include "node.h"
291cb0ef41Sopenharmony_ci#include "node_binding.h"
301cb0ef41Sopenharmony_ci#include "node_mutex.h"
311cb0ef41Sopenharmony_ci#include "tracing/trace_event.h"
321cb0ef41Sopenharmony_ci#include "util.h"
331cb0ef41Sopenharmony_ci#include "uv.h"
341cb0ef41Sopenharmony_ci#include "v8.h"
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#include <cstdint>
371cb0ef41Sopenharmony_ci#include <cstdlib>
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci#include <string>
401cb0ef41Sopenharmony_ci#include <vector>
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cistruct sockaddr;
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cinamespace node {
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cinamespace builtins {
471cb0ef41Sopenharmony_ciclass BuiltinLoader;
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cinamespace per_process {
511cb0ef41Sopenharmony_ciextern Mutex env_var_mutex;
521cb0ef41Sopenharmony_ciextern uint64_t node_start_time;
531cb0ef41Sopenharmony_ci}  // namespace per_process
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// Forward declaration
561cb0ef41Sopenharmony_ciclass Environment;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
591cb0ef41Sopenharmony_ci// Sets address and port properties on the info object and returns it.
601cb0ef41Sopenharmony_ci// If |info| is omitted, a new object is returned.
611cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Object> AddressToJS(
621cb0ef41Sopenharmony_ci    Environment* env,
631cb0ef41Sopenharmony_ci    const sockaddr* addr,
641cb0ef41Sopenharmony_ci    v8::Local<v8::Object> info = v8::Local<v8::Object>());
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_citemplate <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
671cb0ef41Sopenharmony_civoid GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
681cb0ef41Sopenharmony_ci  T* wrap;
691cb0ef41Sopenharmony_ci  ASSIGN_OR_RETURN_UNWRAP(&wrap,
701cb0ef41Sopenharmony_ci                          args.Holder(),
711cb0ef41Sopenharmony_ci                          args.GetReturnValue().Set(UV_EBADF));
721cb0ef41Sopenharmony_ci  CHECK(args[0]->IsObject());
731cb0ef41Sopenharmony_ci  sockaddr_storage storage;
741cb0ef41Sopenharmony_ci  int addrlen = sizeof(storage);
751cb0ef41Sopenharmony_ci  sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
761cb0ef41Sopenharmony_ci  const int err = F(&wrap->handle_, addr, &addrlen);
771cb0ef41Sopenharmony_ci  if (err == 0)
781cb0ef41Sopenharmony_ci    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
791cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_civoid PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack);
831cb0ef41Sopenharmony_civoid PrintCaughtException(v8::Isolate* isolate,
841cb0ef41Sopenharmony_ci                          v8::Local<v8::Context> context,
851cb0ef41Sopenharmony_ci                          const v8::TryCatch& try_catch);
861cb0ef41Sopenharmony_cistd::string FormatCaughtException(v8::Isolate* isolate,
871cb0ef41Sopenharmony_ci                                  v8::Local<v8::Context> context,
881cb0ef41Sopenharmony_ci                                  const v8::TryCatch& try_catch);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_civoid ResetStdio();  // Safe to call more than once and from signal handlers.
911cb0ef41Sopenharmony_ci#ifdef __POSIX__
921cb0ef41Sopenharmony_civoid SignalExit(int signal, siginfo_t* info, void* ucontext);
931cb0ef41Sopenharmony_ci#endif
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_cistd::string GetProcessTitle(const char* default_title);
961cb0ef41Sopenharmony_cistd::string GetHumanReadableProcessName();
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_civ8::Maybe<bool> InitializeBaseContextForSnapshot(
991cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context);
1001cb0ef41Sopenharmony_civ8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
1011cb0ef41Sopenharmony_civ8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ciclass NodeArrayBufferAllocator : public ArrayBufferAllocator {
1041cb0ef41Sopenharmony_ci public:
1051cb0ef41Sopenharmony_ci  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  void* Allocate(size_t size) override;  // Defined in src/node.cc
1081cb0ef41Sopenharmony_ci  void* AllocateUninitialized(size_t size) override;
1091cb0ef41Sopenharmony_ci  void Free(void* data, size_t size) override;
1101cb0ef41Sopenharmony_ci  void* Reallocate(void* data, size_t old_size, size_t size) override;
1111cb0ef41Sopenharmony_ci  virtual void RegisterPointer(void* data, size_t size) {
1121cb0ef41Sopenharmony_ci    total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci  virtual void UnregisterPointer(void* data, size_t size) {
1151cb0ef41Sopenharmony_ci    total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  NodeArrayBufferAllocator* GetImpl() final { return this; }
1191cb0ef41Sopenharmony_ci  inline uint64_t total_mem_usage() const {
1201cb0ef41Sopenharmony_ci    return total_mem_usage_.load(std::memory_order_relaxed);
1211cb0ef41Sopenharmony_ci  }
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci private:
1241cb0ef41Sopenharmony_ci  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
1251cb0ef41Sopenharmony_ci  std::atomic<size_t> total_mem_usage_ {0};
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  // Delegate to V8's allocator for compatibility with the V8 memory cage.
1281cb0ef41Sopenharmony_ci  std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{
1291cb0ef41Sopenharmony_ci      v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
1301cb0ef41Sopenharmony_ci};
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ciclass DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {
1331cb0ef41Sopenharmony_ci public:
1341cb0ef41Sopenharmony_ci  ~DebuggingArrayBufferAllocator() override;
1351cb0ef41Sopenharmony_ci  void* Allocate(size_t size) override;
1361cb0ef41Sopenharmony_ci  void* AllocateUninitialized(size_t size) override;
1371cb0ef41Sopenharmony_ci  void Free(void* data, size_t size) override;
1381cb0ef41Sopenharmony_ci  void* Reallocate(void* data, size_t old_size, size_t size) override;
1391cb0ef41Sopenharmony_ci  void RegisterPointer(void* data, size_t size) override;
1401cb0ef41Sopenharmony_ci  void UnregisterPointer(void* data, size_t size) override;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci private:
1431cb0ef41Sopenharmony_ci  void RegisterPointerInternal(void* data, size_t size);
1441cb0ef41Sopenharmony_ci  void UnregisterPointerInternal(void* data, size_t size);
1451cb0ef41Sopenharmony_ci  Mutex mutex_;
1461cb0ef41Sopenharmony_ci  std::unordered_map<void*, size_t> allocations_;
1471cb0ef41Sopenharmony_ci};
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_cinamespace Buffer {
1501cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
1511cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
1521cb0ef41Sopenharmony_ci// Takes ownership of |data|.
1531cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Object> New(Environment* env,
1541cb0ef41Sopenharmony_ci                               char* data,
1551cb0ef41Sopenharmony_ci                               size_t length,
1561cb0ef41Sopenharmony_ci                               void (*callback)(char* data, void* hint),
1571cb0ef41Sopenharmony_ci                               void* hint);
1581cb0ef41Sopenharmony_ci// Takes ownership of |data|.  Must allocate |data| with the current Isolate's
1591cb0ef41Sopenharmony_ci// ArrayBuffer::Allocator().
1601cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Object> New(Environment* env,
1611cb0ef41Sopenharmony_ci                               char* data,
1621cb0ef41Sopenharmony_ci                               size_t length);
1631cb0ef41Sopenharmony_ci// Creates a Buffer instance over an existing ArrayBuffer.
1641cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Uint8Array> New(Environment* env,
1651cb0ef41Sopenharmony_ci                                   v8::Local<v8::ArrayBuffer> ab,
1661cb0ef41Sopenharmony_ci                                   size_t byte_offset,
1671cb0ef41Sopenharmony_ci                                   size_t length);
1681cb0ef41Sopenharmony_ci// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
1691cb0ef41Sopenharmony_ci// Utf8Value and TwoByteValue).
1701cb0ef41Sopenharmony_ci// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
1711cb0ef41Sopenharmony_ci// changed.
1721cb0ef41Sopenharmony_ci// If |buf| contains actual data, this method takes ownership of |buf|'s
1731cb0ef41Sopenharmony_ci// underlying buffer. However, |buf| itself can be reused even after this call,
1741cb0ef41Sopenharmony_ci// but its capacity, if increased through AllocateSufficientStorage, is not
1751cb0ef41Sopenharmony_ci// guaranteed to stay the same.
1761cb0ef41Sopenharmony_citemplate <typename T>
1771cb0ef41Sopenharmony_cistatic v8::MaybeLocal<v8::Object> New(Environment* env,
1781cb0ef41Sopenharmony_ci                                      MaybeStackBuffer<T>* buf) {
1791cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Object> ret;
1801cb0ef41Sopenharmony_ci  char* src = reinterpret_cast<char*>(buf->out());
1811cb0ef41Sopenharmony_ci  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci  if (buf->IsAllocated())
1841cb0ef41Sopenharmony_ci    ret = New(env, src, len_in_bytes);
1851cb0ef41Sopenharmony_ci  else if (!buf->IsInvalidated())
1861cb0ef41Sopenharmony_ci    ret = Copy(env, src, len_in_bytes);
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  if (ret.IsEmpty())
1891cb0ef41Sopenharmony_ci    return ret;
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci  if (buf->IsAllocated())
1921cb0ef41Sopenharmony_ci    buf->Release();
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  return ret;
1951cb0ef41Sopenharmony_ci}
1961cb0ef41Sopenharmony_ci}  // namespace Buffer
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Value> InternalMakeCallback(
1991cb0ef41Sopenharmony_ci    Environment* env,
2001cb0ef41Sopenharmony_ci    v8::Local<v8::Object> resource,
2011cb0ef41Sopenharmony_ci    v8::Local<v8::Object> recv,
2021cb0ef41Sopenharmony_ci    const v8::Local<v8::Function> callback,
2031cb0ef41Sopenharmony_ci    int argc,
2041cb0ef41Sopenharmony_ci    v8::Local<v8::Value> argv[],
2051cb0ef41Sopenharmony_ci    async_context asyncContext);
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate,
2081cb0ef41Sopenharmony_ci                                           v8::Local<v8::Object> recv,
2091cb0ef41Sopenharmony_ci                                           v8::Local<v8::Function> callback,
2101cb0ef41Sopenharmony_ci                                           int argc,
2111cb0ef41Sopenharmony_ci                                           v8::Local<v8::Value> argv[]);
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ciclass InternalCallbackScope {
2141cb0ef41Sopenharmony_ci public:
2151cb0ef41Sopenharmony_ci  enum Flags {
2161cb0ef41Sopenharmony_ci    kNoFlags = 0,
2171cb0ef41Sopenharmony_ci    // Indicates whether 'before' and 'after' hooks should be skipped.
2181cb0ef41Sopenharmony_ci    kSkipAsyncHooks = 1,
2191cb0ef41Sopenharmony_ci    // Indicates whether nextTick and microtask queues should be skipped.
2201cb0ef41Sopenharmony_ci    // This should only be used when there is no call into JS in this scope.
2211cb0ef41Sopenharmony_ci    // (The HTTP parser also uses it for some weird backwards
2221cb0ef41Sopenharmony_ci    // compatibility issues, but it shouldn't.)
2231cb0ef41Sopenharmony_ci    kSkipTaskQueues = 2
2241cb0ef41Sopenharmony_ci  };
2251cb0ef41Sopenharmony_ci  InternalCallbackScope(Environment* env,
2261cb0ef41Sopenharmony_ci                        v8::Local<v8::Object> object,
2271cb0ef41Sopenharmony_ci                        const async_context& asyncContext,
2281cb0ef41Sopenharmony_ci                        int flags = kNoFlags);
2291cb0ef41Sopenharmony_ci  // Utility that can be used by AsyncWrap classes.
2301cb0ef41Sopenharmony_ci  explicit InternalCallbackScope(AsyncWrap* async_wrap, int flags = 0);
2311cb0ef41Sopenharmony_ci  ~InternalCallbackScope();
2321cb0ef41Sopenharmony_ci  void Close();
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci  inline bool Failed() const { return failed_; }
2351cb0ef41Sopenharmony_ci  inline void MarkAsFailed() { failed_ = true; }
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci private:
2381cb0ef41Sopenharmony_ci  Environment* env_;
2391cb0ef41Sopenharmony_ci  async_context async_context_;
2401cb0ef41Sopenharmony_ci  v8::Local<v8::Object> object_;
2411cb0ef41Sopenharmony_ci  bool skip_hooks_;
2421cb0ef41Sopenharmony_ci  bool skip_task_queues_;
2431cb0ef41Sopenharmony_ci  bool failed_ = false;
2441cb0ef41Sopenharmony_ci  bool pushed_ids_ = false;
2451cb0ef41Sopenharmony_ci  bool closed_ = false;
2461cb0ef41Sopenharmony_ci};
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ciclass DebugSealHandleScope {
2491cb0ef41Sopenharmony_ci public:
2501cb0ef41Sopenharmony_ci  explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
2511cb0ef41Sopenharmony_ci#ifdef DEBUG
2521cb0ef41Sopenharmony_ci    : actual_scope_(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
2531cb0ef41Sopenharmony_ci#endif
2541cb0ef41Sopenharmony_ci  {}
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci private:
2571cb0ef41Sopenharmony_ci#ifdef DEBUG
2581cb0ef41Sopenharmony_ci  v8::SealHandleScope actual_scope_;
2591cb0ef41Sopenharmony_ci#endif
2601cb0ef41Sopenharmony_ci};
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ciclass ThreadPoolWork {
2631cb0ef41Sopenharmony_ci public:
2641cb0ef41Sopenharmony_ci  explicit inline ThreadPoolWork(Environment* env, const char* type)
2651cb0ef41Sopenharmony_ci      : env_(env), type_(type) {
2661cb0ef41Sopenharmony_ci    CHECK_NOT_NULL(env);
2671cb0ef41Sopenharmony_ci  }
2681cb0ef41Sopenharmony_ci  inline virtual ~ThreadPoolWork() = default;
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci  inline void ScheduleWork();
2711cb0ef41Sopenharmony_ci  inline int CancelWork();
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ci  virtual void DoThreadPoolWork() = 0;
2741cb0ef41Sopenharmony_ci  virtual void AfterThreadPoolWork(int status) = 0;
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci  Environment* env() const { return env_; }
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci private:
2791cb0ef41Sopenharmony_ci  Environment* env_;
2801cb0ef41Sopenharmony_ci  uv_work_t work_req_;
2811cb0ef41Sopenharmony_ci  const char* type_;
2821cb0ef41Sopenharmony_ci};
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci#define TRACING_CATEGORY_NODE "node"
2851cb0ef41Sopenharmony_ci#define TRACING_CATEGORY_NODE1(one)                                           \
2861cb0ef41Sopenharmony_ci    TRACING_CATEGORY_NODE ","                                                 \
2871cb0ef41Sopenharmony_ci    TRACING_CATEGORY_NODE "." #one
2881cb0ef41Sopenharmony_ci#define TRACING_CATEGORY_NODE2(one, two)                                      \
2891cb0ef41Sopenharmony_ci    TRACING_CATEGORY_NODE ","                                                 \
2901cb0ef41Sopenharmony_ci    TRACING_CATEGORY_NODE "." #one ","                                        \
2911cb0ef41Sopenharmony_ci    TRACING_CATEGORY_NODE "." #one "." #two
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci// Functions defined in node.cc that are exposed via the bootstrapper object
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ci#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
2961cb0ef41Sopenharmony_ci#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
2971cb0ef41Sopenharmony_ci#endif  // defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_cinamespace credentials {
3001cb0ef41Sopenharmony_cibool SafeGetenv(const char* key,
3011cb0ef41Sopenharmony_ci                std::string* text,
3021cb0ef41Sopenharmony_ci                std::shared_ptr<KVStore> env_vars = nullptr,
3031cb0ef41Sopenharmony_ci                v8::Isolate* isolate = nullptr);
3041cb0ef41Sopenharmony_ci}  // namespace credentials
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_civoid DefineZlibConstants(v8::Local<v8::Object> target);
3071cb0ef41Sopenharmony_civ8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
3081cb0ef41Sopenharmony_ci                        uv_loop_t* event_loop,
3091cb0ef41Sopenharmony_ci                        MultiIsolatePlatform* platform,
3101cb0ef41Sopenharmony_ci                        bool has_snapshot_data = false);
3111cb0ef41Sopenharmony_ci// This overload automatically picks the right 'main_script_id' if no callback
3121cb0ef41Sopenharmony_ci// was provided by the embedder.
3131cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Value> StartExecution(Environment* env,
3141cb0ef41Sopenharmony_ci                                         StartExecutionCallback cb = nullptr);
3151cb0ef41Sopenharmony_civ8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
3161cb0ef41Sopenharmony_civoid MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
3171cb0ef41Sopenharmony_ci
3181cb0ef41Sopenharmony_ciclass InitializationResultImpl final : public InitializationResult {
3191cb0ef41Sopenharmony_ci public:
3201cb0ef41Sopenharmony_ci  ~InitializationResultImpl();
3211cb0ef41Sopenharmony_ci  int exit_code() const { return exit_code_; }
3221cb0ef41Sopenharmony_ci  bool early_return() const { return early_return_; }
3231cb0ef41Sopenharmony_ci  const std::vector<std::string>& args() const { return args_; }
3241cb0ef41Sopenharmony_ci  const std::vector<std::string>& exec_args() const { return exec_args_; }
3251cb0ef41Sopenharmony_ci  const std::vector<std::string>& errors() const { return errors_; }
3261cb0ef41Sopenharmony_ci  MultiIsolatePlatform* platform() const { return platform_; }
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci  int exit_code_ = 0;
3291cb0ef41Sopenharmony_ci  std::vector<std::string> args_;
3301cb0ef41Sopenharmony_ci  std::vector<std::string> exec_args_;
3311cb0ef41Sopenharmony_ci  std::vector<std::string> errors_;
3321cb0ef41Sopenharmony_ci  bool early_return_ = false;
3331cb0ef41Sopenharmony_ci  MultiIsolatePlatform* platform_ = nullptr;
3341cb0ef41Sopenharmony_ci};
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_civoid SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
3371cb0ef41Sopenharmony_civoid SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
3381cb0ef41Sopenharmony_civoid SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ci#if HAVE_INSPECTOR
3411cb0ef41Sopenharmony_cinamespace profiler {
3421cb0ef41Sopenharmony_civoid StartProfilers(Environment* env);
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci#endif  // HAVE_INSPECTOR
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci#ifdef __POSIX__
3471cb0ef41Sopenharmony_cistatic constexpr unsigned kMaxSignal = 32;
3481cb0ef41Sopenharmony_ci#endif
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_cibool HasSignalJSHandler(int signum);
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ci#ifdef _WIN32
3531cb0ef41Sopenharmony_citypedef SYSTEMTIME TIME_TYPE;
3541cb0ef41Sopenharmony_ci#else  // UNIX, OSX
3551cb0ef41Sopenharmony_citypedef struct tm TIME_TYPE;
3561cb0ef41Sopenharmony_ci#endif
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_cidouble GetCurrentTimeInMicroseconds();
3591cb0ef41Sopenharmony_ciint WriteFileSync(const char* path, uv_buf_t buf);
3601cb0ef41Sopenharmony_ciint WriteFileSync(v8::Isolate* isolate,
3611cb0ef41Sopenharmony_ci                  const char* path,
3621cb0ef41Sopenharmony_ci                  v8::Local<v8::String> string);
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ciclass DiagnosticFilename {
3651cb0ef41Sopenharmony_ci public:
3661cb0ef41Sopenharmony_ci  static void LocalTime(TIME_TYPE* tm_struct);
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci  inline DiagnosticFilename(Environment* env,
3691cb0ef41Sopenharmony_ci                            const char* prefix,
3701cb0ef41Sopenharmony_ci                            const char* ext);
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ci  inline DiagnosticFilename(uint64_t thread_id,
3731cb0ef41Sopenharmony_ci                            const char* prefix,
3741cb0ef41Sopenharmony_ci                            const char* ext);
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci  inline const char* operator*() const;
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci private:
3791cb0ef41Sopenharmony_ci  static std::string MakeFilename(
3801cb0ef41Sopenharmony_ci      uint64_t thread_id,
3811cb0ef41Sopenharmony_ci      const char* prefix,
3821cb0ef41Sopenharmony_ci      const char* ext);
3831cb0ef41Sopenharmony_ci
3841cb0ef41Sopenharmony_ci  std::string filename_;
3851cb0ef41Sopenharmony_ci};
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_cinamespace heap {
3881cb0ef41Sopenharmony_civ8::Maybe<void> WriteSnapshot(Environment* env, const char* filename);
3891cb0ef41Sopenharmony_ci}
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_cinamespace heap {
3921cb0ef41Sopenharmony_ci
3931cb0ef41Sopenharmony_civoid DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);
3941cb0ef41Sopenharmony_ciusing HeapSnapshotPointer =
3951cb0ef41Sopenharmony_ci  DeleteFnPtr<const v8::HeapSnapshot, DeleteHeapSnapshot>;
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ciBaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
3981cb0ef41Sopenharmony_ci    Environment* env, HeapSnapshotPointer&& snapshot);
3991cb0ef41Sopenharmony_ci}  // namespace heap
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_cinamespace fs {
4021cb0ef41Sopenharmony_cistd::string Basename(const std::string& str, const std::string& extension);
4031cb0ef41Sopenharmony_ci}  // namespace fs
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_cinode_module jsvm_module_to_node_module(const jsvm_module* mod);
4061cb0ef41Sopenharmony_ci
4071cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4081cb0ef41Sopenharmony_ci                         const std::vector<SnapshotIndex>& v);
4091cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4101cb0ef41Sopenharmony_ci                         const std::vector<std::string>& vec);
4111cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4121cb0ef41Sopenharmony_ci                         const std::vector<PropInfo>& vec);
4131cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output, const PropInfo& d);
4141cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& d);
4151cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4161cb0ef41Sopenharmony_ci                         const ImmediateInfo::SerializeInfo& d);
4171cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4181cb0ef41Sopenharmony_ci                         const TickInfo::SerializeInfo& d);
4191cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4201cb0ef41Sopenharmony_ci                         const AsyncHooks::SerializeInfo& d);
4211cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output, const SnapshotMetadata& d);
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_cinamespace performance {
4241cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& output,
4251cb0ef41Sopenharmony_ci                         const PerformanceState::SerializeInfo& d);
4261cb0ef41Sopenharmony_ci}
4271cb0ef41Sopenharmony_ci
4281cb0ef41Sopenharmony_cibool linux_at_secure();
4291cb0ef41Sopenharmony_ci}  // namespace node
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci#endif  // SRC_NODE_INTERNALS_H_
434