• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_MAIN_INSTANCE_H_
2 #define SRC_NODE_MAIN_INSTANCE_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include <cstddef>
7 #include <memory>
8 
9 #include "node.h"
10 #include "util.h"
11 #include "uv.h"
12 #include "v8.h"
13 
14 namespace node {
15 
16 class ExternalReferenceRegistry;
17 struct EnvSerializeInfo;
18 struct SnapshotData;
19 
20 // TODO(joyeecheung): align this with the Worker/WorkerThreadData class.
21 // We may be able to create an abstract class to reuse some of the routines.
22 class NodeMainInstance {
23  public:
24   // To create a main instance that does not own the isolate,
25   // The caller needs to do:
26   //
27   //   Isolate* isolate = Isolate::Allocate();
28   //   platform->RegisterIsolate(isolate, loop);
29   //   isolate->Initialize(...);
30   //   isolate->Enter();
31   //   std::unique_ptr<NodeMainInstance> main_instance =
32   //       NodeMainInstance::Create(isolate, loop, args, exec_args);
33   //
34   // When tearing it down:
35   //
36   //   main_instance->Cleanup();  // While the isolate is entered
37   //   isolate->Exit();
38   //   isolate->Dispose();
39   //   platform->UnregisterIsolate(isolate);
40   //
41   // After calling Dispose() the main_instance is no longer accessible.
42   static std::unique_ptr<NodeMainInstance> Create(
43       v8::Isolate* isolate,
44       uv_loop_t* event_loop,
45       MultiIsolatePlatform* platform,
46       const std::vector<std::string>& args,
47       const std::vector<std::string>& exec_args);
48 
49   void Dispose();
50 
51   // Create a main instance that owns the isolate
52   NodeMainInstance(const SnapshotData* snapshot_data,
53                    uv_loop_t* event_loop,
54                    MultiIsolatePlatform* platform,
55                    const std::vector<std::string>& args,
56                    const std::vector<std::string>& exec_args);
57   ~NodeMainInstance();
58 
59   // Start running the Node.js instances, return the exit code when finished.
60   int Run();
61   void Run(int* exit_code, Environment* env);
62 
isolate_data()63   IsolateData* isolate_data() { return isolate_data_.get(); }
64 
65   DeleteFnPtr<Environment, FreeEnvironment> CreateMainEnvironment(
66       int* exit_code);
67 
68   NodeMainInstance(const NodeMainInstance&) = delete;
69   NodeMainInstance& operator=(const NodeMainInstance&) = delete;
70   NodeMainInstance(NodeMainInstance&&) = delete;
71   NodeMainInstance& operator=(NodeMainInstance&&) = delete;
72 
73  private:
74   NodeMainInstance(v8::Isolate* isolate,
75                    uv_loop_t* event_loop,
76                    MultiIsolatePlatform* platform,
77                    const std::vector<std::string>& args,
78                    const std::vector<std::string>& exec_args);
79 
80   std::vector<std::string> args_;
81   std::vector<std::string> exec_args_;
82   std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;
83   v8::Isolate* isolate_;
84   MultiIsolatePlatform* platform_;
85   std::unique_ptr<IsolateData> isolate_data_;
86   std::unique_ptr<v8::Isolate::CreateParams> isolate_params_;
87   const SnapshotData* snapshot_data_ = nullptr;
88 };
89 
90 }  // namespace node
91 
92 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
93 #endif  // SRC_NODE_MAIN_INSTANCE_H_
94