beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / lua / texluajitc.w
blobf54f9c8d4a4bc8064b644a1f23be94e8ab124e38
1 /*
2 ** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc.
3 ** Copyright (C) 2005-2012 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 */
9 @ @c
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #define luajit_c
16 #include "lua.h"
17 #include "lauxlib.h"
18 #include "lualib.h"
19 #include "luajit.h"
21 #include "lj_arch.h"
23 #if LJ_TARGET_POSIX
24 #include <unistd.h>
25 #define lua_stdin_is_tty() isatty(0)
26 #elif LJ_TARGET_WINDOWS
27 #include <io.h>
28 #ifdef __BORLANDC__
29 #define lua_stdin_is_tty() isatty(_fileno(stdin))
30 #else
31 #define lua_stdin_is_tty() _isatty(_fileno(stdin))
32 #endif
33 #else
34 #define lua_stdin_is_tty() 1
35 #endif
37 #if !LJ_TARGET_CONSOLE
38 #include <signal.h>
39 #endif
41 #include "lua/luatex-api.h"
43 static lua_State *globalL = NULL;
44 static const char *progname = LUA_PROGNAME;
46 #if !LJ_TARGET_CONSOLE
47 static void lstop(lua_State *L, lua_Debug *ar)
49 (void)ar; /* unused arg. */
50 lua_sethook(L, NULL, 0, 0);
51 /* Avoid luaL_error -- a C hook doesn't add an extra frame. */
52 luaL_where(L, 0);
53 lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
54 lua_error(L);
57 static void laction(int i)
59 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
60 terminate process (default action) */
61 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
63 #endif
65 static void print_usage(void)
67 fprintf(stderr,
68 "usage: %s [options]... [script [args]...].\n"
69 "Available options are:\n"
70 " -e chunk Execute string " LUA_QL("chunk") ".\n"
71 " -l name Require library " LUA_QL("name") ".\n"
72 " -b ... Save or list bytecode.\n"
73 " -j cmd Perform LuaJIT control command.\n"
74 " -O[opt] Control LuaJIT optimizations.\n"
75 " -i Enter interactive mode after executing " LUA_QL("script") ".\n"
76 " -v Show version information.\n"
77 " -E Ignore environment variables.\n"
78 " -- Stop handling options.\n"
79 " - Execute stdin and stop handling options.\n"
81 progname);
82 fflush(stderr);
85 static void l_message(const char *pname, const char *msg)
87 if (pname) fprintf(stderr, "%s: ", pname);
88 fprintf(stderr, "%s\n", msg);
89 fflush(stderr);
92 static int report(lua_State *L, int status)
94 if (status && !lua_isnil(L, -1)) {
95 const char *msg = lua_tostring(L, -1);
96 if (msg == NULL) msg = "(error object is not a string)";
97 l_message(progname, msg);
98 lua_pop(L, 1);
100 return status;
103 static int traceback(lua_State *L)
105 if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
106 if (lua_isnoneornil(L, 1) ||
107 !luaL_callmeta(L, 1, "__tostring") ||
108 !lua_isstring(L, -1))
109 return 1; /* Return non-string error object. */
110 lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
112 luaL_traceback(L, L, lua_tostring(L, 1), 1);
113 return 1;
116 static int docall(lua_State *L, int narg, int clear)
118 int status;
119 int base = lua_gettop(L) - narg; /* function index */
120 lua_pushcfunction(L, traceback); /* push traceback function */
121 lua_insert(L, base); /* put it under chunk and args */
122 #if !LJ_TARGET_CONSOLE
123 signal(SIGINT, laction);
124 #endif
125 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
126 #if !LJ_TARGET_CONSOLE
127 signal(SIGINT, SIG_DFL);
128 #endif
129 lua_remove(L, base); /* remove traceback function */
130 /* force a complete garbage collection in case of errors */
131 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
132 return status;
135 static void print_version(void)
137 fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout);
140 static void print_jit_status(lua_State *L)
142 int n;
143 const char *s;
144 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
145 lua_getfield(L, -1, "jit"); /* Get jit.* module table. */
146 lua_remove(L, -2);
147 lua_getfield(L, -1, "status");
148 lua_remove(L, -2);
149 n = lua_gettop(L);
150 lua_call(L, 0, LUA_MULTRET);
151 fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout);
152 for (n++; (s = lua_tostring(L, n)); n++) {
153 putc(' ', stdout);
154 fputs(s, stdout);
156 putc('\n', stdout);
159 static int getargs(lua_State *L, char **argv, int n)
161 int narg;
162 int i;
163 int argc = 0;
164 while (argv[argc]) argc++; /* count total number of arguments */
165 narg = argc - (n + 1); /* number of arguments to the script */
166 luaL_checkstack(L, narg + 3, "too many arguments to script");
167 for (i = n+1; i < argc; i++)
168 lua_pushstring(L, argv[i]);
169 lua_createtable(L, narg, n + 1);
170 for (i = 0; i < argc; i++) {
171 lua_pushstring(L, argv[i]);
172 lua_rawseti(L, -2, i - n);
174 return narg;
177 static int dofile(lua_State *L, const char *name)
179 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
180 return report(L, status);
183 static int dostring(lua_State *L, const char *s, const char *name)
185 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
186 return report(L, status);
189 static int dolibrary(lua_State *L, const char *name)
191 lua_getglobal(L, "require");
192 lua_pushstring(L, name);
193 return report(L, docall(L, 1, 1));
196 static void write_prompt(lua_State *L, int firstline)
198 const char *p;
199 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
200 p = lua_tostring(L, -1);
201 if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;
202 fputs(p, stdout);
203 fflush(stdout);
204 lua_pop(L, 1); /* remove global */
207 static int incomplete(lua_State *L, int status)
209 if (status == LUA_ERRSYNTAX) {
210 size_t lmsg;
211 const char *msg = lua_tolstring(L, -1, &lmsg);
212 const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
213 if (strstr(msg, LUA_QL("<eof>")) == tp) {
214 lua_pop(L, 1);
215 return 1;
218 return 0; /* else... */
221 static int pushline(lua_State *L, int firstline)
223 char buf[LUA_MAXINPUT];
224 write_prompt(L, firstline);
225 if (fgets(buf, LUA_MAXINPUT, stdin)) {
226 size_t len = strlen(buf);
227 if (len > 0 && buf[len-1] == '\n')
228 buf[len-1] = '\0';
229 if (firstline && buf[0] == '=')
230 lua_pushfstring(L, "return %s", buf+1);
231 else
232 lua_pushstring(L, buf);
233 return 1;
235 return 0;
238 static int loadline(lua_State *L)
240 int status;
241 lua_settop(L, 0);
242 if (!pushline(L, 1))
243 return -1; /* no input */
244 for (;;) { /* repeat until gets a complete line */
245 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
246 if (!incomplete(L, status)) break; /* cannot try to add lines? */
247 if (!pushline(L, 0)) /* no more input? */
248 return -1;
249 lua_pushliteral(L, "\n"); /* add a new line... */
250 lua_insert(L, -2); /* ...between the two lines */
251 lua_concat(L, 3); /* join them */
253 lua_remove(L, 1); /* remove line */
254 return status;
257 static void dotty(lua_State *L)
259 int status;
260 const char *oldprogname = progname;
261 progname = NULL;
262 while ((status = loadline(L)) != -1) {
263 if (status == 0) status = docall(L, 0, 0);
264 report(L, status);
265 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
266 lua_getglobal(L, "print");
267 lua_insert(L, 1);
268 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
269 l_message(progname,
270 lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
271 lua_tostring(L, -1)));
274 lua_settop(L, 0); /* clear stack */
275 fputs("\n", stdout);
276 fflush(stdout);
277 progname = oldprogname;
280 static int handle_script(lua_State *L, char **argv, int n)
282 int status;
283 const char *fname;
284 int narg = getargs(L, argv, n); /* collect arguments */
285 lua_setglobal(L, "arg");
286 fname = argv[n];
287 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
288 fname = NULL; /* stdin */
289 status = luaL_loadfile(L, fname);
290 lua_insert(L, -(narg+1));
291 if (status == 0)
292 status = docall(L, narg, 0);
293 else
294 lua_pop(L, narg);
295 return report(L, status);
298 /* Load add-on module. */
299 static int loadjitmodule(lua_State *L)
301 lua_getglobal(L, "require");
302 lua_pushliteral(L, "jit.");
303 lua_pushvalue(L, -3);
304 lua_concat(L, 2);
305 if (lua_pcall(L, 1, 1, 0)) {
306 const char *msg = lua_tostring(L, -1);
307 if (msg && !strncmp(msg, "module ", 7)) {
308 err:
309 l_message(progname,
310 "unknown luaJIT command or jit.* modules not installed");
311 return 1;
312 } else {
313 return report(L, 1);
316 lua_getfield(L, -1, "start");
317 if (lua_isnil(L, -1)) goto err;
318 lua_remove(L, -2); /* Drop module table. */
319 return 0;
322 /* Run command with options. */
323 static int runcmdopt(lua_State *L, const char *opt)
325 int narg = 0;
326 if (opt && *opt) {
327 for (;;) { /* Split arguments. */
328 const char *p = strchr(opt, ',');
329 narg++;
330 if (!p) break;
331 if (p == opt)
332 lua_pushnil(L);
333 else
334 lua_pushlstring(L, opt, (size_t)(p - opt));
335 opt = p + 1;
337 if (*opt)
338 lua_pushstring(L, opt);
339 else
340 lua_pushnil(L);
342 return report(L, lua_pcall(L, narg, 0, 0));
345 /* JIT engine control command: try jit library first or load add-on module. */
346 static int dojitcmd(lua_State *L, const char *cmd)
348 const char *opt = strchr(cmd, '=');
349 lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd));
350 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
351 lua_getfield(L, -1, "jit"); /* Get jit.* module table. */
352 lua_remove(L, -2);
353 lua_pushvalue(L, -2);
354 lua_gettable(L, -2); /* Lookup library function. */
355 if (!lua_isfunction(L, -1)) {
356 lua_pop(L, 2); /* Drop non-function and jit.* table, keep module name. */
357 if (loadjitmodule(L))
358 return 1;
359 } else {
360 lua_remove(L, -2); /* Drop jit.* table. */
362 lua_remove(L, -2); /* Drop module name. */
363 return runcmdopt(L, opt ? opt+1 : opt);
366 /* Optimization flags. */
367 static int dojitopt(lua_State *L, const char *opt)
369 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
370 lua_getfield(L, -1, "jit.opt"); /* Get jit.opt.* module table. */
371 lua_remove(L, -2);
372 lua_getfield(L, -1, "start");
373 lua_remove(L, -2);
374 return runcmdopt(L, opt);
377 /* Save or list bytecode. */
378 static int dobytecode(lua_State *L, char **argv)
380 int narg = 0;
381 lua_pushliteral(L, "bcsave");
382 if (loadjitmodule(L))
383 return 1;
384 if (argv[0][2]) {
385 narg++;
386 argv[0][1] = '-';
387 lua_pushstring(L, argv[0]+1);
389 for (argv++; *argv != NULL; narg++, argv++)
390 lua_pushstring(L, *argv);
391 return report(L, lua_pcall(L, narg, 0, 0));
394 /* check that argument has no extra characters at the end */
395 #define notail(x) {if ((x)[2] != '\0') return -1;}
397 #define FLAGS_INTERACTIVE 1
398 #define FLAGS_VERSION 2
399 #define FLAGS_EXEC 4
400 #define FLAGS_OPTION 8
401 #define FLAGS_NOENV 16
403 static int collectargs(char **argv, int *flags)
405 int i;
406 for (i = 1; argv[i] != NULL; i++) {
407 if (argv[i][0] != '-') /* Not an option? */
408 return i;
409 switch (argv[i][1]) { /* Check option. */
410 case '-':
411 notail(argv[i]);
412 return (argv[i+1] != NULL ? i+1 : 0);
413 case '\0':
414 return i;
415 case 'i':
416 notail(argv[i]);
417 *flags |= FLAGS_INTERACTIVE;
418 /* fallthrough */
419 case 'v':
420 notail(argv[i]);
421 *flags |= FLAGS_VERSION;
422 break;
423 case 'e':
424 *flags |= FLAGS_EXEC;
425 case 'j': /* LuaJIT extension */
426 case 'l':
427 *flags |= FLAGS_OPTION;
428 if (argv[i][2] == '\0') {
429 i++;
430 if (argv[i] == NULL) return -1;
432 break;
433 case 'O': break; /* LuaJIT extension */
434 case 'b': /* LuaJIT extension */
435 if (*flags) return -1;
436 *flags |= FLAGS_EXEC;
437 return 0;
438 case 'E':
439 *flags |= FLAGS_NOENV;
440 break;
441 default: return -1; /* invalid option */
444 return 0;
447 static int runargs(lua_State *L, char **argv, int n)
449 int i;
450 for (i = 1; i < n; i++) {
451 if (argv[i] == NULL) continue;
452 lua_assert(argv[i][0] == '-');
453 switch (argv[i][1]) { /* option */
454 case 'e': {
455 const char *chunk = argv[i] + 2;
456 if (*chunk == '\0') chunk = argv[++i];
457 lua_assert(chunk != NULL);
458 if (dostring(L, chunk, "=(command line)") != 0)
459 return 1;
460 break;
462 case 'l': {
463 const char *filename = argv[i] + 2;
464 if (*filename == '\0') filename = argv[++i];
465 lua_assert(filename != NULL);
466 if (dolibrary(L, filename))
467 return 1; /* stop if file fails */
468 break;
470 case 'j': { /* LuaJIT extension */
471 const char *cmd = argv[i] + 2;
472 if (*cmd == '\0') cmd = argv[++i];
473 lua_assert(cmd != NULL);
474 if (dojitcmd(L, cmd))
475 return 1;
476 break;
478 case 'O': /* LuaJIT extension */
479 if (dojitopt(L, argv[i] + 2))
480 return 1;
481 break;
482 case 'b': /* LuaJIT extension */
483 return dobytecode(L, argv+i);
484 default: break;
487 return 0;
490 static int handle_luainit(lua_State *L)
492 #if LJ_TARGET_CONSOLE
493 const char *init = NULL;
494 #else
495 const char *init = getenv(LUA_INIT);
496 #endif
497 if (init == NULL)
498 return 0; /* status OK */
499 else if (init[0] == 64)
500 return dofile(L, init+1);
501 else
502 return dostring(L, init, "=" LUA_INIT);
505 struct Smain {
506 char **argv;
507 int argc;
508 int status;
511 static int pmain(lua_State *L)
513 struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
514 char **argv = s->argv;
515 int script;
516 int flags = 0;
517 globalL = L;
518 if (argv[0] && argv[0][0]) progname = argv[0];
519 LUAJIT_VERSION_SYM(); /* linker-enforced version check */
520 script = collectargs(argv, &flags);
521 if (script < 0) { /* invalid args? */
522 print_usage();
523 s->status = 1;
524 return 0;
526 if ((flags & FLAGS_NOENV)) {
527 lua_pushboolean(L, 1);
528 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
530 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
531 luaL_openlibs(L); /* open libraries */
532 lua_gc(L, LUA_GCRESTART, -1);
533 if (!(flags & FLAGS_NOENV)) {
534 s->status = handle_luainit(L);
535 if (s->status != 0) return 0;
537 if ((flags & FLAGS_VERSION)) print_version();
538 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
539 if (s->status != 0) return 0;
540 if (script) {
541 s->status = handle_script(L, argv, script);
542 if (s->status != 0) return 0;
544 if ((flags & FLAGS_INTERACTIVE)) {
545 print_jit_status(L);
546 dotty(L);
547 } else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {
548 if (lua_stdin_is_tty()) {
549 print_version();
550 print_jit_status(L);
551 dotty(L);
552 } else {
553 dofile(L, NULL); /* executes stdin as a file */
556 return 0;
559 int luac_main(int argc, char **argv)
561 int status;
562 struct Smain s;
563 lua_State *L = lua_open(); /* create state */
564 if (L == NULL) {
565 l_message(argv[0], "cannot create state: not enough memory");
566 return EXIT_FAILURE;
568 s.argc = argc;
569 s.argv = argv;
570 status = lua_cpcall(L, pmain, &s);
571 report(L, status);
572 lua_close(L);
573 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;