diff options
-rw-r--r-- | lauxlib.c | 26 | ||||
-rw-r--r-- | ltests.c | 9 |
2 files changed, 22 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.263 2014/05/12 21:44:17 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.264 2014/06/26 17:25:11 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 | */ |
@@ -883,22 +883,26 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { | |||
883 | 883 | ||
884 | 884 | ||
885 | /* | 885 | /* |
886 | ** stripped-down 'require'. Calls 'openf' to open a module, | 886 | ** Stripped-down 'require': After checking "loaded" table, calls 'openf' |
887 | ** registers the result in 'package.loaded' table and, if 'glb' | 887 | ** to open a module, registers the result in 'package.loaded' table and, |
888 | ** is true, also registers the result in the global table. | 888 | ** if 'glb' is true, also registers the result in the global table. |
889 | ** Leaves resulting module on the top. | 889 | ** Leaves resulting module on the top. |
890 | */ | 890 | */ |
891 | LUALIB_API void luaL_requiref (lua_State *L, const char *modname, | 891 | LUALIB_API void luaL_requiref (lua_State *L, const char *modname, |
892 | lua_CFunction openf, int glb) { | 892 | lua_CFunction openf, int glb) { |
893 | lua_pushcfunction(L, openf); | ||
894 | lua_pushstring(L, modname); /* argument to open function */ | ||
895 | lua_call(L, 1, 1); /* open module */ | ||
896 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); | 893 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); |
897 | lua_pushvalue(L, -2); /* make copy of module (call result) */ | 894 | lua_getfield(L, -1, modname); /* _LOADED[modname] */ |
898 | lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ | 895 | if (!lua_toboolean(L, -1)) { /* package not already loaded? */ |
899 | lua_pop(L, 1); /* remove _LOADED table */ | 896 | lua_pop(L, 1); /* remove field */ |
897 | lua_pushcfunction(L, openf); | ||
898 | lua_pushstring(L, modname); /* argument to open function */ | ||
899 | lua_call(L, 1, 1); /* call 'openf' to open module */ | ||
900 | lua_pushvalue(L, -1); /* make copy of module (call result) */ | ||
901 | lua_setfield(L, -3, modname); /* _LOADED[modname] = module */ | ||
902 | } | ||
903 | lua_remove(L, -2); /* remove _LOADED table */ | ||
900 | if (glb) { | 904 | if (glb) { |
901 | lua_pushvalue(L, -1); /* copy of 'mod' */ | 905 | lua_pushvalue(L, -1); /* copy of module */ |
902 | lua_setglobal(L, modname); /* _G[modname] = module */ | 906 | lua_setglobal(L, modname); /* _G[modname] = module */ |
903 | } | 907 | } |
904 | } | 908 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltests.c,v 2.173 2014/06/19 18:29:30 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.174 2014/06/26 17:25:11 roberto Exp roberto $ |
3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -839,7 +839,12 @@ static int loadlib (lua_State *L) { | |||
839 | }; | 839 | }; |
840 | lua_State *L1 = getstate(L); | 840 | lua_State *L1 = getstate(L); |
841 | int i; | 841 | int i; |
842 | luaL_requiref(L1, "package", luaopen_package, 1); | 842 | luaL_requiref(L1, "package", luaopen_package, 0); |
843 | lua_assert(lua_type(L1, -1) == LUA_TTABLE); | ||
844 | /* 'requiref' should not reload module already loaded... */ | ||
845 | luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */ | ||
846 | /* ...but should return the same module */ | ||
847 | lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); | ||
843 | luaL_getsubtable(L1, LUA_REGISTRYINDEX, "_PRELOAD"); | 848 | luaL_getsubtable(L1, LUA_REGISTRYINDEX, "_PRELOAD"); |
844 | for (i = 0; libs[i].name; i++) { | 849 | for (i = 0; libs[i].name; i++) { |
845 | lua_pushcfunction(L1, libs[i].func); | 850 | lua_pushcfunction(L1, libs[i].func); |