GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_main_instance.h Lines: 1 1 100.0 %
Date: 2021-09-30 04:12:30 Branches: 0 0 - %

Line Branch Exec Source
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
19
// TODO(joyeecheung): align this with the Worker/WorkerThreadData class.
20
// We may be able to create an abstract class to reuse some of the routines.
21
class NodeMainInstance {
22
 public:
23
  // To create a main instance that does not own the isolate,
24
  // The caller needs to do:
25
  //
26
  //   Isolate* isolate = Isolate::Allocate();
27
  //   platform->RegisterIsolate(isolate, loop);
28
  //   isolate->Initialize(...);
29
  //   isolate->Enter();
30
  //   std::unique_ptr<NodeMainInstance> main_instance =
31
  //       NodeMainInstance::Create(isolate, loop, args, exec_args);
32
  //
33
  // When tearing it down:
34
  //
35
  //   main_instance->Cleanup();  // While the isolate is entered
36
  //   isolate->Exit();
37
  //   isolate->Dispose();
38
  //   platform->UnregisterIsolate(isolate);
39
  //
40
  // After calling Dispose() the main_instance is no longer accessible.
41
  static std::unique_ptr<NodeMainInstance> Create(
42
      v8::Isolate* isolate,
43
      uv_loop_t* event_loop,
44
      MultiIsolatePlatform* platform,
45
      const std::vector<std::string>& args,
46
      const std::vector<std::string>& exec_args);
47
48
  void Dispose();
49
50
  // Create a main instance that owns the isolate
51
  NodeMainInstance(
52
      v8::Isolate::CreateParams* params,
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
      const std::vector<size_t>* per_isolate_data_indexes = nullptr);
58
  ~NodeMainInstance();
59
60
  // Start running the Node.js instances, return the exit code when finished.
61
  int Run(const EnvSerializeInfo* env_info);
62
  void Run(int* exit_code, Environment* env);
63
64
12
  IsolateData* isolate_data() { return isolate_data_.get(); }
65
66
  DeleteFnPtr<Environment, FreeEnvironment> CreateMainEnvironment(
67
      int* exit_code, const EnvSerializeInfo* env_info);
68
69
  // If nullptr is returned, the binary is not built with embedded
70
  // snapshot.
71
  static const std::vector<size_t>* GetIsolateDataIndices();
72
  static v8::StartupData* GetEmbeddedSnapshotBlob();
73
  static const EnvSerializeInfo* GetEnvSerializeInfo();
74
  static const std::vector<intptr_t>& CollectExternalReferences();
75
76
  static const size_t kNodeContextIndex = 0;
77
  NodeMainInstance(const NodeMainInstance&) = delete;
78
  NodeMainInstance& operator=(const NodeMainInstance&) = delete;
79
  NodeMainInstance(NodeMainInstance&&) = delete;
80
  NodeMainInstance& operator=(NodeMainInstance&&) = delete;
81
82
 private:
83
  NodeMainInstance(v8::Isolate* isolate,
84
                   uv_loop_t* event_loop,
85
                   MultiIsolatePlatform* platform,
86
                   const std::vector<std::string>& args,
87
                   const std::vector<std::string>& exec_args);
88
89
  static std::unique_ptr<ExternalReferenceRegistry> registry_;
90
  std::vector<std::string> args_;
91
  std::vector<std::string> exec_args_;
92
  std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;
93
  v8::Isolate* isolate_;
94
  MultiIsolatePlatform* platform_;
95
  std::unique_ptr<IsolateData> isolate_data_;
96
  bool owns_isolate_ = false;
97
  bool deserialize_mode_ = false;
98
};
99
100
}  // namespace node
101
102
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
103
#endif  // SRC_NODE_MAIN_INSTANCE_H_