aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-16 11:51:36 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-16 11:51:36 -0300
commitc229ed597f939eacfe1e9b7113e2a082fe93a3ae (patch)
treeee78d0fbdb485b54099820da90e97ab24a881c56 /lauxlib.c
parent16b41105215e3cad719ffb121caca0d065e05b6e (diff)
downloadlua-c229ed597f939eacfe1e9b7113e2a082fe93a3ae.tar.gz
lua-c229ed597f939eacfe1e9b7113e2a082fe93a3ae.tar.bz2
lua-c229ed597f939eacfe1e9b7113e2a082fe93a3ae.zip
'requiref' checks 'package.loaded' before loading a module
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 0ad4cc59..563362e5 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -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*/
891LUALIB_API void luaL_requiref (lua_State *L, const char *modname, 891LUALIB_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}