lua

A copy of the Lua development repository
Log | Files | Refs | README

commit dba17070ac2a6a54079b0b935635377545a3b764
parent 569eefbf73a6c41b16aa6dc2d4f1c52ebde54084
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu, 17 Jun 2004 11:06:30 -0300

optional error for accesss to undefined variables/fields

Diffstat:
Mlauxlib.c | 4+++-
Mlstate.c | 3++-
Mlua.c | 25++++++++++++++++---------
3 files changed, 21 insertions(+), 11 deletions(-)

diff --git 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 @@ -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 @@ -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();