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