optional error for accesss to undefined variables/fields

This commit is contained in:
Roberto Ierusalimschy 2004-06-17 11:06:52 -03:00
parent 569eefbf73
commit dba17070ac
3 changed files with 21 additions and 11 deletions

View File

@ -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);

View File

@ -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);

25
lua.c
View File

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