Skip to content

Commit

Permalink
GeanyLua: Add compatibility for Lua 5.1 and LuaJIT
Browse files Browse the repository at this point in the history
  • Loading branch information
xiota committed Oct 13, 2024
1 parent 599766d commit e93bacb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions geanylua/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
Expand Down
2 changes: 2 additions & 0 deletions geanylua/glspi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <string.h>
#include <ctype.h>

#include "glspi_compat.h"

#include <geanyplugin.h>
#define main_widgets geany->main_widgets

Expand Down
55 changes: 55 additions & 0 deletions geanylua/glspi_compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Lua compatibility functions */

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>


/*
* 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 */
28 changes: 28 additions & 0 deletions geanylua/glspi_compat.h
Original file line number Diff line number Diff line change
@@ -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 */
2 changes: 2 additions & 0 deletions geanylua/glspi_kfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <lualib.h>
#include <lauxlib.h>

#include "glspi_compat.h"

#define LUA_MODULE_NAME "keyfile"
#define MetaName "_g_key_file_metatable"

Expand Down
1 change: 1 addition & 0 deletions geanylua/gsdlg_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <lualib.h>
#include <lauxlib.h>

#include "glspi_compat.h"

#define GSDLG_ALL_IN_ONE
#include "gsdlg.h"
Expand Down

0 comments on commit e93bacb

Please sign in to comment.