Showing the changes to src/lua.c (1 of 41 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /* |
| 2 | −** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp $ | |
| 2 | +** $Id: lua.c,v 1.213 2014/06/30 19:48:08 roberto Exp $ | |
| 3 | 3 | ** Lua stand-alone interpreter |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -115,7 +115,7 @@ static void lstop (lua_State *L, lua_Debug *ar) { · 109 unmodified lines | ||
| 115 | 115 | |
| 116 | 116 | /* |
| 117 | 117 | ** Function to be called at a C signal. Because a C signal cannot |
| 118 | −** just change a Lua state (as there is no proper syncronization), | |
| 118 | +** just change a Lua state (as there is no proper synchronization), | |
| 119 | 119 | ** this function only sets a hook that, when called, will stop the |
| 120 | 120 | ** interpreter. |
| 121 | 121 | */ |
| @@ -284,7 +284,7 @@ static const char *get_prompt (lua_State *L, int firstline) { · 162 unmodified lines | ||
| 284 | 284 | /* |
| 285 | 285 | ** Check whether 'status' signals a syntax error and the error |
| 286 | 286 | ** message at the top of the stack ends with the above mark for |
| 287 | −** incoplete statements. | |
| 287 | +** incomplete statements. | |
| 288 | 288 | */ |
| 289 | 289 | static int incomplete (lua_State *L, int status) { |
| 290 | 290 | if (status == LUA_ERRSYNTAX) { |
| @@ -417,25 +417,31 @@ static void doREPL (lua_State *L) { · 126 unmodified lines | ||
| 417 | 417 | |
| 418 | 418 | |
| 419 | 419 | /* |
| 420 | −** Push on the stack 'n' strings from 'argv' | |
| 420 | +** Push on the stack the contents of table 'arg' from 1 to #arg | |
| 421 | 421 | */ |
| 422 | −static void pushargs (lua_State *L, char **argv, int n) { | |
| 423 | − int i; | |
| 422 | +static int pushargs (lua_State *L) { | |
| 423 | + int i, n; | |
| 424 | + lua_getglobal(L, "arg"); | |
| 425 | + if (!lua_istable(L, -1)) | |
| 426 | + luaL_error(L, "'arg' is not a table"); | |
| 427 | + n = (int)luaL_len(L, -1); | |
| 424 | 428 | luaL_checkstack(L, n + 3, "too many arguments to script"); |
| 425 | − for (i = 1; i < n; i++) /* skip 0 (the script name) */ | |
| 426 | − lua_pushstring(L, argv[i]); | |
| 429 | + for (i = 1; i <= n; i++) | |
| 430 | + lua_rawgeti(L, -i, i); | |
| 431 | + lua_remove(L, -i); /* remove table from the stack */ | |
| 432 | + return n; | |
| 427 | 433 | } |
| 428 | 434 | |
| 429 | 435 | |
| 430 | −static int handle_script (lua_State *L, char **argv, int n) { | |
| 436 | +static int handle_script (lua_State *L, char **argv) { | |
| 431 | 437 | int status; |
| 432 | 438 | const char *fname = argv[0]; |
| 433 | 439 | if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) |
| 434 | 440 | fname = NULL; /* stdin */ |
| 435 | 441 | status = luaL_loadfile(L, fname); |
| 436 | 442 | if (status == LUA_OK) { |
| 437 | − pushargs(L, argv, n); /* push arguments to script */ | |
| 438 | − status = docall(L, n - 1, LUA_MULTRET); | |
| 443 | + int n = pushargs(L); /* push arguments to script */ | |
| 444 | + status = docall(L, n, LUA_MULTRET); | |
| 439 | 445 | } |
| 440 | 446 | return report(L, status); |
| 441 | 447 | } |
| @@ -570,7 +576,7 @@ static int pmain (lua_State *L) { · 128 unmodified lines | ||
| 570 | 576 | if (!runargs(L, argv, script)) /* execute arguments -e and -l */ |
| 571 | 577 | return 0; /* something failed */ |
| 572 | 578 | if (script < argc && /* execute main script (if there is one) */ |
| 573 | − handle_script(L, argv + script, argc - script) != LUA_OK) | |
| 579 | + handle_script(L, argv + script) != LUA_OK) | |
| 574 | 580 | return 0; |
| 575 | 581 | if (args & has_i) /* -i option? */ |
| 576 | 582 | doREPL(L); /* do read-eval-print loop */ |