diff options
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 38 |
1 files changed, 17 insertions, 21 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.39 2005/08/17 19:05:04 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.40 2005/08/25 15:39:16 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 | ** |
@@ -511,15 +511,9 @@ static int ll_module (lua_State *L) { | |||
511 | lua_getfield(L, 2, modname); /* get _LOADED[modname] */ | 511 | lua_getfield(L, 2, modname); /* get _LOADED[modname] */ |
512 | if (!lua_istable(L, -1)) { /* not found? */ | 512 | if (!lua_istable(L, -1)) { /* not found? */ |
513 | lua_pop(L, 1); /* remove previous result */ | 513 | lua_pop(L, 1); /* remove previous result */ |
514 | luaL_getfield(L, LUA_GLOBALSINDEX, modname); /* try global variable */ | 514 | /* try global variable (and create one if it does not exist) */ |
515 | if (!lua_istable(L, -1)) { | 515 | if (luaL_findtable(L, LUA_GLOBALSINDEX, modname) != NULL) |
516 | if (!lua_isnil(L, -1)) | 516 | return luaL_error(L, "name conflict for module " LUA_QS, modname); |
517 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | ||
518 | lua_pop(L, 1); | ||
519 | lua_newtable(L); /* create it */ | ||
520 | lua_pushvalue(L, -1); /* register it with given name */ | ||
521 | luaL_setfield(L, LUA_GLOBALSINDEX, modname); | ||
522 | } | ||
523 | lua_pushvalue(L, -1); | 517 | lua_pushvalue(L, -1); |
524 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ | 518 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ |
525 | } | 519 | } |
@@ -573,6 +567,12 @@ static void setpath (lua_State *L, const char *fieldname, const char *envname, | |||
573 | } | 567 | } |
574 | 568 | ||
575 | 569 | ||
570 | static const luaL_reg pk_funcs[] = { | ||
571 | {"loadlib", ll_loadlib}, | ||
572 | {NULL, NULL} | ||
573 | }; | ||
574 | |||
575 | |||
576 | static const luaL_reg ll_funcs[] = { | 576 | static const luaL_reg ll_funcs[] = { |
577 | {"module", ll_module}, | 577 | {"module", ll_module}, |
578 | {"require", ll_require}, | 578 | {"require", ll_require}, |
@@ -591,9 +591,11 @@ LUALIB_API int luaopen_package (lua_State *L) { | |||
591 | lua_pushcfunction(L, gctm); | 591 | lua_pushcfunction(L, gctm); |
592 | lua_setfield(L, -2, "__gc"); | 592 | lua_setfield(L, -2, "__gc"); |
593 | /* create `package' table */ | 593 | /* create `package' table */ |
594 | lua_newtable(L); | 594 | luaL_register(L, LUA_LOADLIBNAME, pk_funcs); |
595 | lua_pushvalue(L, -1); | 595 | #if defined(LUA_COMPAT_LOADLIB) |
596 | lua_setglobal(L, LUA_LOADLIBNAME); | 596 | lua_getfield(L, -1, "loadlib"); |
597 | lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); | ||
598 | #endif | ||
597 | lua_pushvalue(L, -1); | 599 | lua_pushvalue(L, -1); |
598 | lua_setfield(L, LUA_REGISTRYINDEX, "_PACKAGE"); | 600 | lua_setfield(L, LUA_REGISTRYINDEX, "_PACKAGE"); |
599 | lua_pushvalue(L, -1); | 601 | lua_pushvalue(L, -1); |
@@ -618,15 +620,9 @@ LUALIB_API int luaopen_package (lua_State *L) { | |||
618 | /* set field `preload' */ | 620 | /* set field `preload' */ |
619 | lua_newtable(L); | 621 | lua_newtable(L); |
620 | lua_setfield(L, -2, "preload"); | 622 | lua_setfield(L, -2, "preload"); |
621 | /* create `loadlib' function */ | ||
622 | lua_pushcfunction(L, ll_loadlib); | ||
623 | #if defined(LUA_COMPAT_LOADLIB) | ||
624 | lua_pushvalue(L, -1); | ||
625 | lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); | ||
626 | #endif | ||
627 | lua_setfield(L, -2, "loadlib"); | ||
628 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 623 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
629 | luaL_register(L, NULL, ll_funcs); /* open lib into global table */ | 624 | luaL_register(L, NULL, ll_funcs); /* open lib into global table */ |
630 | return 1; | 625 | lua_pop(L, 1); |
626 | return 1; /* return 'package' table */ | ||
631 | } | 627 | } |
632 | 628 | ||