static int qt_require(lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_settop(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); // index 2 lua_getfield(L, 2, name); if (lua_toboolean(L, -1)) return 1; const char *filename = pushfilename(L, name); // index 3 QLibrary library(QString::fromLocal8Bit(filename)); if (! library.load()) luaL_error(L, "cannot load " LUA_QS, filename); lua_pushfstring(L, "luaopen_%s", name); // index 4 lua_CFunction func = (lua_CFunction)library.resolve(lua_tostring(L, -1)); if (! func) luaL_error(L, "no symbol " LUA_QS " in module " LUA_QS, lua_tostring(L, -1), filename); lua_pushboolean(L, 1); lua_setfield(L, 2, name); lua_pushcfunction(L, func); lua_pushstring(L, name); lua_call(L, 1, 1); if (! lua_isnil(L, -1)) lua_setfield(L, 2, name); lua_getfield(L, 2, name); return 1; }
static int path_require(lua_State *L) { const char *filename; lua_CFunction func; void *handle; const char *name = luaL_checkstring(L, 1); lua_settop(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); /* index 2 */ lua_getfield(L, 2, name); if (lua_toboolean(L, -1)) return 1; filename = pushfilename(L, name); /* index 3 */ LL_LOAD(handle, filename); if (! handle) luaL_error(L, "cannot load " LUA_QS, filename); lua_pushfstring(L, "luaopen_%s", name); /* index 4 */ func = (lua_CFunction)LL_SYM(handle, lua_tostring(L, -1)); if (! func) luaL_error(L, "no symbol " LUA_QS " in module " LUA_QS, lua_tostring(L, -1), filename); lua_pushboolean(L, 1); lua_setfield(L, 2, name); lua_pushcfunction(L, func); lua_pushstring(L, name); lua_call(L, 1, 1); if (! lua_isnil(L, -1)) lua_setfield(L, 2, name); lua_getfield(L, 2, name); return 1; }