| 695 | |
| 696 | |
| 697 | static int ll_require (lua_State *L) { |
| 698 | const char *name = luaL_checkstring(L, 1); |
| 699 | lua_settop(L, 1); /* LOADED table will be at index 2 */ |
| 700 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
| 701 | lua_getfield(L, 2, name); /* LOADED[name] */ |
| 702 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 703 | return 1; /* package is already loaded */ |
| 704 | /* else must load package */ |
| 705 | lua_pop(L, 1); /* remove 'getfield' result */ |
| 706 | findloader(L, name); |
| 707 | lua_rotate(L, -2, 1); /* function <-> loader data */ |
| 708 | lua_pushvalue(L, 1); /* name is 1st argument to module loader */ |
| 709 | lua_pushvalue(L, -3); /* loader data is 2nd argument */ |
| 710 | /* stack: ...; loader data; loader function; mod. name; loader data */ |
| 711 | lua_call(L, 2, 1); /* run loader to load module */ |
| 712 | /* stack: ...; loader data; result from loader */ |
| 713 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 714 | lua_setfield(L, 2, name); /* LOADED[name] = returned value */ |
| 715 | else |
| 716 | lua_pop(L, 1); /* pop nil */ |
| 717 | if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ |
| 718 | lua_pushboolean(L, 1); /* use true as result */ |
| 719 | lua_copy(L, -1, -2); /* replace loader result */ |
| 720 | lua_setfield(L, 2, name); /* LOADED[name] = true */ |
| 721 | } |
| 722 | lua_rotate(L, -2, 1); /* loader data <-> module result */ |
| 723 | return 2; /* return module result and loader data */ |
| 724 | } |
| 725 | |
| 726 | /* }====================================================== */ |
| 727 |
nothing calls this directly
no test coverage detected