aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-06-17 11:06:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-06-17 11:06:52 -0300
commitdba17070ac2a6a54079b0b935635377545a3b764 (patch)
tree57d454c3f58bb5a41dcd99c058113e443b3ed897
parent569eefbf73a6c41b16aa6dc2d4f1c52ebde54084 (diff)
downloadlua-dba17070ac2a6a54079b0b935635377545a3b764.tar.gz
lua-dba17070ac2a6a54079b0b935635377545a3b764.tar.bz2
lua-dba17070ac2a6a54079b0b935635377545a3b764.zip
optional error for accesss to undefined variables/fields
-rw-r--r--lauxlib.c4
-rw-r--r--lstate.c3
-rw-r--r--lua.c25
3 files changed, 21 insertions, 11 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 765065c5..e1890780 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -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);
diff --git a/lstate.c b/lstate.c
index db4efa03..f8f3f148 100644
--- a/lstate.c
+++ b/lstate.c
@@ -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);
diff --git a/lua.c b/lua.c
index 112a20cc..d57db025 100644
--- a/lua.c
+++ b/lua.c
@@ -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
254static 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
256static int handle_argv (char *argv[], int *interactive) { 264static 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();