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
|
#!/usr/bin/env node
"use strict";
const lua = require('../../src/lua.js');
const lapi = require('../../src/lapi.js');
const lauxlib = require('../../src/lauxlib.js');
const linit = require('../../src/linit.js');
const fs = require('fs');
const readlineSync = require('readline-sync');
const stdin = lua.to_luastring("=stdin");
const _PROMPT = lua.to_luastring("_PROMPT");
const _PROMPT2 = lua.to_luastring("_PROMPT2");
const report = function(L, status) {
if (status !== lua.thread_status.LUA_OK) {
lauxlib.lua_writestringerror(`${lapi.lua_tojsstring(L, -1)}\n`);
lapi.lua_pop(L, 1);
}
return status;
};
const docall = function(L, narg, nres) {
let status = lapi.lua_pcall(L, narg, nres, 0);
return status;
};
const dochunk = function(L, status) {
if (status === lua.thread_status.LUA_OK) {
status = docall(L, 0, 0);
}
return report(L, status);
};
const dofile = function(L, name) {
return dochunk(L, lauxlib.luaL_loadfile(L, lua.to_luastring(name)));
};
const dostring = function(L, s, name) {
let buffer = lua.to_luastring(s);
return dochunk(L, lauxlib.luaL_loadbuffer(L, buffer, buffer.length, lua.to_luastring(name)));
};
const dolibrary = function(L, name) {
lapi.lua_getglobal(L, "require");
lapi.lua_pushliteral(L, name);
let status = docall(L, 1, 1); /* call 'require(name)' */
if (status === lua.thread_status.LUA_OK)
lapi.lua_setglobal(L, name); /* global[name] = require return */
return report(L, status);
};
let progname = process.argv[1];
const print_usage = function(badoption) {
lauxlib.lua_writestringerror(`${progname}: `);
if (badoption[1] === "e" || badoption[1] === "l")
lauxlib.lua_writestringerror(`'${badoption}' needs argument\n`);
else
lauxlib.lua_writestringerror(`'unrecognized option '${badoption}'\n`);
lauxlib.lua_writestringerror(
`usage: ${progname} [options] [script [args]]\n` +
"Available options are:\n" +
" -e stat execute string 'stat'\n" +
" -i enter interactive mode after executing 'script'\n" +
" -l name require library 'name'\n" +
" -v show version information\n" +
" -E ignore environment variables\n" +
" -- stop handling options\n" +
" - stop handling options and execute stdin\n"
);
};
const L = lauxlib.luaL_newstate();
let script = 2; // Where to start args from
let has_E = false;
let has_i = false;
let has_v = false;
let has_e = false;
for (let i = 2; i<process.argv.length; i++) {
script = i;
if (process.argv[i][0] != "-") {
break;
}
switch(process.argv[i][1]) {
case '-':
if (process.argv[i][2]) {
print_usage(process.argv[script]);
return process.exit(1);
}
script = i + 1;
/* falls through */
case void 0: /* script name is '-' */
i = process.argv.length;
break;
case 'E':
has_E = true;
break;
case 'i':
has_i = true;
/* (-i implies -v) */
/* falls through */
case 'v':
if (process.argv[i].length > 2) {
/* invalid option */
print_usage(process.argv[script]);
return process.exit(1);
}
has_v = true;
break;
case 'e':
has_e = true;
/* falls through */
case 'l': /* both options need an argument */
if (process.argv[i].length < 2) { /* no concatenated argument? */
i++; /* try next 'process.argv' */
if (process.argv.length <= i || process.argv[i][0] === '-') {
/* no next argument or it is another option */
print_usage(process.argv[script]);
return process.exit(1);
}
}
break;
default: /* invalid option */
print_usage(process.argv[script]);
return process.exit(1);
}
}
if (has_v)
console.log(lua.FENGARI_COPYRIGHT);
if (has_E) {
/* signal for libraries to ignore env. vars. */
lapi.lua_pushboolean(L, 1);
lapi.lua_setfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring("LUA_NOENV"));
}
/* open standard libraries */
linit.luaL_openlibs(L);
/* create table 'arg' */
lapi.lua_createtable(L, process.argv.length - (script + 1), script + 1);
for (let i = 0; i < process.argv.length; i++) {
lapi.lua_pushliteral(L, process.argv[i]);
lapi.lua_seti(L, -2, i - script); /* TODO: rawseti */
}
lapi.lua_setglobal(L, lua.to_luastring("arg"));
if (!has_E) {
/* run LUA_INIT */
let name = "LUA_INIT"+lua.LUA_VERSUFFIX;
let init = process.env[name];
if (!init) {
name = "LUA_INIT";
init = process.env[name];
}
if (init) {
let status;
if (init[0] === '@') {
status = dofile(L, init.substring(1));
} else {
status = dostring(L, init, name);
}
if (status !== lua.thread_status.LUA_OK) {
return process.exit(1);
}
}
}
/* execute arguments -e and -l */
for (let i = 1; i < script; i++) {
let option = process.argv[i][1];
if (option == 'e' || option == 'l') {
let extra = process.argv[i].substring(2); /* both options need an argument */
if (extra.length === 0)
extra = process.argv[++i];
let status;
if (option == 'e') {
status = dostring(L, extra, "=(command line)");
} else {
status = dolibrary(L, extra);
}
if (status !== lua.thread_status.LUA_OK) {
return process.exit(1);
}
}
}
const pushargs = function(L) {
if (lapi.lua_getglobal(L, lua.to_luastring("arg")) !== lua.constant_types.LUA_TTABLE)
lauxlib.luaL_error(L, lua.to_luastring("'arg' is not a table"));
let n = lauxlib.luaL_len(L, -1);
lauxlib.luaL_checkstack(L, n+3, lua.to_luastring("too many arguments to script"));
let i;
for (i=1; i<=n; i++)
lapi.lua_rawgeti(L, -i, i);
lapi.lua_remove(L, -i);
return n;
};
const handle_script = function(L, argv) {
let fname = argv[0];
let status;
if (fname === "-" && argv[-1] !== "--")
fname = void 0; /* stdin */
status = lauxlib.luaL_loadfile(L, lua.to_luastring(fname));
if (status === lua.thread_status.LUA_OK) {
let n = pushargs(L); /* push arguments to script */
status = docall(L, n, lua.LUA_MULTRET);
}
return report(L, status);
};
const doREPL = function(L) {
for (;;) {
lapi.lua_getglobal(L, _PROMPT);
let input = readlineSync.prompt({
prompt: lapi.lua_tojsstring(L, -1) || '> '
});
lapi.lua_pop(L, 1);
if (input.length === 0)
continue;
let status;
{
let buffer = lua.to_luastring("return " + input);
status = lauxlib.luaL_loadbuffer(L, buffer, buffer.length, stdin);
}
if (status !== lua.thread_status.LUA_OK) {
lapi.lua_pop(L, 1);
let buffer = lua.to_luastring(input);
if (lauxlib.luaL_loadbuffer(L, buffer, buffer.length, stdin) === lua.thread_status.LUA_OK) {
status = lua.thread_status.LUA_OK;
}
}
while (status === lua.thread_status.LUA_ERRSYNTAX && lapi.lua_tojsstring(L, -1).endsWith("<eof>")) {
/* continuation */
lapi.lua_pop(L, 1);
lapi.lua_getglobal(L, _PROMPT2);
input += "\n" + readlineSync.prompt({
prompt: lapi.lua_tojsstring(L, -1) || '>> '
});
lapi.lua_pop(L, 1);
let buffer = lua.to_luastring(input);
status = lauxlib.luaL_loadbuffer(L, buffer, buffer.length, stdin);
}
if (status === lua.thread_status.LUA_OK) {
status = docall(L, 0, lua.LUA_MULTRET);
}
if (status === lua.thread_status.LUA_OK) {
let n = lapi.lua_gettop(L);
if (n > 0) { /* any result to be printed? */
lapi.lua_getglobal(L, lua.to_luastring("print"));
lapi.lua_insert(L, 1);
if (lapi.lua_pcall(L, n, 0, 0) != lua.thread_status.LUA_OK) {
lauxlib.lua_writestringerror(`error calling 'print' (${lapi.lua_tojsstring(L, -1)})\n`);
}
}
} else {
report(L, status);
}
lapi.lua_settop(L, 0); /* remove eventual returns */
}
};
if (script < process.argv.length && /* execute main script (if there is one) */
handle_script(L, process.argv.slice(script)) !== lua.thread_status.LUA_OK) {
/* success */
} else if (has_i) {
doREPL(L);
} else if (script == process.argv.length && !has_e && !has_v) { /* no arguments? */
if (process.stdin.isTTY) { /* running in interactive mode? */
console.log(lua.FENGARI_COPYRIGHT);
doREPL(L); /* do read-eval-print loop */
} else {
dofile(L, null);
}
}
|