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