/
main.cc
298 lines (276 loc) · 8.6 KB
/
main.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <cstdio>
#include <cstdlib>
#include <locale>
#include <string>
#include <vector>
#include <iv/string_view.h>
#include <iv/about.h>
#include <iv/cmdline.h>
#include <iv/platform_io.h>
#include <iv/lv5/lv5.h>
#include <iv/lv5/railgun/command.h>
#include <iv/lv5/railgun/interactive.h>
#include <iv/lv5/breaker/command.h>
#include <iv/lv5/melt/melt.h>
#if defined(IV_OS_MACOSX) || defined(IV_OS_LINUX) || defined(IV_OS_BSD)
#include <signal.h>
#endif
namespace {
void InitContext(iv::lv5::Context* ctx) {
iv::lv5::Error::Dummy dummy;
ctx->DefineFunction<&iv::lv5::Print, 1>("print");
ctx->DefineFunction<&iv::lv5::Log, 1>("log"); // this is simply output log function
ctx->DefineFunction<&iv::lv5::Quit, 1>("quit");
ctx->DefineFunction<&iv::lv5::CollectGarbage, 0>("gc");
ctx->DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
ctx->DefineFunction<&iv::lv5::railgun::Dis, 1>("dis");
iv::lv5::melt::Console::Export(ctx, &dummy);
}
#if defined(IV_ENABLE_JIT)
int BreakerExecute(const iv::core::string_view& data,
const std::string& filename, bool statistics) {
iv::lv5::Error::Standard e;
iv::lv5::breaker::Context ctx;
InitContext(&ctx);
ctx.DefineFunction<&iv::lv5::breaker::Run, 1>("run");
ctx.DefineFunction<&iv::lv5::breaker::Load, 1>("load");
std::shared_ptr<iv::core::FileSource>
src(new iv::core::FileSource(data, filename));
iv::lv5::breaker::ExecuteInGlobal(&ctx, src, &e);
if (e) {
e.Dump(&ctx, stderr);
return EXIT_FAILURE;
}
ctx.Validate();
return EXIT_SUCCESS;
}
int BreakerExecuteFiles(const std::vector<std::string>& filenames) {
iv::lv5::Error::Standard e;
iv::lv5::breaker::Context ctx;
InitContext(&ctx);
ctx.DefineFunction<&iv::lv5::breaker::Run, 1>("run");
ctx.DefineFunction<&iv::lv5::breaker::Load, 1>("load");
std::vector<char> res;
for (std::vector<std::string>::const_iterator it = filenames.begin(),
last = filenames.end(); it != last; ++it) {
if (!iv::core::io::ReadFile(*it, &res)) {
return EXIT_FAILURE;
}
std::shared_ptr<iv::core::FileSource>
src(new iv::core::FileSource(
iv::core::string_view(res.data(), res.size()), *it));
res.clear();
iv::lv5::breaker::ExecuteInGlobal(&ctx, src, &e);
if (e) {
e.Dump(&ctx, stderr);
return EXIT_FAILURE;
}
}
ctx.Validate();
return EXIT_SUCCESS;
}
#endif
int RailgunExecute(const iv::core::string_view& data,
const std::string& filename, bool statistics) {
iv::lv5::Error::Standard e;
iv::lv5::railgun::Context ctx;
InitContext(&ctx);
ctx.DefineFunction<&iv::lv5::railgun::Run, 1>("run");
std::shared_ptr<iv::core::FileSource>
src(new iv::core::FileSource(data, filename));
iv::lv5::railgun::ExecuteInGlobal(&ctx, src, &e);
if (e) {
e.Dump(&ctx, stderr);
return EXIT_FAILURE;
}
if (statistics) {
ctx.vm()->DumpStatistics();
}
ctx.Validate();
return EXIT_SUCCESS;
}
int RailgunExecuteFiles(const std::vector<std::string>& filenames) {
iv::lv5::Error::Standard e;
iv::lv5::railgun::Context ctx;
InitContext(&ctx);
ctx.DefineFunction<&iv::lv5::railgun::Run, 1>("run");
std::vector<char> res;
for (std::vector<std::string>::const_iterator it = filenames.begin(),
last = filenames.end(); it != last; ++it) {
if (!iv::core::io::ReadFile(*it, &res)) {
return EXIT_FAILURE;
}
std::shared_ptr<iv::core::FileSource>
src(new iv::core::FileSource(
iv::core::string_view(res.data(), res.size()), *it));
res.clear();
iv::lv5::railgun::ExecuteInGlobal(&ctx, src, &e);
if (e) {
e.Dump(&ctx, stderr);
return EXIT_FAILURE;
}
}
ctx.Validate();
return EXIT_SUCCESS;
}
int DisAssemble(const iv::core::string_view& data,
const std::string& filename) {
iv::lv5::railgun::Context ctx;
iv::lv5::Error::Standard e;
std::shared_ptr<iv::core::FileSource>
src(new iv::core::FileSource(data, filename));
iv::lv5::railgun::Code* code =
iv::lv5::railgun::CompileInGlobal(&ctx, src, true, &e);
if (e) {
return EXIT_FAILURE;
}
iv::lv5::railgun::OutputDisAssembler dis(&ctx, stdout);
dis.DisAssembleGlobal(*code);
return EXIT_SUCCESS;
}
int Ast(const iv::core::string_view& data, const std::string& filename) {
iv::core::FileSource src(data, filename);
iv::lv5::railgun::Context ctx;
iv::lv5::AstFactory factory(&ctx);
iv::core::Parser<iv::lv5::AstFactory,
iv::core::FileSource> parser(&factory,
src, ctx.symbol_table());
const iv::lv5::FunctionLiteral* const global = parser.ParseProgram();
if (!global) {
std::fprintf(stderr, "%s\n", parser.error().c_str());
return EXIT_FAILURE;
}
iv::core::ast::AstSerializer<iv::lv5::AstFactory> ser;
ser.Visit(global);
const std::u16string str = ser.out();
iv::core::unicode::FPutsUTF16(stdout, str.begin(), str.end());
return EXIT_SUCCESS;
}
} // namespace anonymous
int main(int argc, char **argv) {
iv::lv5::FPU fpu;
iv::lv5::program::Init(argc, argv);
iv::lv5::Init();
iv::cmdline::Parser cmd("lv5");
cmd.Add("help",
"help",
'h', "print this message");
cmd.Add("version",
"version",
'v', "print the version");
cmd.AddList<std::string>(
"file",
"file",
'f', "script file to load");
cmd.Add("signal",
"",
's', "install signal handlers");
cmd.Add<std::string>(
"execute",
"execute",
'e', "execute command", false);
cmd.Add("ast",
"ast",
0, "print ast");
cmd.Add("interp",
"interp",
0, "use interpreter");
cmd.Add("railgun",
"railgun",
0, "force railgun VM");
cmd.Add("dis",
"dis",
'd', "print bytecode");
cmd.Add("statistics",
"statistics",
0, "print statistics");
cmd.Add("copyright",
"copyright",
0, "print the copyright");
cmd.set_footer("[program_file] [arguments]");
const bool cmd_parse_success = cmd.Parse(argc, argv);
if (!cmd_parse_success) {
std::fprintf(stderr, "%s\n%s",
cmd.error().c_str(), cmd.usage().c_str());
return EXIT_FAILURE;
}
if (cmd.Exist("help")) {
std::fputs(cmd.usage().c_str(), stdout);
return EXIT_SUCCESS;
}
if (cmd.Exist("version")) {
const char* jit
#if defined(IV_ENABLE_JIT)
= "on";
#else
= "off";
#endif
std::printf("lv5 %s (compiled %s %s with JIT [%s])\n", IV_VERSION, __DATE__, __TIME__, jit);
return EXIT_SUCCESS;
}
if (cmd.Exist("copyright")) {
std::printf("lv5 - %s\n", IV_COPYRIGHT);
return EXIT_SUCCESS;
}
if (cmd.Exist("signal")) {
#if defined(IV_OS_MACOSX) || defined(IV_OS_LINUX) || defined(IV_OS_BSD)
signal(SIGILL, _exit);
signal(SIGFPE, _exit);
signal(SIGBUS, _exit);
signal(SIGSEGV, _exit);
#endif
}
const std::vector<std::string>& rest = cmd.rest();
if (!rest.empty() || cmd.Exist("file") || cmd.Exist("execute")) {
std::vector<char> res;
std::string filename;
if (cmd.Exist("file")) {
const std::vector<std::string>& vec = cmd.GetList<std::string>("file");
if (!cmd.Exist("ast") && !cmd.Exist("dis") && !cmd.Exist("interp")) {
if (cmd.Exist("railgun")) {
return RailgunExecuteFiles(vec);
}
#if defined(IV_ENABLE_JIT)
return BreakerExecuteFiles(vec);
#else
return RailgunExecuteFiles(vec);
#endif
}
for (std::vector<std::string>::const_iterator it = vec.begin(),
last = vec.end(); it != last; ++it, filename.push_back(' ')) {
filename.append(*it);
if (!iv::core::io::ReadFile(*it, &res)) {
return EXIT_FAILURE;
}
}
} else if (cmd.Exist("execute")) {
const std::string& com = cmd.Get<std::string>("execute");
filename = "<command>";
res.insert(res.end(), com.begin(), com.end());
} else {
filename = rest.front();
if (!iv::core::io::ReadFile(filename, &res)) {
return EXIT_FAILURE;
}
}
const iv::core::string_view src(res.data(), res.size());
if (cmd.Exist("ast")) {
return Ast(src, filename);
} else if (cmd.Exist("dis")) {
return DisAssemble(src, filename);
} else if (cmd.Exist("railgun")) {
return RailgunExecute(src, filename, cmd.Exist("statistics"));
} else {
#if defined(IV_ENABLE_JIT)
return BreakerExecute(src, filename, cmd.Exist("statistics"));
#else
return RailgunExecute(src, filename, cmd.Exist("statistics"));
#endif
}
} else {
// Interactive Shell Mode
iv::lv5::railgun::Interactive shell(cmd.Exist("dis"));
return shell.Run();
}
return 0;
}