Compare commits

...

5 Commits

Author SHA1 Message Date
Francesco Abbate f77e0a4e4f Add missing file for lbitlib 2021-10-02 23:57:28 +02:00
Francesco Abbate df9c844a56 Update wrap version for luajit 2021-10-02 23:52:43 +02:00
Francesco Abbate 26dcac82fc Fix luajit compatibility for new code
The more recent version of lite-xl use the bit32 library and
the lua_Buffer functions. These function were not implemented
by the minimal compatiblity layer and are now added with this commit.

The code is adapted from:

https://github.com/keplerproject/lua-compat-5.2
2021-10-02 23:39:04 +02:00
Francesco Abbate 19debaf9ca Fix problem with gmatch on utf8 characters when using luajit 2021-10-02 20:15:03 +02:00
Francesco Abbate a8af0c7acc Initial draft of transition to use LuaJIT2 instead of Lua 5.2
There is problem with utf8 expression matching on common.utf8_chars
2021-10-02 20:14:59 +02:00
13 changed files with 479 additions and 7 deletions

View File

@ -8,7 +8,7 @@ end
function common.utf8_chars(text)
return text:gmatch("[\0-\x7f\xc2-\xf4][\x80-\xbf]*")
return text:gmatch("[\x01-\x7f\xc2-\xf4][\x80-\xbf]*")
end

5
data/core/compat.lua Normal file
View File

@ -0,0 +1,5 @@
table.unpack = unpack
table.pack = function(...)
return { n = select('#', ...), ... }
end

View File

@ -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"

View File

@ -22,6 +22,29 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
## lua-compat-5.2
The MIT License (MIT)
Copyright (c) 2013 Hisham Muhammad
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## Fira Sans
Digitized data copyright (c) 2012-2015, The Mozilla Foundation and Telefonica S.A.

View File

