From dba17070ac2a6a54079b0b935635377545a3b764 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Jun 2004 11:06:52 -0300 Subject: [PATCH] optional error for accesss to undefined variables/fields --- lauxlib.c | 4 +++- lstate.c | 3 ++- lua.c | 25 ++++++++++++++++--------- 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 @@ /* -** $Id: lauxlib.c,v 1.114 2004/06/02 13:50:46 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.115 2004/06/02 19:06:14 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -243,6 +243,8 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, if (lua_isnil(L, -1)) { /* no? */ lua_pop(L, 1); lua_newtable(L); /* create it */ + if (lua_getmetatable(L, LUA_GLOBALSINDEX)) + lua_setmetatable(L, -2); /* share metatable with global table */ lua_pushvalue(L, -1); /* register it with given name */ 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 @@ /* -** $Id: lstate.c,v 2.7 2004/05/31 18:51:50 roberto Exp roberto $ +** $Id: lstate.c,v 2.8 2004/06/02 19:09:36 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -87,6 +87,7 @@ static void f_luaopen (lua_State *L, void *ud) { setbit(L->marked, FIXEDBIT); stack_init(L, L); /* init stack */ sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */ + hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */ sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 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 @@ /* -** $Id: lua.c,v 1.126 2004/05/31 18:51:50 roberto Exp roberto $ +** $Id: lua.c,v 1.127 2004/06/16 20:22:43 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -251,6 +251,14 @@ static void manual_input (void) { } +static int checkvar (lua_State *l) { + const char *name = lua_tostring(l, 2); + if (name) + luaL_error(l, "attempt to access undefined variable `%s'", name); + return 0; +} + + #define clearinteractive(i) (*i &= 2) static int handle_argv (char *argv[], int *interactive) { @@ -290,6 +298,13 @@ static int handle_argv (char *argv[], int *interactive) { print_version(); break; } + case 'w': { + if (lua_getmetatable(L, LUA_GLOBALSINDEX)) { + lua_pushcfunction(L, checkvar); + lua_setfield(L, -2, "__index"); + } + break; + } case 'e': { const char *chunk = argv[i] + 2; clearinteractive(interactive); @@ -313,14 +328,6 @@ static int handle_argv (char *argv[], int *interactive) { return 1; /* stop if file fails */ break; } - case 'c': { - l_message(progname, "option `-c' is deprecated"); - break; - } - case 's': { - l_message(progname, "option `-s' is deprecated"); - break; - } default: { clearinteractive(interactive); print_usage();