Browse Source

details (using lua_setglobal/lua_getglobal instead of explicit
use of the global table)

Roberto Ierusalimschy 13 years ago
parent
commit
4cca1a436d
2 changed files with 8 additions and 15 deletions
  1. 3 5
      lauxlib.c
  2. 5 10
      lua.c

+ 3 - 5
lauxlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.c,v 1.241 2012/03/18 16:52:49 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.242 2012/03/19 22:57:14 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -892,10 +892,8 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
   lua_setfield(L, -2, modname);  /* _LOADED[modname] = module */
   lua_pop(L, 1);  /* remove _LOADED table */
   if (glb) {
-    lua_pushglobaltable(L);
-    lua_pushvalue(L, -2);  /* copy of 'mod' */
-    lua_setfield(L, -2, modname);  /* _G[modname] = module */
-    lua_pop(L, 1);  /* remove _G table */
+    lua_pushvalue(L, -1);  /* copy of 'mod' */
+    lua_setglobal(L, modname);  /* _G[modname] = module */
   }
 }
 

+ 5 - 10
lua.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.c,v 1.202 2011/08/17 20:19:52 roberto Exp roberto $
+** $Id: lua.c,v 1.203 2011/12/12 16:34:03 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 */
@@ -223,16 +223,11 @@ static int dostring (lua_State *L, const char *s, const char *name) {
 
 static int dolibrary (lua_State *L, const char *name) {
   int status;
-  lua_pushglobaltable(L);
-  lua_getfield(L, -1, "require");
+  lua_getglobal(L, "require");
   lua_pushstring(L, name);
-  status = docall(L, 1, 1);
-  if (status == LUA_OK) {
-    lua_setfield(L, -2, name);  /* global[name] = require return */
-    lua_pop(L, 1);  /* remove global table */
-  }
-  else
-    lua_remove(L, -2);  /* remove global table (below error msg.) */
+  status = docall(L, 1, 1);  /* call 'require(name)' */
+  if (status == LUA_OK)
+    lua_setglobal(L, name);  /* global[name] = require return */
   return report(L, status);
 }