diff --git a/celestia/configure.in b/celestia/configure.in index 766cd11..1652787 100644 --- a/celestia/configure.in +++ b/celestia/configure.in @@ -408,11 +408,15 @@ AC_ARG_WITH([lua], if (test "$enable_lua" != "no"); then LUA_VER=0 - 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])) ] - ) ] + 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 diff --git a/celestia/src/celengine/scriptorbit.cpp b/celestia/src/celengine/scriptorbit.cpp index a8057b4..ac56bd8 100644 --- a/celestia/src/celengine/scriptorbit.cpp +++ b/celestia/src/celengine/scriptorbit.cpp @@ -73,8 +73,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"; @@ -92,8 +91,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) { @@ -157,9 +155,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. @@ -206,8 +203,7 @@ ScriptedOrbit::computePosition(double tjd) const { Point3d pos(0.0, 0.0, 0.0); - 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/celestia/src/celengine/scriptrotation.cpp b/celestia/src/celengine/scriptrotation.cpp index 1d03493..a7d70b8 100644 --- a/celestia/src/celengine/scriptrotation.cpp +++ b/celestia/src/celengine/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/celestia/src/celestia/celx.cpp b/celestia/src/celestia/celx.cpp index fe58eb7..be3f2fa 100644 --- a/celestia/src/celestia/celx.cpp +++ b/celestia/src/celestia/celx.cpp @@ -50,6 +50,10 @@ #include "celx.h" #include "celestiacore.h" +#ifndef lua_open +#define lua_open() luaL_newstate() +#endif + using namespace std; const char* CelxLua::ClassNames[] = @@ -293,14 +297,7 @@ void CelxLua::initMaps() 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) - lua_gettable(l, index - 1); - else - lua_gettable(l, index); + lua_getfield(l, index, key); } @@ -582,8 +579,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; @@ -653,7 +649,11 @@ static int resumeLuaThread(lua_State *L, lua_State *co, int 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 @@ -767,8 +767,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) @@ -960,8 +959,11 @@ int LuaState::loadScript(istream& in, const string& streamname) lua_pushstring(state, streamname.c_str()); 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'; @@ -3100,8 +3102,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"); @@ -3430,16 +3431,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)); @@ -3475,7 +3474,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); @@ -3989,7 +3988,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/celestia/src/celestia/celx_gl.cpp b/celestia/src/celestia/celx_gl.cpp index e322928..11ea8f4 100644 --- a/celestia/src/celestia/celx_gl.cpp +++ b/celestia/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"); }