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