| 100 | static const int sentinel_ = 0; |
| 101 | #define sentinel ((void *)&sentinel_) |
| 102 | static int ll_require (lua_State *L) { |
| 103 | const char *name = luaL_checkstring(L, 1); |
| 104 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 105 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 106 | lua_getfield(L, 2, name); |
| 107 | if (lua_toboolean(L, -1)) { /* is it there? */ |
| 108 | if (lua_touserdata(L, -1) == sentinel) /* check loops */ |
| 109 | luaL_error(L, "loop or previous error loading module " LUA_QS, name); |
| 110 | return 1; /* package is already loaded */ |
| 111 | } |
| 112 | skr_load_file(L); |
| 113 | lua_pushlightuserdata(L, sentinel); |
| 114 | lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ |
| 115 | lua_pushstring(L, name); /* pass name as argument to module */ |
| 116 | lua_call(L, 1, 1); /* run loaded module */ |
| 117 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 118 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
| 119 | lua_getfield(L, 2, name); |
| 120 | if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ |
| 121 | lua_pushboolean(L, 1); /* use true as result */ |
| 122 | lua_pushvalue(L, -1); /* extra copy to be returned */ |
| 123 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
| 124 | } |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | static const luaL_Reg ll_funcs[] = { |
| 129 | {"require", ll_require}, |
nothing calls this directly
no test coverage detected