aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-08-25 12:39:16 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-08-25 12:39:16 -0300
commit39cdbce23edfd443699e396900a70de3fcff46c7 (patch)
tree821660333387a4afb34c699763f72832ef384804
parent8974b59e4014dd1ca79dec6994d4c53ac226de5f (diff)
downloadlua-39cdbce23edfd443699e396900a70de3fcff46c7.tar.gz
lua-39cdbce23edfd443699e396900a70de3fcff46c7.tar.bz2
lua-39cdbce23edfd443699e396900a70de3fcff46c7.zip
no more '-w' option + new way to check module existence
-rw-r--r--lauxlib.c29
-rw-r--r--loadlib.c27
-rw-r--r--lstate.c3
-rw-r--r--lua.c18
4 files changed, 32 insertions, 45 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 95c33218..e99f6f60 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.147 2005/08/18 16:04:05 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.148 2005/08/18 20:36:26 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*/
@@ -231,22 +231,23 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
231 const luaL_reg *l, int nup) { 231 const luaL_reg *l, int nup) {
232 if (libname) { 232 if (libname) {
233 /* check whether lib already exists */ 233 /* check whether lib already exists */
234 luaL_getfield(L, LUA_GLOBALSINDEX, libname); 234 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
235 if (lua_isnil(L, -1)) { /* not found? */ 235 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
236 if (!lua_istable(L, -1)) { /* not found? */
236 lua_pop(L, 1); /* remove previous result */ 237 lua_pop(L, 1); /* remove previous result */
237 lua_newtable(L); /* create it */ 238 luaL_getfield(L, LUA_GLOBALSINDEX, libname); /* try global variable */
238 if (lua_getmetatable(L, LUA_GLOBALSINDEX)) 239 if (!lua_istable(L, -1)) {
239 lua_setmetatable(L, -2); /* share metatable with global table */ 240 if (!lua_isnil(L, -1))
240 /* register it with given name */ 241 luaL_error(L, "name conflict for module " LUA_QS, libname);
242 lua_pop(L, 1);
243 lua_newtable(L); /* create it */
244 lua_pushvalue(L, -1); /* register it with given name */
245 luaL_setfield(L, LUA_GLOBALSINDEX, libname);
246 }
241 lua_pushvalue(L, -1); 247 lua_pushvalue(L, -1);
242 luaL_setfield(L, LUA_GLOBALSINDEX, libname); 248 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
243 } 249 }
244 else if (!lua_istable(L, -1)) 250 lua_remove(L, -2); /* remove _LOADED table */
245 luaL_error(L, "name conflict for library " LUA_QS, libname);
246 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
247 lua_pushvalue(L, -2);
248 lua_setfield(L, -2, libname); /* _LOADED[modname] = new table */
249 lua_pop(L, 1); /* remove _LOADED table */
250 lua_insert(L, -(nup+1)); /* move library table to below upvalues */ 251 lua_insert(L, -(nup+1)); /* move library table to below upvalues */
251 } 252 }
252 for (; l->name; l++) { 253 for (; l->name; l++) {
diff --git a/loadlib.c b/loadlib.c
index 81d46172..1b310330 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.38 2005/08/15 14:12:32 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.39 2005/08/17 19:05:04 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -508,24 +508,29 @@ static int ll_module (lua_State *L) {
508 const char *dot; 508 const char *dot;
509 lua_settop(L, 1); 509 lua_settop(L, 1);
510 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 510 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
511 /* try to find given table */ 511 lua_getfield(L, 2, modname); /* get _LOADED[modname] */
512 luaL_getfield(L, LUA_GLOBALSINDEX, modname); 512 if (!lua_istable(L, -1)) { /* not found? */
513 if (lua_isnil(L, -1)) { /* not found? */
514 lua_pop(L, 1); /* remove previous result */ 513 lua_pop(L, 1); /* remove previous result */
515 lua_newtable(L); /* create it */ 514 luaL_getfield(L, LUA_GLOBALSINDEX, modname); /* try global variable */
516 /* register it with given name */ 515 if (!lua_istable(L, -1)) {
516 if (!lua_isnil(L, -1))
517 return luaL_error(L, "name conflict for module " LUA_QS, modname);
518 lua_pop(L, 1);
519 lua_newtable(L); /* create it */
520 lua_pushvalue(L, -1); /* register it with given name */
521 luaL_setfield(L, LUA_GLOBALSINDEX, modname);
522 }
517 lua_pushvalue(L, -1); 523 lua_pushvalue(L, -1);
518 luaL_setfield(L, LUA_GLOBALSINDEX, modname); 524 lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
519 } 525 }
520 else if (!lua_istable(L, -1))
521 return luaL_error(L, "name conflict for module " LUA_QS, modname);
522 /* check whether table already has a _NAME field */ 526 /* check whether table already has a _NAME field */
523 lua_getfield(L, -1, "_NAME"); 527 lua_getfield(L, -1, "_NAME");
524 if (!lua_isnil(L, -1)) /* is table an initialized module? */ 528 if (!lua_isnil(L, -1)) /* is table an initialized module? */
525 lua_pop(L, 1); 529 lua_pop(L, 1);
526 else { /* no; initialize it */ 530 else { /* no; initialize it */
527 lua_pop(L, 1); 531 lua_pop(L, 1);
528 lua_newtable(L); /* create new metatable */ 532 /* create new metatable */
533 lua_newtable(L);
529 lua_pushvalue(L, LUA_GLOBALSINDEX); 534 lua_pushvalue(L, LUA_GLOBALSINDEX);
530 lua_setfield(L, -2, "__index"); /* mt.__index = _G */ 535 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
531 lua_setmetatable(L, -2); 536 lua_setmetatable(L, -2);
@@ -540,8 +545,6 @@ static int ll_module (lua_State *L) {
540 lua_pushlstring(L, modname, dot - modname); 545 lua_pushlstring(L, modname, dot - modname);
541 lua_setfield(L, -2, "_PACKAGE"); 546 lua_setfield(L, -2, "_PACKAGE");
542 } 547 }
543 lua_pushvalue(L, -1);
544 lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
545 setfenv(L); 548 setfenv(L);
546 return 0; 549 return 0;
547} 550}
diff --git a/lstate.c b/lstate.c
index 36f43ade..9b979036 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.31 2005/05/05 15:34:03 roberto Exp roberto $ 2** $Id: lstate.c,v 2.32 2005/06/03 20:15:58 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*/
@@ -72,7 +72,6 @@ static void f_luaopen (lua_State *L, void *ud) {
72 UNUSED(ud); 72 UNUSED(ud);
73 stack_init(L, L); /* init stack */ 73 stack_init(L, L); /* init stack */
74 sethvalue(L, gt(L), luaH_new(L, 0, 20)); /* table of globals */ 74 sethvalue(L, gt(L), luaH_new(L, 0, 20)); /* table of globals */
75 hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */
76 sethvalue(L, registry(L), luaH_new(L, 6, 20)); /* registry */ 75 sethvalue(L, registry(L), luaH_new(L, 6, 20)); /* registry */
77 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 76 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
78 luaT_init(L); 77 luaT_init(L);
diff --git a/lua.c b/lua.c
index b25e1c1f..40da9b16 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.145 2005/06/03 20:16:16 roberto Exp roberto $ 2** $Id: lua.c,v 1.146 2005/06/28 13:01:50 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*/
@@ -48,7 +48,6 @@ static void print_usage (void) {
48 " -i enter interactive mode after executing " LUA_QL("script") "\n" 48 " -i enter interactive mode after executing " LUA_QL("script") "\n"
49 " -l name require library " LUA_QL("name") "\n" 49 " -l name require library " LUA_QL("name") "\n"
50 " -v show version information\n" 50 " -v show version information\n"
51 " -w trap access to undefined globals\n"
52 " -- stop handling options\n" , 51 " -- stop handling options\n" ,
53 progname); 52 progname);
54} 53}
@@ -223,14 +222,6 @@ static void dotty (lua_State *L) {
223} 222}
224 223
225 224
226static int checkvar (lua_State *L) {
227 const char *name = lua_tostring(L, 2);
228 if (name)
229 luaL_error(L, "attempt to access undefined variable " LUA_QS, name);
230 return 0;
231}
232
233
234#define clearinteractive(i) (*i &= 2) 225#define clearinteractive(i) (*i &= 2)
235 226
236static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) { 227static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
@@ -268,13 +259,6 @@ static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
268 print_version(); 259 print_version();
269 break; 260 break;
270 } 261 }
271 case 'w': {
272 if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
273 lua_pushcfunction(L, checkvar);
274 lua_setfield(L, -2, "__index");
275 }
276 break;
277 }
278 case 'e': { 262 case 'e': {
279 const char *chunk = argv[i] + 2; 263 const char *chunk = argv[i] + 2;
280 clearinteractive(interactive); 264 clearinteractive(interactive);