| 13217 | |
| 13218 | |
| 13219 | static int ll_require (lua_State *L) { |
| 13220 | const char *name = luaL_checkstring(L, 1); |
| 13221 | int i; |
| 13222 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 13223 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 13224 | lua_getfield(L, 2, name); |
| 13225 | if (lua_toboolean(L, -1)) { /* is it there? */ |
| 13226 | if (lua_touserdata(L, -1) == sentinel) /* check loops */ |
| 13227 | luaL_error(L, "loop or previous error loading module " LUA_QS, name); |
| 13228 | return 1; /* package is already loaded */ |
| 13229 | } |
| 13230 | /* else must load it; iterate over available loaders */ |
| 13231 | lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); |
| 13232 | if (!lua_istable(L, -1)) |
| 13233 | luaL_error(L, LUA_QL("package.loaders") " must be a table"); |
| 13234 | lua_pushliteral(L, ""); /* error message accumulator */ |
| 13235 | for (i=1; ; i++) { |
| 13236 | lua_rawgeti(L, -2, i); /* get a loader */ |
| 13237 | if (lua_isnil(L, -1)) |
| 13238 | luaL_error(L, "module " LUA_QS " not found:%s", |
| 13239 | name, lua_tostring(L, -2)); |
| 13240 | lua_pushstring(L, name); |
| 13241 | lua_call(L, 1, 1); /* call it */ |
| 13242 | if (lua_isfunction(L, -1)) /* did it find module? */ |
| 13243 | break; /* module loaded successfully */ |
| 13244 | else if (lua_isstring(L, -1)) /* loader returned error message? */ |
| 13245 | lua_concat(L, 2); /* accumulate it */ |
| 13246 | else |
| 13247 | lua_pop(L, 1); |
| 13248 | } |
| 13249 | lua_pushlightuserdata(L, sentinel); |
| 13250 | lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ |
| 13251 | lua_pushstring(L, name); /* pass name as argument to module */ |
| 13252 | lua_call(L, 1, 1); /* run loaded module */ |
| 13253 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 13254 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
| 13255 | lua_getfield(L, 2, name); |
| 13256 | if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ |
| 13257 | lua_pushboolean(L, 1); /* use true as result */ |
| 13258 | lua_pushvalue(L, -1); /* extra copy to be returned */ |
| 13259 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
| 13260 | } |
| 13261 | return 1; |
| 13262 | } |
| 13263 | |
| 13264 | /* }====================================================== */ |
| 13265 |
nothing calls this directly
no test coverage detected