| 527 | |
| 528 | |
| 529 | static void findloader (lua_State *L, const char *name) { |
| 530 | int i; |
| 531 | luaL_Buffer msg; /* to build error message */ |
| 532 | luaL_buffinit(L, &msg); |
| 533 | /* push 'package.searchers' to index 3 in the stack */ |
| 534 | if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) |
| 535 | luaL_error(L, "'package.searchers' must be a table"); |
| 536 | /* iterate over available searchers to find a loader */ |
| 537 | for (i = 1; ; i++) { |
| 538 | if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ |
| 539 | lua_pop(L, 1); /* remove nil */ |
| 540 | luaL_pushresult(&msg); /* create error message */ |
| 541 | luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); |
| 542 | } |
| 543 | lua_pushstring(L, name); |
| 544 | lua_call(L, 1, 2); /* call it */ |
| 545 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
| 546 | return; /* module loader found */ |
| 547 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 548 | lua_pop(L, 1); /* remove extra return */ |
| 549 | luaL_addvalue(&msg); /* concatenate error message */ |
| 550 | } |
| 551 | else |
| 552 | lua_pop(L, 2); /* remove both returns */ |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | |
| 557 | static int ll_require (lua_State *L) { |
no test coverage detected