Skip to content
Permalink
Browse files
Start of Lua 5.4 compatibility
  • Loading branch information
TheCycoONE committed Jul 4, 2020
1 parent 215f779 commit e78df0e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
@@ -82,7 +82,7 @@ end

-- Check Lua version
if _VERSION ~= "Lua 5.1" then
if _VERSION == "Lua 5.2" or _VERSION == "Lua 5.3" then
if _VERSION == "Lua 5.2" or _VERSION == "Lua 5.3" or _VERSION == "Lua 5.4" then
-- Compatibility: Keep the global unpack function
unpack = table.unpack
-- Compatibility: Provide a replacement for deprecated ipairs()
@@ -26,7 +26,7 @@ strict_declare_global "unpermanent"

local th_getfenv
local th_getupvalue
if _G._VERSION == "Lua 5.2" or _G._VERSION == "Lua 5.3" then
if _G._VERSION == "Lua 5.2" or _G._VERSION == "Lua 5.3" or _G._VERSION == "Lua 5.4" then
th_getfenv = function(f)
local _, val = nil, nil
if type(f) == "function" then
@@ -106,7 +106,7 @@ function loadfile_envcall(filename)
return loadstring_envcall(result, "@" .. filename)
end

if _G._VERSION == "Lua 5.2" or _G._VERSION == "Lua 5.3" then
if _G._VERSION == "Lua 5.2" or _G._VERSION == "Lua 5.3" or _G._VERSION == "Lua 5.4" then
function loadstring_envcall(contents, chunkname)
-- Lua 5.2+ lacks setfenv()
-- load() still only allows a chunk to have an environment set once, so
@@ -133,6 +133,7 @@ int l_get_key_modifiers(lua_State* L) {
int l_mainloop(lua_State* L) {
luaL_checktype(L, 1, LUA_TTHREAD);
lua_State* dispatcher = lua_tothread(L, 1);
int resume_stack_size = 0;

fps_ctrl* fps_control = (fps_ctrl*)lua_touserdata(L, luaT_upvalueindex(1));
SDL_TimerID timer = SDL_AddTimer(30, timer_frame_callback, nullptr);
@@ -261,31 +262,31 @@ int l_mainloop(lua_State* L) {
break;
}
if (nargs != 0) {
if (luaT_resume(dispatcher, dispatcher, nargs) != LUA_YIELD) {
if (luaT_resume(dispatcher, dispatcher, nargs, &resume_stack_size) != LUA_YIELD) {
goto leave_loop;
}
do_frame = do_frame || (lua_toboolean(dispatcher, 1) != 0);
lua_settop(dispatcher, 0);
lua_pop(dispatcher, resume_stack_size);
}
} while (SDL_PollEvent(&e) != 0);
if (do_timer) {
lua_pushliteral(dispatcher, "timer");
if (luaT_resume(dispatcher, dispatcher, 1) != LUA_YIELD) {
if (luaT_resume(dispatcher, dispatcher, 1, &resume_stack_size) != LUA_YIELD) {
break;
}
do_frame = do_frame || (lua_toboolean(dispatcher, 1) != 0);
lua_settop(dispatcher, 0);
lua_pop(dispatcher, resume_stack_size);
}
if (do_frame || !fps_control->limit_fps) {
do {
if (fps_control->track_fps) {
fps_control->count_frame();
}
lua_pushliteral(dispatcher, "frame");
if (luaT_resume(dispatcher, dispatcher, 1) != LUA_YIELD) {
if (luaT_resume(dispatcher, dispatcher, 1, &resume_stack_size) != LUA_YIELD) {
goto leave_loop;
}
lua_settop(dispatcher, 0);
lua_pop(dispatcher, resume_stack_size);
} while (fps_control->limit_fps == false && SDL_PollEvent(nullptr) == 0);
}

@@ -106,12 +106,18 @@ inline int luaT_load(lua_State* L, lua_Reader r, void* d, const char* s,
#endif
}

// Compatibility for missing from argument on lua_resume in 5.1
inline int luaT_resume(lua_State* L, lua_State* f, int n) {
#if LUA_VERSION_NUM >= 502
return lua_resume(L, f, n);
// Compatibility for older versions of lua_resume
inline int luaT_resume(lua_State* L, lua_State* f, int n, int *nresults) {
#if LUA_VERSION_NUM >= 504
return lua_resume(L, f, n, nresults);
#elif LUA_VERSION_NUM >= 502
int res = lua_resume(L, f, n);
*nresults = lua_gettop(L);
return res;
#else
return lua_resume(L, n);
int res = lua_resume(L, n);
*nresults = lua_gettop(L);
return res;
#endif
}

0 comments on commit e78df0e

Please sign in to comment.