From e713e912795f1d009ab93db00326941015296aad Mon Sep 17 00:00:00 2001 From: Charlotte Meyer Date: Thu, 30 Jul 2020 21:34:34 +0000 Subject: [PATCH] Fix lua5.1 compat (for luajit) --- include/luak/lua_compat.h | 22 ++++++++++++++++++++++ kiwmi/luak/kiwmi_cursor.c | 5 +++-- kiwmi/luak/kiwmi_keyboard.c | 5 +++-- kiwmi/luak/kiwmi_output.c | 5 +++-- kiwmi/luak/kiwmi_renderer.c | 3 ++- kiwmi/luak/kiwmi_server.c | 5 +++-- kiwmi/luak/kiwmi_view.c | 5 +++-- kiwmi/luak/lua_compat.c | 24 ++++++++++++++++++++++++ kiwmi/meson.build | 1 + 9 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 include/luak/lua_compat.h create mode 100644 kiwmi/luak/lua_compat.c diff --git a/include/luak/lua_compat.h b/include/luak/lua_compat.h new file mode 100644 index 0000000..ff1d567 --- /dev/null +++ b/include/luak/lua_compat.h @@ -0,0 +1,22 @@ +/* Copyright (c), Charlotte Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef KIWMI_LUAK_LUA_COMPAT_H +#define KIWMI_LUAK_LUA_COMPAT_H + +#include +#include + +#define luaC_newlibtable(L, l) \ + lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) + +#define luaC_newlib(L, l) \ + (luaC_newlibtable(L, l), luaC_setfuncs(L, l, 0)) + +void luaC_setfuncs(lua_State *L, const luaL_Reg *l, int nup); + +#endif /* KIWMI_LUAK_LUA_COMPAT_H */ diff --git a/kiwmi/luak/kiwmi_cursor.c b/kiwmi/luak/kiwmi_cursor.c index f6bf2a1..a70bb06 100644 --- a/kiwmi/luak/kiwmi_cursor.c +++ b/kiwmi/luak/kiwmi_cursor.c @@ -18,6 +18,7 @@ #include "input/cursor.h" #include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_view.h" +#include "luak/lua_compat.h" #include "luak/luak.h" static int @@ -241,9 +242,9 @@ luaK_kiwmi_cursor_register(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_setfuncs(L, kiwmi_cursor_methods, 0); + luaC_setfuncs(L, kiwmi_cursor_methods, 0); - luaL_newlib(L, kiwmi_cursor_events); + luaC_newlib(L, kiwmi_cursor_events); lua_setfield(L, -2, "__events"); lua_pushcfunction(L, luaK_usertype_ref_equal); diff --git a/kiwmi/luak/kiwmi_keyboard.c b/kiwmi/luak/kiwmi_keyboard.c index 4e5779b..fe731c0 100644 --- a/kiwmi/luak/kiwmi_keyboard.c +++ b/kiwmi/luak/kiwmi_keyboard.c @@ -15,6 +15,7 @@ #include "input/keyboard.h" #include "luak/kiwmi_lua_callback.h" +#include "luak/lua_compat.h" static int l_kiwmi_keyboard_modifiers(lua_State *L) @@ -266,9 +267,9 @@ luaK_kiwmi_keyboard_register(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_setfuncs(L, kiwmi_keyboard_methods, 0); + luaC_setfuncs(L, kiwmi_keyboard_methods, 0); - luaL_newlib(L, kiwmi_keyboard_events); + luaC_newlib(L, kiwmi_keyboard_events); lua_setfield(L, -2, "__events"); lua_pushcfunction(L, luaK_usertype_ref_equal); diff --git a/kiwmi/luak/kiwmi_output.c b/kiwmi/luak/kiwmi_output.c index 8fa53aa..8c3b7f6 100644 --- a/kiwmi/luak/kiwmi_output.c +++ b/kiwmi/luak/kiwmi_output.c @@ -15,6 +15,7 @@ #include "desktop/output.h" #include "luak/kiwmi_lua_callback.h" +#include "luak/lua_compat.h" #include "server.h" static int @@ -285,9 +286,9 @@ luaK_kiwmi_output_register(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_setfuncs(L, kiwmi_output_methods, 0); + luaC_setfuncs(L, kiwmi_output_methods, 0); - luaL_newlib(L, kiwmi_output_events); + luaC_newlib(L, kiwmi_output_events); lua_setfield(L, -2, "__events"); lua_pushcfunction(L, luaK_usertype_ref_equal); diff --git a/kiwmi/luak/kiwmi_renderer.c b/kiwmi/luak/kiwmi_renderer.c index 1d48f2a..c12d19f 100644 --- a/kiwmi/luak/kiwmi_renderer.c +++ b/kiwmi/luak/kiwmi_renderer.c @@ -16,6 +16,7 @@ #include #include "desktop/output.h" +#include "luak/lua_compat.h" #include "luak/luak.h" struct kiwmi_renderer { @@ -119,7 +120,7 @@ luaK_kiwmi_renderer_register(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_setfuncs(L, kiwmi_renderer_methods, 0); + luaC_setfuncs(L, kiwmi_renderer_methods, 0); lua_pushcfunction(L, luaK_usertype_ref_equal); lua_setfield(L, -2, "__eq"); diff --git a/kiwmi/luak/kiwmi_server.c b/kiwmi/luak/kiwmi_server.c index 35e04df..2d1dc43 100644 --- a/kiwmi/luak/kiwmi_server.c +++ b/kiwmi/luak/kiwmi_server.c @@ -25,6 +25,7 @@ #include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_output.h" #include "luak/kiwmi_view.h" +#include "luak/lua_compat.h" #include "server.h" static int @@ -402,9 +403,9 @@ luaK_kiwmi_server_register(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_setfuncs(L, kiwmi_server_methods, 0); + luaC_setfuncs(L, kiwmi_server_methods, 0); - luaL_newlib(L, kiwmi_server_events); + luaC_newlib(L, kiwmi_server_events); lua_setfield(L, -2, "__events"); lua_pushcfunction(L, luaK_usertype_ref_equal); diff --git a/kiwmi/luak/kiwmi_view.c b/kiwmi/luak/kiwmi_view.c index 125b6c5..5fe8c6a 100644 --- a/kiwmi/luak/kiwmi_view.c +++ b/kiwmi/luak/kiwmi_view.c @@ -22,6 +22,7 @@ #include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_output.h" #include "luak/kiwmi_renderer.h" +#include "luak/lua_compat.h" #include "server.h" static int @@ -779,9 +780,9 @@ luaK_kiwmi_view_register(lua_State *L) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - luaL_setfuncs(L, kiwmi_view_methods, 0); + luaC_setfuncs(L, kiwmi_view_methods, 0); - luaL_newlib(L, kiwmi_view_events); + luaC_newlib(L, kiwmi_view_events); lua_setfield(L, -2, "__events"); lua_pushcfunction(L, luaK_usertype_ref_equal); diff --git a/kiwmi/luak/lua_compat.c b/kiwmi/luak/lua_compat.c new file mode 100644 index 0000000..9eb5730 --- /dev/null +++ b/kiwmi/luak/lua_compat.c @@ -0,0 +1,24 @@ +/* Copyright (c), Charlotte Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "luak/lua_compat.h" + +#include + +void +luaC_setfuncs(lua_State *L, const luaL_Reg *l, int nup) +{ + luaL_checkstack(L, nup, "too many upvalues"); + for (; l->name; ++l) { + for (int i = 0; i < nup; ++i) { + lua_pushvalue(L, -nup); + } + lua_pushcclosure(L, l->func, nup); + lua_setfield(L, -(nup + 2), l->name); + } + lua_pop(L, nup); +} diff --git a/kiwmi/meson.build b/kiwmi/meson.build index 6c79fb0..0081915 100644 --- a/kiwmi/meson.build +++ b/kiwmi/meson.build @@ -18,6 +18,7 @@ kiwmi_sources = files( 'luak/kiwmi_renderer.c', 'luak/kiwmi_server.c', 'luak/kiwmi_view.c', + 'luak/lua_compat.c', 'luak/luak.c', )