summaryrefslogtreecommitdiff
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
parentbeec5af2010ad0df9d95b0aaa4842ded1ac60d8a (diff)
downloadlua-b2aa2ba046502bdcdfa3de4af898810667c1843a.tar.gz
lua-b2aa2ba046502bdcdfa3de4af898810667c1843a.tar.bz2
lua-b2aa2ba046502bdcdfa3de4af898810667c1843a.zip
using constants for "_LOADED" and "PRELOAD"
-rw-r--r--lauxlib.c23
-rw-r--r--lauxlib.h10
-rw-r--r--linit.c6
-rw-r--r--loadlib.c18
-rw-r--r--ltests.c4
5 files changed, 34 insertions, 27 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 */
diff --git a/lauxlib.h b/lauxlib.h
index e0072a43..6238f5ce 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.129 2015/11/23 11:29:43 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*/
@@ -20,6 +20,14 @@
20#define LUA_ERRFILE (LUA_ERRERR+1) 20#define LUA_ERRFILE (LUA_ERRERR+1)
21 21
22 22
23/* key, in the registry, for table of loaded modules */
24#define LUA_LOADED_TABLE "_LOADED"
25
26
27/* key, in the registry, for table of preloaded loaders */
28#define LUA_PRELOAD_TABLE "_PRELOAD"
29
30
23typedef struct luaL_Reg { 31typedef struct luaL_Reg {
24 const char *name; 32 const char *name;
25 lua_CFunction func; 33 lua_CFunction func;
diff --git a/linit.c b/linit.c
index b2142da5..897ae352 100644
--- a/linit.c
+++ b/linit.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: linit.c,v 1.37 2014/12/09 15:00:17 roberto Exp roberto $ 2** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp roberto $
3** Initialization of libraries for lua.c and other clients 3** Initialization of libraries for lua.c and other clients
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,10 +18,10 @@
18** open the library, which is already linked to the application. 18** open the library, which is already linked to the application.
19** For that, do the following code: 19** For that, do the following code:
20** 20**
21** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 21** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
22** lua_pushcfunction(L, luaopen_modname); 22** lua_pushcfunction(L, luaopen_modname);
23** lua_setfield(L, -2, modname); 23** lua_setfield(L, -2, modname);
24** lua_pop(L, 1); // remove _PRELOAD table 24** lua_pop(L, 1); // remove PRELOAD table
25*/ 25*/
26 26
27#include "lprefix.h" 27#include "lprefix.h"
diff --git a/loadlib.c b/loadlib.c
index 24910351..16526603 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.127 2015/11/23 11:30:45 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.128 2016/07/18 17:55:59 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -471,7 +471,7 @@ static int searcher_Croot (lua_State *L) {
471 471
472static int searcher_preload (lua_State *L) { 472static int searcher_preload (lua_State *L) {
473 const char *name = luaL_checkstring(L, 1); 473 const char *name = luaL_checkstring(L, 1);
474 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); 474 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
475 if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ 475 if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
476 lua_pushfstring(L, "\n\tno field package.preload['%s']", name); 476 lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
477 return 1; 477 return 1;
@@ -508,9 +508,9 @@ static void findloader (lua_State *L, const char *name) {
508 508
509static int ll_require (lua_State *L) { 509static int ll_require (lua_State *L) {
510 const char *name = luaL_checkstring(L, 1); 510 const char *name = luaL_checkstring(L, 1);
511 lua_settop(L, 1); /* _LOADED table will be at index 2 */ 511 lua_settop(L, 1); /* LOADED table will be at index 2 */
512 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 512 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
513 lua_getfield(L, 2, name); /* _LOADED[name] */ 513 lua_getfield(L, 2, name); /* LOADED[name] */
514 if (lua_toboolean(L, -1)) /* is it there? */ 514 if (lua_toboolean(L, -1)) /* is it there? */
515 return 1; /* package is already loaded */ 515 return 1; /* package is already loaded */
516 /* else must load package */ 516 /* else must load package */
@@ -520,11 +520,11 @@ static int ll_require (lua_State *L) {
520 lua_insert(L, -2); /* name is 1st argument (before search data) */ 520 lua_insert(L, -2); /* name is 1st argument (before search data) */
521 lua_call(L, 2, 1); /* run loader to load module */ 521 lua_call(L, 2, 1); /* run loader to load module */
522 if (!lua_isnil(L, -1)) /* non-nil return? */ 522 if (!lua_isnil(L, -1)) /* non-nil return? */
523 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ 523 lua_setfield(L, 2, name); /* LOADED[name] = returned value */
524 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ 524 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
525 lua_pushboolean(L, 1); /* use true as result */ 525 lua_pushboolean(L, 1); /* use true as result */
526 lua_pushvalue(L, -1); /* extra copy to be returned */ 526 lua_pushvalue(L, -1); /* extra copy to be returned */
527 lua_setfield(L, 2, name); /* _LOADED[name] = true */ 527 lua_setfield(L, 2, name); /* LOADED[name] = true */
528 } 528 }
529 return 1; 529 return 1;
530} 530}
@@ -689,10 +689,10 @@ LUAMOD_API int luaopen_package (lua_State *L) {
689 LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); 689 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
690 lua_setfield(L, -2, "config"); 690 lua_setfield(L, -2, "config");
691 /* set field 'loaded' */ 691 /* set field 'loaded' */
692 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); 692 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
693 lua_setfield(L, -2, "loaded"); 693 lua_setfield(L, -2, "loaded");
694 /* set field 'preload' */ 694 /* set field 'preload' */
695 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 695 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
696 lua_setfield(L, -2, "preload"); 696 lua_setfield(L, -2, "preload");
697 lua_pushglobaltable(L); 697 lua_pushglobaltable(L);
698 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ 698 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
diff --git a/ltests.c b/ltests.c
index e796fac3..6dba514a 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.209 2015/10/12 16:38:19 roberto Exp roberto $ 2** $Id: ltests.c,v 2.210 2016/11/07 12:38:35 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*/
@@ -869,7 +869,7 @@ static int loadlib (lua_State *L) {
869 luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */ 869 luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */
870 /* ...but should return the same module */ 870 /* ...but should return the same module */
871 lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); 871 lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
872 luaL_getsubtable(L1, LUA_REGISTRYINDEX, "_PRELOAD"); 872 luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
873 for (i = 0; libs[i].name; i++) { 873 for (i = 0; libs[i].name; i++) {
874 lua_pushcfunction(L1, libs[i].func); 874 lua_pushcfunction(L1, libs[i].func);
875 lua_setfield(L1, -2, libs[i].name); 875 lua_setfield(L1, -2, libs[i].name);