| 622 | |
| 623 | |
| 624 | static void findloader (lua_State *L, const char *name) { |
| 625 | int i; |
| 626 | luaL_Buffer msg; /* to build error message */ |
| 627 | /* push 'package.searchers' to index 3 in the stack */ |
| 628 | if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") |
| 629 | != LUA_TTABLE)) |
| 630 | luaL_error(L, "'package.searchers' must be a table"); |
| 631 | luaL_buffinit(L, &msg); |
| 632 | /* iterate over available searchers to find a loader */ |
| 633 | for (i = 1; ; i++) { |
| 634 | luaL_addstring(&msg, "\n\t"); /* error-message prefix */ |
| 635 | if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ |
| 636 | lua_pop(L, 1); /* remove nil */ |
| 637 | luaL_buffsub(&msg, 2); /* remove prefix */ |
| 638 | luaL_pushresult(&msg); /* create error message */ |
| 639 | luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); |
| 640 | } |
| 641 | lua_pushstring(L, name); |
| 642 | lua_call(L, 1, 2); /* call it */ |
| 643 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
| 644 | return; /* module loader found */ |
| 645 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 646 | lua_pop(L, 1); /* remove extra return */ |
| 647 | luaL_addvalue(&msg); /* concatenate error message */ |
| 648 | } |
| 649 | else { /* no error message */ |
| 650 | lua_pop(L, 2); /* remove both returns */ |
| 651 | luaL_buffsub(&msg, 2); /* remove prefix */ |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | |
| 657 | static int ll_require (lua_State *L) { |
no test coverage detected