Fix lua5.1 compat (for luajit)

This commit is contained in:
buffet 2020-07-30 21:34:34 +00:00
parent f270967f8a
commit e713e91279
9 changed files with 64 additions and 11 deletions

22
include/luak/lua_compat.h Normal file
View file

@ -0,0 +1,22 @@
/* Copyright (c), Charlotte Meyer <dev@buffet.sh>
*
* 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 <lauxlib.h>
#include <lua.h>
#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 */

View file

@ -18,6 +18,7 @@
#include "input/cursor.h" #include "input/cursor.h"
#include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_lua_callback.h"
#include "luak/kiwmi_view.h" #include "luak/kiwmi_view.h"
#include "luak/lua_compat.h"
#include "luak/luak.h" #include "luak/luak.h"
static int static int
@ -241,9 +242,9 @@ luaK_kiwmi_cursor_register(lua_State *L)
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); 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_setfield(L, -2, "__events");
lua_pushcfunction(L, luaK_usertype_ref_equal); lua_pushcfunction(L, luaK_usertype_ref_equal);

View file

@ -15,6 +15,7 @@
#include "input/keyboard.h" #include "input/keyboard.h"
#include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_lua_callback.h"
#include "luak/lua_compat.h"
static int static int
l_kiwmi_keyboard_modifiers(lua_State *L) l_kiwmi_keyboard_modifiers(lua_State *L)
@ -266,9 +267,9 @@ luaK_kiwmi_keyboard_register(lua_State *L)
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); 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_setfield(L, -2, "__events");
lua_pushcfunction(L, luaK_usertype_ref_equal); lua_pushcfunction(L, luaK_usertype_ref_equal);

View file

@ -15,6 +15,7 @@
#include "desktop/output.h" #include "desktop/output.h"
#include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_lua_callback.h"
#include "luak/lua_compat.h"
#include "server.h" #include "server.h"
static int static int
@ -285,9 +286,9 @@ luaK_kiwmi_output_register(lua_State *L)
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); 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_setfield(L, -2, "__events");
lua_pushcfunction(L, luaK_usertype_ref_equal); lua_pushcfunction(L, luaK_usertype_ref_equal);

View file

@ -16,6 +16,7 @@
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "desktop/output.h" #include "desktop/output.h"
#include "luak/lua_compat.h"
#include "luak/luak.h" #include "luak/luak.h"
struct kiwmi_renderer { struct kiwmi_renderer {
@ -119,7 +120,7 @@ luaK_kiwmi_renderer_register(lua_State *L)
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); 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_pushcfunction(L, luaK_usertype_ref_equal);
lua_setfield(L, -2, "__eq"); lua_setfield(L, -2, "__eq");

View file

@ -25,6 +25,7 @@
#include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_lua_callback.h"
#include "luak/kiwmi_output.h" #include "luak/kiwmi_output.h"
#include "luak/kiwmi_view.h" #include "luak/kiwmi_view.h"
#include "luak/lua_compat.h"
#include "server.h" #include "server.h"
static int static int
@ -402,9 +403,9 @@ luaK_kiwmi_server_register(lua_State *L)
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); 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_setfield(L, -2, "__events");
lua_pushcfunction(L, luaK_usertype_ref_equal); lua_pushcfunction(L, luaK_usertype_ref_equal);

View file

@ -22,6 +22,7 @@
#include "luak/kiwmi_lua_callback.h" #include "luak/kiwmi_lua_callback.h"
#include "luak/kiwmi_output.h" #include "luak/kiwmi_output.h"
#include "luak/kiwmi_renderer.h" #include "luak/kiwmi_renderer.h"
#include "luak/lua_compat.h"
#include "server.h" #include "server.h"
static int static int
@ -779,9 +780,9 @@ luaK_kiwmi_view_register(lua_State *L)
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); 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_setfield(L, -2, "__events");
lua_pushcfunction(L, luaK_usertype_ref_equal); lua_pushcfunction(L, luaK_usertype_ref_equal);

24
kiwmi/luak/lua_compat.c Normal file
View file

@ -0,0 +1,24 @@
/* Copyright (c), Charlotte Meyer <dev@buffet.sh>
*
* 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 <stdlib.h>
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);
}

View file

@ -18,6 +18,7 @@ kiwmi_sources = files(
'luak/kiwmi_renderer.c', 'luak/kiwmi_renderer.c',
'luak/kiwmi_server.c', 'luak/kiwmi_server.c',
'luak/kiwmi_view.c', 'luak/kiwmi_view.c',
'luak/lua_compat.c',
'luak/luak.c', 'luak/luak.c',
) )