diff --git a/data/core/compat.lua b/data/core/compat.lua index d66af313..1e2a5504 100644 --- a/data/core/compat.lua +++ b/data/core/compat.lua @@ -1,2 +1,5 @@ table.unpack = unpack +table.pack = function(...) + return { n = select('#', ...), ... } +end diff --git a/licenses/licenses.md b/licenses/licenses.md index 8005c4a7..6b74c996 100644 --- a/licenses/licenses.md +++ b/licenses/licenses.md @@ -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. diff --git a/meson.build b/meson.build index 3be3479a..d78a103e 100644 --- a/meson.build +++ b/meson.build @@ -45,7 +45,7 @@ endif if not get_option('source-only') libm = cc.find_library('m', required : false) libdl = cc.find_library('dl', required : false) - 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') diff --git a/src/api/compat.c b/src/api/compat.c index e08b5a26..34d0b07c 100644 --- a/src/api/compat.c +++ b/src/api/compat.c @@ -1,3 +1,5 @@ +#include + #include #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)); +} + diff --git a/src/api/compat.h b/src/api/compat.h index 93220cd5..25b8f96e 100644 --- a/src/api/compat.h +++ b/src/api/compat.h @@ -1,9 +1,25 @@ +/* Adapted from: +** https://github.com/keplerproject/lua-compat-5.2 +*/ #ifndef COMPAT_H #define COMPAT_H +#include + #include #include +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 diff --git a/src/main.c b/src/main.c index 182511d0..4dc0d8b7 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/meson.build b/src/meson.build index b85ab1bb..b9e0d796 100644 --- a/src/meson.build +++ b/src/meson.build @@ -7,6 +7,7 @@ lite_sources = [ 'api/regex.c', 'api/system.c', 'api/process.c', + 'lbitlib.c', 'renderer.c', 'renwindow.c', 'fontdesc.c',