aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/loadlib.c b/loadlib.c
index a6ce30d4..4cf9aec3 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -576,9 +576,14 @@ static int searcher_Croot (lua_State *L) {
576static int searcher_preload (lua_State *L) { 576static int searcher_preload (lua_State *L) {
577 const char *name = luaL_checkstring(L, 1); 577 const char *name = luaL_checkstring(L, 1);
578 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 578 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
579 if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ 579 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
580 lua_pushfstring(L, "\n\tno field package.preload['%s']", name); 580 lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
581 return 1; 581 return 1;
582 }
583 else {
584 lua_pushliteral(L, ":preload:");
585 return 2;
586 }
582} 587}
583 588
584 589
@@ -620,17 +625,23 @@ static int ll_require (lua_State *L) {
620 /* else must load package */ 625 /* else must load package */
621 lua_pop(L, 1); /* remove 'getfield' result */ 626 lua_pop(L, 1); /* remove 'getfield' result */
622 findloader(L, name); 627 findloader(L, name);
623 lua_pushstring(L, name); /* pass name as argument to module loader */ 628 lua_rotate(L, -2, 1); /* function <-> loader data */
624 lua_insert(L, -2); /* name is 1st argument (before search data) */ 629 lua_pushvalue(L, 1); /* name is 1st argument to module loader */
630 lua_pushvalue(L, -3); /* loader data is 2nd argument */
631 /* stack: ...; loader data; loader function; mod. name; loader data */
625 lua_call(L, 2, 1); /* run loader to load module */ 632 lua_call(L, 2, 1); /* run loader to load module */
633 /* stack: ...; loader data; result from loader */
626 if (!lua_isnil(L, -1)) /* non-nil return? */ 634 if (!lua_isnil(L, -1)) /* non-nil return? */
627 lua_setfield(L, 2, name); /* LOADED[name] = returned value */ 635 lua_setfield(L, 2, name); /* LOADED[name] = returned value */
636 else
637 lua_pop(L, 1); /* pop nil */
628 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ 638 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
629 lua_pushboolean(L, 1); /* use true as result */ 639 lua_pushboolean(L, 1); /* use true as result */
630 lua_pushvalue(L, -1); /* extra copy to be returned */ 640 lua_copy(L, -1, -2); /* replace loader result */
631 lua_setfield(L, 2, name); /* LOADED[name] = true */ 641 lua_setfield(L, 2, name); /* LOADED[name] = true */
632 } 642 }
633 return 1; 643 lua_rotate(L, -2, 1); /* loader data <-> module result */
644 return 2; /* return module result and loader data */
634} 645}
635 646
636/* }====================================================== */ 647/* }====================================================== */