-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_instance.h
135 lines (102 loc) · 3.4 KB
/
main_instance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @file main_instance.h
* @author Jichan (development@jc-lab.net / http://ablog.jc-lab.net/ )
* @date 2019/10/21
* @copyright Copyright (C) 2019 jichan.\n
* This software may be modified and distributed under the terms
* of the Apache License 2.0. See the LICENSE file for details.
*/
#ifndef __NODE_APP_MAIN_INSTANCE_H__
#define __NODE_APP_MAIN_INSTANCE_H__
#include <node.h>
#include <uv.h>
#include "vfs_handler.h"
#include "console_handler.h"
#include <vector>
#include <atomic>
namespace node_app {
class MainInstance {
public:
MainInstance();
void initializeOncePerProcess(int argc, char **argv);
int prepare(const char *entry_file = NULL, int exec_argc = 0, const char **exec_argv = NULL);
int run();
void teardownProcess();
void nodeEmitExit();
void setVfsHandler(VfsHandler *handler);
void setConsoleOutputHandler(ConsoleOutputHandler *handler);
static MainInstance *getInstance();
v8::Local<v8::Context> getRootContext();
private:
struct RunEnvironment {
std::unique_ptr<node::ArrayBufferAllocator> array_buffer_allocator_;
v8::Isolate *isolate_;
node::IsolateData *isolate_data_;
v8::Locker locker;
v8::Isolate::Scope isolate_scope;
v8::HandleScope handle_scope;
v8::Local<v8::Context> context_;
node::Environment *env_;
std::string entrypoint_src;
std::atomic_bool stopping_;
RunEnvironment(std::unique_ptr<node::ArrayBufferAllocator> array_buffer_allocator,
v8::Isolate *isolate,
node::IsolateData *isolate_data);
};
uv_loop_t *loop_;
node::tracing::Agent *tracing_agent_;
node::MultiIsolatePlatform *platform_;
VfsHandler *vfs_handler_;
ConsoleOutputHandler *console_out_handler_;
int node_argc_;
char **node_argv_;
int node_exec_argc_;
const char **node_exec_argv_;
std::vector<char *> run_arguments_;
std::unique_ptr<RunEnvironment> run_env_;
void applyVfs(v8::Local<v8::Context> &context);
void applyConsole(v8::Local<v8::Context> &context);
static MainInstance *instance_;
static int argToRelPath(std::string &resolved_relpath,
const v8::FunctionCallbackInfo<v8::Value> &info,
std::string *arg_path = NULL);
static void jsapp_callback_vfs_internalModuleStat(const v8::FunctionCallbackInfo<v8::Value> &info);
static void jsapp_callback_vfs_realpathSync(const v8::FunctionCallbackInfo<v8::Value> &info);
static void jsapp_callback_vfs_readFileSync(const v8::FunctionCallbackInfo<v8::Value> &info);
static void jsapp_callback_console_out(const v8::FunctionCallbackInfo<v8::Value> &info);
};
template<class T>
class MainInstanceWithContext : public MainInstance {
private:
T context_;
public:
void setInstanceContext(T context) {
context_ = std::forward(context);
}
};
template<class T>
class MainInstanceWithContext<std::unique_ptr<T>> : public MainInstance {
private:
std::unique_ptr<T> context_;
public:
void setInstanceContext(std::unique_ptr<T> context) {
context_ = std::move(context);
}
T *getInstanceContext() {
return context_.get();
}
};
template<class T>
class MainInstanceWithContext<std::shared_ptr<T>> : public MainInstance {
private:
std::shared_ptr<T> context_;
public:
void setInstanceContext(std::shared_ptr<T> context) {
context_ = context;
}
T *getInstanceContext() {
return context_.get();
}
};
}
#endif //__NODE_MAIN_INSTANCE_HPP__