diff --git a/celestia.pro b/celestia.pro index fc97f90b04..3a37cf1ee7 100644 --- a/celestia.pro +++ b/celestia.pro @@ -671,7 +671,7 @@ macx { LIBS += macosx/lib/cspice.a } -DEFINES += CELX LUA_VER=0x050100 +DEFINES += CELX LUA_VER=0x050200 # QMAKE_CXXFLAGS += -ffast-math @@ -746,4 +746,4 @@ unix { INSTALLS += target data textures lores_textures hires_textures \ flares models shaders fonts scripts locale extras extras-standard \ configuration desktop icon128 -} \ No newline at end of file +} diff --git a/configure.in b/configure.in index bd6b40e552..0b70581e35 100644 --- a/configure.in +++ b/configure.in @@ -384,13 +384,15 @@ AC_ARG_WITH([lua], if (test "$enable_lua" != "no"); then LUA_VER=0 + PKG_CHECK_MODULES(LUA, lua5.2 >= 5.2.0, LUA_VER=0x050200, [ + PKG_CHECK_MODULES(LUA, lua >= 5.2.0, LUA_VER=0x050200, [ PKG_CHECK_MODULES(LUA, lua5.1 >= 5.1.0, LUA_VER=0x050100, [ PKG_CHECK_MODULES(LUA, lua >= 5.1.0, LUA_VER=0x050100, [ PKG_CHECK_MODULES(LUA, lua50 >= 5.0.0 lua50 < 5.1.0, LUA_VER=0x050000, [ PKG_CHECK_MODULES(LUA, lua >= 5.0.0 lua < 5.1.0, LUA_VER=0x050000, AC_MSG_RESULT([no])) ] ) ] ) ] - ) + ) ] ) ] ) if (test "$LUA_VER" = "0x050000"); then PKG_CHECK_MODULES(LUALIB, lualib50 >= 5.0.0 lualib50 < 5.1.0, , [ PKG_CHECK_MODULES(LUALIB, lualib >= 5.0.0 lualib < 5.1.0, , AC_MSG_RESULT([no])) ] diff --git a/src/celephem/scriptorbit.cpp b/src/celephem/scriptorbit.cpp index e55020b52d..2584831ca3 100644 --- a/src/celephem/scriptorbit.cpp +++ b/src/celephem/scriptorbit.cpp @@ -74,8 +74,7 @@ ScriptedOrbit::initialize(const std::string& moduleName, if (!moduleName.empty()) { - lua_pushstring(luaState, "require"); - lua_gettable(luaState, LUA_GLOBALSINDEX); + lua_getglobal(luaState, "require"); if (!lua_isfunction(luaState, -1)) { clog << "Cannot load ScriptedOrbit package: 'require' function is unavailable\n"; @@ -93,8 +92,7 @@ ScriptedOrbit::initialize(const std::string& moduleName, } // Get the orbit generator function - lua_pushstring(luaState, funcName.c_str()); - lua_gettable(luaState, LUA_GLOBALSINDEX); + lua_getglobal(luaState, funcName.c_str()); if (lua_isfunction(luaState, -1) == 0) { @@ -132,9 +130,8 @@ ScriptedOrbit::initialize(const std::string& moduleName, luaOrbitObjectName = GenerateScriptObjectName(); // Attach the name to the script orbit - lua_pushstring(luaState, luaOrbitObjectName.c_str()); lua_pushvalue(luaState, -2); // dup the orbit object on top of stack - lua_settable(luaState, LUA_GLOBALSINDEX); + lua_setglobal(luaState, luaOrbitObjectName.c_str()); // Now, call orbit object methods to get the bounding radius // and valid time range. @@ -180,9 +177,7 @@ Vector3d ScriptedOrbit::computePosition(double tjd) const { Vector3d pos(Vector3d::Zero()); - - lua_pushstring(luaState, luaOrbitObjectName.c_str()); - lua_gettable(luaState, LUA_GLOBALSINDEX); + lua_getglobal(luaState, luaOrbitObjectName.c_str()); if (lua_istable(luaState, -1)) { lua_pushstring(luaState, "position"); diff --git a/src/celephem/scriptrotation.cpp b/src/celephem/scriptrotation.cpp index e69d0a5750..9d94c93e6f 100644 --- a/src/celephem/scriptrotation.cpp +++ b/src/celephem/scriptrotation.cpp @@ -74,8 +74,7 @@ ScriptedRotation::initialize(const std::string& moduleName, if (!moduleName.empty()) { - lua_pushstring(luaState, "require"); - lua_gettable(luaState, LUA_GLOBALSINDEX); + lua_getglobal(luaState, "require"); if (!lua_isfunction(luaState, -1)) { clog << "Cannot load ScriptedRotation package: 'require' function is unavailable\n"; @@ -93,8 +92,7 @@ ScriptedRotation::initialize(const std::string& moduleName, } // Get the rotation generator function - lua_pushstring(luaState, funcName.c_str()); - lua_gettable(luaState, LUA_GLOBALSINDEX); + lua_getglobal(luaState, funcName.c_str()); if (lua_isfunction(luaState, -1) == 0) { @@ -131,9 +129,8 @@ ScriptedRotation::initialize(const std::string& moduleName, luaRotationObjectName = GenerateScriptObjectName(); // Attach the name to the script rotation - lua_pushstring(luaState, luaRotationObjectName.c_str()); lua_pushvalue(luaState, -2); // dup the rotation object on top of stack - lua_settable(luaState, LUA_GLOBALSINDEX); + lua_setglobal(luaState, luaRotationObjectName.c_str()); // Get the rest of the rotation parameters; they are all optional. period = SafeGetLuaNumber(luaState, -1, "period", 0.0); @@ -160,8 +157,7 @@ ScriptedRotation::spin(double tjd) const { if (tjd != lastTime || !cacheable) { - lua_pushstring(luaState, luaRotationObjectName.c_str()); - lua_gettable(luaState, LUA_GLOBALSINDEX); + lua_getglobal(luaState, luaRotationObjectName.c_str()); if (lua_istable(luaState, -1)) { lua_pushstring(luaState, "orientation"); diff --git a/src/celestia/celx.cpp b/src/celestia/celx.cpp old mode 100644 new mode 100755 index 5ad0298cb1..41ac4e6cb6 --- a/src/celestia/celx.cpp +++ b/src/celestia/celx.cpp @@ -272,9 +272,13 @@ static void openLuaLibrary(lua_State* l, const char* name, lua_CFunction func) { +#if LUA_VER >= 0x050200 + luaL_requiref(l, name, func, 1); +#else lua_pushcfunction(l, func); lua_pushstring(l, name); lua_call(l, 1, 0); +#endif } #endif @@ -301,8 +305,13 @@ static void getField(lua_State* l, int index, const char* key) // When we move to Lua 5.1, this will be replaced by: // lua_getfield(l, index, key); lua_pushstring(l, key); - - if (index != LUA_GLOBALSINDEX && index != LUA_REGISTRYINDEX) +#ifdef LUA_GLOBALSINDEX + if (index == LUA_GLOBALSINDEX) { + lua_gettable(l, index); + return; + } +#endif + if (index != LUA_REGISTRYINDEX) lua_gettable(l, index - 1); else lua_gettable(l, index); @@ -507,7 +516,7 @@ LuaState::LuaState() : ioMode(NoIO), eventHandlerEnabled(false) { - state = lua_open(); + state = luaL_newstate(); timer = CreateTimer(); screenshotCount = 0; } @@ -587,8 +596,7 @@ void LuaState::cleanup() lua_pop(state,1); } } - lua_pushstring(costate, CleanupCallback); - lua_gettable(costate, LUA_GLOBALSINDEX); + lua_getglobal(costate, CleanupCallback); if (lua_isnil(costate, -1)) { return; @@ -657,8 +665,11 @@ static int resumeLuaThread(lua_State *L, lua_State *co, int narg) //if (!lua_checkstack(co, narg)) // luaL_error(L, "too many arguments to resume"); lua_xmove(L, co, narg); - +#if LUA_VER >= 0x050200 + status = lua_resume(co, NULL, narg); +#else status = lua_resume(co, narg); +#endif #if LUA_VER >= 0x050100 if (status == 0 || status == LUA_YIELD) #else @@ -772,8 +783,7 @@ bool LuaState::charEntered(const char* c_p) int stack_top = lua_gettop(costate); #endif bool result = true; - lua_pushstring(costate, KbdCallback); - lua_gettable(costate, LUA_GLOBALSINDEX); + lua_getglobal(costate, KbdCallback); lua_pushstring(costate, c_p); timeout = getTime() + 1.0; if (lua_pcall(costate, 1, 1, 0) != 0) @@ -966,7 +976,12 @@ int LuaState::loadScript(istream& in, const string& streamname) lua_settable(state, LUA_REGISTRYINDEX); } +#if LUA_VER >= 0x050200 + int status = lua_load(state, readStreamChunk, &info, streamname.c_str(), + NULL); +#else int status = lua_load(state, readStreamChunk, &info, streamname.c_str()); +#endif if (status != 0) cout << "Error loading script: " << lua_tostring(state, -1) << '\n'; @@ -3227,8 +3242,7 @@ static int celestia_requestkeyboard(lua_State* l) if (lua_toboolean(l, 2)) { // Check for existence of charEntered: - lua_pushstring(l, KbdCallback); - lua_gettable(l, LUA_GLOBALSINDEX); + lua_getglobal(l, KbdCallback); if (lua_isnil(l, -1)) { Celx_DoError(l, "script requested keyboard, but did not provide callback"); @@ -3690,16 +3704,14 @@ bool LuaState::init(CelestiaCore* appCore) return false; } - lua_pushstring(state, "KM_PER_MICROLY"); lua_pushnumber(state, (lua_Number)KM_PER_LY/1e6); - lua_settable(state, LUA_GLOBALSINDEX); + lua_setglobal(state, "KM_PER_MICROLY"); loadLuaLibs(state); // Create the celestia object - lua_pushstring(state, "celestia"); celestia_new(state, appCore); - lua_settable(state, LUA_GLOBALSINDEX); + lua_setglobal(state, "celestia"); // add reference to appCore in the registry lua_pushstring(state, "celestia-appcore"); lua_pushlightuserdata(state, static_cast(appCore)); @@ -3714,8 +3726,7 @@ bool LuaState::init(CelestiaCore* appCore) lua_settable(state, LUA_REGISTRYINDEX); #if 0 - lua_pushstring(state, "dofile"); - lua_gettable(state, LUA_GLOBALSINDEX); // function "dofile" on stack + lua_getglobal(state, "dofile"); // function "dofile" on stack lua_pushstring(state, "luainit.celx"); // parameter if (lua_pcall(state, 1, 0, 0) != 0) // execute it { @@ -3735,7 +3746,7 @@ bool LuaState::init(CelestiaCore* appCore) void LuaState::setLuaPath(const string& s) { #if LUA_VER >= 0x050100 - lua_getfield(state, LUA_GLOBALSINDEX, "package"); + lua_getglobal(state, "package"); lua_pushstring(state, s.c_str()); lua_setfield(state, -2, "path"); lua_pop(state, 1); @@ -4249,7 +4260,7 @@ void LuaState::allowLuaPackageAccess() openLuaLibrary(state, LUA_LOADLIBNAME, luaopen_package); // Disallow loadlib - lua_getfield(state, LUA_GLOBALSINDEX, "package"); + lua_getglobal(state, "package"); lua_pushnil(state); lua_setfield(state, -2, "loadlib"); lua_pop(state, 1); diff --git a/src/celestia/celx_gl.cpp b/src/celestia/celx_gl.cpp index c654c9a0cf..04898cfb72 100644 --- a/src/celestia/celx_gl.cpp +++ b/src/celestia/celx_gl.cpp @@ -221,7 +221,6 @@ static int gl_PushMatrix(lua_State* l) void LoadLuaGraphicsLibrary(lua_State* l) { CelxLua celx(l); - lua_pushstring(l, "gl"); lua_newtable(l); celx.registerMethod("Frustum", gl_Frustum); @@ -259,11 +258,10 @@ void LoadLuaGraphicsLibrary(lua_State* l) celx.registerValue("NEAREST", GL_NEAREST); celx.registerValue("SRC_ALPHA", GL_SRC_ALPHA); celx.registerValue("ONE_MINUS_SRC_ALPHA", GL_ONE_MINUS_SRC_ALPHA); - lua_settable(l, LUA_GLOBALSINDEX); + lua_setglobal(l, "gl"); - lua_pushstring(l, "glu"); lua_newtable(l); celx.registerMethod("LookAt", glu_LookAt); celx.registerMethod("Ortho2D", glu_Ortho2D); - lua_settable(l, LUA_GLOBALSINDEX); + lua_setglobal(l, "glu"); }