Skip to content

Commit

Permalink
Merge pull request #27 from joequant/lua5.2fixes
Browse files Browse the repository at this point in the history
Lua 5.2 fixes
  • Loading branch information
Alexell committed Jan 23, 2018
2 parents 295b99f + 44eaac7 commit a9ceda5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
4 changes: 2 additions & 2 deletions celestia.pro
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ macx {
LIBS += macosx/lib/cspice.a
}

DEFINES += CELX LUA_VER=0x050100
DEFINES += CELX LUA_VER=0x050200

# QMAKE_CXXFLAGS += -ffast-math

Expand Down Expand Up @@ -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
}
}
4 changes: 3 additions & 1 deletion configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -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])) ]
Expand Down
13 changes: 4 additions & 9 deletions src/celephem/scriptorbit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 4 additions & 8 deletions src/celephem/scriptrotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down
47 changes: 29 additions & 18 deletions src/celestia/celx.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
Expand Down Expand Up @@ -507,7 +516,7 @@ LuaState::LuaState() :
ioMode(NoIO),
eventHandlerEnabled(false)
{
state = lua_open();
state = luaL_newstate();
timer = CreateTimer();
screenshotCount = 0;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<void*>(appCore));
Expand All @@ -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
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/celestia/celx_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}

0 comments on commit a9ceda5

Please sign in to comment.