diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-04-26 16:38:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-04-26 16:38:52 -0300 |
| commit | ec22fc963a9c80d81724ccdd17e967995fc988c9 (patch) | |
| tree | 7f62bdd50d10c0aa085ddbf3a37a8376576ab643 | |
| parent | c54f5f64c95d73de8a4b66592e6f033469b07fe4 (diff) | |
| download | lua-ec22fc963a9c80d81724ccdd17e967995fc988c9.tar.gz lua-ec22fc963a9c80d81724ccdd17e967995fc988c9.tar.bz2 lua-ec22fc963a9c80d81724ccdd17e967995fc988c9.zip | |
detail (separate code to create 'searchers' table into a new function)
| -rw-r--r-- | loadlib.c | 27 |
1 files changed, 15 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.108 2011/12/12 16:34:03 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.109 2012/04/11 16:35:32 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 | ** |
| @@ -672,12 +672,22 @@ static const luaL_Reg ll_funcs[] = { | |||
| 672 | }; | 672 | }; |
| 673 | 673 | ||
| 674 | 674 | ||
| 675 | static const lua_CFunction searchers[] = | 675 | static void createsearcherstable (lua_State *L) { |
| 676 | {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; | 676 | static const lua_CFunction searchers[] = |
| 677 | {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; | ||
| 678 | int i; | ||
| 679 | /* create 'searchers' table */ | ||
| 680 | lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); | ||
| 681 | /* fill it with pre-defined searchers */ | ||
| 682 | for (i=0; searchers[i] != NULL; i++) { | ||
| 683 | lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ | ||
| 684 | lua_pushcclosure(L, searchers[i], 1); | ||
| 685 | lua_rawseti(L, -2, i+1); | ||
| 686 | } | ||
| 687 | } | ||
| 677 | 688 | ||
| 678 | 689 | ||
| 679 | LUAMOD_API int luaopen_package (lua_State *L) { | 690 | LUAMOD_API int luaopen_package (lua_State *L) { |
| 680 | int i; | ||
| 681 | /* create table CLIBS to keep track of loaded C libraries */ | 691 | /* create table CLIBS to keep track of loaded C libraries */ |
| 682 | luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); | 692 | luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); |
| 683 | lua_createtable(L, 0, 1); /* metatable for CLIBS */ | 693 | lua_createtable(L, 0, 1); /* metatable for CLIBS */ |
| @@ -686,14 +696,7 @@ LUAMOD_API int luaopen_package (lua_State *L) { | |||
| 686 | lua_setmetatable(L, -2); | 696 | lua_setmetatable(L, -2); |
| 687 | /* create `package' table */ | 697 | /* create `package' table */ |
| 688 | luaL_newlib(L, pk_funcs); | 698 | luaL_newlib(L, pk_funcs); |
| 689 | /* create 'searchers' table */ | 699 | createsearcherstable(L); |
| 690 | lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); | ||
| 691 | /* fill it with pre-defined searchers */ | ||
| 692 | for (i=0; searchers[i] != NULL; i++) { | ||
| 693 | lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ | ||
| 694 | lua_pushcclosure(L, searchers[i], 1); | ||
| 695 | lua_rawseti(L, -2, i+1); | ||
| 696 | } | ||
| 697 | #if defined(LUA_COMPAT_LOADERS) | 700 | #if defined(LUA_COMPAT_LOADERS) |
| 698 | lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ | 701 | lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ |
| 699 | lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ | 702 | lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ |
