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 | |
| parent | 569eefbf73a6c41b16aa6dc2d4f1c52ebde54084 (diff) | |
| download | lua-dba17070ac2a6a54079b0b935635377545a3b764.tar.gz lua-dba17070ac2a6a54079b0b935635377545a3b764.tar.bz2 lua-dba17070ac2a6a54079b0b935635377545a3b764.zip | |
optional error for accesss to undefined variables/fields
| -rw-r--r-- | lauxlib.c | 4 | ||||
| -rw-r--r-- | lstate.c | 3 | ||||
| -rw-r--r-- | lua.c | 25 |
3 files changed, 21 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.114 2004/06/02 13:50:46 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.115 2004/06/02 19:06:14 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -243,6 +243,8 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | |||
| 243 | if (lua_isnil(L, -1)) { /* no? */ | 243 | if (lua_isnil(L, -1)) { /* no? */ |
| 244 | lua_pop(L, 1); | 244 | lua_pop(L, 1); |
| 245 | lua_newtable(L); /* create it */ | 245 | lua_newtable(L); /* create it */ |
| 246 | if (lua_getmetatable(L, LUA_GLOBALSINDEX)) | ||
| 247 | lua_setmetatable(L, -2); /* share metatable with global table */ | ||
| 246 | lua_pushvalue(L, -1); | 248 | lua_pushvalue(L, -1); |
| 247 | /* register it with given name */ | 249 | /* register it with given name */ |
| 248 | lua_setglobal(L, libname); | 250 | lua_setglobal(L, libname); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 2.7 2004/05/31 18:51:50 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.8 2004/06/02 19:09:36 roberto Exp roberto $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -87,6 +87,7 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 87 | setbit(L->marked, FIXEDBIT); | 87 | setbit(L->marked, FIXEDBIT); |
| 88 | stack_init(L, L); /* init stack */ | 88 | stack_init(L, L); /* init stack */ |
| 89 | sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */ | 89 | sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */ |
| 90 | hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */ | ||
| 90 | sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */ | 91 | sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */ |
| 91 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | 92 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
| 92 | luaT_init(L); | 93 | luaT_init(L); |
| @@ -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(); |
