From d1e7139e3ec51dc165e2d01694a6d73292601046 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sun, 29 Nov 2020 11:45:32 +0100 Subject: [PATCH] Initial draft of transition to use LuaJIT2 instead of Lua 5.2 There is problem with utf8 expression matching on common.utf8_chars --- data/core/compat.lua | 2 ++ data/core/init.lua | 1 + meson.build | 4 +-- src/api/api.h | 2 ++ src/api/compat.c | 64 +++++++++++++++++++++++++++++++++++++++++ src/api/compat.h | 17 +++++++++++ src/meson.build | 1 + subprojects/lua.wrap | 4 --- subprojects/luajit.wrap | 5 ++++ 9 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 data/core/compat.lua create mode 100644 src/api/compat.c create mode 100644 src/api/compat.h delete mode 100644 subprojects/lua.wrap create mode 100644 subprojects/luajit.wrap diff --git a/data/core/compat.lua b/data/core/compat.lua new file mode 100644 index 00000000..d66af313 --- /dev/null +++ b/data/core/compat.lua @@ -0,0 +1,2 @@ +table.unpack = unpack + diff --git a/data/core/init.lua b/data/core/init.lua index 9ddf68e3..942eb4a7 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -1,5 +1,6 @@ require "core.strict" require "core.regex" +require "core.compat" local common = require "core.common" local config = require "core.config" local style = require "core.style" diff --git a/meson.build b/meson.build index f1d2b354..11467205 100644 --- a/meson.build +++ b/meson.build @@ -46,8 +46,8 @@ if not get_option('source-only') libm = cc.find_library('m', required : false) libdl = cc.find_library('dl', required : false) threads_dep = dependency('threads') - lua_dep = dependency('lua5.2', fallback: ['lua', 'lua_dep'], - default_options: ['shared=false', 'use_readline=false', 'app=false'] + lua_dep = dependency('luajit', fallback: ['luajit', 'lua_dep'], + default_options: ['default_library=static'] ) pcre2_dep = dependency('libpcre2-8') sdl_dep = dependency('sdl2', method: 'config-tool') diff --git a/src/api/api.h b/src/api/api.h index 51ebb9a8..3e44f5a2 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -5,6 +5,8 @@ #include "lauxlib.h" #include "lualib.h" +#include "compat.h" + #define API_TYPE_FONT "Font" #define API_TYPE_REPLACE "Replace" #define API_TYPE_PROCESS "Process" diff --git a/src/api/compat.c b/src/api/compat.c new file mode 100644 index 00000000..e08b5a26 --- /dev/null +++ b/src/api/compat.c @@ -0,0 +1,64 @@ +#include + +#include "compat.h" + +static int lua_absindex (lua_State *L, int i) { + if (i < 0 && i > LUA_REGISTRYINDEX) + i += lua_gettop(L) + 1; + return i; +} + +int luaL_getsubtable (lua_State *L, int i, const char *name) { + int abs_i = lua_absindex(L, i); + luaL_checkstack(L, 3, "not enough stack slots"); + lua_pushstring(L, name); + lua_gettable(L, abs_i); + if (lua_istable(L, -1)) + return 1; + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, name); + lua_pushvalue(L, -2); + lua_settable(L, abs_i); + return 0; +} + +void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) { + luaL_checkstack(L, 3, "not enough stack slots available"); + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, modname); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); + lua_call(L, 1, 1); + lua_pushvalue(L, -1); + lua_setfield(L, -3, modname); + } + if (glb) { + lua_pushvalue(L, -1); + lua_setglobal(L, modname); + } + lua_replace(L, -2); +} + +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)); /* table must be below the upvalues, the name and the closure */ + } + lua_pop(L, nup); /* remove upvalues */ +} + + +void luaL_setmetatable (lua_State *L, const char *tname) { + luaL_checkstack(L, 1, "not enough stack slots"); + luaL_getmetatable(L, tname); + lua_setmetatable(L, -2); +} + diff --git a/src/api/compat.h b/src/api/compat.h new file mode 100644 index 00000000..93220cd5 --- /dev/null +++ b/src/api/compat.h @@ -0,0 +1,17 @@ +#ifndef COMPAT_H +#define COMPAT_H + +#include +#include + +#define luaL_newlibtable(L, l) \ + (lua_createtable(L, 0, sizeof(l)/sizeof(*(l))-1)) +#define luaL_newlib(L, l) \ + (luaL_newlibtable(L, l), luaL_register(L, NULL, l)) + +extern void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb); +extern void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); +extern void luaL_setmetatable (lua_State *L, const char *tname); +extern int luaL_getsubtable (lua_State *L, int i, const char *name); + +#endif diff --git a/src/meson.build b/src/meson.build index 2da04fda..88de22e2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,4 +1,5 @@ lite_sources = [ + 'api/compat.c', 'api/api.c', 'api/cp_replace.c', 'api/renderer.c', diff --git a/subprojects/lua.wrap b/subprojects/lua.wrap deleted file mode 100644 index 1be7895b..00000000 --- a/subprojects/lua.wrap +++ /dev/null @@ -1,4 +0,0 @@ -[wrap-git] -directory = lua -url = https://github.com/franko/lua -revision = v5.2.4-7 diff --git a/subprojects/luajit.wrap b/subprojects/luajit.wrap new file mode 100644 index 00000000..aca831ca --- /dev/null +++ b/subprojects/luajit.wrap @@ -0,0 +1,5 @@ +[wrap-git] +directory = luajit +url = https://github.com/franko/luajit +revision = v2.0.5-lhelper2 +