Drop support for ancient Lua 5.0

pull/301/head
Hleb Valoshka 2019-06-05 16:22:06 +03:00
parent b59a25508d
commit 9f6cdadee3
3 changed files with 12 additions and 156 deletions

View File

@ -13,18 +13,10 @@
#define _CELENGINE_SCRIPTOBJECT_H_
#ifndef LUA_VER
#define LUA_VER 0x050000
#define LUA_VER 0x050100
#endif
#if LUA_VER >= 0x050100
#include "lua.hpp"
#else
extern "C" {
#include "lua.h"
#include "lualib.h"
}
#endif
#include <string>
#include <celengine/parser.h>

View File

@ -318,9 +318,6 @@ void CelxLua::initLineColorMap()
}
#if LUA_VER >= 0x050100
// Load a Lua library--in Lua 5.1, the luaopen_* functions cannot be called
// directly. They most be invoked through the Lua state.
static void openLuaLibrary(lua_State* l,
const char* name,
lua_CFunction func)
@ -333,7 +330,6 @@ static void openLuaLibrary(lua_State* l,
lua_call(l, 1, 0);
#endif
}
#endif
void CelxLua::initMaps()
@ -470,11 +466,7 @@ CelestiaCore* getAppCore(lua_State* l, FatalErrors fatalErrors)
LuaState::LuaState() :
timeout(MaxTimeslice)
{
#if LUA_VER >= 0x050100
state = luaL_newstate();
#else
state = lua_open();
#endif
timer = new Timer();
screenshotCount = 0;
}
@ -620,11 +612,7 @@ static int resumeLuaThread(lua_State *L, lua_State *co, int narg)
#else
status = lua_resume(co, narg);
#endif
#if LUA_VER >= 0x050100
if (status == 0 || status == LUA_YIELD)
#else
if (status == 0)
#endif
{
int nres = lua_gettop(co);
//if (!lua_checkstack(L, narg))
@ -687,13 +675,9 @@ bool LuaState::charEntered(const char* c_p)
int stackTop = lua_gettop(costate);
if (c_p[0] == 'y')
{
#if LUA_VER >= 0x050100
openLuaLibrary(costate, LUA_LOADLIBNAME, luaopen_package);
openLuaLibrary(costate, LUA_IOLIBNAME, luaopen_io);
openLuaLibrary(costate, LUA_OSLIBNAME, luaopen_os);
#else
lua_iolibopen(costate);
#endif
ioMode = IOAllowed;
}
else
@ -727,9 +711,6 @@ bool LuaState::charEntered(const char* c_p)
lua_settop(costate,stackTop);
return true;
}
#if LUA_VER < 0x050100
int stack_top = lua_gettop(costate);
#endif
bool result = true;
lua_getglobal(costate, KbdCallback);
lua_pushstring(costate, c_p);
@ -748,10 +729,6 @@ bool LuaState::charEntered(const char* c_p)
lua_pop(costate, 1);
}
#if LUA_VER < 0x050100
// cleanup stack - is this necessary?
lua_settop(costate, stack_top);
#endif
return result;
}
@ -962,24 +939,10 @@ int LuaState::resume()
if (errorMessage == nullptr)
errorMessage = "Unknown script error";
#if LUA_VER < 0x050100
// This is a nasty hack required in Lua 5.0, where there's no
// way to distinguish between a thread returning because it completed
// or yielded. Thus, we continue to resume the script until we get
// an error. The 'cannot resume dead coroutine' error appears when
// there were no other errors, and execution terminates normally.
// In Lua 5.1, we can simply check the thread status to find out
// if it's done executing.
if (strcmp(errorMessage, "cannot resume dead coroutine") != 0)
#endif
{
cout << "Error: " << errorMessage << '\n';
CelestiaCore* appCore = getAppCore(co);
if (appCore != nullptr)
{
appCore->fatalError(errorMessage);
}
}
cout << "Error: " << errorMessage << '\n';
CelestiaCore* appCore = getAppCore(co);
if (appCore != nullptr)
appCore->fatalError(errorMessage);
return 1; // just the error string
}
@ -991,11 +954,9 @@ int LuaState::resume()
timeout = getTime() + 1.0;
}
#if LUA_VER >= 0x050100
// The thread status is zero if it has terminated normally
if (lua_status(co) == 0)
alive = false;
#endif
// The thread status is zero if it has terminated normally
if (lua_status(co) == 0)
alive = false;
return nArgs; // arguments from yield
}
@ -1117,14 +1078,9 @@ void LuaState::requestIO()
string policy = appCore->getConfig()->scriptSystemAccessPolicy;
if (policy == "allow")
{
#if LUA_VER >= 0x050100
openLuaLibrary(costate, LUA_LOADLIBNAME, luaopen_package);
openLuaLibrary(costate, LUA_IOLIBNAME, luaopen_io);
openLuaLibrary(costate, LUA_OSLIBNAME, luaopen_os);
#else
lua_iolibopen(costate);
#endif
//luaopen_io(costate);
ioMode = IOAllowed;
}
else if (policy == "deny")
@ -1336,7 +1292,6 @@ bool LuaState::init(CelestiaCore* appCore)
CelxLua::initMaps();
// Import the base, table, string, and math libraries
#if LUA_VER >= 0x050100
openLuaLibrary(state, "", luaopen_base);
openLuaLibrary(state, LUA_MATHLIBNAME, luaopen_math);
openLuaLibrary(state, LUA_TABLIBNAME, luaopen_table);
@ -1347,12 +1302,6 @@ bool LuaState::init(CelestiaCore* appCore)
// Make the package library, except the loadlib function, available
// for celx regardless of script system access policy.
allowLuaPackageAccess();
#else
lua_baselibopen(state);
lua_mathlibopen(state);
lua_tablibopen(state);
lua_strlibopen(state);
#endif
// Add an easy to use wait function, so that script writers can
// live in ignorance of coroutines. There will probably be a significant
@ -1408,87 +1357,17 @@ bool LuaState::init(CelestiaCore* appCore)
void LuaState::setLuaPath(const string& s)
{
#if LUA_VER >= 0x050100
lua_getglobal(state, "package");
lua_pushstring(state, s.c_str());
lua_setfield(state, -2, "path");
lua_pop(state, 1);
#else
lua_pushstring(state, "LUA_PATH");
lua_pushstring(state, s.c_str());
lua_settable(state, LUA_GLOBALSINDEX);
#endif
}
#if LUA_VER < 0x050100
// ======================== loadlib ===================================
/*
* This is an implementation of loadlib based on the dlfcn interface.
* The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
* NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
* as an emulation layer on top of native functions.
*/
#ifndef _WIN32
extern "C" {
#include <lualib.h>
/* #include <lauxlib.h.h> */
#include <dlfcn.h>
}
#if 0
static int x_loadlib(lua_State *L)
{
/* temp -- don't have lauxlib
const char *path=luaL_checkstring(L,1);
const char *init=luaL_checkstring(L,2);
*/
cout << "loading lua lib\n"; cout.flush();
const char *path=lua_tostring(L,1);
const char *init=lua_tostring(L,2);
void *lib=dlopen(path,RTLD_NOW);
if (lib!=nullptr)
{
lua_CFunction f=(lua_CFunction) dlsym(lib,init);
if (f!=nullptr)
{
lua_pushlightuserdata(L,lib);
lua_pushcclosure(L,f,1);
return 1;
}
}
/* else return appropriate error messages */
lua_pushnil(L);
lua_pushstring(L,dlerror());
lua_pushstring(L,(lib!=nullptr) ? "init" : "open");
if (lib!=nullptr) dlclose(lib);
return 3;
}
#endif
#endif // _WIN32
#endif // LUA_VER < 0x050100
// ==================== Load Libraries ================================================
static void loadLuaLibs(lua_State* state)
{
#if LUA_VER >= 0x050100
openLuaLibrary(state, LUA_DBLIBNAME, luaopen_debug);
#else
luaopen_debug(state);
#endif
// TODO: Not required with Lua 5.1
#if 0
#ifndef _WIN32
lua_pushstring(state, "xloadlib");
lua_pushcfunction(state, x_loadlib);
lua_settable(state, LUA_GLOBALSINDEX);
#endif
#endif
CreateObjectMetaTable(state);
CreateObserverMetaTable(state);
@ -1512,13 +1391,9 @@ static void loadLuaLibs(lua_State* state)
void LuaState::allowSystemAccess()
{
#if LUA_VER >= 0x050100
openLuaLibrary(state, LUA_LOADLIBNAME, luaopen_package);
openLuaLibrary(state, LUA_IOLIBNAME, luaopen_io);
openLuaLibrary(state, LUA_OSLIBNAME, luaopen_os);
#else
luaopen_io(state);
#endif
ioMode = IOAllowed;
}
@ -1527,7 +1402,6 @@ void LuaState::allowSystemAccess()
// function.
void LuaState::allowLuaPackageAccess()
{
#if LUA_VER >= 0x050100
openLuaLibrary(state, LUA_LOADLIBNAME, luaopen_package);
// Disallow loadlib
@ -1535,7 +1409,6 @@ void LuaState::allowLuaPackageAccess()
lua_pushnil(state);
lua_setfield(state, -2, "loadlib");
lua_pop(state, 1);
#endif
}

View File

@ -12,23 +12,14 @@
#ifndef _CELESTIA_CELX_H_
#define _CELESTIA_CELX_H_
#ifndef LUA_VER
#define LUA_VER 0x050100
#endif
#include <iostream>
#include <string>
#include <vector>
#ifndef LUA_VER
#define LUA_VER 0x050000
#endif
#if LUA_VER >= 0x050100
#include "lua.hpp"
#else
extern "C" {
#include "lua.h"
#include "lualib.h"
}
#endif
#include <celutil/timer.h>
#include <celengine/observer.h>