diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-17 11:06:52 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-17 11:06:52 -0300 |
commit | dba17070ac2a6a54079b0b935635377545a3b764 (patch) | |
tree | 57d454c3f58bb5a41dcd99c058113e443b3ed897 /lua.c | |
parent | 569eefbf73a6c41b16aa6dc2d4f1c52ebde54084 (diff) | |
download | lua-dba17070ac2a6a54079b0b935635377545a3b764.tar.gz lua-dba17070ac2a6a54079b0b935635377545a3b764.tar.bz2 lua-dba17070ac2a6a54079b0b935635377545a3b764.zip |
optional error for accesss to undefined variables/fields
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 25 |
1 files changed, 16 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.126 2004/05/31 18:51:50 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.127 2004/06/16 20:22:43 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 | */ |
@@ -251,6 +251,14 @@ static void manual_input (void) { | |||
251 | } | 251 | } |
252 | 252 | ||
253 | 253 | ||
254 | static int checkvar (lua_State *l) { | ||
255 | const char *name = lua_tostring(l, 2); | ||
256 | if (name) | ||
257 | luaL_error(l, "attempt to access undefined variable `%s'", name); | ||
258 | return 0; | ||
259 | } | ||
260 | |||
261 | |||
254 | #define clearinteractive(i) (*i &= 2) | 262 | #define clearinteractive(i) (*i &= 2) |
255 | 263 | ||
256 | static int handle_argv (char *argv[], int *interactive) { | 264 | static int handle_argv (char *argv[], int *interactive) { |
@@ -290,6 +298,13 @@ static int handle_argv (char *argv[], int *interactive) { | |||
290 | print_version(); | 298 | print_version(); |
291 | break; | 299 | break; |
292 | } | 300 | } |
301 | case 'w': { | ||
302 | if (lua_getmetatable(L, LUA_GLOBALSINDEX)) { | ||
303 | lua_pushcfunction(L, checkvar); | ||
304 | lua_setfield(L, -2, "__index"); | ||
305 | } | ||
306 | break; | ||
307 | } | ||
293 | case 'e': { | 308 | case 'e': { |
294 | const char *chunk = argv[i] + 2; | 309 | const char *chunk = argv[i] + 2; |
295 | clearinteractive(interactive); | 310 | clearinteractive(interactive); |
@@ -313,14 +328,6 @@ static int handle_argv (char *argv[], int *interactive) { | |||
313 | return 1; /* stop if file fails */ | 328 | return 1; /* stop if file fails */ |
314 | break; | 329 | break; |
315 | } | 330 | } |
316 | case 'c': { | ||
317 | l_message(progname, "option `-c' is deprecated"); | ||
318 | break; | ||
319 | } | ||
320 | case 's': { | ||
321 | l_message(progname, "option `-s' is deprecated"); | ||
322 | break; | ||
323 | } | ||
324 | default: { | 331 | default: { |
325 | clearinteractive(interactive); | 332 | clearinteractive(interactive); |
326 | print_usage(); | 333 | print_usage(); |