From 5197200ac1d49366f205a62a09a377fdece62ff9 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 29 May 2020 16:22:42 +0200 Subject: [PATCH] elua: allow building with interpreted (non-luajit) lua This doesn't fully work yet as e.g. Eolian bindings require the bitop module only present in luajit for now. In order to build, you will need to install https://github.com/q66/cffi-lua in order to provide the FFI. It needs to be built for the Lua version you are building EFL with. --- src/bin/elua/main.c | 9 ++++++++- src/lib/elua/cache.c | 17 ++++++++++++++--- src/lib/elua/elua.c | 16 ++++++++++++++-- src/lib/elua/elua_private.h | 12 ++++++++++++ src/lib/elua/io.c | 6 +++--- src/lib/elua/meson.build | 7 +++++++ 6 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index ed1b4074a4..0c228b68f0 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -138,6 +138,13 @@ elua_bin_shutdown(Elua_State *es, int c) exit(c); } +#if LUA_VERSION_NUM < 502 +# define elua_cpcall(L, f, u) lua_cpcall(L, f, u) +#else +# define elua_cpcall(L, f, u) \ + (lua_pushcfunction(L, f), lua_pushlightuserdata(L, u), lua_pcall(L, 1, 0, 0)) +#endif + int main(int argc, char **argv) { @@ -175,7 +182,7 @@ main(int argc, char **argv) m.argv = argv; m.status = 0; - elua_bin_shutdown(es, !!(lua_cpcall(elua_state_lua_state_get(es), elua_main, &m) || m.status)); + elua_bin_shutdown(es, !!(elua_cpcall(elua_state_lua_state_get(es), elua_main, &m) || m.status)); return 0; /* never gets here */ } diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index c731b0495a..ff11268ab2 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -7,6 +7,12 @@ /* bytecode caching */ +#if LUA_VERSION_NUM > 501 +# define elua_load(L, reader, data, chunkname) lua_load(L, reader, data, chunkname, NULL) +#else +# define elua_load(L, reader, data, chunkname) lua_load(L, reader, data, chunkname) +#endif + static Eina_File * check_bc(Eina_File *of, const char *fname, Eina_Bool *bc) { @@ -108,7 +114,7 @@ static int elua_loadstdin(lua_State *L) { char buff[LUAL_BUFFERSIZE]; - int status = lua_load(L, getf, &buff, "=stdin"); + int status = elua_load(L, getf, &buff, "=stdin"); if (ferror(stdin)) { lua_pop(L, 1); @@ -163,7 +169,7 @@ elua_io_loadfile(const Elua_State *es, const char *fname) lua_remove(L, -2); return LUA_ERRFILE; } - status = lua_load(L, getf_map, &s, chname); + status = elua_load(L, getf_map, &s, chname); eina_file_map_free(f, s.fmap); eina_file_close(f); if (status) @@ -186,7 +192,7 @@ elua_io_loadfile(const Elua_State *es, const char *fname) } /* loaded original file, pop old error and load again */ lua_pop(L, 1); - status = lua_load(L, getf_map, &s, chname); + status = elua_load(L, getf_map, &s, chname); eina_file_map_free(f, s.fmap); eina_file_close(f); /* force write new bytecode */ @@ -215,7 +221,12 @@ loadfile(lua_State *L) if (hasenv) { lua_pushvalue(L, 3); +#if LUA_VERSION_NUM < 502 lua_setfenv(L, -2); +#else + if (!lua_setupvalue(L, -2, 1)) + lua_pop(L, 1); +#endif } return 1; } diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index baaed81721..250025e970 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -66,6 +66,10 @@ elua_shutdown(void) return _elua_init_counter; } +#ifdef ENABLE_LUA_OLD +int luaopen_cffi(lua_State *L); +#endif + EAPI Elua_State * elua_state_new(const char *progname) { @@ -77,6 +81,14 @@ elua_state_new(const char *progname) ret->luastate = L; if (progname) ret->progname = eina_stringshare_add(progname); luaL_openlibs(L); +#ifdef ENABLE_LUA_OLD + /* make sure to inject cffi-lua to preload so that the system gets it */ + lua_getglobal(L, "package"); + lua_getfield(L, -1, "preload"); + lua_pushcfunction(L, luaopen_cffi); + lua_setfield(L, -2, "ffi"); + lua_pop(L, 2); +#endif /* on 64-bit, split the state pointer into two and reconstruct later */ size_t retn = (size_t)ret; if (sizeof(void *) < sizeof(lua_Number)) @@ -424,7 +436,7 @@ _elua_state_i18n_setup(Elua_State *es) if (elua_util_error_report(es, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_createtable(es->luastate, 0, 0); - luaL_register(es->luastate, NULL, gettextlib); + elua_register(es->luastate, gettextlib); lua_call(es->luastate, 1, 0); return EINA_TRUE; } @@ -507,7 +519,7 @@ _elua_state_modules_setup(const Elua_State *es) return EINA_FALSE; lua_pushcfunction(es->luastate, _elua_module_system_init); lua_createtable(es->luastate, 0, 0); - luaL_register(es->luastate, NULL, _elua_cutillib); + elua_register(es->luastate, _elua_cutillib); lua_call(es->luastate, 2, 0); return EINA_TRUE; } diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index 6ecd7a80f0..02e99a481b 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -50,4 +50,16 @@ extern int _elua_log_dom; int _elua_io_popen(lua_State *L); Eina_Bool _elua_state_io_setup(const Elua_State *es); +#if LUA_VERSION_NUM < 502 + +#define elua_register(L, lib) luaL_register(L, NULL, 0) +#define elua_rawlen(L, i) lua_rawlen(L, i) + +#else + +#define elua_register(L, lib) luaL_setfuncs(L, lib, 0) +#define elua_rawlen(L, i) lua_rawlen(L, i) + +#endif + #endif diff --git a/src/lib/elua/io.c b/src/lib/elua/io.c index 142f61b716..15c0c44924 100644 --- a/src/lib/elua/io.c +++ b/src/lib/elua/io.c @@ -167,7 +167,7 @@ read_line(lua_State *L, FILE *f) if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { luaL_pushresult(&b); - return (lua_strlen(L, -1) > 0); + return (elua_rawlen(L, -1) > 0); } l = strlen(p); if (!l || p[l - 1] != '\n') @@ -198,7 +198,7 @@ read_chars(lua_State *L, FILE *f, size_t n) n -= nr; } while (n > 0 && nr == rlen); luaL_pushresult(&b); - return (n == 0 || lua_strlen(L, -1) > 0); + return (n == 0 || elua_rawlen(L, -1) > 0); } static int @@ -338,7 +338,7 @@ elua_newfile(lua_State *L) { lua_pushvalue(L, -1); lua_setfield (L, -2, "__index"); - luaL_register(L, NULL, elua_popenlib); + elua_register(L, elua_popenlib); } lua_setmetatable(L, -2); return f; diff --git a/src/lib/elua/meson.build b/src/lib/elua/meson.build index 227d211584..15b66aefdb 100644 --- a/src/lib/elua/meson.build +++ b/src/lib/elua/meson.build @@ -1,6 +1,13 @@ elua_deps = [eina, eo, efl, ecore, ecore_file, intl] elua_pub_deps = [lua] +if get_option('lua-interpreter') == 'lua' + luaver_min = cc.compute_int('LUA_VERSION_NUM - 500', + prefix: '#include ', dependencies: lua + ) + elua_deps += dependency('cffi-lua-5.@0@'.format(luaver_min)) +endif + elua_src = ['elua.c', 'io.c', 'cache.c'] elua_header_src = ['Elua.h']