-
Notifications
You must be signed in to change notification settings - Fork 13
/
cli.js
executable file
·241 lines (232 loc) · 7.33 KB
/
cli.js
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
#!/usr/bin/env node
var parse = require('./parser');
var Closure = require('./interpreter').Closure;
var globals = require('./globals');
var nopt = require("nopt");
var execFile = require("child_process").execFile;
var readFile = require('fs').readFile;
var statFile = require('fs').stat;
var pathResolve = require("path").resolve;
var pathJoin = require("path").join;
var dirname = require('path').dirname;
var basename = require('path').basename;
// Compile lua files to luajit on the fly with a locking queue for concurrent
// requests.
var queues = {};
function luaToBytecode(path, callback) {
if (path in queues) {
return queues[path].push(callback);
}
var queue = queues[path] = [callback];
function callbacks(err, newpath) {
delete queues[path];
queue.forEach(function (callback) {
callback(err, newpath);
});
}
var newpath = pathJoin(dirname(path), "." + basename(path) + "x");
statFile(path, function (err, stat) {
if (err) return callbacks(err);
statFile(newpath, function (err, stat2) {
if (err && err.code !== "ENOENT") {
return callbacks(err);
}
if (stat2 && stat2.mtime >= stat.mtime) {
return callbacks(null, newpath);
}
execFile("luajit", ["-b", path, newpath], function (err, stdout, stderr) {
if (err) return callbacks(err);
if (stderr) return callbacks(stderr);
callbacks(null, newpath);
});
});
});
}
// Compile a lua script to javascript source string
function compile(path, callback) {
if (/\.luax/.test(path)) {
// Load already compiled bytecode
loadBytecode(path);
}
if (/\.lua$/.test(path)) {
luaToBytecode(path, function (err, newpath) {
if (err) return callback(err);
loadBytecode(newpath);
});
}
function loadBytecode(path) {
readFile(path, function (err, buffer) {
if (err) return callback(err);
generate(buffer);
});
}
function generate(buffer) {
var program;
try {
program = parse(buffer);
}
catch (err) {
return callback(err);
}
callback(null, program);
}
}
var options = nopt({
"serve": Number,
"execute": Boolean,
"print": Boolean,
"json": Boolean,
"uglify": Boolean,
"beautify": Boolean,
"lines": Boolean
}, {
"x": ["--execute"],
"p": ["--print"],
"j": ["--json"],
"u": ["--uglify"],
"b": ["--beautify"],
"l": ["--lines"]
});
if (options.serve) {
if (options.serve === 1) options.serve = 8080;
var urlParse = require('url').parse;
var base = process.cwd();
var send = require('send');
console.log("BASE", base);
var server = require('http').createServer(function (req, res) {
var url = urlParse(req.url);
if (url.pathname === "/parser.js") {
return send(req, pathJoin(__dirname, "/parser.js")).pipe(res);
}
if (url.pathname === "/interpreter.js") {
return send(req, pathJoin(__dirname, "/interpreter.js")).pipe(res);
}
if (url.pathname === "/runtime.js") {
return send(req, pathJoin(__dirname, "/runtime.js")).pipe(res);
}
if (url.pathname === "/globals.js") {
return send(req, pathJoin(__dirname, "/globals.js")).pipe(res);
}
if (url.pathname === "/browser-buffer.js") {
return send(req, pathJoin(__dirname, "/browser-buffer.js")).pipe(res);
}
var path = pathJoin(base, url.pathname);
if (path[path.length - 1] === "/") {
path += "index.html";
}
console.log(req.method, path);
if (/\.luax$/.test(path)) {
luaToBytecode(path.substr(0, path.length - 1), function (err, path) {
if (err) {
if (err.code === "ENOENT") {
res.statusCode = 404;
return res.end();
}
res.statusCode = 500;
return res.end(err.stack);
}
console.log("sending", path, "for", req.url);
send(req, path)
.hidden(true)
.pipe(res);
});
return;
}
send(req, url.pathname)
.root(base)
.pipe(res);
});
server.listen(options.serve, function () {
console.log("Serving server listening at", server.address());
});
}
else {
// Default to running if not specified
if (options.execute === undefined && !options.print && !options.json) {
options.execute = true;
}
var filename = options.argv.remain[0];
if (!filename) {
console.error([
"Usage: brozula [OPTION...] program.lua[x]",
"Brozula compiles lua files to bytecode and then executes them using a JS VM",
"The lua -> luax (luajit bytecode) step is done by using luajit",
"",
"Examples:",
" brozula myprogram.lua",
" brozula --print myprogram.lua",
" brozula --json myprogram.lua",
" brozula -pb myprogram.luax",
"",
" Main operation mode:",
"",
" -x, --execute Execute the generated javascript",
" (This is the default behavior)",
" -p, --print Print the generated javascript",
" -j, --json Print the generated javascript as JSON",
" --serve port Serve the current folder over HTTP auto-compiling",
" any lua scripts requested",
"",
" Operation modifiers:",
"",
" -u, --uglify Compress the generated javascript using uglify-js",
" -b, --beautify Beautify the generated javascript using uglify-js",
" -l, --lines Show line numbers when printing",
""
].join("\n"));
process.exit(-1);
}
filename = pathResolve(process.cwd(), filename);
compile(filename, function (err, protos) {
if (err) throw err;
if (options.json) {
console.log(options.beautify ? JSON.stringify(protos, null, 2) : JSON.stringify(protos));
} else if (options.print) {
console.log(require('util').inspect(protos, false, 3 + (options.beautify ? 2 : 0), true) + "\n");
}
if (options.execute) {
(new Closure(protos[protos.length - 1])).toFunction(globals)();
}
// program = "(function () {\n\n" + program + "\n\n}());";
// if (options.uglify) {
// var UglifyJS = require("uglify-js");
// var toplevel_ast = UglifyJS.parse(program);
// toplevel_ast.figure_out_scope();
// var compressor = UglifyJS.Compressor({});
// var compressed_ast = toplevel_ast.transform(compressor);
// compressed_ast.figure_out_scope();
// compressed_ast.compute_char_frequency();
// compressed_ast.mangle_names();
// program = compressed_ast.print_to_string({});
// }
// if (options.beautify) {
// var UglifyJS = require("uglify-js");
// var toplevel_ast = UglifyJS.parse(program);
// toplevel_ast.figure_out_scope();
// program = toplevel_ast.print_to_string({
// beautify: true,
// indent_level: 2
// });
// }
// if (options.print) {
// if (options.lines) {
// var lines = program.split("\n");
// var digits = Math.ceil(Math.log(lines.length) / Math.LN10);
// var padding = "";
// for (var i = 0; i < digits; i++) {
// padding += "0";
// }
// console.log(lines.map(function (line, i) {
// var num = (i + 1) + "";
// return "\033[34m" + padding.substr(num.length) + num + "\033[0m " + line;
// }).join("\n"));
// }
// else {
// console.log(program);
// }
// }
// if (options.execute) {
// eval(program);
// }
});
}