| 476 | |
| 477 | |
| 478 | static void findloader (lua_State *L, const char *name) { |
| 479 | int i; |
| 480 | luaL_Buffer msg; /* to build error message */ |
| 481 | luaL_buffinit(L, &msg); |
| 482 | lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */ |
| 483 | if (!lua_istable(L, 3)) |
| 484 | luaL_error(L, LUA_QL("package.searchers") " must be a table"); |
| 485 | /* iterate over available searchers to find a loader */ |
| 486 | for (i = 1; ; i++) { |
| 487 | lua_rawgeti(L, 3, i); /* get a searcher */ |
| 488 | if (lua_isnil(L, -1)) { /* no more searchers? */ |
| 489 | lua_pop(L, 1); /* remove nil */ |
| 490 | luaL_pushresult(&msg); /* create error message */ |
| 491 | luaL_error(L, "module " LUA_QS " not found:%s", |
| 492 | name, lua_tostring(L, -1)); |
| 493 | } |
| 494 | lua_pushstring(L, name); |
| 495 | lua_call(L, 1, 2); /* call it */ |
| 496 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
| 497 | return; /* module loader found */ |
| 498 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 499 | lua_pop(L, 1); /* remove extra return */ |
| 500 | luaL_addvalue(&msg); /* concatenate error message */ |
| 501 | } |
| 502 | else |
| 503 | lua_pop(L, 2); /* remove both returns */ |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | |
| 508 | static int ll_require (lua_State *L) { |
no test coverage detected