From 10200585a7d634b9d8688ecf8afab5b203619508 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 26 Jun 2014 14:08:52 -0300 Subject: [PATCH] var-arguments to the script come from 'arg' table (not from original 'argv' array) --- lua.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lua.c b/lua.c index 7a1cdb90..e2dc836f 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.210 2014/02/26 15:27:56 roberto Exp roberto $ +** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -417,25 +417,31 @@ static void doREPL (lua_State *L) { /* -** Push on the stack 'n' strings from 'argv' +** Push on the stack the contents of table 'arg' from 1 to #arg */ -static void pushargs (lua_State *L, char **argv, int n) { - int i; +static int pushargs (lua_State *L) { + int i, n; + lua_getglobal(L, "arg"); + if (!lua_istable(L, -1)) + luaL_error(L, "'arg' is not a table"); + n = (int)luaL_len(L, -1); luaL_checkstack(L, n + 3, "too many arguments to script"); - for (i = 1; i < n; i++) /* skip 0 (the script name) */ - lua_pushstring(L, argv[i]); + for (i = 1; i <= n; i++) + lua_rawgeti(L, -i, i); + lua_remove(L, -i); /* remove table from the stack */ + return n; } -static int handle_script (lua_State *L, char **argv, int n) { +static int handle_script (lua_State *L, char **argv) { int status; const char *fname = argv[0]; if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) fname = NULL; /* stdin */ status = luaL_loadfile(L, fname); if (status == LUA_OK) { - pushargs(L, argv, n); /* push arguments to script */ - status = docall(L, n - 1, LUA_MULTRET); + int n = pushargs(L); /* push arguments to script */ + status = docall(L, n, LUA_MULTRET); } return report(L, status); } @@ -570,7 +576,7 @@ static int pmain (lua_State *L) { if (!runargs(L, argv, script)) /* execute arguments -e and -l */ return 0; /* something failed */ if (script < argc && /* execute main script (if there is one) */ - handle_script(L, argv + script, argc - script) != LUA_OK) + handle_script(L, argv + script) != LUA_OK) return 0; if (args & has_i) /* -i option? */ doREPL(L); /* do read-eval-print loop */