| 567 | |
| 568 | |
| 569 | static void findloader (lua_State *L, const char *name) { |
| 570 | int i; |
| 571 | luaL_Buffer msg; /* to build error message */ |
| 572 | luaL_buffinit(L, &msg); |
| 573 | /* push 'package.searchers' to index 3 in the stack */ |
| 574 | if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) |
| 575 | luaL_error(L, "'package.searchers' must be a table"); |
| 576 | /* iterate over available searchers to find a loader */ |
| 577 | for (i = 1; ; i++) { |
| 578 | if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ |
| 579 | lua_pop(L, 1); /* remove nil */ |
| 580 | luaL_pushresult(&msg); /* create error message */ |
| 581 | luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); |
| 582 | } |
| 583 | lua_pushstring(L, name); |
| 584 | lua_call(L, 1, 2); /* call it */ |
| 585 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
| 586 | return; /* module loader found */ |
| 587 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 588 | lua_pop(L, 1); /* remove extra return */ |
| 589 | luaL_addvalue(&msg); /* concatenate error message */ |
| 590 | } |
| 591 | else |
| 592 | lua_pop(L, 2); /* remove both returns */ |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | |
| 597 | static int ll_require (lua_State *L) { |
no test coverage detected