summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@samsung.com>2020-05-29 16:22:42 +0200
committerDaniel Kolesa <d.kolesa@samsung.com>2020-05-29 16:40:05 +0200
commit5197200ac1d49366f205a62a09a377fdece62ff9 (patch)
tree2ee8965984d211e0f4f38b77a11f8babb3ca49dc
parent0b5a8fc127ab282648828304fe06a2652dc3553a (diff)
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.
-rw-r--r--src/bin/elua/main.c9
-rw-r--r--src/lib/elua/cache.c17
-rw-r--r--src/lib/elua/elua.c16
-rw-r--r--src/lib/elua/elua_private.h12
-rw-r--r--src/lib/elua/io.c6
-rw-r--r--src/lib/elua/meson.build7
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)
138 exit(c); 138 exit(c);
139} 139}
140 140
141#if LUA_VERSION_NUM < 502
142# define elua_cpcall(L, f, u) lua_cpcall(L, f, u)
143#else
144# define elua_cpcall(L, f, u) \
145 (lua_pushcfunction(L, f), lua_pushlightuserdata(L, u), lua_pcall(L, 1, 0, 0))
146#endif
147
141int 148int
142main(int argc, char **argv) 149main(int argc, char **argv)
143{ 150{
@@ -175,7 +182,7 @@ main(int argc, char **argv)
175 m.argv = argv; 182 m.argv = argv;
176 m.status = 0; 183 m.status = 0;
177 184
178 elua_bin_shutdown(es, !!(lua_cpcall(elua_state_lua_state_get(es), elua_main, &m) || m.status)); 185 elua_bin_shutdown(es, !!(elua_cpcall(elua_state_lua_state_get(es), elua_main, &m) || m.status));
179 186
180 return 0; /* never gets here */ 187 return 0; /* never gets here */
181} 188}
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 @@
7 7
8/* bytecode caching */ 8/* bytecode caching */
9 9
10#if LUA_VERSION_NUM > 501
11# define elua_load(L, reader, data, chunkname) lua_load(L, reader, data, chunkname, NULL)
12#else
13# define elua_load(L, reader, data, chunkname) lua_load(L, reader, data, chunkname)
14#endif
15
10static Eina_File * 16static Eina_File *
11check_bc(Eina_File *of, const char *fname, Eina_Bool *bc) 17check_bc(Eina_File *of, const char *fname, Eina_Bool *bc)
12{ 18{
@@ -108,7 +114,7 @@ static int
108elua_loadstdin(lua_State *L) 114elua_loadstdin(lua_State *L)
109{ 115{
110 char buff[LUAL_BUFFERSIZE]; 116 char buff[LUAL_BUFFERSIZE];
111 int status = lua_load(L, getf, &buff, "=stdin"); 117 int status = elua_load(L, getf, &buff, "=stdin");
112 if (ferror(stdin)) 118 if (ferror(stdin))
113 { 119 {
114 lua_pop(L, 1); 120 lua_pop(L, 1);
@@ -163,7 +169,7 @@ elua_io_loadfile(const Elua_State *es, const char *fname)
163 lua_remove(L, -2); 169 lua_remove(L, -2);
164 return LUA_ERRFILE; 170 return LUA_ERRFILE;
165 } 171 }
166 status = lua_load(L, getf_map, &s, chname); 172 status = elua_load(L, getf_map, &s, chname);
167 eina_file_map_free(f, s.fmap); 173 eina_file_map_free(f, s.fmap);
168 eina_file_close(f); 174 eina_file_close(f);
169 if (status) 175 if (status)
@@ -186,7 +192,7 @@ elua_io_loadfile(const Elua_State *es, const char *fname)
186 } 192 }
187 /* loaded original file, pop old error and load again */ 193 /* loaded original file, pop old error and load again */
188 lua_pop(L, 1); 194 lua_pop(L, 1);
189 status = lua_load(L, getf_map, &s, chname); 195 status = elua_load(L, getf_map, &s, chname);
190 eina_file_map_free(f, s.fmap); 196 eina_file_map_free(f, s.fmap);
191 eina_file_close(f); 197 eina_file_close(f);
192 /* force write new bytecode */ 198 /* force write new bytecode */
@@ -215,7 +221,12 @@ loadfile(lua_State *L)
215 if (hasenv) 221 if (hasenv)
216 { 222 {
217 lua_pushvalue(L, 3); 223 lua_pushvalue(L, 3);
224#if LUA_VERSION_NUM < 502
218 lua_setfenv(L, -2); 225 lua_setfenv(L, -2);
226#else
227 if (!lua_setupvalue(L, -2, 1))
228 lua_pop(L, 1);
229#endif
219 } 230 }
220 return 1; 231 return 1;
221 } 232 }
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)
66 return _elua_init_counter; 66 return _elua_init_counter;
67} 67}
68 68
69#ifdef ENABLE_LUA_OLD
70int luaopen_cffi(lua_State *L);
71#endif
72
69EAPI Elua_State * 73EAPI Elua_State *
70elua_state_new(const char *progname) 74elua_state_new(const char *progname)
71{ 75{
@@ -77,6 +81,14 @@ elua_state_new(const char *progname)
77 ret->luastate = L; 81 ret->luastate = L;
78 if (progname) ret->progname = eina_stringshare_add(progname); 82 if (progname) ret->progname = eina_stringshare_add(progname);
79 luaL_openlibs(L); 83 luaL_openlibs(L);
84#ifdef ENABLE_LUA_OLD
85 /* make sure to inject cffi-lua to preload so that the system gets it */
86 lua_getglobal(L, "package");
87 lua_getfield(L, -1, "preload");
88 lua_pushcfunction(L, luaopen_cffi);
89 lua_setfield(L, -2, "ffi");
90 lua_pop(L, 2);
91#endif
80 /* on 64-bit, split the state pointer into two and reconstruct later */ 92 /* on 64-bit, split the state pointer into two and reconstruct later */
81 size_t retn = (size_t)ret; 93 size_t retn = (size_t)ret;
82 if (sizeof(void *) < sizeof(lua_Number)) 94 if (sizeof(void *) < sizeof(lua_Number))
@@ -424,7 +436,7 @@ _elua_state_i18n_setup(Elua_State *es)
424 if (elua_util_error_report(es, elua_io_loadfile(es, buf))) 436 if (elua_util_error_report(es, elua_io_loadfile(es, buf)))
425 return EINA_FALSE; 437 return EINA_FALSE;
426 lua_createtable(es->luastate, 0, 0); 438 lua_createtable(es->luastate, 0, 0);
427 luaL_register(es->luastate, NULL, gettextlib); 439 elua_register(es->luastate, gettextlib);
428 lua_call(es->luastate, 1, 0); 440 lua_call(es->luastate, 1, 0);
429 return EINA_TRUE; 441 return EINA_TRUE;
430} 442}
@@ -507,7 +519,7 @@ _elua_state_modules_setup(const Elua_State *es)
507 return EINA_FALSE; 519 return EINA_FALSE;
508 lua_pushcfunction(es->luastate, _elua_module_system_init); 520 lua_pushcfunction(es->luastate, _elua_module_system_init);
509 lua_createtable(es->luastate, 0, 0); 521 lua_createtable(es->luastate, 0, 0);
510 luaL_register(es->luastate, NULL, _elua_cutillib); 522 elua_register(es->luastate, _elua_cutillib);
511 lua_call(es->luastate, 2, 0); 523 lua_call(es->luastate, 2, 0);
512 return EINA_TRUE; 524 return EINA_TRUE;
513} 525}
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;
50int _elua_io_popen(lua_State *L); 50int _elua_io_popen(lua_State *L);
51Eina_Bool _elua_state_io_setup(const Elua_State *es); 51Eina_Bool _elua_state_io_setup(const Elua_State *es);
52 52
53#if LUA_VERSION_NUM < 502
54
55#define elua_register(L, lib) luaL_register(L, NULL, 0)
56#define elua_rawlen(L, i) lua_rawlen(L, i)
57
58#else
59
60#define elua_register(L, lib) luaL_setfuncs(L, lib, 0)
61#define elua_rawlen(L, i) lua_rawlen(L, i)
62
63#endif
64
53#endif 65#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)
167 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) 167 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL)
168 { 168 {
169 luaL_pushresult(&b); 169 luaL_pushresult(&b);
170 return (lua_strlen(L, -1) > 0); 170 return (elua_rawlen(L, -1) > 0);
171 } 171 }
172 l = strlen(p); 172 l = strlen(p);
173 if (!l || p[l - 1] != '\n') 173 if (!l || p[l - 1] != '\n')
@@ -198,7 +198,7 @@ read_chars(lua_State *L, FILE *f, size_t n)
198 n -= nr; 198 n -= nr;
199 } while (n > 0 && nr == rlen); 199 } while (n > 0 && nr == rlen);
200 luaL_pushresult(&b); 200 luaL_pushresult(&b);
201 return (n == 0 || lua_strlen(L, -1) > 0); 201 return (n == 0 || elua_rawlen(L, -1) > 0);
202} 202}
203 203
204static int 204static int
@@ -338,7 +338,7 @@ elua_newfile(lua_State *L)
338 { 338 {
339 lua_pushvalue(L, -1); 339 lua_pushvalue(L, -1);
340 lua_setfield (L, -2, "__index"); 340 lua_setfield (L, -2, "__index");
341 luaL_register(L, NULL, elua_popenlib); 341 elua_register(L, elua_popenlib);
342 } 342 }
343 lua_setmetatable(L, -2); 343 lua_setmetatable(L, -2);
344 return f; 344 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 @@
1elua_deps = [eina, eo, efl, ecore, ecore_file, intl] 1elua_deps = [eina, eo, efl, ecore, ecore_file, intl]
2elua_pub_deps = [lua] 2elua_pub_deps = [lua]
3 3
4if get_option('lua-interpreter') == 'lua'
5 luaver_min = cc.compute_int('LUA_VERSION_NUM - 500',
6 prefix: '#include <lua.h>', dependencies: lua
7 )
8 elua_deps += dependency('cffi-lua-5.@0@'.format(luaver_min))
9endif
10
4elua_src = ['elua.c', 'io.c', 'cache.c'] 11elua_src = ['elua.c', 'io.c', 'cache.c']
5elua_header_src = ['Elua.h'] 12elua_header_src = ['Elua.h']
6 13