aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-12-04 18:17:24 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-12-04 18:17:24 -0200
commitb2aa2ba046502bdcdfa3de4af898810667c1843a (patch)
tree9d9d4ad6c8e2040996fa4147d59fcb79d1a6d777 /lauxlib.c
parentbeec5af2010ad0df9d95b0aaa4842ded1ac60d8a (diff)
downloadlua-b2aa2ba046502bdcdfa3de4af898810667c1843a.tar.gz
lua-b2aa2ba046502bdcdfa3de4af898810667c1843a.tar.bz2
lua-b2aa2ba046502bdcdfa3de4af898810667c1843a.zip
using constants for "_LOADED" and "PRELOAD"
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/lauxlib.c b/lauxlib.c
index e951d83d..35ad7846 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.286 2016/01/08 15:33:09 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.287 2016/12/04 20:09:45 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*/
@@ -69,12 +69,11 @@ static int findfield (lua_State *L, int objidx, int level) {
69 69
70/* 70/*
71** Search for a name for a function in all loaded modules 71** Search for a name for a function in all loaded modules
72** (registry._LOADED).
73*/ 72*/
74static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { 73static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
75 int top = lua_gettop(L); 74 int top = lua_gettop(L);
76 lua_getinfo(L, "f", ar); /* push function */ 75 lua_getinfo(L, "f", ar); /* push function */
77 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 76 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
78 if (findfield(L, top + 1, 2)) { 77 if (findfield(L, top + 1, 2)) {
79 const char *name = lua_tostring(L, -1); 78 const char *name = lua_tostring(L, -1);
80 if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ 79 if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
@@ -891,23 +890,23 @@ static int libsize (const luaL_Reg *l) {
891 890
892/* 891/*
893** Find or create a module table with a given name. The function 892** Find or create a module table with a given name. The function
894** first looks at the _LOADED table and, if that fails, try a 893** first looks at the LOADED table and, if that fails, try a
895** global variable with that name. In any case, leaves on the stack 894** global variable with that name. In any case, leaves on the stack
896** the module table. 895** the module table.
897*/ 896*/
898LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, 897LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
899 int sizehint) { 898 int sizehint) {
900 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ 899 luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1);
901 if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */ 900 if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */
902 lua_pop(L, 1); /* remove previous result */ 901 lua_pop(L, 1); /* remove previous result */
903 /* try global variable (and create one if it does not exist) */ 902 /* try global variable (and create one if it does not exist) */
904 lua_pushglobaltable(L); 903 lua_pushglobaltable(L);
905 if (luaL_findtable(L, 0, modname, sizehint) != NULL) 904 if (luaL_findtable(L, 0, modname, sizehint) != NULL)
906 luaL_error(L, "name conflict for module '%s'", modname); 905 luaL_error(L, "name conflict for module '%s'", modname);
907 lua_pushvalue(L, -1); 906 lua_pushvalue(L, -1);
908 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ 907 lua_setfield(L, -3, modname); /* LOADED[modname] = new table */
909 } 908 }
910 lua_remove(L, -2); /* remove _LOADED table */ 909 lua_remove(L, -2); /* remove LOADED table */
911} 910}
912 911
913 912
@@ -971,17 +970,17 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
971*/ 970*/
972LUALIB_API void luaL_requiref (lua_State *L, const char *modname, 971LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
973 lua_CFunction openf, int glb) { 972 lua_CFunction openf, int glb) {
974 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); 973 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
975 lua_getfield(L, -1, modname); /* _LOADED[modname] */ 974 lua_getfield(L, -1, modname); /* LOADED[modname] */
976 if (!lua_toboolean(L, -1)) { /* package not already loaded? */ 975 if (!lua_toboolean(L, -1)) { /* package not already loaded? */
977 lua_pop(L, 1); /* remove field */ 976 lua_pop(L, 1); /* remove field */
978 lua_pushcfunction(L, openf); 977 lua_pushcfunction(L, openf);
979 lua_pushstring(L, modname); /* argument to open function */ 978 lua_pushstring(L, modname); /* argument to open function */
980 lua_call(L, 1, 1); /* call 'openf' to open module */ 979 lua_call(L, 1, 1); /* call 'openf' to open module */
981 lua_pushvalue(L, -1); /* make copy of module (call result) */ 980 lua_pushvalue(L, -1); /* make copy of module (call result) */
982 lua_setfield(L, -3, modname); /* _LOADED[modname] = module */ 981 lua_setfield(L, -3, modname); /* LOADED[modname] = module */
983 } 982 }
984 lua_remove(L, -2); /* remove _LOADED table */ 983 lua_remove(L, -2); /* remove LOADED table */
985 if (glb) { 984 if (glb) {
986 lua_pushvalue(L, -1); /* copy of module */ 985 lua_pushvalue(L, -1); /* copy of module */
987 lua_setglobal(L, modname); /* _G[modname] = module */ 986 lua_setglobal(L, modname); /* _G[modname] = module */