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