Browse Source

no more '-w' option + new way to check module existence

Roberto Ierusalimschy 20 years ago
parent
commit
39cdbce23e
4 changed files with 32 additions and 45 deletions
  1. 15 14
      lauxlib.c
  2. 15 12
      loadlib.c
  3. 1 2
      lstate.c
  4. 1 17
      lua.c

+ 15 - 14
lauxlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.c,v 1.147 2005/08/18 16:04:05 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.148 2005/08/18 20:36:26 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -231,22 +231,23 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
                               const luaL_reg *l, int nup) {
                               const luaL_reg *l, int nup) {
   if (libname) {
   if (libname) {
     /* check whether lib already exists */
     /* check whether lib already exists */
-    luaL_getfield(L, LUA_GLOBALSINDEX, libname);
-    if (lua_isnil(L, -1)) {  /* not found? */
+    lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
+    lua_getfield(L, -1, libname);  /* get _LOADED[libname] */
+    if (!lua_istable(L, -1)) {  /* not found? */
       lua_pop(L, 1);  /* remove previous result */
       lua_pop(L, 1);  /* remove previous result */
-      lua_newtable(L);  /* create it */
-      if (lua_getmetatable(L, LUA_GLOBALSINDEX))
-        lua_setmetatable(L, -2);  /* share metatable with global table */
-      /* register it with given name */
+      luaL_getfield(L, LUA_GLOBALSINDEX, libname);  /* try global variable */
+      if (!lua_istable(L, -1)) {
+        if (!lua_isnil(L, -1))
+          luaL_error(L, "name conflict for module " LUA_QS, libname);
+        lua_pop(L, 1);
+        lua_newtable(L);  /* create it */
+        lua_pushvalue(L, -1);  /* register it with given name */
+        luaL_setfield(L, LUA_GLOBALSINDEX, libname);
+      }
       lua_pushvalue(L, -1);
       lua_pushvalue(L, -1);
-      luaL_setfield(L, LUA_GLOBALSINDEX, libname);
+      lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
     }
     }
-    else if (!lua_istable(L, -1))
-      luaL_error(L, "name conflict for library " LUA_QS, libname);
-    lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
-    lua_pushvalue(L, -2);
-    lua_setfield(L, -2, libname);  /* _LOADED[modname] = new table */
-    lua_pop(L, 1);  /* remove _LOADED table */
+    lua_remove(L, -2);  /* remove _LOADED table */
     lua_insert(L, -(nup+1));  /* move library table to below upvalues */
     lua_insert(L, -(nup+1));  /* move library table to below upvalues */
   }
   }
   for (; l->name; l++) {
   for (; l->name; l++) {

+ 15 - 12
loadlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: loadlib.c,v 1.38 2005/08/15 14:12:32 roberto Exp roberto $
+** $Id: loadlib.c,v 1.39 2005/08/17 19:05:04 roberto Exp roberto $
 ** Dynamic library loader for Lua
 ** Dynamic library loader for Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 **
 **
@@ -508,24 +508,29 @@ static int ll_module (lua_State *L) {
   const char *dot;
   const char *dot;
   lua_settop(L, 1);
   lua_settop(L, 1);
   lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
   lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
-  /* try to find given table */
-  luaL_getfield(L, LUA_GLOBALSINDEX, modname);
-  if (lua_isnil(L, -1)) {  /* not found? */
+  lua_getfield(L, 2, modname);  /* get _LOADED[modname] */
+  if (!lua_istable(L, -1)) {  /* not found? */
     lua_pop(L, 1);  /* remove previous result */
     lua_pop(L, 1);  /* remove previous result */
-    lua_newtable(L);  /* create it */
-    /* register it with given name */
+    luaL_getfield(L, LUA_GLOBALSINDEX, modname);  /* try global variable */
+    if (!lua_istable(L, -1)) {
+      if (!lua_isnil(L, -1))
+        return luaL_error(L, "name conflict for module " LUA_QS, modname);
+      lua_pop(L, 1);
+      lua_newtable(L);  /* create it */
+      lua_pushvalue(L, -1);  /* register it with given name */
+      luaL_setfield(L, LUA_GLOBALSINDEX, modname);
+    }
     lua_pushvalue(L, -1);
     lua_pushvalue(L, -1);
-    luaL_setfield(L, LUA_GLOBALSINDEX, modname);
+    lua_setfield(L, 2, modname);  /* _LOADED[modname] = new table */
   }
   }
-  else if (!lua_istable(L, -1))
-    return luaL_error(L, "name conflict for module " LUA_QS, modname);
   /* check whether table already has a _NAME field */
   /* check whether table already has a _NAME field */
   lua_getfield(L, -1, "_NAME");
   lua_getfield(L, -1, "_NAME");
   if (!lua_isnil(L, -1))  /* is table an initialized module? */
   if (!lua_isnil(L, -1))  /* is table an initialized module? */
     lua_pop(L, 1);
     lua_pop(L, 1);
   else {  /* no; initialize it */
   else {  /* no; initialize it */
     lua_pop(L, 1);
     lua_pop(L, 1);
-    lua_newtable(L);  /* create new metatable */
+    /* create new metatable */
+    lua_newtable(L);
     lua_pushvalue(L, LUA_GLOBALSINDEX);
     lua_pushvalue(L, LUA_GLOBALSINDEX);
     lua_setfield(L, -2, "__index");  /* mt.__index = _G */
     lua_setfield(L, -2, "__index");  /* mt.__index = _G */
     lua_setmetatable(L, -2);
     lua_setmetatable(L, -2);
@@ -540,8 +545,6 @@ static int ll_module (lua_State *L) {
     lua_pushlstring(L, modname, dot - modname);
     lua_pushlstring(L, modname, dot - modname);
     lua_setfield(L, -2, "_PACKAGE");
     lua_setfield(L, -2, "_PACKAGE");
   }
   }
-  lua_pushvalue(L, -1);
-  lua_setfield(L, 2, modname);  /* _LOADED[modname] = new table */
   setfenv(L);
   setfenv(L);
   return 0;
   return 0;
 }
 }

+ 1 - 2
lstate.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.c,v 2.31 2005/05/05 15:34:03 roberto Exp roberto $
+** $Id: lstate.c,v 2.32 2005/06/03 20:15:58 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -72,7 +72,6 @@ static void f_luaopen (lua_State *L, void *ud) {
   UNUSED(ud);
   UNUSED(ud);
   stack_init(L, L);  /* init stack */
   stack_init(L, L);  /* init stack */
   sethvalue(L, gt(L), luaH_new(L, 0, 20));  /* table of globals */
   sethvalue(L, gt(L), luaH_new(L, 0, 20));  /* table of globals */
-  hvalue(gt(L))->metatable = luaH_new(L, 0, 0);  /* globals metatable */
   sethvalue(L, registry(L), luaH_new(L, 6, 20));  /* registry */
   sethvalue(L, registry(L), luaH_new(L, 6, 20));  /* registry */
   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
   luaT_init(L);
   luaT_init(L);

+ 1 - 17
lua.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.c,v 1.145 2005/06/03 20:16:16 roberto Exp roberto $
+** $Id: lua.c,v 1.146 2005/06/28 13:01:50 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -48,7 +48,6 @@ static void print_usage (void) {
   "  -i       enter interactive mode after executing " LUA_QL("script") "\n"
   "  -i       enter interactive mode after executing " LUA_QL("script") "\n"
   "  -l name  require library " LUA_QL("name") "\n"
   "  -l name  require library " LUA_QL("name") "\n"
   "  -v       show version information\n"
   "  -v       show version information\n"
-  "  -w       trap access to undefined globals\n"
   "  --       stop handling options\n" ,
   "  --       stop handling options\n" ,
   progname);
   progname);
 }
 }
@@ -223,14 +222,6 @@ static void dotty (lua_State *L) {
 }
 }
 
 
 
 
-static int checkvar (lua_State *L) {
-  const char *name = lua_tostring(L, 2);
-  if (name)
-    luaL_error(L, "attempt to access undefined variable " LUA_QS, name);
-  return 0;
-}
-
-
 #define clearinteractive(i)	(*i &= 2)
 #define clearinteractive(i)	(*i &= 2)
 
 
 static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
 static 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) {
           print_version();
           print_version();
           break;
           break;
         }
         }
-        case 'w': {
-          if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
-            lua_pushcfunction(L, checkvar);
-            lua_setfield(L, -2, "__index");
-          }
-          break;
-        }
         case 'e': {
         case 'e': {
           const char *chunk = argv[i] + 2;
           const char *chunk = argv[i] + 2;
           clearinteractive(interactive);
           clearinteractive(interactive);