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
This commit is contained in:
Francesco Abbate 2021-10-02 23:39:04 +02:00
parent a76b3a6f0e
commit 09a91d6d0b
7 changed files with 167 additions and 1 deletions

View File

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

View File

@ -49,6 +49,29 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## 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

@ -47,7 +47,7 @@ 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('luajit', fallback: ['luajit', 'lua_dep'],
lua_dep = dependency('luajit', fallback: ['luajit', 'luajit_dep'],
default_options: ['default_library=static']
)
pcre2_dep = dependency('libpcre2-8')

View File

@ -1,3 +1,5 @@
#include <string.h>
#include <lauxlib.h>
#include "compat.h"
@ -62,3 +64,80 @@ void luaL_setmetatable (lua_State *L, const char *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));
}

View File

@ -1,9 +1,25 @@
/* 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) \
@ -13,5 +29,45 @@ extern void luaL_requiref (lua_State *L, const char *modname, lua_CFunction open
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

View File

@ -85,6 +85,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");
@ -121,6 +123,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

@ -8,6 +8,7 @@ lite_sources = [
'api/system.c',
'api/process.c',
'dirmonitor.c',
'lbitlib.c',
'renderer.c',
'renwindow.c',
'fontdesc.c',