elua: allow building with interpreted (non-luajit) lua
authorDaniel Kolesa <d.kolesa@samsung.com>
Fri, 29 May 2020 14:22:42 +0000 (16:22 +0200)
committerJongmin Lee <jm105.lee@samsung.com>
Sun, 31 May 2020 21:34:44 +0000 (06:34 +0900)
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
src/lib/elua/cache.c
src/lib/elua/elua.c
src/lib/elua/elua_private.h
src/lib/elua/io.c
src/lib/elua/meson.build

index ed1b407..0c228b6 100644 (file)
@@ -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 */
 }
index c731b04..ff11268 100644 (file)
@@ -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;
      }
index baaed81..250025e 100644 (file)
@@ -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;
 }
index 6ecd7a8..02e99a4 100644 (file)
@@ -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
index 142f61b..15c0c44 100644 (file)
@@ -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;
index 227d211..15b66ae 100644 (file)
@@ -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 <lua.h>', 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']