MCPcopy Create free account
hub / github.com/RomanKubiak/ctrlr / ll_require

Function ll_require

Source/Misc/lua/src/lua.c:13219–13262  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 14

lua_settopFunction · 0.85
lua_getfieldFunction · 0.85
lua_tobooleanFunction · 0.85
lua_touserdataFunction · 0.85
luaL_errorFunction · 0.85
lua_rawgetiFunction · 0.85
lua_pushstringFunction · 0.85
lua_callFunction · 0.85
lua_isstringFunction · 0.85
lua_concatFunction · 0.85
lua_pushlightuserdataFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected