diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-05-31 15:51:50 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-05-31 15:51:50 -0300 |
| commit | 616438fe9ab5e3ae7d73e9ad838e9b7bdea1ea59 (patch) | |
| tree | 78322d820e3af1ca6645ed08eaa65a8f0aa04fec /lua.c | |
| parent | 47eda6ebd83785908ac26f8dd06dff36a7d42664 (diff) | |
| download | lua-616438fe9ab5e3ae7d73e9ad838e9b7bdea1ea59.tar.gz lua-616438fe9ab5e3ae7d73e9ad838e9b7bdea1ea59.tar.bz2 lua-616438fe9ab5e3ae7d73e9ad838e9b7bdea1ea59.zip | |
new way to use `vararg' parameters (with `...')
Diffstat (limited to 'lua.c')
| -rw-r--r-- | lua.c | 37 |
1 files changed, 23 insertions, 14 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.124 2003/10/23 18:06:22 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.125 2004/04/30 20:13:38 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -113,30 +113,32 @@ static void print_version (void) { | |||
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | static void getargs (char *argv[], int n) { | 116 | static int getargs (char *argv[], int n) { |
| 117 | int i; | 117 | int i, narg; |
| 118 | for (i=n+1; argv[i]; i++) { | ||
| 119 | luaL_checkstack(L, 1, "too many arguments to script"); | ||
| 120 | lua_pushstring(L, argv[i]); | ||
| 121 | } | ||
| 122 | narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */ | ||
| 118 | lua_newtable(L); | 123 | lua_newtable(L); |
| 119 | for (i=0; argv[i]; i++) { | 124 | for (i=0; argv[i]; i++) { |
| 120 | lua_pushnumber(L, i - n); | 125 | lua_pushnumber(L, i - n); |
| 121 | lua_pushstring(L, argv[i]); | 126 | lua_pushstring(L, argv[i]); |
| 122 | lua_rawset(L, -3); | 127 | lua_rawset(L, -3); |
| 123 | } | 128 | } |
| 124 | } | 129 | return narg; |
| 125 | |||
| 126 | |||
| 127 | static int docall (int status) { | ||
| 128 | if (status == 0) status = lcall(0, 1); | ||
| 129 | return report(status); | ||
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | 132 | ||
| 133 | static int file_input (const char *name) { | 133 | static int file_input (const char *name) { |
| 134 | return docall(luaL_loadfile(L, name)); | 134 | int status = luaL_loadfile(L, name) || lcall(0, 1); |
| 135 | return report(status); | ||
| 135 | } | 136 | } |
| 136 | 137 | ||
| 137 | 138 | ||
| 138 | static int dostring (const char *s, const char *name) { | 139 | static int dostring (const char *s, const char *name) { |
| 139 | return docall(luaL_loadbuffer(L, s, strlen(s), name)); | 140 | int status = luaL_loadbuffer(L, s, strlen(s), name) || lcall(0, 1); |
| 141 | return report(status); | ||
| 140 | } | 142 | } |
| 141 | 143 | ||
| 142 | 144 | ||
| @@ -329,10 +331,17 @@ static int handle_argv (char *argv[], int *interactive) { | |||
| 329 | } endloop: | 331 | } endloop: |
| 330 | if (argv[i] != NULL) { | 332 | if (argv[i] != NULL) { |
| 331 | const char *filename = argv[i]; | 333 | const char *filename = argv[i]; |
| 332 | getargs(argv, i); /* collect arguments */ | 334 | int narg = getargs(argv, i); /* collect arguments */ |
| 333 | clearinteractive(interactive); | 335 | int status; |
| 334 | lua_setglobal(L, "arg"); | 336 | lua_setglobal(L, "arg"); |
| 335 | return file_input(filename); /* stop scanning arguments */ | 337 | clearinteractive(interactive); |
| 338 | status = luaL_loadfile(L, filename); | ||
| 339 | lua_insert(L, -(narg+1)); | ||
| 340 | if (status == 0) | ||
| 341 | status = lcall(narg, 0); | ||
| 342 | else | ||
| 343 | lua_pop(L, narg); | ||
| 344 | return report(status); | ||
| 336 | } | 345 | } |
| 337 | } | 346 | } |
| 338 | return 0; | 347 | return 0; |