@ -45,8 +45,8 @@ endif
if not get_option('source-only')
libm = cc.find_library('m', required : false)
libdl = cc.find_library('dl', required : false)
lua_dep = dependency('lua5.2', fallback: ['lua', 'lua_dep'],
default_options: ['shared=false', 'use_readline=false', 'app=false']
lua_dep = dependency('luajit', fallback: ['luajit', 'luajit_dep'],
default_options: ['default_library=static']
)
pcre2_dep = dependency('libpcre2-8')
sdl_dep = dependency('sdl2', method: 'config-tool')

View File

@ -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"

143
src/api/compat.c Normal file
View File

@ -0,0 +1,143 @@
#include <string.h>
#include <lauxlib.h>
#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);
}
void luaL_buffinit (lua_State *L, luaL_Buffer_52 *B) {
/* make it crash if used via pointer to a 5.1-style luaL_Buffer */
B->b.p = NULL;
B->b.L = NULL;
B->b.lvl = 0;
/* reuse the buffer from the 5.1-style luaL_Buffer though! */
B->ptr = B->b.buffer;
B->capacity = LUAL_BUFFERSIZE;
B->nelems = 0;
B->L2 = L;
}
char *luaL_prepbuffsize (luaL_Buffer_52 *B, size_t s) {
if (B->capacity - B->nelems < s) { /* needs to grow */
char* newptr = NULL;
size_t newcap = B->capacity * 2;
if (newcap - B->nelems < s)
newcap = B->nelems + s;
if (newcap < B->capacity) /* overflow */
luaL_error(B->L2, "buffer too large");
newptr = lua_newuserdata(B->L2, newcap);
memcpy(newptr, B->ptr, B->nelems);
if (B->ptr != B->b.buffer)
lua_replace(B->L2, -2); /* remove old buffer */
B->ptr = newptr;
B->capacity = newcap;
}
return B->ptr+B->nelems;
}
void luaL_addlstring (luaL_Buffer_52 *B, const char *s, size_t l) {
memcpy(luaL_prepbuffsize(B, l), s, l);
luaL_addsize(B, l);
}
void luaL_addvalue (luaL_Buffer_52 *B) {
size_t len = 0;
const char *s = lua_tolstring(B->L2, -1, &len);
if (!s)
luaL_error(B->L2, "cannot convert value to string");
if (B->ptr != B->b.buffer)
lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
luaL_addlstring(B, s, len);
lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
}
void luaL_pushresult (luaL_Buffer_52 *B) {
lua_pushlstring(B->L2, B->ptr, B->nelems);
if (B->ptr != B->b.buffer)
lua_replace(B->L2, -2); /* remove userdata buffer */
}
#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
#define lua_unsigned2number(u) \
(((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
lua_Unsigned luaL_checkunsigned (lua_State *L, int i) {
lua_Unsigned result;
lua_Number n = lua_tonumber(L, i);
if (n == 0 && !lua_isnumber(L, i))
luaL_checktype(L, i, LUA_TNUMBER);
lua_number2unsigned(result, n);
return result;
}
lua_Unsigned luaL_optunsigned (lua_State *L, int i, lua_Unsigned def) {
return luaL_opt(L, luaL_checkunsigned, i, def);
}
void lua_pushunsigned (lua_State *L, lua_Unsigned n) {
lua_pushnumber(L, lua_unsigned2number(n));
}

73
src/api/compat.h Normal file
View File

@ -0,0 +1,73 @@
/* Adapted from:
** https://github.com/keplerproject/lua-compat-5.2
*/
#ifndef COMPAT_H
#define COMPAT_H
#include <stdint.h>
#include <lua.h>
#include <lauxlib.h>
typedef uint32_t lua_Unsigned;
typedef struct luaL_Buffer_52 {
luaL_Buffer b; /* make incorrect code crash! */
char *ptr;
size_t nelems;
size_t capacity;
lua_State *L2;
} luaL_Buffer_52;
#define luaL_Buffer luaL_Buffer_52
#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);
extern void lua_pushunsigned (lua_State *L, lua_Unsigned n);
extern lua_Unsigned luaL_checkunsigned (lua_State *L, int i);
extern lua_Unsigned luaL_optunsigned (lua_State *L, int i, lua_Unsigned def);
#define lua_rawlen(L, i) lua_objlen(L, i)
#define luaL_buffinit luaL_buffinit_52
void luaL_buffinit (lua_State *L, luaL_Buffer_52 *B);
#define luaL_prepbuffsize luaL_prepbuffsize_52
char *luaL_prepbuffsize (luaL_Buffer_52 *B, size_t s);
#define luaL_addlstring luaL_addlstring_52
void luaL_addlstring (luaL_Buffer_52 *B, const char *s, size_t l);
#define luaL_addvalue luaL_addvalue_52
void luaL_addvalue (luaL_Buffer_52 *B);
#define luaL_pushresult luaL_pushresult_52
void luaL_pushresult (luaL_Buffer_52 *B);
#undef luaL_buffinitsize
#define luaL_buffinitsize(L, B, s) \
(luaL_buffinit(L, B), luaL_prepbuffsize(B, s))
#undef luaL_prepbuffer
#define luaL_prepbuffer(B) \
luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
#undef luaL_addsize
#define luaL_addsize(B, s) \
((B)->nelems += (s))
#undef luaL_addstring
#define luaL_addstring(B, s) \
luaL_addlstring(B, s, strlen(s))
#undef luaL_pushresultsize
#define luaL_pushresultsize(B, s) \
(luaL_addsize(B, s), luaL_pushresult(B))
#endif

218
src/lbitlib.c Normal file
View File

@ -0,0 +1,218 @@
/*
** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp $
** Standard library for bitwise operations
** See Copyright Notice in lua.h
*/
#define lbitlib_c
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "api/compat.h"
/********************************************************************/
/* number of bits to consider in a number */
#if !defined(LUA_NBITS)
#define LUA_NBITS 32
#endif
/*
** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
** be made in two parts to avoid problems when LUA_NBITS is equal to the
** number of bits in a lua_Unsigned.)
*/
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
/* macro to trim extra bits */
#define trim(x) ((x) & ALLONES)
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
static lua_Unsigned andaux (lua_State *L) {
int i, n = lua_gettop(L);
lua_Unsigned r = ~(lua_Unsigned)0;
for (i = 1; i <= n; i++)
r &= luaL_checkunsigned(L, i);
return trim(r);
}
static int b_and (lua_State *L) {
lua_Unsigned r = andaux(L);
lua_pushunsigned(L, r);
return 1;
}
static int b_test (lua_State *L) {
lua_Unsigned r = andaux(L);
lua_pushboolean(L, r != 0);
return 1;
}
static int b_or (lua_State *L) {
int i, n = lua_gettop(L);
lua_Unsigned r = 0;
for (i = 1; i <= n; i++)
r |= luaL_checkunsigned(L, i);
lua_pushunsigned(L, trim(r));
return 1;
}
static int b_xor (lua_State *L) {
int i, n = lua_gettop(L);
lua_Unsigned r = 0;
for (i = 1; i <= n; i++)
r ^= luaL_checkunsigned(L, i);
lua_pushunsigned(L, trim(r));
return 1;
}
static int b_not (lua_State *L) {
lua_Unsigned r = ~luaL_checkunsigned(L, 1);
lua_pushunsigned(L, trim(r));
return 1;
}
static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
if (i < 0) { /* shift right? */
i = -i;
r = trim(r);
if (i >= LUA_NBITS) r = 0;
else r >>= i;
}
else { /* shift left */
if (i >= LUA_NBITS) r = 0;
else r <<= i;
r = trim(r);
}
lua_pushunsigned(L, r);
return 1;
}
static int b_lshift (lua_State *L) {
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2));
}
static int b_rshift (lua_State *L) {
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2));
}
static int b_arshift (lua_State *L) {
lua_Unsigned r = luaL_checkunsigned(L, 1);
lua_Integer i = luaL_checkinteger(L, 2);
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
return b_shift(L, r, -i);
else { /* arithmetic shift for 'negative' number */
if (i >= LUA_NBITS) r = ALLONES;
else
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
lua_pushunsigned(L, r);
return 1;
}
}
static int b_rot (lua_State *L, lua_Integer d) {
lua_Unsigned r = luaL_checkunsigned(L, 1);
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
r = trim(r);
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
r = (r << i) | (r >> (LUA_NBITS - i));
lua_pushunsigned(L, trim(r));
return 1;
}
static int b_lrot (lua_State *L) {
return b_rot(L, luaL_checkinteger(L, 2));
}
static int b_rrot (lua_State *L) {
return b_rot(L, -luaL_checkinteger(L, 2));
}
/*
** get field and width arguments for field-manipulation functions,
** checking whether they are valid.
** ('luaL_error' called without 'return' to avoid later warnings about
** 'width' being used uninitialized.)
*/
static int fieldargs (lua_State *L, int farg, int *width) {
lua_Integer f = luaL_checkinteger(L, farg);
lua_Integer w = luaL_optinteger(L, farg + 1, 1);
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
if (f + w > LUA_NBITS)
luaL_error(L, "trying to access non-existent bits");
*width = (int)w;
return (int)f;
}
static int b_extract (lua_State *L) {
int w;
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
int f = fieldargs(L, 2, &w);
r = (r >> f) & mask(w);
lua_pushunsigned(L, r);
return 1;
}
static int b_replace (lua_State *L) {
int w;
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
lua_Unsigned v = luaL_checkunsigned(L, 2);
int f = fieldargs(L, 3, &w);
int m = mask(w);
v &= m; /* erase bits outside given width */
r = (r & ~(m << f)) | (v << f);
lua_pushunsigned(L, r);
return 1;
}
static const luaL_Reg bitlib[] = {
{"arshift", b_arshift},
{"band", b_and},
{"bnot", b_not},
{"bor", b_or},
{"bxor", b_xor},
{"btest", b_test},
{"extract", b_extract},
{"lrotate", b_lrot},
{"lshift", b_lshift},
{"replace", b_replace},
{"rrotate", b_rrot},
{"rshift", b_rshift},
{NULL, NULL}
};
extern int luaopen_bit32 (lua_State *L) {
luaL_newlib(L, bitlib);
return 1;
}

View File

@ -83,6 +83,8 @@ void set_macos_bundle_resources(lua_State *L);
#endif
#endif
int luaopen_bit32 (lua_State *L);
int main(int argc, char **argv) {
#ifdef _WIN32
HINSTANCE lib = LoadLibrary("user32.dll");
@ -117,6 +119,8 @@ int main(int argc, char **argv) {
init_lua:
L = luaL_newstate();
luaL_openlibs(L);
luaopen_bit32(L);
lua_setglobal(L, "bit32");
api_load_libs(L);

View File

@ -1,4 +1,5 @@
lite_sources = [
'api/compat.c',
'api/api.c',
'api/cp_replace.c',
'api/renderer.c',
@ -6,6 +7,7 @@ lite_sources = [
'api/regex.c',
'api/system.c',
'api/process.c',
'lbitlib.c',
'renderer.c',
'renwindow.c',
'fontdesc.c',

View File

@ -1,4 +0,0 @@
[wrap-git]
directory = lua
url = https://github.com/franko/lua
revision = v5.2.4-7

5
subprojects/luajit.wrap Normal file
View File

@ -0,0 +1,5 @@
[wrap-git]
directory = luajit
url = https://github.com/franko/luajit
revision = v2.0.5-lhelper3