diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-07-02 08:38:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-07-02 08:38:13 -0300 |
commit | 7192afafeeb1a96b3de60af90a72cd8762b09d94 (patch) | |
tree | d1e7e061822f755c33cc497d98ea451c7e7e32e8 /lauxlib.c | |
parent | a139e2e003e0b62b7d34eeda20dd2354e74885f9 (diff) | |
download | lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.tar.gz lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.tar.bz2 lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.zip |
new module policy: C modules do not create globals and do not register
themselves with 'require' (let 'require' do its work); new auxiliary
functions luaL_newlib/luaL_newlibtable/luaL_setfuncs/luaL_requiref.
Old luaL_register will be deprecated.
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 45 |
1 files changed, 41 insertions, 4 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.215 2010/06/09 17:53:59 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.216 2010/06/30 17:40:27 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 | */ |
@@ -729,20 +729,33 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | |||
729 | luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ | 729 | luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ |
730 | lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ | 730 | lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ |
731 | } | 731 | } |
732 | luaL_setfuncs(L, l, nup); | ||
733 | } | ||
734 | |||
735 | /* }====================================================== */ | ||
736 | |||
737 | /* | ||
738 | ** set functions from list 'l' into table at top - 'nup'; each | ||
739 | ** function gets the 'nup' elements at the top as upvalues. | ||
740 | ** Returns with only the table at the stack. | ||
741 | */ | ||
742 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { | ||
732 | luaL_checkstack(L, nup, "too many upvalues"); | 743 | luaL_checkstack(L, nup, "too many upvalues"); |
733 | for (; l && l->name; l++) { /* fill the table with given functions */ | 744 | for (; l && l->name; l++) { /* fill the table with given functions */ |
734 | int i; | 745 | int i; |
735 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | 746 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ |
736 | lua_pushvalue(L, -nup); | 747 | lua_pushvalue(L, -nup); |
737 | lua_pushcclosure(L, l->func, nup); | 748 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ |
738 | lua_setfield(L, -(nup + 2), l->name); | 749 | lua_setfield(L, -(nup + 2), l->name); |
739 | } | 750 | } |
740 | lua_pop(L, nup); /* remove upvalues */ | 751 | lua_pop(L, nup); /* remove upvalues */ |
741 | } | 752 | } |
742 | 753 | ||
743 | /* }====================================================== */ | ||
744 | |||
745 | 754 | ||
755 | /* | ||
756 | ** ensure that stack[idx][fname] has a table and push that table | ||
757 | ** into the stack | ||
758 | */ | ||
746 | LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) { | 759 | LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) { |
747 | lua_getfield(L, idx, fname); | 760 | lua_getfield(L, idx, fname); |
748 | if (lua_istable(L, -1)) return; /* table already there */ | 761 | if (lua_istable(L, -1)) return; /* table already there */ |
@@ -756,6 +769,30 @@ LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) { | |||
756 | } | 769 | } |
757 | 770 | ||
758 | 771 | ||
772 | /* | ||
773 | ** stripped-down 'require'. Calls 'openf' to open a module, | ||
774 | ** registers the result in 'package.loaded' table and, if 'glb' | ||
775 | ** is true, also registers the result in the global table. | ||
776 | ** Leaves resulting module on the top. | ||
777 | */ | ||
778 | LUALIB_API void luaL_requiref (lua_State *L, const char *modname, | ||
779 | lua_CFunction openf, int glb) { | ||
780 | lua_pushcfunction(L, openf); | ||
781 | lua_pushstring(L, modname); /* argument to open function */ | ||
782 | lua_call(L, 1, 1); /* open module */ | ||
783 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
784 | lua_pushvalue(L, -2); /* make copy of module (call result) */ | ||
785 | lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ | ||
786 | lua_pop(L, 1); /* remove _LOADED table */ | ||
787 | if (glb) { | ||
788 | lua_pushglobaltable(L); | ||
789 | lua_pushvalue(L, -2); /* copy of 'mod' */ | ||
790 | lua_setfield(L, -2, modname); /* _G[modname] = module */ | ||
791 | lua_pop(L, 1); /* remove _G table */ | ||
792 | } | ||
793 | } | ||
794 | |||
795 | |||
759 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | 796 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, |
760 | const char *r) { | 797 | const char *r) { |
761 | const char *wild; | 798 | const char *wild; |