| 573 | #endif |
| 574 | |
| 575 | static void findloader (lua_State *L, const char *name) { |
| 576 | luaL_Buffer msg; /* to build error message */ |
| 577 | luaL_buffinit(L, &msg); |
| 578 | lua_pushstring(L, name); |
| 579 | #if defined(ARDUPILOT_BUILD) |
| 580 | // since we only use lua modules there's no point |
| 581 | // in searching for C or other types of modules |
| 582 | // so we just call the lua searcher |
| 583 | searcher_Lua(L); |
| 584 | if (lua_isfunction(L, -2)) { /* module loader found? */ |
| 585 | return; /* module loader already on stack */ |
| 586 | } else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 587 | lua_pop(L, 1); /* remove extra return */ |
| 588 | luaL_addvalue(&msg); /* concatenate error message */ |
| 589 | } else { |
| 590 | lua_pop(L, 2); /* remove both returns */ |
| 591 | } |
| 592 | // module not found |
| 593 | lua_pop(L, 1); /* remove nil */ |
| 594 | luaL_pushresult(&msg); /* create error message */ |
| 595 | luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); |
| 596 | #else |
| 597 | /* push 'package.searchers' to index 3 in the stack */ |
| 598 | if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) |
| 599 | luaL_error(L, "'package.searchers' must be a table"); |
| 600 | /* iterate over available searchers to find a loader */ |
| 601 | for (i = 1; ; i++) { |
| 602 | if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ |
| 603 | lua_pop(L, 1); /* remove nil */ |
| 604 | luaL_pushresult(&msg); /* create error message */ |
| 605 | luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); |
| 606 | } |
| 607 | lua_pushstring(L, name); |
| 608 | lua_call(L, 1, 2); /* call it */ |
| 609 | if (lua_isfunction(L, -2)) /* did it find a loader? */ |
| 610 | return; /* module loader found */ |
| 611 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */ |
| 612 | lua_pop(L, 1); /* remove extra return */ |
| 613 | luaL_addvalue(&msg); /* concatenate error message */ |
| 614 | } |
| 615 | else |
| 616 | lua_pop(L, 2); /* remove both returns */ |
| 617 | } |
| 618 | #endif |
| 619 | } |
| 620 | |
| 621 | |
| 622 | static int ll_require (lua_State *L) { |
no test coverage detected