11cb0ef41Sopenharmony_ci#ifndef SRC_NODE_MAIN_INSTANCE_H_
21cb0ef41Sopenharmony_ci#define SRC_NODE_MAIN_INSTANCE_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include <cstddef>
71cb0ef41Sopenharmony_ci#include <memory>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "node.h"
101cb0ef41Sopenharmony_ci#include "util.h"
111cb0ef41Sopenharmony_ci#include "uv.h"
121cb0ef41Sopenharmony_ci#include "v8.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace node {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciclass ExternalReferenceRegistry;
171cb0ef41Sopenharmony_cistruct EnvSerializeInfo;
181cb0ef41Sopenharmony_cistruct SnapshotData;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// TODO(joyeecheung): align this with the Worker/WorkerThreadData class.
211cb0ef41Sopenharmony_ci// We may be able to create an abstract class to reuse some of the routines.
221cb0ef41Sopenharmony_ciclass NodeMainInstance {
231cb0ef41Sopenharmony_ci public:
241cb0ef41Sopenharmony_ci  // To create a main instance that does not own the isolate,
251cb0ef41Sopenharmony_ci  // The caller needs to do:
261cb0ef41Sopenharmony_ci  //
271cb0ef41Sopenharmony_ci  //   Isolate* isolate = Isolate::Allocate();
281cb0ef41Sopenharmony_ci  //   platform->RegisterIsolate(isolate, loop);
291cb0ef41Sopenharmony_ci  //   isolate->Initialize(...);
301cb0ef41Sopenharmony_ci  //   isolate->Enter();
311cb0ef41Sopenharmony_ci  //   std::unique_ptr<NodeMainInstance> main_instance =
321cb0ef41Sopenharmony_ci  //       NodeMainInstance::Create(isolate, loop, args, exec_args);
331cb0ef41Sopenharmony_ci  //
341cb0ef41Sopenharmony_ci  // When tearing it down:
351cb0ef41Sopenharmony_ci  //
361cb0ef41Sopenharmony_ci  //   main_instance->Cleanup();  // While the isolate is entered
371cb0ef41Sopenharmony_ci  //   isolate->Exit();
381cb0ef41Sopenharmony_ci  //   isolate->Dispose();
391cb0ef41Sopenharmony_ci  //   platform->UnregisterIsolate(isolate);
401cb0ef41Sopenharmony_ci  //
411cb0ef41Sopenharmony_ci  // After calling Dispose() the main_instance is no longer accessible.
421cb0ef41Sopenharmony_ci  static std::unique_ptr<NodeMainInstance> Create(
431cb0ef41Sopenharmony_ci      v8::Isolate* isolate,
441cb0ef41Sopenharmony_ci      uv_loop_t* event_loop,
451cb0ef41Sopenharmony_ci      MultiIsolatePlatform* platform,
461cb0ef41Sopenharmony_ci      const std::vector<std::string>& args,
471cb0ef41Sopenharmony_ci      const std::vector<std::string>& exec_args);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  void Dispose();
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  // Create a main instance that owns the isolate
521cb0ef41Sopenharmony_ci  NodeMainInstance(const SnapshotData* snapshot_data,
531cb0ef41Sopenharmony_ci                   uv_loop_t* event_loop,
541cb0ef41Sopenharmony_ci                   MultiIsolatePlatform* platform,
551cb0ef41Sopenharmony_ci                   const std::vector<std::string>& args,
561cb0ef41Sopenharmony_ci                   const std::vector<std::string>& exec_args);
571cb0ef41Sopenharmony_ci  ~NodeMainInstance();
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  // Start running the Node.js instances, return the exit code when finished.
601cb0ef41Sopenharmony_ci  int Run();
611cb0ef41Sopenharmony_ci  void Run(int* exit_code, Environment* env);
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  IsolateData* isolate_data() { return isolate_data_.get(); }
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  DeleteFnPtr<Environment, FreeEnvironment> CreateMainEnvironment(
661cb0ef41Sopenharmony_ci      int* exit_code);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  NodeMainInstance(const NodeMainInstance&) = delete;
691cb0ef41Sopenharmony_ci  NodeMainInstance& operator=(const NodeMainInstance&) = delete;
701cb0ef41Sopenharmony_ci  NodeMainInstance(NodeMainInstance&&) = delete;
711cb0ef41Sopenharmony_ci  NodeMainInstance& operator=(NodeMainInstance&&) = delete;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci private:
741cb0ef41Sopenharmony_ci  NodeMainInstance(v8::Isolate* isolate,
751cb0ef41Sopenharmony_ci                   uv_loop_t* event_loop,
761cb0ef41Sopenharmony_ci                   MultiIsolatePlatform* platform,
771cb0ef41Sopenharmony_ci                   const std::vector<std::string>& args,
781cb0ef41Sopenharmony_ci                   const std::vector<std::string>& exec_args);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  std::vector<std::string> args_;
811cb0ef41Sopenharmony_ci  std::vector<std::string> exec_args_;
821cb0ef41Sopenharmony_ci  std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;
831cb0ef41Sopenharmony_ci  v8::Isolate* isolate_;
841cb0ef41Sopenharmony_ci  MultiIsolatePlatform* platform_;
851cb0ef41Sopenharmony_ci  std::unique_ptr<IsolateData> isolate_data_;
861cb0ef41Sopenharmony_ci  std::unique_ptr<v8::Isolate::CreateParams> isolate_params_;
871cb0ef41Sopenharmony_ci  const SnapshotData* snapshot_data_ = nullptr;
881cb0ef41Sopenharmony_ci};
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci}  // namespace node
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
931cb0ef41Sopenharmony_ci#endif  // SRC_NODE_MAIN_INSTANCE_H_
94