diff options
Diffstat (limited to 'src/luajit.c')
-rw-r--r-- | src/luajit.c | 519 |
1 files changed, 519 insertions, 0 deletions
diff --git a/src/luajit.c b/src/luajit.c new file mode 100644 index 00000000..9153975b --- /dev/null +++ b/src/luajit.c | |||
@@ -0,0 +1,519 @@ | |||
1 | /* | ||
2 | ** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc. | ||
3 | ** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h | ||
4 | ** | ||
5 | ** Major portions taken verbatim or adapted from the Lua interpreter. | ||
6 | ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h | ||
7 | */ | ||
8 | |||
9 | #include <signal.h> | ||
10 | #include <stdio.h> | ||
11 | #include <stdlib.h> | ||
12 | #include <string.h> | ||
13 | |||
14 | #define luajit_c | ||
15 | |||
16 | #include "lua.h" | ||
17 | #include "lauxlib.h" | ||
18 | #include "lualib.h" | ||
19 | #include "luajit.h" | ||
20 | |||
21 | #if defined(LUA_USE_POSIX) | ||
22 | #include <unistd.h> | ||
23 | #define lua_stdin_is_tty() isatty(0) | ||
24 | #elif defined(LUA_USE_WIN) | ||
25 | #include <io.h> | ||
26 | #ifdef __BORLANDC__ | ||
27 | #define lua_stdin_is_tty() isatty(_fileno(stdin)) | ||
28 | #else | ||
29 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) | ||
30 | #endif | ||
31 | #else | ||
32 | #define lua_stdin_is_tty() 1 | ||
33 | #endif | ||
34 | |||
35 | static lua_State *globalL = NULL; | ||
36 | static const char *progname = LUA_PROGNAME; | ||
37 | |||
38 | static void lstop(lua_State *L, lua_Debug *ar) | ||
39 | { | ||
40 | (void)ar; /* unused arg. */ | ||
41 | lua_sethook(L, NULL, 0, 0); | ||
42 | /* Avoid luaL_error -- a C hook doesn't add an extra frame. */ | ||
43 | luaL_where(L, 0); | ||
44 | lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1)); | ||
45 | lua_error(L); | ||
46 | } | ||
47 | |||
48 | static void laction(int i) | ||
49 | { | ||
50 | signal(i, SIG_DFL); /* if another SIGINT happens before lstop, | ||
51 | terminate process (default action) */ | ||
52 | lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); | ||
53 | } | ||
54 | |||
55 | static void print_usage(void) | ||
56 | { | ||
57 | fprintf(stderr, | ||
58 | "usage: %s [options] [script [args]].\n" | ||
59 | "Available options are:\n" | ||
60 | " -e stat execute string " LUA_QL("stat") "\n" | ||
61 | " -l name require library " LUA_QL("name") "\n" | ||
62 | " -j cmd perform LuaJIT control command\n" | ||
63 | " -O[lvl] set LuaJIT optimization level\n" | ||
64 | " -i enter interactive mode after executing " LUA_QL("script") "\n" | ||
65 | " -v show version information\n" | ||
66 | " -- stop handling options\n" | ||
67 | " - execute stdin and stop handling options\n" | ||
68 | , | ||
69 | progname); | ||
70 | fflush(stderr); | ||
71 | } | ||
72 | |||
73 | static void l_message(const char *pname, const char *msg) | ||
74 | { | ||
75 | if (pname) fprintf(stderr, "%s: ", pname); | ||
76 | fprintf(stderr, "%s\n", msg); | ||
77 | fflush(stderr); | ||
78 | } | ||
79 | |||
80 | static int report(lua_State *L, int status) | ||
81 | { | ||
82 | if (status && !lua_isnil(L, -1)) { | ||
83 | const char *msg = lua_tostring(L, -1); | ||
84 | if (msg == NULL) msg = "(error object is not a string)"; | ||
85 | l_message(progname, msg); | ||
86 | lua_pop(L, 1); | ||
87 | } | ||
88 | return status; | ||
89 | } | ||
90 | |||
91 | static int traceback(lua_State *L) | ||
92 | { | ||
93 | if (!lua_isstring(L, 1)) /* 'message' not a string? */ | ||
94 | return 1; /* keep it intact */ | ||
95 | lua_getfield(L, LUA_GLOBALSINDEX, "debug"); | ||
96 | if (!lua_istable(L, -1)) { | ||
97 | lua_pop(L, 1); | ||
98 | return 1; | ||
99 | } | ||
100 | lua_getfield(L, -1, "traceback"); | ||
101 | if (!lua_isfunction(L, -1)) { | ||
102 | lua_pop(L, 2); | ||
103 | return 1; | ||
104 | } | ||
105 | lua_pushvalue(L, 1); /* pass error message */ | ||
106 | lua_pushinteger(L, 2); /* skip this function and traceback */ | ||
107 | lua_call(L, 2, 1); /* call debug.traceback */ | ||
108 | return 1; | ||
109 | } | ||
110 | |||
111 | static int docall(lua_State *L, int narg, int clear) | ||
112 | { | ||
113 | int status; | ||
114 | int base = lua_gettop(L) - narg; /* function index */ | ||
115 | lua_pushcfunction(L, traceback); /* push traceback function */ | ||
116 | lua_insert(L, base); /* put it under chunk and args */ | ||
117 | signal(SIGINT, laction); | ||
118 | status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); | ||
119 | signal(SIGINT, SIG_DFL); | ||
120 | lua_remove(L, base); /* remove traceback function */ | ||
121 | /* force a complete garbage collection in case of errors */ | ||
122 | if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); | ||
123 | return status; | ||
124 | } | ||
125 | |||
126 | static void print_version(void) | ||
127 | { | ||
128 | fprintf(stderr, | ||
129 | LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n"); | ||
130 | } | ||
131 | |||
132 | static void print_jit_status(lua_State *L) | ||
133 | { | ||
134 | int n; | ||
135 | const char *s; | ||
136 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
137 | lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ | ||
138 | lua_remove(L, -2); | ||
139 | lua_getfield(L, -1, "status"); | ||
140 | lua_remove(L, -2); | ||
141 | n = lua_gettop(L); | ||
142 | lua_call(L, 0, LUA_MULTRET); | ||
143 | fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stderr); | ||
144 | for (n++; (s = lua_tostring(L, n)); n++) | ||
145 | fprintf(stderr, " %s", s); | ||
146 | fputs("\n", stdout); | ||
147 | } | ||
148 | |||
149 | static int getargs(lua_State *L, char **argv, int n) | ||
150 | { | ||
151 | int narg; | ||
152 | int i; | ||
153 | int argc = 0; | ||
154 | while (argv[argc]) argc++; /* count total number of arguments */ | ||
155 | narg = argc - (n + 1); /* number of arguments to the script */ | ||
156 | luaL_checkstack(L, narg + 3, "too many arguments to script"); | ||
157 | for (i = n+1; i < argc; i++) | ||
158 | lua_pushstring(L, argv[i]); | ||
159 | lua_createtable(L, narg, n + 1); | ||
160 | for (i = 0; i < argc; i++) { | ||
161 | lua_pushstring(L, argv[i]); | ||
162 | lua_rawseti(L, -2, i - n); | ||
163 | } | ||
164 | return narg; | ||
165 | } | ||
166 | |||
167 | static int dofile(lua_State *L, const char *name) | ||
168 | { | ||
169 | int status = luaL_loadfile(L, name) || docall(L, 0, 1); | ||
170 | return report(L, status); | ||
171 | } | ||
172 | |||
173 | static int dostring(lua_State *L, const char *s, const char *name) | ||
174 | { | ||
175 | int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); | ||
176 | return report(L, status); | ||
177 | } | ||
178 | |||
179 | static int dolibrary(lua_State *L, const char *name) | ||
180 | { | ||
181 | lua_getglobal(L, "require"); | ||
182 | lua_pushstring(L, name); | ||
183 | return report(L, docall(L, 1, 1)); | ||
184 | } | ||
185 | |||
186 | static void write_prompt(lua_State *L, int firstline) | ||
187 | { | ||
188 | const char *p; | ||
189 | lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); | ||
190 | p = lua_tostring(L, -1); | ||
191 | if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2; | ||
192 | fputs(p, stdout); | ||
193 | fflush(stdout); | ||
194 | lua_pop(L, 1); /* remove global */ | ||
195 | } | ||
196 | |||
197 | static int incomplete(lua_State *L, int status) | ||
198 | { | ||
199 | if (status == LUA_ERRSYNTAX) { | ||
200 | size_t lmsg; | ||
201 | const char *msg = lua_tolstring(L, -1, &lmsg); | ||
202 | const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1); | ||
203 | if (strstr(msg, LUA_QL("<eof>")) == tp) { | ||
204 | lua_pop(L, 1); | ||
205 | return 1; | ||
206 | } | ||
207 | } | ||
208 | return 0; /* else... */ | ||
209 | } | ||
210 | |||
211 | static int pushline(lua_State *L, int firstline) | ||
212 | { | ||
213 | char buf[LUA_MAXINPUT]; | ||
214 | write_prompt(L, firstline); | ||
215 | if (fgets(buf, LUA_MAXINPUT, stdin)) { | ||
216 | size_t len = strlen(buf); | ||
217 | if (len > 0 && buf[len-1] == '\n') | ||
218 | buf[len-1] = '\0'; | ||
219 | if (firstline && buf[0] == '=') | ||
220 | lua_pushfstring(L, "return %s", buf+1); | ||
221 | else | ||
222 | lua_pushstring(L, buf); | ||
223 | return 1; | ||
224 | } | ||
225 | return 0; | ||
226 | } | ||
227 | |||
228 | static int loadline(lua_State *L) | ||
229 | { | ||
230 | int status; | ||
231 | lua_settop(L, 0); | ||
232 | if (!pushline(L, 1)) | ||
233 | return -1; /* no input */ | ||
234 | for (;;) { /* repeat until gets a complete line */ | ||
235 | status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); | ||
236 | if (!incomplete(L, status)) break; /* cannot try to add lines? */ | ||
237 | if (!pushline(L, 0)) /* no more input? */ | ||
238 | return -1; | ||
239 | lua_pushliteral(L, "\n"); /* add a new line... */ | ||
240 | lua_insert(L, -2); /* ...between the two lines */ | ||
241 | lua_concat(L, 3); /* join them */ | ||
242 | } | ||
243 | lua_remove(L, 1); /* remove line */ | ||
244 | return status; | ||
245 | } | ||
246 | |||
247 | static void dotty(lua_State *L) | ||
248 | { | ||
249 | int status; | ||
250 | const char *oldprogname = progname; | ||
251 | progname = NULL; | ||
252 | while ((status = loadline(L)) != -1) { | ||
253 | if (status == 0) status = docall(L, 0, 0); | ||
254 | report(L, status); | ||
255 | if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ | ||
256 | lua_getglobal(L, "print"); | ||
257 | lua_insert(L, 1); | ||
258 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) | ||
259 | l_message(progname, | ||
260 | lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", | ||
261 | lua_tostring(L, -1))); | ||
262 | } | ||
263 | } | ||
264 | lua_settop(L, 0); /* clear stack */ | ||
265 | fputs("\n", stdout); | ||
266 | fflush(stdout); | ||
267 | progname = oldprogname; | ||
268 | } | ||
269 | |||
270 | static int handle_script(lua_State *L, char **argv, int n) | ||
271 | { | ||
272 | int status; | ||
273 | const char *fname; | ||
274 | int narg = getargs(L, argv, n); /* collect arguments */ | ||
275 | lua_setglobal(L, "arg"); | ||
276 | fname = argv[n]; | ||
277 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) | ||
278 | fname = NULL; /* stdin */ | ||
279 | status = luaL_loadfile(L, fname); | ||
280 | lua_insert(L, -(narg+1)); | ||
281 | if (status == 0) | ||
282 | status = docall(L, narg, 0); | ||
283 | else | ||
284 | lua_pop(L, narg); | ||
285 | return report(L, status); | ||
286 | } | ||
287 | |||
288 | /* Load add-on module. */ | ||
289 | static int loadjitmodule(lua_State *L, const char *notfound) | ||
290 | { | ||
291 | lua_getglobal(L, "require"); | ||
292 | lua_pushliteral(L, "jit."); | ||
293 | lua_pushvalue(L, -3); | ||
294 | lua_concat(L, 2); | ||
295 | if (lua_pcall(L, 1, 1, 0)) { | ||
296 | const char *msg = lua_tostring(L, -1); | ||
297 | if (msg && !strncmp(msg, "module ", 7)) { | ||
298 | err: | ||
299 | l_message(progname, notfound); | ||
300 | return 1; | ||
301 | } else { | ||
302 | return report(L, 1); | ||
303 | } | ||
304 | } | ||
305 | lua_getfield(L, -1, "start"); | ||
306 | if (lua_isnil(L, -1)) goto err; | ||
307 | lua_remove(L, -2); /* Drop module table. */ | ||
308 | return 0; | ||
309 | } | ||
310 | |||
311 | /* Run command with options. */ | ||
312 | static int runcmdopt(lua_State *L, const char *opt) | ||
313 | { | ||
314 | int narg = 0; | ||
315 | if (opt && *opt) { | ||
316 | for (;;) { /* Split arguments. */ | ||
317 | const char *p = strchr(opt, ','); | ||
318 | narg++; | ||
319 | if (!p) break; | ||
320 | if (p == opt) | ||
321 | lua_pushnil(L); | ||
322 | else | ||
323 | lua_pushlstring(L, opt, (size_t)(p - opt)); | ||
324 | opt = p + 1; | ||
325 | } | ||
326 | if (*opt) | ||
327 | lua_pushstring(L, opt); | ||
328 | else | ||
329 | lua_pushnil(L); | ||
330 | } | ||
331 | return report(L, lua_pcall(L, narg, 0, 0)); | ||
332 | } | ||
333 | |||
334 | /* JIT engine control command: try jit library first or load add-on module. */ | ||
335 | static int dojitcmd(lua_State *L, const char *cmd) | ||
336 | { | ||
337 | const char *opt = strchr(cmd, '='); | ||
338 | lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); | ||
339 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
340 | lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ | ||
341 | lua_remove(L, -2); | ||
342 | lua_pushvalue(L, -2); | ||
343 | lua_gettable(L, -2); /* Lookup library function. */ | ||
344 | if (!lua_isfunction(L, -1)) { | ||
345 | lua_pop(L, 2); /* Drop non-function and jit.* table, keep module name. */ | ||
346 | if (loadjitmodule(L, "unknown luaJIT command")) | ||
347 | return 1; | ||
348 | } else { | ||
349 | lua_remove(L, -2); /* Drop jit.* table. */ | ||
350 | } | ||
351 | lua_remove(L, -2); /* Drop module name. */ | ||
352 | return runcmdopt(L, opt ? opt+1 : opt); | ||
353 | } | ||
354 | |||
355 | /* Optimization flags. */ | ||
356 | static int dojitopt(lua_State *L, const char *opt) | ||
357 | { | ||
358 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
359 | lua_getfield(L, -1, "jit.opt"); /* Get jit.opt.* module table. */ | ||
360 | lua_remove(L, -2); | ||
361 | lua_getfield(L, -1, "start"); | ||
362 | lua_remove(L, -2); | ||
363 | return runcmdopt(L, opt); | ||
364 | } | ||
365 | |||
366 | /* check that argument has no extra characters at the end */ | ||
367 | #define notail(x) {if ((x)[2] != '\0') return -1;} | ||
368 | |||
369 | static int collectargs(char **argv, int *pi, int *pv, int *pe) | ||
370 | { | ||
371 | int i; | ||
372 | for (i = 1; argv[i] != NULL; i++) { | ||
373 | if (argv[i][0] != '-') /* not an option? */ | ||
374 | return i; | ||
375 | switch (argv[i][1]) { /* option */ | ||
376 | case '-': | ||
377 | notail(argv[i]); | ||
378 | return (argv[i+1] != NULL ? i+1 : 0); | ||
379 | case '\0': | ||
380 | return i; | ||
381 | case 'i': | ||
382 | notail(argv[i]); | ||
383 | *pi = 1; /* go through */ | ||
384 | case 'v': | ||
385 | notail(argv[i]); | ||
386 | *pv = 1; | ||
387 | break; | ||
388 | case 'e': | ||
389 | *pe = 1; /* go through */ | ||
390 | case 'j': /* LuaJIT extension */ | ||
391 | case 'l': | ||
392 | if (argv[i][2] == '\0') { | ||
393 | i++; | ||
394 | if (argv[i] == NULL) return -1; | ||
395 | } | ||
396 | break; | ||
397 | case 'O': break; /* LuaJIT extension */ | ||
398 | default: return -1; /* invalid option */ | ||
399 | } | ||
400 | } | ||
401 | return 0; | ||
402 | } | ||
403 | |||
404 | static int runargs(lua_State *L, char **argv, int n) | ||
405 | { | ||
406 | int i; | ||
407 | for (i = 1; i < n; i++) { | ||
408 | if (argv[i] == NULL) continue; | ||
409 | lua_assert(argv[i][0] == '-'); | ||
410 | switch (argv[i][1]) { /* option */ | ||
411 | case 'e': { | ||
412 | const char *chunk = argv[i] + 2; | ||
413 | if (*chunk == '\0') chunk = argv[++i]; | ||
414 | lua_assert(chunk != NULL); | ||
415 | if (dostring(L, chunk, "=(command line)") != 0) | ||
416 | return 1; | ||
417 | break; | ||
418 | } | ||
419 | case 'l': { | ||
420 | const char *filename = argv[i] + 2; | ||
421 | if (*filename == '\0') filename = argv[++i]; | ||
422 | lua_assert(filename != NULL); | ||
423 | if (dolibrary(L, filename)) | ||
424 | return 1; /* stop if file fails */ | ||
425 | break; | ||
426 | } | ||
427 | case 'j': { /* LuaJIT extension */ | ||
428 | const char *cmd = argv[i] + 2; | ||
429 | if (*cmd == '\0') cmd = argv[++i]; | ||
430 | lua_assert(cmd != NULL); | ||
431 | if (dojitcmd(L, cmd)) | ||
432 | return 1; | ||
433 | break; | ||
434 | } | ||
435 | case 'O': /* LuaJIT extension */ | ||
436 | if (dojitopt(L, argv[i] + 2)) | ||
437 | return 1; | ||
438 | break; | ||
439 | default: break; | ||
440 | } | ||
441 | } | ||
442 | return 0; | ||
443 | } | ||
444 | |||
445 | static int handle_luainit(lua_State *L) | ||
446 | { | ||
447 | const char *init = getenv(LUA_INIT); | ||
448 | if (init == NULL) | ||
449 | return 0; /* status OK */ | ||
450 | else if (init[0] == '@') | ||
451 | return dofile(L, init+1); | ||
452 | else | ||
453 | return dostring(L, init, "=" LUA_INIT); | ||
454 | } | ||
455 | |||
456 | struct Smain { | ||
457 | int argc; | ||
458 | char **argv; | ||
459 | int status; | ||
460 | }; | ||
461 | |||
462 | static int pmain(lua_State *L) | ||
463 | { | ||
464 | struct Smain *s = (struct Smain *)lua_touserdata(L, 1); | ||
465 | char **argv = s->argv; | ||
466 | int script; | ||
467 | int has_i = 0, has_v = 0, has_e = 0; | ||
468 | globalL = L; | ||
469 | if (argv[0] && argv[0][0]) progname = argv[0]; | ||
470 | LUAJIT_VERSION_SYM(); /* linker-enforced version check */ | ||
471 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | ||
472 | luaL_openlibs(L); /* open libraries */ | ||
473 | lua_gc(L, LUA_GCRESTART, 0); | ||
474 | s->status = handle_luainit(L); | ||
475 | if (s->status != 0) return 0; | ||
476 | script = collectargs(argv, &has_i, &has_v, &has_e); | ||
477 | if (script < 0) { /* invalid args? */ | ||
478 | print_usage(); | ||
479 | s->status = 1; | ||
480 | return 0; | ||
481 | } | ||
482 | if (has_v) print_version(); | ||
483 | s->status = runargs(L, argv, (script > 0) ? script : s->argc); | ||
484 | if (s->status != 0) return 0; | ||
485 | if (script) | ||
486 | s->status = handle_script(L, argv, script); | ||
487 | if (s->status != 0) return 0; | ||
488 | if (has_i) { | ||
489 | print_jit_status(L); | ||
490 | dotty(L); | ||
491 | } else if (script == 0 && !has_e && !has_v) { | ||
492 | if (lua_stdin_is_tty()) { | ||
493 | print_version(); | ||
494 | print_jit_status(L); | ||
495 | dotty(L); | ||
496 | } else { | ||
497 | dofile(L, NULL); /* executes stdin as a file */ | ||
498 | } | ||
499 | } | ||
500 | return 0; | ||
501 | } | ||
502 | |||
503 | int main(int argc, char **argv) | ||
504 | { | ||
505 | int status; | ||
506 | struct Smain s; | ||
507 | lua_State *L = lua_open(); /* create state */ | ||
508 | if (L == NULL) { | ||
509 | l_message(argv[0], "cannot create state: not enough memory"); | ||
510 | return EXIT_FAILURE; | ||
511 | } | ||
512 | s.argc = argc; | ||
513 | s.argv = argv; | ||
514 | status = lua_cpcall(L, pmain, &s); | ||
515 | report(L, status); | ||
516 | lua_close(L); | ||
517 | return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; | ||
518 | } | ||
519 | |||