From dba17070ac2a6a54079b0b935635377545a3b764 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Jun 2004 11:06:52 -0300 Subject: optional error for accesss to undefined variables/fields --- lua.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'lua.c') 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(); -- cgit v1.2.3-55-g6feb