From d2a8537f474bfc63707dd4030d2e5779c02280d7 Mon Sep 17 00:00:00 2001 From: xiota Date: Mon, 6 Mar 2023 15:09:13 -0800 Subject: [PATCH] GeanyLua: Add compatibility for Lua 5.1 and LuaJIT --- geanylua/Makefile.am | 2 ++ geanylua/glspi.h | 2 ++ geanylua/glspi_compat.c | 55 +++++++++++++++++++++++++++++++++++++++++ geanylua/glspi_compat.h | 28 +++++++++++++++++++++ geanylua/glspi_kfile.c | 2 ++ geanylua/gsdlg_lua.c | 1 + 6 files changed, 90 insertions(+) create mode 100644 geanylua/glspi_compat.c create mode 100644 geanylua/glspi_compat.h diff --git a/geanylua/Makefile.am b/geanylua/Makefile.am index 6a8b055a2..4d49e0029 100644 --- a/geanylua/Makefile.am +++ b/geanylua/Makefile.am @@ -24,6 +24,8 @@ libgeanylua_la_SOURCES = \ glspi_keycmd.h \ glspi_sci.h \ glspi_ver.h \ + glspi_compat.c \ + glspi_compat.h \ gsdlg.h geanylua_la_CFLAGS = \ diff --git a/geanylua/glspi.h b/geanylua/glspi.h index 9f172b8e2..bd32139ce 100644 --- a/geanylua/glspi.h +++ b/geanylua/glspi.h @@ -16,6 +16,8 @@ #include #include +#include "glspi_compat.h" + #include #define main_widgets geany->main_widgets diff --git a/geanylua/glspi_compat.c b/geanylua/glspi_compat.c new file mode 100644 index 000000000..79fc0a459 --- /dev/null +++ b/geanylua/glspi_compat.c @@ -0,0 +1,55 @@ +/* Lua compatibility functions */ + +#include +#include +#include + + +/* + * Compatibility functions for Lua 5.1 and LuaJIT + */ +#if LUA_VERSION_NUM==501 + +/* Adapted from Lua 5.2.0 */ +void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { + luaL_checkstack(L, nup+1, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + lua_pushstring(L, l->name); + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -(nup+1)); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_settable(L, -(nup + 3)); + } + lua_pop(L, nup); /* remove upvalues */ +} + +#endif /* LUAJIT_VERSION_NUM */ + + +/* + * Compatibility functions for Lua 5.1 only + */ +#if LUA_VERSION_NUM==501 && !defined LUAJIT_VERSION_NUM + +void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, + int level) +{ + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); + lua_call(L, 2, 1); + + return 1; +} + +#endif /* LUA_VERSION_NUM */ diff --git a/geanylua/glspi_compat.h b/geanylua/glspi_compat.h new file mode 100644 index 000000000..81c5fd6f8 --- /dev/null +++ b/geanylua/glspi_compat.h @@ -0,0 +1,28 @@ +/* Lua compatibility functions */ + +#ifndef GLSPI_COMPAT +#define GLSPI_COMPAT 1 + +/* + * Compatibility functions for Lua 5.1 and LuaJIT + */ +#if LUA_VERSION_NUM==501 + +#define lua_rawlen(L,i) lua_objlen(L, (i)) + +void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); + +#endif /* LUAJIT_VERSION_NUM */ + + +/* + * Compatibility functions for Lua 5.1 only + */ +#if LUA_VERSION_NUM==501 && !defined LUAJIT_VERSION_NUM + +void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level); + +#endif /* LUA_VERSION_NUM */ + + +#endif /* GLSPI_COMPAT */ diff --git a/geanylua/glspi_kfile.c b/geanylua/glspi_kfile.c index ca469cd8c..e20161f88 100644 --- a/geanylua/glspi_kfile.c +++ b/geanylua/glspi_kfile.c @@ -12,6 +12,8 @@ #include #include +#include "glspi_compat.h" + #define LUA_MODULE_NAME "keyfile" #define MetaName "_g_key_file_metatable" diff --git a/geanylua/gsdlg_lua.c b/geanylua/gsdlg_lua.c index 34df24365..397ef27c2 100644 --- a/geanylua/gsdlg_lua.c +++ b/geanylua/gsdlg_lua.c @@ -35,6 +35,7 @@ #include #include +#include "glspi_compat.h" #define GSDLG_ALL_IN_ONE #include "gsdlg.h"