From e0917331f63d06805708243ab9e02958ab4b620f Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 13:56:19 +0100 Subject: [PATCH 01/91] elua lib: add Elua_State (used later to hold elua-specific data) --- src/lib/elua/Elua.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 8df01de3cc..7fdd19fa85 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -58,6 +58,11 @@ extern "C" { #include #include +typedef struct _Elua_State +{ + lua_State *luastate; +} Elua_State; + EAPI int elua_init(void); EAPI int elua_shutdown(void); From bf4837dbbfdb300372b9fe7a57378e580e4dad23 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:00:57 +0100 Subject: [PATCH 02/91] elua lib: add elua_state_new and elua_state_free --- src/lib/elua/Elua.h | 3 +++ src/lib/elua/elua.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 7fdd19fa85..69c72b05b9 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -66,6 +66,9 @@ typedef struct _Elua_State EAPI int elua_init(void); EAPI int elua_shutdown(void); +EAPI Elua_State *elua_state_new(void); +EAPI void elua_state_free(Elua_State *state); + EAPI int elua_report_error(lua_State *L, const char *pname, int status); EAPI void elua_state_setup_i18n(lua_State *L); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 2f88e7be02..a6bb3914cd 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -62,6 +62,27 @@ elua_shutdown(void) return _elua_init_counter; } +EAPI Elua_State * +elua_state_new(void) +{ + Elua_State *ret = NULL; + lua_State *L = luaL_newstate(); + if (!L) + return NULL; + ret = malloc(sizeof(Elua_State)); + ret->luastate = L; + luaL_openlibs(L); + return ret; +} + +EAPI void +elua_state_free(Elua_State *state) +{ + if (!state) return; + if (state->luastate) lua_close(state->luastate); + free(state); +} + static void _elua_errmsg(const char *pname, const char *msg) { From e0066ca9e7d7ad1c667d17d7d7a6e84f29f1a546 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:14:49 +0100 Subject: [PATCH 03/91] elua: use elua_state_new/free --- src/bin/elua/main.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 3716a51984..7b63461522 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -269,37 +269,37 @@ elua_doscript(lua_State *L, int argc, char **argv, int n, int *quit) } void -elua_bin_shutdown(lua_State *L, int c) +elua_bin_shutdown(Elua_State *es, int c) { void *data; INF("elua shutdown"); - EINA_LIST_FREE(elua_modlist, data) + if (es) EINA_LIST_FREE(elua_modlist, data) { - lua_rawgeti(L, LUA_REGISTRYINDEX, (size_t)data); - lua_call(L, 0, 0); + lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, (size_t)data); + lua_call(es->luastate, 0, 0); } if (elua_prefix) eina_prefix_free(elua_prefix); - if (L) lua_close(L); + if (es) elua_state_free(es); if (_el_log_domain != EINA_LOG_DOMAIN_GLOBAL) eina_log_domain_unregister(_el_log_domain); elua_shutdown(); exit(c); } -static int elua_cb_ref = LUA_REFNIL; -static lua_State *elua_state = NULL; +static int elua_cb_ref = LUA_REFNIL; +static Elua_State *elua_state = NULL; static void elua_smart_cb_wrapper(void *data, void *obj EINA_UNUSED, void *einfo EINA_UNUSED) { int idx = (size_t)data; - lua_rawgeti(elua_state, LUA_REGISTRYINDEX, elua_cb_ref); - lua_rawgeti(elua_state, -1, idx); - lua_call(elua_state, 0, 0); - lua_pop(elua_state, 1); + lua_rawgeti(elua_state->luastate, LUA_REGISTRYINDEX, elua_cb_ref); + lua_rawgeti(elua_state->luastate, -1, idx); + lua_call(elua_state->luastate, 0, 0); + lua_pop(elua_state->luastate, 1); } static int @@ -530,7 +530,7 @@ int main(int argc, char **argv) { struct Main_Data m; - lua_State *L = NULL; + Elua_State *es = NULL; #ifdef ENABLE_NLS setlocale(LC_ALL, ""); @@ -550,13 +550,13 @@ main(int argc, char **argv) INF("elua logging initialized: %d", _el_log_domain); - if (!(L = luaL_newstate())) + if (!(es = elua_state_new())) { ERR("could not initialize elua state."); - elua_bin_shutdown(L, 1); + elua_bin_shutdown(es, 1); } - elua_state = L; + elua_state = es; INF("elua lua state created"); @@ -564,7 +564,7 @@ main(int argc, char **argv) m.argv = argv; m.status = 0; - elua_bin_shutdown(L, !!(lua_cpcall(L, elua_main, &m) || m.status)); + elua_bin_shutdown(es, !!(lua_cpcall(es->luastate, elua_main, &m) || m.status)); return 0; /* never gets here */ } From 357d902b87d0b0e74f3db400f77db985f8add19f Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:29:58 +0100 Subject: [PATCH 04/91] elua lib: add elua_state_from_lua_get --- src/lib/elua/Elua.h | 2 ++ src/lib/elua/elua.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 69c72b05b9..2f1f41af68 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -69,6 +69,8 @@ EAPI int elua_shutdown(void); EAPI Elua_State *elua_state_new(void); EAPI void elua_state_free(Elua_State *state); +EAPI Elua_State *elua_state_from_lua_get(lua_State *L); + EAPI int elua_report_error(lua_State *L, const char *pname, int status); EAPI void elua_state_setup_i18n(lua_State *L); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index a6bb3914cd..bdd5606fed 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -72,6 +72,8 @@ elua_state_new(void) ret = malloc(sizeof(Elua_State)); ret->luastate = L; luaL_openlibs(L); + lua_pushlightuserdata(L, ret); + lua_setfield(L, LUA_REGISTRYINDEX, "elua_ptr"); return ret; } @@ -83,6 +85,20 @@ elua_state_free(Elua_State *state) free(state); } +EAPI Elua_State * +elua_state_from_lua_get(lua_State *L) +{ + lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr"); + if (!lua_isnil(L, -1)) + { + void *st = lua_touserdata(L, -1); + lua_pop(L, 1); + return (Elua_State *)st; + } + lua_pop(L, 1); + return NULL; +} + static void _elua_errmsg(const char *pname, const char *msg) { From c1487c8b11ebcca01c28111ac5dcf742e0e61276 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:43:52 +0100 Subject: [PATCH 05/91] elua: elua_report_error now takes Elua_State* --- src/bin/elua/main.c | 42 ++++++++++++++++++++++++------------------ src/lib/elua/Elua.h | 2 +- src/lib/elua/elua.c | 9 +++++---- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 7b63461522..43d62c6936 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -70,9 +70,10 @@ elua_traceback(lua_State *L) } static int -elua_docall(lua_State *L, int narg, int nret) +elua_docall(Elua_State *es, int narg, int nret) { int status; + lua_State *L = es->luastate; int bs = lua_gettop(L) - narg; lua_pushcfunction(L, elua_traceback); lua_insert(L, bs); @@ -188,25 +189,29 @@ elua_register_require(lua_State *L) } static int -elua_dolib(lua_State *L, const char *libname) +elua_dolib(Elua_State *es, const char *libname) { + lua_State *L = es->luastate; lua_rawgeti(L, LUA_REGISTRYINDEX, elua_require_ref); lua_pushstring(L, libname); - return elua_report_error(L, elua_progname, lua_pcall(L, 1, 0, 0)); + return elua_report_error(es, elua_progname, lua_pcall(L, 1, 0, 0)); } static int -elua_dofile(lua_State *L, const char *fname) +elua_dofile(Elua_State *es, const char *fname) { - return elua_report_error(L, elua_progname, elua_io_loadfile(L, fname) - || elua_docall(L, 0, 1)); + return elua_report_error(es, elua_progname, + elua_io_loadfile(es->luastate, fname) + || elua_docall(es, 0, 1)); } static int -elua_dostr(lua_State *L, const char *chunk, const char *chname) +elua_dostr(Elua_State *es, const char *chunk, const char *chname) { - return elua_report_error(L, elua_progname, luaL_loadbuffer(L, chunk, - strlen(chunk), chname) || elua_docall(L, 0, 0)); + return elua_report_error(es, elua_progname, + luaL_loadbuffer(es->luastate, chunk, strlen(chunk), + chname) + || elua_docall(es, 0, 0)); } static Eina_Bool @@ -225,9 +230,10 @@ elua_loadapp(lua_State *L, const char *appname) } static int -elua_doscript(lua_State *L, int argc, char **argv, int n, int *quit) +elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) { int status; + lua_State *L = es->luastate; const char *fname = argv[n]; int narg = elua_getargs(L, argc, argv, n); lua_setglobal(L, "arg"); @@ -254,7 +260,7 @@ elua_doscript(lua_State *L, int argc, char **argv, int n, int *quit) lua_insert(L, -(narg + 1)); if (!status) { - status = elua_docall(L, narg, 1); + status = elua_docall(es, narg, 1); } else { @@ -265,7 +271,7 @@ elua_doscript(lua_State *L, int argc, char **argv, int n, int *quit) *quit = lua_toboolean(L, -1); lua_pop(L, 1); } - return elua_report_error(L, elua_progname, status); + return elua_report_error(es, elua_progname, status); } void @@ -448,7 +454,7 @@ elua_main(lua_State *L) } } snprintf(modfile, sizeof(modfile), "%s/module.lua", coref); - if (elua_report_error(L, elua_progname, elua_io_loadfile(L, modfile))) + if (elua_report_error(elua_state, elua_progname, elua_io_loadfile(L, modfile))) { m->status = 1; return 0; @@ -464,7 +470,7 @@ elua_main(lua_State *L) lua_call(L, 2, 0); snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref); - if (elua_report_error(L, elua_progname, elua_io_loadfile(L, modfile))) + if (elua_report_error(elua_state, elua_progname, elua_io_loadfile(L, modfile))) { m->status = 1; return 0; @@ -484,14 +490,14 @@ elua_main(lua_State *L) { case ARG_CODE: if (!hasexec) hasexec = EINA_TRUE; - if (elua_dostr(L, data->value, "=(command line)")) + if (elua_dostr(elua_state, data->value, "=(command line)")) { m->status = 1; return 0; } break; case ARG_LIBRARY: - if (elua_dolib(L, data->value)) + if (elua_dolib(elua_state, data->value)) { m->status = 1; return 0; @@ -509,13 +515,13 @@ elua_main(lua_State *L) if (optind < argc) { int quit = 0; - if ((m->status = elua_doscript(L, argc, argv, optind, &quit))) return 0; + if ((m->status = elua_doscript(elua_state, argc, argv, optind, &quit))) return 0; if (quit) return 0; } else if (!hasexec) { int quit; - if ((m->status = elua_dofile(L, NULL))) return 0; + if ((m->status = elua_dofile(elua_state, NULL))) return 0; quit = lua_toboolean(L, -1); lua_pop(L, 1); if (quit) return 0; diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 2f1f41af68..623c9925b8 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -71,7 +71,7 @@ EAPI void elua_state_free(Elua_State *state); EAPI Elua_State *elua_state_from_lua_get(lua_State *L); -EAPI int elua_report_error(lua_State *L, const char *pname, int status); +EAPI int elua_report_error(Elua_State *es, const char *pname, int status); EAPI void elua_state_setup_i18n(lua_State *L); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index bdd5606fed..957d4e4bbe 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -106,13 +106,14 @@ _elua_errmsg(const char *pname, const char *msg) } EAPI int -elua_report_error(lua_State *L, const char *pname, int status) +elua_report_error(Elua_State *es, const char *pname, int status) { - if (status && !lua_isnil(L, -1)) + if (!es || !es->luastate) return status; + if (status && !lua_isnil(es->luastate, -1)) { - const char *msg = lua_tostring(L, -1); + const char *msg = lua_tostring(es->luastate, -1); _elua_errmsg(pname, msg ? msg : "(non-string error)"); - lua_pop(L, 1); + lua_pop(es->luastate, 1); } return status; } From 17a54ff8e0b7c4430c510713ec636160175dbea8 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:46:31 +0100 Subject: [PATCH 06/91] elua: use more Elua_State --- src/bin/elua/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 43d62c6936..9750325ef8 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -85,8 +85,9 @@ elua_docall(Elua_State *es, int narg, int nret) } static int -elua_getargs(lua_State *L, int argc, char **argv, int n) +elua_getargs(Elua_State *es, int argc, char **argv, int n) { + lua_State *L = es->luastate; int i; int narg = argc - (n + 1); luaL_checkstack(L, narg + 3, "too many arguments to script"); @@ -235,7 +236,7 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) int status; lua_State *L = es->luastate; const char *fname = argv[n]; - int narg = elua_getargs(L, argc, argv, n); + int narg = elua_getargs(es, argc, argv, n); lua_setglobal(L, "arg"); if (fname[0] == '-' && !fname[1]) { From cf98f846e90f3c48da7514f3f612bf4e351db5c5 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:52:06 +0100 Subject: [PATCH 07/91] elua lib: Elua_State for elua_state_setup_i18n --- src/bin/elua/main.c | 2 +- src/lib/elua/Elua.h | 4 ++-- src/lib/elua/elua.c | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 9750325ef8..e184734784 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -476,7 +476,7 @@ elua_main(lua_State *L) m->status = 1; return 0; } - elua_state_setup_i18n(L); + elua_state_setup_i18n(elua_state); lua_call(L, 1, 0); elua_io_register(L); diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 623c9925b8..2b06a805c0 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -67,13 +67,13 @@ EAPI int elua_init(void); EAPI int elua_shutdown(void); EAPI Elua_State *elua_state_new(void); -EAPI void elua_state_free(Elua_State *state); +EAPI void elua_state_free(Elua_State *es); EAPI Elua_State *elua_state_from_lua_get(lua_State *L); EAPI int elua_report_error(Elua_State *es, const char *pname, int status); -EAPI void elua_state_setup_i18n(lua_State *L); +EAPI void elua_state_setup_i18n(Elua_State *es); EAPI int elua_io_popen(lua_State *L); EAPI int elua_io_loadfile(lua_State *L, const char *fname); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 957d4e4bbe..6601953f27 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -78,11 +78,11 @@ elua_state_new(void) } EAPI void -elua_state_free(Elua_State *state) +elua_state_free(Elua_State *es) { - if (!state) return; - if (state->luastate) lua_close(state->luastate); - free(state); + if (!es) return; + if (es->luastate) lua_close(es->luastate); + free(es); } EAPI Elua_State * @@ -153,19 +153,19 @@ const luaL_reg gettextlib[] = }; EAPI void -elua_state_setup_i18n(lua_State *L) +elua_state_setup_i18n(Elua_State *es) { #ifdef ENABLE_NLS char *(*dgettextp)(const char*, const char*) = dgettext; char *(*dngettextp)(const char*, const char*, const char*, unsigned long) = dngettext; #endif - lua_createtable(L, 0, 0); - luaL_register(L, NULL, gettextlib); + lua_createtable(es->luastate, 0, 0); + luaL_register(es->luastate, NULL, gettextlib); #ifdef ENABLE_NLS - lua_pushlightuserdata(L, *((void**)&dgettextp)); - lua_setfield(L, -2, "dgettext"); - lua_pushlightuserdata(L, *((void**)&dngettextp)); - lua_setfield(L, -2, "dngettext"); + lua_pushlightuserdata(es->luastate, *((void**)&dgettextp)); + lua_setfield(es->luastate, -2, "dgettext"); + lua_pushlightuserdata(es->luastate, *((void**)&dngettextp)); + lua_setfield(es->luastate, -2, "dngettext"); #endif } From 446fdd68c91930d47b41177a8188c4073f39c01f Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 14:54:43 +0100 Subject: [PATCH 08/91] elua: Elua_state for elua_loadapp --- src/bin/elua/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index e184734784..81735218ad 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -216,8 +216,9 @@ elua_dostr(Elua_State *es, const char *chunk, const char *chname) } static Eina_Bool -elua_loadapp(lua_State *L, const char *appname) +elua_loadapp(Elua_State *es, const char *appname) { + lua_State *L = es->luastate; lua_rawgeti(L, LUA_REGISTRYINDEX, elua_appload_ref); lua_pushstring(L, appname); lua_call(L, 1, 2); @@ -252,7 +253,7 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) status = elua_io_loadfile(L, fname); } else - status = !elua_loadapp(L, fname); + status = !elua_loadapp(es, fname); } else { From 1dcc62c7c2b3e73b31bbd874a8889ab7400dddde Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 15:03:30 +0100 Subject: [PATCH 09/91] elua: pass Elua_State through Main_Data --- src/bin/elua/main.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 81735218ad..d5b2f740b3 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -323,9 +323,10 @@ elua_register_callbacks(lua_State *L) struct Main_Data { - int argc; - char **argv; - int status; + Elua_State *es; + int argc; + char **argv; + int status; }; const luaL_reg cutillib[] = @@ -385,6 +386,7 @@ elua_main(lua_State *L) int ch; struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1); + Elua_State *es = m->es; int argc = m->argc; char **argv = m->argv; @@ -456,7 +458,7 @@ elua_main(lua_State *L) } } snprintf(modfile, sizeof(modfile), "%s/module.lua", coref); - if (elua_report_error(elua_state, elua_progname, elua_io_loadfile(L, modfile))) + if (elua_report_error(es, elua_progname, elua_io_loadfile(L, modfile))) { m->status = 1; return 0; @@ -472,12 +474,12 @@ elua_main(lua_State *L) lua_call(L, 2, 0); snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref); - if (elua_report_error(elua_state, elua_progname, elua_io_loadfile(L, modfile))) + if (elua_report_error(es, elua_progname, elua_io_loadfile(L, modfile))) { m->status = 1; return 0; } - elua_state_setup_i18n(elua_state); + elua_state_setup_i18n(es); lua_call(L, 1, 0); elua_io_register(L); @@ -492,14 +494,14 @@ elua_main(lua_State *L) { case ARG_CODE: if (!hasexec) hasexec = EINA_TRUE; - if (elua_dostr(elua_state, data->value, "=(command line)")) + if (elua_dostr(es, data->value, "=(command line)")) { m->status = 1; return 0; } break; case ARG_LIBRARY: - if (elua_dolib(elua_state, data->value)) + if (elua_dolib(es, data->value)) { m->status = 1; return 0; @@ -517,13 +519,13 @@ elua_main(lua_State *L) if (optind < argc) { int quit = 0; - if ((m->status = elua_doscript(elua_state, argc, argv, optind, &quit))) return 0; + if ((m->status = elua_doscript(es, argc, argv, optind, &quit))) return 0; if (quit) return 0; } else if (!hasexec) { int quit; - if ((m->status = elua_dofile(elua_state, NULL))) return 0; + if ((m->status = elua_dofile(es, NULL))) return 0; quit = lua_toboolean(L, -1); lua_pop(L, 1); if (quit) return 0; @@ -568,6 +570,7 @@ main(int argc, char **argv) INF("elua lua state created"); + m.es = es; m.argc = argc; m.argv = argv; m.status = 0; From 92ad720127d1f7d43378e90c7961682840574cad Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 15:11:53 +0100 Subject: [PATCH 10/91] elua: remove the smart cb wrapper (ffi callbacks work fine for us) --- src/bin/elua/main.c | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index d5b2f740b3..6aec596e79 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -297,29 +297,7 @@ elua_bin_shutdown(Elua_State *es, int c) exit(c); } -static int elua_cb_ref = LUA_REFNIL; -static Elua_State *elua_state = NULL; - -static void -elua_smart_cb_wrapper(void *data, void *obj EINA_UNUSED, void *einfo EINA_UNUSED) -{ - int idx = (size_t)data; - lua_rawgeti(elua_state->luastate, LUA_REGISTRYINDEX, elua_cb_ref); - lua_rawgeti(elua_state->luastate, -1, idx); - lua_call(elua_state->luastate, 0, 0); - lua_pop(elua_state->luastate, 1); -} - -static int -elua_register_callbacks(lua_State *L) -{ - union { void (*fptr)(void*, void*, void*); void *ptr; } u; - lua_pushvalue(L, 1); - elua_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); - u.fptr = elua_smart_cb_wrapper; - lua_pushlightuserdata(L, u.ptr); - return 1; -} +static Elua_State *elua_state = NULL; struct Main_Data { @@ -331,10 +309,9 @@ struct Main_Data const luaL_reg cutillib[] = { - { "init_module" , elua_init_module }, - { "register_callbacks", elua_register_callbacks }, - { "popenv" , elua_io_popen }, - { NULL , NULL } + { "init_module", elua_init_module }, + { "popenv" , elua_io_popen }, + { NULL , NULL } }; static void From 098f2ad4cd53c13d52e9496de98ed30966479aad Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 16:30:44 +0100 Subject: [PATCH 11/91] elua: no need for luaL_openlibs, the lib does that --- src/bin/elua/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 6aec596e79..4310b96caf 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -404,8 +404,6 @@ elua_main(lua_State *L) lua_gc(L, LUA_GCSTOP, 0); - luaL_openlibs(L); - elua_prefix = eina_prefix_new(elua_progname, elua_main, "ELUA", "elua", "checkme", PACKAGE_BIN_DIR, "", PACKAGE_DATA_DIR, LOCALE_DIR); From ed3f24943f17dbc2459572ccdde34e62eaf945eb Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 16:43:14 +0100 Subject: [PATCH 12/91] elua lib: add elua_state_dirs_set --- src/bin/elua/main.c | 2 ++ src/lib/elua/Elua.h | 8 ++++++++ src/lib/elua/elua.c | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 4310b96caf..5869e2d9c9 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -402,6 +402,8 @@ elua_main(lua_State *L) INF("arguments parsed"); + elua_state_dirs_set(es, coredir, moddir, appsdir); + lua_gc(L, LUA_GCSTOP, 0); elua_prefix = eina_prefix_new(elua_progname, elua_main, "ELUA", "elua", "checkme", diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 2b06a805c0..e4df8afdb1 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -54,6 +54,8 @@ extern "C" { #ifdef EFL_BETA_API_SUPPORT +#include + #include #include #include @@ -61,6 +63,9 @@ extern "C" { typedef struct _Elua_State { lua_State *luastate; + Eina_Stringshare *coredir; + Eina_Stringshare *moddir; + Eina_Stringshare *appsdir; } Elua_State; EAPI int elua_init(void); @@ -69,6 +74,9 @@ EAPI int elua_shutdown(void); EAPI Elua_State *elua_state_new(void); EAPI void elua_state_free(Elua_State *es); +EAPI void elua_state_dirs_set(Elua_State *es, const char *core, + const char *mods, const char *apps); + EAPI Elua_State *elua_state_from_lua_get(lua_State *L); EAPI int elua_report_error(Elua_State *es, const char *pname, int status); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 6601953f27..3446ac4cd2 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -69,7 +69,7 @@ elua_state_new(void) lua_State *L = luaL_newstate(); if (!L) return NULL; - ret = malloc(sizeof(Elua_State)); + ret = calloc(1, sizeof(Elua_State)); ret->luastate = L; luaL_openlibs(L); lua_pushlightuserdata(L, ret); @@ -82,9 +82,22 @@ elua_state_free(Elua_State *es) { if (!es) return; if (es->luastate) lua_close(es->luastate); + eina_stringshare_del(es->coredir); + eina_stringshare_del(es->moddir); + eina_stringshare_del(es->appsdir); free(es); } +EAPI void +elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, + const char *apps) +{ + if (!es) return; + if (core) es->coredir = eina_stringshare_add(core); + if (mods) es->moddir = eina_stringshare_add(mods); + if (apps) es->appsdir = eina_stringshare_add(apps); +} + EAPI Elua_State * elua_state_from_lua_get(lua_State *L) { From 01aecbe273685f6afd459a3dabb249061b839d1a Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 8 Apr 2015 16:46:30 +0100 Subject: [PATCH 13/91] elua: use the dir paths from Elua_State --- src/bin/elua/main.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 5869e2d9c9..7c0784069d 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -39,6 +39,7 @@ static int elua_require_ref = LUA_REFNIL; static int elua_appload_ref = LUA_REFNIL; static const char *elua_progname = NULL; static Eina_Prefix *elua_prefix = NULL; +static Elua_State *elua_state = NULL; static int _el_log_domain = -1; @@ -124,11 +125,11 @@ elua_init_module(lua_State *L) static int elua_register_require(lua_State *L) { - const char *corepath = lua_touserdata(L, lua_upvalueindex(1)); - const char *modpath = lua_touserdata(L, lua_upvalueindex(2)); - const char *appspath = lua_touserdata(L, lua_upvalueindex(3)); - Eina_List *largs = lua_touserdata(L, lua_upvalueindex(4)), *l = NULL; - Eina_Bool noenv = lua_toboolean (L, lua_upvalueindex(5)); + const char *corepath = elua_state->coredir; + const char *modpath = elua_state->moddir; + const char *appspath = elua_state->appsdir; + Eina_List *largs = lua_touserdata(L, lua_upvalueindex(1)), *l = NULL; + Eina_Bool noenv = lua_toboolean (L, lua_upvalueindex(2)); Arg_Data *data = NULL; char corepathbuf[PATH_MAX], modpathbuf[PATH_MAX], appspathbuf[PATH_MAX]; int n = 3; @@ -297,8 +298,6 @@ elua_bin_shutdown(Elua_State *es, int c) exit(c); } -static Elua_State *elua_state = NULL; - struct Main_Data { Elua_State *es; @@ -440,12 +439,9 @@ elua_main(lua_State *L) m->status = 1; return 0; } - lua_pushlightuserdata(L, coredir); - lua_pushlightuserdata(L, moddir); - lua_pushlightuserdata(L, appsdir); lua_pushlightuserdata(L, largs); lua_pushboolean (L, noenv); - lua_pushcclosure(L, elua_register_require, 5); + lua_pushcclosure(L, elua_register_require, 2); lua_createtable(L, 0, 0); luaL_register(L, NULL, cutillib); lua_call(L, 2, 0); From e4baa1bc124b7706b2ccc04e01377075ab313a2e Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 11:06:48 +0100 Subject: [PATCH 14/91] elua lib: use eina safety checks --- src/lib/elua/elua.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 3446ac4cd2..e5a8387ff2 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -92,7 +92,7 @@ EAPI void elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, const char *apps) { - if (!es) return; + EINA_SAFETY_ON_NULL_RETURN(es); if (core) es->coredir = eina_stringshare_add(core); if (mods) es->moddir = eina_stringshare_add(mods); if (apps) es->appsdir = eina_stringshare_add(apps); @@ -101,6 +101,7 @@ elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, EAPI Elua_State * elua_state_from_lua_get(lua_State *L) { + EINA_SAFETY_ON_NULL_RETURN_VAL(L, NULL); lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr"); if (!lua_isnil(L, -1)) { @@ -121,7 +122,7 @@ _elua_errmsg(const char *pname, const char *msg) EAPI int elua_report_error(Elua_State *es, const char *pname, int status) { - if (!es || !es->luastate) return status; + EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); if (status && !lua_isnil(es->luastate, -1)) { const char *msg = lua_tostring(es->luastate, -1); From 128aa3a1ea2fb392e03adeec0179aad4211394a7 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 13:48:06 +0100 Subject: [PATCH 15/91] elua lib: elua_io_loadfile now uses Elua_State --- src/bin/elua/main.c | 21 ++++++++++----------- src/lib/elua/Elua.h | 2 +- src/lib/elua/cache.c | 8 ++++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 7c0784069d..cdd88f79af 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -203,7 +203,7 @@ static int elua_dofile(Elua_State *es, const char *fname) { return elua_report_error(es, elua_progname, - elua_io_loadfile(es->luastate, fname) + elua_io_loadfile(es, fname) || elua_docall(es, 0, 1)); } @@ -236,10 +236,9 @@ static int elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) { int status; - lua_State *L = es->luastate; const char *fname = argv[n]; int narg = elua_getargs(es, argc, argv, n); - lua_setglobal(L, "arg"); + lua_setglobal(es->luastate, "arg"); if (fname[0] == '-' && !fname[1]) { fname = NULL; @@ -251,28 +250,28 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) if (f) { fclose(f); - status = elua_io_loadfile(L, fname); + status = elua_io_loadfile(es, fname); } else status = !elua_loadapp(es, fname); } else { - status = elua_io_loadfile(L, fname); + status = elua_io_loadfile(es, fname); } - lua_insert(L, -(narg + 1)); + lua_insert(es->luastate, -(narg + 1)); if (!status) { status = elua_docall(es, narg, 1); } else { - lua_pop(L, narg); + lua_pop(es->luastate, narg); } if (!status) { - *quit = lua_toboolean(L, -1); - lua_pop(L, 1); + *quit = lua_toboolean(es->luastate, -1); + lua_pop(es->luastate, 1); } return elua_report_error(es, elua_progname, status); } @@ -434,7 +433,7 @@ elua_main(lua_State *L) } } snprintf(modfile, sizeof(modfile), "%s/module.lua", coref); - if (elua_report_error(es, elua_progname, elua_io_loadfile(L, modfile))) + if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) { m->status = 1; return 0; @@ -447,7 +446,7 @@ elua_main(lua_State *L) lua_call(L, 2, 0); snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref); - if (elua_report_error(es, elua_progname, elua_io_loadfile(L, modfile))) + if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) { m->status = 1; return 0; diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index e4df8afdb1..2010a5aa96 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -84,7 +84,7 @@ EAPI int elua_report_error(Elua_State *es, const char *pname, int status); EAPI void elua_state_setup_i18n(Elua_State *es); EAPI int elua_io_popen(lua_State *L); -EAPI int elua_io_loadfile(lua_State *L, const char *fname); +EAPI int elua_io_loadfile(Elua_State *es, const char *fname); EAPI void elua_io_register(lua_State *L); #endif diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index e272843c69..2bc0cbef05 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -150,13 +150,16 @@ getf_map(lua_State *L EINA_UNUSED, void *ud, size_t *size) } EAPI int -elua_io_loadfile(lua_State *L, const char *fname) +elua_io_loadfile(Elua_State *es, const char *fname) { Map_Stream s; int status; Eina_File *f; const char *chname; Eina_Bool bcache = EINA_FALSE; + lua_State *L; + if (!es || !es->luastate) return -1; + L = es->luastate; if (!fname) { return elua_loadstdin(L); @@ -187,8 +190,9 @@ elua_io_loadfile(lua_State *L, const char *fname) static int loadfile(lua_State *L) { + Elua_State *es = elua_state_from_lua_get(L); const char *fname = luaL_optstring(L, 1, NULL); - int status = elua_io_loadfile(L, fname), + int status = elua_io_loadfile(es, fname), hasenv = (lua_gettop(L) >= 3); if (!status) { From f17e8025b8cbf36b4cf125ac32ed89c0f17627a3 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 13:56:08 +0100 Subject: [PATCH 16/91] elua lib: more Elua_State usage --- src/bin/elua/main.c | 2 +- src/lib/elua/Elua.h | 2 +- src/lib/elua/cache.c | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index cdd88f79af..b32058d0c6 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -454,7 +454,7 @@ elua_main(lua_State *L) elua_state_setup_i18n(es); lua_call(L, 1, 0); - elua_io_register(L); + elua_io_register(es); lua_gc(L, LUA_GCRESTART, 0); INF("elua lua state initialized"); diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 2010a5aa96..f782c414eb 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -85,7 +85,7 @@ EAPI void elua_state_setup_i18n(Elua_State *es); EAPI int elua_io_popen(lua_State *L); EAPI int elua_io_loadfile(Elua_State *es, const char *fname); -EAPI void elua_io_register(lua_State *L); +EAPI void elua_io_register(Elua_State *es); #endif diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index 2bc0cbef05..26ce948ead 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -209,8 +209,9 @@ loadfile(lua_State *L) } EAPI void -elua_io_register(lua_State *L) +elua_io_register(Elua_State *es) { - lua_pushcfunction(L, loadfile); - lua_setglobal(L, "loadfile"); + EINA_SAFETY_ON_FALSE_RETURN(es && es->luastate); + lua_pushcfunction(es->luastate, loadfile); + lua_setglobal(es->luastate, "loadfile"); } From 19bb7eb4608c30733f92a655d758dbfa525cf1f8 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 14:17:38 +0100 Subject: [PATCH 17/91] elua lib: new APIs to retrieve dirs --- src/bin/elua/main.c | 6 +++--- src/lib/elua/Elua.h | 4 ++++ src/lib/elua/elua.c | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index b32058d0c6..b6693aa00c 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -125,9 +125,9 @@ elua_init_module(lua_State *L) static int elua_register_require(lua_State *L) { - const char *corepath = elua_state->coredir; - const char *modpath = elua_state->moddir; - const char *appspath = elua_state->appsdir; + const char *corepath = elua_state_core_dir_get(elua_state); + const char *modpath = elua_state_mod_dir_get(elua_state); + const char *appspath = elua_state_apps_dir_get(elua_state); Eina_List *largs = lua_touserdata(L, lua_upvalueindex(1)), *l = NULL; Eina_Bool noenv = lua_toboolean (L, lua_upvalueindex(2)); Arg_Data *data = NULL; diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index f782c414eb..702a3f0f07 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -77,6 +77,10 @@ EAPI void elua_state_free(Elua_State *es); EAPI void elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, const char *apps); +EAPI Eina_Stringshare *elua_state_core_dir_get(Elua_State *es); +EAPI Eina_Stringshare *elua_state_mod_dir_get(Elua_State *es); +EAPI Eina_Stringshare *elua_state_apps_dir_get(Elua_State *es); + EAPI Elua_State *elua_state_from_lua_get(lua_State *L); EAPI int elua_report_error(Elua_State *es, const char *pname, int status); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index e5a8387ff2..7382bdeff5 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -98,6 +98,27 @@ elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, if (apps) es->appsdir = eina_stringshare_add(apps); } +EAPI Eina_Stringshare * +elua_state_core_dir_get(Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); + return es->coredir; +} + +EAPI Eina_Stringshare * +elua_state_mod_dir_get(Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); + return es->moddir; +} + +EAPI Eina_Stringshare * +elua_state_apps_dir_get(Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); + return es->moddir; +} + EAPI Elua_State * elua_state_from_lua_get(lua_State *L) { From 51a5d09018b554530031c8ee0556aef7e289e260 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 14:21:18 +0100 Subject: [PATCH 18/91] elua lib: const correctness --- src/lib/elua/Elua.h | 17 +++++++++-------- src/lib/elua/cache.c | 4 ++-- src/lib/elua/elua.c | 10 +++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 702a3f0f07..5c960fd39f 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -72,24 +72,25 @@ EAPI int elua_init(void); EAPI int elua_shutdown(void); EAPI Elua_State *elua_state_new(void); +EAPI Elua_State *elua_state_from_lua_get(lua_State *L); + EAPI void elua_state_free(Elua_State *es); EAPI void elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, const char *apps); -EAPI Eina_Stringshare *elua_state_core_dir_get(Elua_State *es); -EAPI Eina_Stringshare *elua_state_mod_dir_get(Elua_State *es); -EAPI Eina_Stringshare *elua_state_apps_dir_get(Elua_State *es); +EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); +EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); +EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); -EAPI Elua_State *elua_state_from_lua_get(lua_State *L); +EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); -EAPI int elua_report_error(Elua_State *es, const char *pname, int status); +EAPI void elua_state_setup_i18n(const Elua_State *es); -EAPI void elua_state_setup_i18n(Elua_State *es); +EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); +EAPI void elua_io_register(const Elua_State *es); EAPI int elua_io_popen(lua_State *L); -EAPI int elua_io_loadfile(Elua_State *es, const char *fname); -EAPI void elua_io_register(Elua_State *es); #endif diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index 26ce948ead..8cd1617f61 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -150,7 +150,7 @@ getf_map(lua_State *L EINA_UNUSED, void *ud, size_t *size) } EAPI int -elua_io_loadfile(Elua_State *es, const char *fname) +elua_io_loadfile(const Elua_State *es, const char *fname) { Map_Stream s; int status; @@ -209,7 +209,7 @@ loadfile(lua_State *L) } EAPI void -elua_io_register(Elua_State *es) +elua_io_register(const Elua_State *es) { EINA_SAFETY_ON_FALSE_RETURN(es && es->luastate); lua_pushcfunction(es->luastate, loadfile); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 7382bdeff5..2a31eae647 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -99,21 +99,21 @@ elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, } EAPI Eina_Stringshare * -elua_state_core_dir_get(Elua_State *es) +elua_state_core_dir_get(const Elua_State *es) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); return es->coredir; } EAPI Eina_Stringshare * -elua_state_mod_dir_get(Elua_State *es) +elua_state_mod_dir_get(const Elua_State *es) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); return es->moddir; } EAPI Eina_Stringshare * -elua_state_apps_dir_get(Elua_State *es) +elua_state_apps_dir_get(const Elua_State *es) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); return es->moddir; @@ -141,7 +141,7 @@ _elua_errmsg(const char *pname, const char *msg) } EAPI int -elua_report_error(Elua_State *es, const char *pname, int status) +elua_report_error(const Elua_State *es, const char *pname, int status) { EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); if (status && !lua_isnil(es->luastate, -1)) @@ -188,7 +188,7 @@ const luaL_reg gettextlib[] = }; EAPI void -elua_state_setup_i18n(Elua_State *es) +elua_state_setup_i18n(const Elua_State *es) { #ifdef ENABLE_NLS char *(*dgettextp)(const char*, const char*) = dgettext; From b563ae007e6c46569e35848bdc1ff2a1898c5390 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 14:34:21 +0100 Subject: [PATCH 19/91] elua lib: add elua_state_lua_state_get --- src/lib/elua/Elua.h | 2 ++ src/lib/elua/elua.c | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 5c960fd39f..8681d0b9e6 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -83,6 +83,8 @@ EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); +EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); + EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); EAPI void elua_state_setup_i18n(const Elua_State *es); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 2a31eae647..f14dc2997c 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -119,6 +119,13 @@ elua_state_apps_dir_get(const Elua_State *es) return es->moddir; } +EAPI lua_State * +elua_state_lua_state_get(const Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); + return es->luastate; +} + EAPI Elua_State * elua_state_from_lua_get(lua_State *L) { From 1bc0bd1aa7941d8dceb42868b3b21df2ed65181c Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 14:35:37 +0100 Subject: [PATCH 20/91] elua: use the new state get API --- src/bin/elua/main.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index b6693aa00c..d3655df783 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -74,7 +74,7 @@ static int elua_docall(Elua_State *es, int narg, int nret) { int status; - lua_State *L = es->luastate; + lua_State *L = elua_state_lua_state_get(es); int bs = lua_gettop(L) - narg; lua_pushcfunction(L, elua_traceback); lua_insert(L, bs); @@ -88,7 +88,7 @@ elua_docall(Elua_State *es, int narg, int nret) static int elua_getargs(Elua_State *es, int argc, char **argv, int n) { - lua_State *L = es->luastate; + lua_State *L = elua_state_lua_state_get(es); int i; int narg = argc - (n + 1); luaL_checkstack(L, narg + 3, "too many arguments to script"); @@ -193,7 +193,7 @@ elua_register_require(lua_State *L) static int elua_dolib(Elua_State *es, const char *libname) { - lua_State *L = es->luastate; + lua_State *L = elua_state_lua_state_get(es); lua_rawgeti(L, LUA_REGISTRYINDEX, elua_require_ref); lua_pushstring(L, libname); return elua_report_error(es, elua_progname, lua_pcall(L, 1, 0, 0)); @@ -211,7 +211,7 @@ static int elua_dostr(Elua_State *es, const char *chunk, const char *chname) { return elua_report_error(es, elua_progname, - luaL_loadbuffer(es->luastate, chunk, strlen(chunk), + luaL_loadbuffer(elua_state_lua_state_get(es), chunk, strlen(chunk), chname) || elua_docall(es, 0, 0)); } @@ -219,7 +219,7 @@ elua_dostr(Elua_State *es, const char *chunk, const char *chname) static Eina_Bool elua_loadapp(Elua_State *es, const char *appname) { - lua_State *L = es->luastate; + lua_State *L = elua_state_lua_state_get(es); lua_rawgeti(L, LUA_REGISTRYINDEX, elua_appload_ref); lua_pushstring(L, appname); lua_call(L, 1, 2); @@ -238,7 +238,7 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) int status; const char *fname = argv[n]; int narg = elua_getargs(es, argc, argv, n); - lua_setglobal(es->luastate, "arg"); + lua_setglobal(elua_state_lua_state_get(es), "arg"); if (fname[0] == '-' && !fname[1]) { fname = NULL; @@ -259,19 +259,19 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) { status = elua_io_loadfile(es, fname); } - lua_insert(es->luastate, -(narg + 1)); + lua_insert(elua_state_lua_state_get(es), -(narg + 1)); if (!status) { status = elua_docall(es, narg, 1); } else { - lua_pop(es->luastate, narg); + lua_pop(elua_state_lua_state_get(es), narg); } if (!status) { - *quit = lua_toboolean(es->luastate, -1); - lua_pop(es->luastate, 1); + *quit = lua_toboolean(elua_state_lua_state_get(es), -1); + lua_pop(elua_state_lua_state_get(es), 1); } return elua_report_error(es, elua_progname, status); } @@ -284,8 +284,8 @@ elua_bin_shutdown(Elua_State *es, int c) if (es) EINA_LIST_FREE(elua_modlist, data) { - lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, (size_t)data); - lua_call(es->luastate, 0, 0); + lua_rawgeti(elua_state_lua_state_get(es), LUA_REGISTRYINDEX, (size_t)data); + lua_call(elua_state_lua_state_get(es), 0, 0); } if (elua_prefix) eina_prefix_free(elua_prefix); @@ -547,7 +547,7 @@ main(int argc, char **argv) m.argv = argv; m.status = 0; - elua_bin_shutdown(es, !!(lua_cpcall(es->luastate, elua_main, &m) || m.status)); + elua_bin_shutdown(es, !!(lua_cpcall(elua_state_lua_state_get(es), elua_main, &m) || m.status)); return 0; /* never gets here */ } From ad999df598680c43c492d5db776a6eeb50e45ec3 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 14:37:12 +0100 Subject: [PATCH 21/91] elua lib: make Elua_State fields private (opaque pointers) --- src/lib/elua/Elua.h | 8 +------- src/lib/elua/elua_private.h | 8 ++++++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 8681d0b9e6..0b452cf210 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -60,13 +60,7 @@ extern "C" { #include #include -typedef struct _Elua_State -{ - lua_State *luastate; - Eina_Stringshare *coredir; - Eina_Stringshare *moddir; - Eina_Stringshare *appsdir; -} Elua_State; +typedef struct _Elua_State Elua_State; EAPI int elua_init(void); EAPI int elua_shutdown(void); diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index 0e1e9be515..5c634880e8 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -27,6 +27,14 @@ #include #include +struct _Elua_State +{ + lua_State *luastate; + Eina_Stringshare *coredir; + Eina_Stringshare *moddir; + Eina_Stringshare *appsdir; +}; + extern int _elua_log_dom; #define DBG(...) EINA_LOG_DOM_DBG(_elua_log_dom, __VA_ARGS__) From 329fae97b0a1190b4a5c9349fb9a9aebbeb4f6c7 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 16:12:15 +0100 Subject: [PATCH 22/91] elua lib: manage bound modules within Elua_State --- src/bin/elua/main.c | 29 +---------------------------- src/lib/elua/Elua.h | 1 + src/lib/elua/elua.c | 31 ++++++++++++++++++++++++++++++- src/lib/elua/elua_private.h | 1 + 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index d3655df783..44113c3a34 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -34,7 +34,6 @@ enum ARG_LIBDIR }; -static Eina_List *elua_modlist = NULL; static int elua_require_ref = LUA_REFNIL; static int elua_appload_ref = LUA_REFNIL; static const char *elua_progname = NULL; @@ -105,23 +104,6 @@ elua_getargs(Elua_State *es, int argc, char **argv, int n) return narg; } -static int -elua_init_module(lua_State *L) -{ - if (!lua_isnoneornil(L, 1)) - { - lua_pushvalue(L, 1); - lua_call(L, 0, 0); - } - if (!lua_isnoneornil(L, 2)) - { - lua_pushvalue(L, 2); - elua_modlist = eina_list_append(elua_modlist, - (void*)(size_t)luaL_ref(L, LUA_REGISTRYINDEX)); - } - return 0; -} - static int elua_register_require(lua_State *L) { @@ -279,17 +261,8 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) void elua_bin_shutdown(Elua_State *es, int c) { - void *data; INF("elua shutdown"); - - if (es) EINA_LIST_FREE(elua_modlist, data) - { - lua_rawgeti(elua_state_lua_state_get(es), LUA_REGISTRYINDEX, (size_t)data); - lua_call(elua_state_lua_state_get(es), 0, 0); - } - if (elua_prefix) eina_prefix_free(elua_prefix); - if (es) elua_state_free(es); if (_el_log_domain != EINA_LOG_DOMAIN_GLOBAL) eina_log_domain_unregister(_el_log_domain); @@ -307,7 +280,7 @@ struct Main_Data const luaL_reg cutillib[] = { - { "init_module", elua_init_module }, + { "init_module", elua_module_init }, { "popenv" , elua_io_popen }, { NULL , NULL } }; diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 0b452cf210..b72ada52c7 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -87,6 +87,7 @@ EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); EAPI void elua_io_register(const Elua_State *es); EAPI int elua_io_popen(lua_State *L); +EAPI int elua_module_init(lua_State *L); #endif diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index f14dc2997c..3ab2448427 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -81,7 +81,18 @@ EAPI void elua_state_free(Elua_State *es) { if (!es) return; - if (es->luastate) lua_close(es->luastate); + if (es->luastate) + { + void *data; + EINA_LIST_FREE(es->cmods, data) + { + lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, (size_t)data); + lua_call(es->luastate, 0, 0); + } + lua_close(es->luastate); + } + else if (es->cmods) + eina_list_free(es->cmods); eina_stringshare_del(es->coredir); eina_stringshare_del(es->moddir); eina_stringshare_del(es->appsdir); @@ -211,3 +222,21 @@ elua_state_setup_i18n(const Elua_State *es) lua_setfield(es->luastate, -2, "dngettext"); #endif } + +EAPI int +elua_module_init(lua_State *L) +{ + Elua_State *es = elua_state_from_lua_get(L); + if (!lua_isnoneornil(L, 1)) + { + lua_pushvalue(L, 1); + lua_call(L, 0, 0); + } + if (!lua_isnoneornil(L, 2)) + { + lua_pushvalue(L, 2); + es->cmods = eina_list_append(es->cmods, + (void*)(size_t)luaL_ref(L, LUA_REGISTRYINDEX)); + } + return 0; +} diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index 5c634880e8..722d3f2b20 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -33,6 +33,7 @@ struct _Elua_State Eina_Stringshare *coredir; Eina_Stringshare *moddir; Eina_Stringshare *appsdir; + Eina_List *cmods; }; extern int _elua_log_dom; From 498501b9d77af8f841ae01e302e8f4f50fb7d8d4 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 9 Apr 2015 17:33:35 +0100 Subject: [PATCH 23/91] elua: remove EFL_RUN_IN_TREE hack (pass cmdline args) --- src/Makefile_Elua.am | 2 -- src/Makefile_Elua_Helper.am | 7 ++++- src/bin/elua/main.c | 57 +++++++++++++------------------------ 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/Makefile_Elua.am b/src/Makefile_Elua.am index a6609d2699..8a5fc821e1 100644 --- a/src/Makefile_Elua.am +++ b/src/Makefile_Elua.am @@ -14,7 +14,6 @@ lib_elua_libelua_la_SOURCES = \ lib_elua_libelua_la_CPPFLAGS = -I$(top_builddir)/src/lib/efl @ELUA_CFLAGS@ \ -DLOCALE_DIR=\"@LOCALE_DIR@\" \ - -DPACKAGE_SRC_DIR=\"$(abs_top_srcdir)\" \ -DPACKAGE_BIN_DIR=\"$(bindir)\" \ -DPACKAGE_DATA_DIR=\"$(datadir)/elua\" @@ -33,7 +32,6 @@ bin_elua_elua_SOURCES = \ bin_elua_elua_CPPFLAGS = -I$(top_builddir)/src/lib/efl @ELUA_CFLAGS@ \ -DLOCALE_DIR=\"@LOCALE_DIR@\" \ - -DPACKAGE_SRC_DIR=\"$(abs_top_srcdir)\" \ -DPACKAGE_BIN_DIR=\"$(bindir)\" \ -DPACKAGE_DATA_DIR=\"$(datadir)/elua\" diff --git a/src/Makefile_Elua_Helper.am b/src/Makefile_Elua_Helper.am index 0be2ad00bf..479a31ddb6 100644 --- a/src/Makefile_Elua_Helper.am +++ b/src/Makefile_Elua_Helper.am @@ -3,7 +3,12 @@ ELUA_GEN = @elua_bin@ lualian _ELUA_GEN_DEP = @elua_bin@ else ELUA_GEN = ELUA_EOLIAN_LIBRARY_PATH=$(top_builddir)/src/lib/eolian/.libs \ - EFL_RUN_IN_TREE=1 $(top_builddir)/src/bin/elua/elua${EXEEXT} lualian + $(top_builddir)/src/bin/elua/elua${EXEEXT} \ + -I$(abs_top_srcdir)/src/bindings/luajit \ + -C$(abs_top_srcdir)/src/scripts/elua/core \ + -M$(abs_top_srcdir)/src/scripts/elua/modules \ + -A$(abs_top_srcdir)/src/scripts/elua/apps \ + lualian _ELUA_GEN_DEP = bin/elua/elua${EXEEXT} scripts/elua/apps/lualian.lua \ scripts/elua/modules/lualian.lua \ scripts/elua/modules/getopt.lua scripts/elua/core/util.lua \ diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 44113c3a34..2873339645 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -119,40 +119,31 @@ elua_register_require(lua_State *L) elua_require_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 2); elua_appload_ref = luaL_ref(L, LUA_REGISTRYINDEX); - if (getenv("EFL_RUN_IN_TREE")) + if (!corepath) { - corepath = PACKAGE_SRC_DIR "/src/scripts/elua/core"; - modpath = PACKAGE_SRC_DIR "/src/scripts/elua/modules"; - appspath = PACKAGE_SRC_DIR "/src/scripts/elua/apps"; + if (noenv || !(corepath = getenv("ELUA_CORE_DIR")) || !corepath[0]) + { + corepath = corepathbuf; + snprintf(corepathbuf, sizeof(corepathbuf), "%s/core", + eina_prefix_data_get(elua_prefix)); + } } - else + if (!modpath) { - if (!corepath) + if (noenv || !(modpath = getenv("ELUA_MODULES_DIR")) || !modpath[0]) { - if (noenv || !(corepath = getenv("ELUA_CORE_DIR")) || !corepath[0]) - { - corepath = corepathbuf; - snprintf(corepathbuf, sizeof(corepathbuf), "%s/core", - eina_prefix_data_get(elua_prefix)); - } + modpath = modpathbuf; + snprintf(modpathbuf, sizeof(modpathbuf), "%s/modules", + eina_prefix_data_get(elua_prefix)); } - if (!modpath) + } + if (!appspath) + { + if (noenv || !(appspath = getenv("ELUA_APPS_DIR")) || !appspath[0]) { - if (noenv || !(modpath = getenv("ELUA_MODULES_DIR")) || !modpath[0]) - { - modpath = modpathbuf; - snprintf(modpathbuf, sizeof(modpathbuf), "%s/modules", - eina_prefix_data_get(elua_prefix)); - } - } - if (!appspath) - { - if (noenv || !(appspath = getenv("ELUA_APPS_DIR")) || !appspath[0]) - { - appspath = appspathbuf; - snprintf(appspathbuf, sizeof(appspathbuf), "%s/apps", - eina_prefix_data_get(elua_prefix)); - } + appspath = appspathbuf; + snprintf(appspathbuf, sizeof(appspathbuf), "%s/apps", + eina_prefix_data_get(elua_prefix)); } } lua_pushfstring(L, "%s/?.lua;", corepath); @@ -388,15 +379,7 @@ elua_main(lua_State *L) return 0; } - if (getenv("EFL_RUN_IN_TREE")) - { - Arg_Data *v = malloc(sizeof(Arg_Data)); - v->type = ARG_LIBDIR; - v->value = PACKAGE_SRC_DIR "/src/bindings/luajit"; - largs = eina_list_append(largs, v); - coref = PACKAGE_SRC_DIR "/src/scripts/elua/core"; - } - else if (!(coref = coredir)) + if (!(coref = coredir)) { if (noenv || !(coref = getenv("ELUA_CORE_DIR")) || !coref[0]) { From c1cfcc35b91c59980861caa96abfb374b8a7efcb Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 11:29:34 +0100 Subject: [PATCH 24/91] elua: remove path decision from register_require --- src/bin/elua/main.c | 72 +++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 2873339645..38cf5cea67 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -111,41 +111,12 @@ elua_register_require(lua_State *L) const char *modpath = elua_state_mod_dir_get(elua_state); const char *appspath = elua_state_apps_dir_get(elua_state); Eina_List *largs = lua_touserdata(L, lua_upvalueindex(1)), *l = NULL; - Eina_Bool noenv = lua_toboolean (L, lua_upvalueindex(2)); Arg_Data *data = NULL; - char corepathbuf[PATH_MAX], modpathbuf[PATH_MAX], appspathbuf[PATH_MAX]; int n = 3; lua_pushvalue(L, 1); elua_require_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 2); elua_appload_ref = luaL_ref(L, LUA_REGISTRYINDEX); - if (!corepath) - { - if (noenv || !(corepath = getenv("ELUA_CORE_DIR")) || !corepath[0]) - { - corepath = corepathbuf; - snprintf(corepathbuf, sizeof(corepathbuf), "%s/core", - eina_prefix_data_get(elua_prefix)); - } - } - if (!modpath) - { - if (noenv || !(modpath = getenv("ELUA_MODULES_DIR")) || !modpath[0]) - { - modpath = modpathbuf; - snprintf(modpathbuf, sizeof(modpathbuf), "%s/modules", - eina_prefix_data_get(elua_prefix)); - } - } - if (!appspath) - { - if (noenv || !(appspath = getenv("ELUA_APPS_DIR")) || !appspath[0]) - { - appspath = appspathbuf; - snprintf(appspathbuf, sizeof(appspathbuf), "%s/apps", - eina_prefix_data_get(elua_prefix)); - } - } lua_pushfstring(L, "%s/?.lua;", corepath); EINA_LIST_FOREACH(largs, l, data) { @@ -317,10 +288,9 @@ elua_main(lua_State *L) hasexec = EINA_FALSE; Eina_List *largs = NULL, *l = NULL; Arg_Data *data = NULL; - const char *coref = NULL; char *coredir = NULL, *moddir = NULL, *appsdir = NULL; + char coredirbuf[PATH_MAX], moddirbuf[PATH_MAX], appsdirbuf[PATH_MAX]; char modfile[PATH_MAX]; - char corefbuf[PATH_MAX]; int ch; @@ -364,8 +334,6 @@ elua_main(lua_State *L) INF("arguments parsed"); - elua_state_dirs_set(es, coredir, moddir, appsdir); - lua_gc(L, LUA_GCSTOP, 0); elua_prefix = eina_prefix_new(elua_progname, elua_main, "ELUA", "elua", "checkme", @@ -379,29 +347,49 @@ elua_main(lua_State *L) return 0; } - if (!(coref = coredir)) + if (!coredir) { - if (noenv || !(coref = getenv("ELUA_CORE_DIR")) || !coref[0]) + if (noenv || !(coredir = getenv("ELUA_CORE_DIR")) || !coredir[0]) { - coref = corefbuf; - snprintf(corefbuf, sizeof(corefbuf), "%s/core", - eina_prefix_data_get(elua_prefix)); + coredir = coredirbuf; + snprintf(coredirbuf, sizeof(coredirbuf), "%s/core", + eina_prefix_data_get(elua_prefix)); } } - snprintf(modfile, sizeof(modfile), "%s/module.lua", coref); + if (!moddir) + { + if (noenv || !(moddir = getenv("ELUA_MODULES_DIR")) || !moddir[0]) + { + moddir = moddirbuf; + snprintf(moddirbuf, sizeof(moddirbuf), "%s/modules", + eina_prefix_data_get(elua_prefix)); + } + } + if (!appsdir) + { + if (noenv || !(appsdir = getenv("ELUA_APPS_DIR")) || !appsdir[0]) + { + appsdir = appsdirbuf; + snprintf(appsdirbuf, sizeof(appsdirbuf), "%s/apps", + eina_prefix_data_get(elua_prefix)); + } + } + + elua_state_dirs_set(es, coredir, moddir, appsdir); + + snprintf(modfile, sizeof(modfile), "%s/module.lua", coredir); if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) { m->status = 1; return 0; } lua_pushlightuserdata(L, largs); - lua_pushboolean (L, noenv); - lua_pushcclosure(L, elua_register_require, 2); + lua_pushcclosure(L, elua_register_require, 1); lua_createtable(L, 0, 0); luaL_register(L, NULL, cutillib); lua_call(L, 2, 0); - snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref); + snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coredir); if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) { m->status = 1; From 5f6109bc4776084b093662215146f7c16247d62f Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 11:42:09 +0100 Subject: [PATCH 25/91] elua: new API elua_state_dirs_fill + utilize --- src/bin/elua/main.c | 89 +++++++++++++++++++-------------------------- src/lib/elua/Elua.h | 1 + src/lib/elua/elua.c | 38 +++++++++++++++++++ 3 files changed, 76 insertions(+), 52 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 38cf5cea67..ed83b7027b 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -288,8 +288,7 @@ elua_main(lua_State *L) hasexec = EINA_FALSE; Eina_List *largs = NULL, *l = NULL; Arg_Data *data = NULL; - char *coredir = NULL, *moddir = NULL, *appsdir = NULL; - char coredirbuf[PATH_MAX], moddirbuf[PATH_MAX], appsdirbuf[PATH_MAX]; + const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; char modfile[PATH_MAX]; int ch; @@ -306,29 +305,31 @@ elua_main(lua_State *L) { switch (ch) { - case 'h': - elua_print_help(elua_progname, stdout); - return 0; - case 'C': - coredir = optarg; + case 'h': + elua_print_help(elua_progname, stdout); + return 0; + case 'C': + coredir = optarg; + break; + case 'M': + moddir = optarg; + break; + case 'A': + appsdir = optarg; + break; + case 'e': + case 'l': + case 'I': + { + Arg_Data *v = malloc(sizeof(Arg_Data)); + v->type = (ch == 'e') ? ARG_CODE : ((ch == 'l') + ? ARG_LIBRARY : ARG_LIBDIR); + v->value = optarg; + largs = eina_list_append(largs, v); break; - case 'M': - moddir = optarg; - break; - case 'A': - appsdir = optarg; - break; - case 'e': - case 'l': - case 'I': - { - Arg_Data *v = malloc(sizeof(Arg_Data)); - v->type = (ch == 'e') ? ARG_CODE : ((ch == 'l') - ? ARG_LIBRARY : ARG_LIBDIR); - v->value = optarg; - largs = eina_list_append(largs, v); - break; - } + } + case 'E': + noenv = EINA_TRUE; } } @@ -347,35 +348,19 @@ elua_main(lua_State *L) return 0; } - if (!coredir) - { - if (noenv || !(coredir = getenv("ELUA_CORE_DIR")) || !coredir[0]) - { - coredir = coredirbuf; - snprintf(coredirbuf, sizeof(coredirbuf), "%s/core", - eina_prefix_data_get(elua_prefix)); - } - } - if (!moddir) - { - if (noenv || !(moddir = getenv("ELUA_MODULES_DIR")) || !moddir[0]) - { - moddir = moddirbuf; - snprintf(moddirbuf, sizeof(moddirbuf), "%s/modules", - eina_prefix_data_get(elua_prefix)); - } - } - if (!appsdir) - { - if (noenv || !(appsdir = getenv("ELUA_APPS_DIR")) || !appsdir[0]) - { - appsdir = appsdirbuf; - snprintf(appsdirbuf, sizeof(appsdirbuf), "%s/apps", - eina_prefix_data_get(elua_prefix)); - } - } - elua_state_dirs_set(es, coredir, moddir, appsdir); + elua_state_dirs_fill(es, noenv); + + coredir = elua_state_core_dir_get(es); + moddir = elua_state_mod_dir_get(es); + appsdir = elua_state_apps_dir_get(es); + + if (!coredir || !moddir || !appsdir) + { + ERR("could not set one or more script directories"); + m->status = 1; + return 0; + } snprintf(modfile, sizeof(modfile), "%s/module.lua", coredir); if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index b72ada52c7..a05b82433c 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -72,6 +72,7 @@ EAPI void elua_state_free(Elua_State *es); EAPI void elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, const char *apps); +EAPI void elua_state_dirs_fill(Elua_State *es, Eina_Bool ignore_env); EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 3ab2448427..ae5da0e012 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -109,6 +109,44 @@ elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, if (apps) es->appsdir = eina_stringshare_add(apps); } +EAPI void +elua_state_dirs_fill(Elua_State *es, Eina_Bool ignore_env) +{ + const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; + char coredirbuf[PATH_MAX], moddirbuf[PATH_MAX], appsdirbuf[PATH_MAX]; + EINA_SAFETY_ON_NULL_RETURN(es); + if (!(coredir = es->coredir)) + { + if (ignore_env || !(coredir = getenv("ELUA_CORE_DIR")) || !coredir[0]) + { + coredir = coredirbuf; + snprintf(coredirbuf, sizeof(coredirbuf), "%s/core", + eina_prefix_data_get(_elua_pfx)); + } + if (coredir) es->coredir = eina_stringshare_add(coredir); + } + if (!(moddir = es->moddir)) + { + if (ignore_env || !(moddir = getenv("ELUA_MODULES_DIR")) || !moddir[0]) + { + moddir = moddirbuf; + snprintf(moddirbuf, sizeof(moddirbuf), "%s/modules", + eina_prefix_data_get(_elua_pfx)); + } + if (moddir) es->moddir = eina_stringshare_add(moddir); + } + if (!(appsdir = es->appsdir)) + { + if (ignore_env || !(appsdir = getenv("ELUA_APPS_DIR")) || !appsdir[0]) + { + appsdir = appsdirbuf; + snprintf(appsdirbuf, sizeof(appsdirbuf), "%s/apps", + eina_prefix_data_get(_elua_pfx)); + } + if (appsdir) es->appsdir = eina_stringshare_add(appsdir); + } +} + EAPI Eina_Stringshare * elua_state_core_dir_get(const Elua_State *es) { From 87a8e51cd3457fc8a0cc89add65bc281e0f122db Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 11:44:30 +0100 Subject: [PATCH 26/91] elua: remove elua_prefix from main runtime --- src/Makefile_Elua.am | 4 +--- src/bin/elua/main.c | 13 ------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/Makefile_Elua.am b/src/Makefile_Elua.am index 8a5fc821e1..49ba426711 100644 --- a/src/Makefile_Elua.am +++ b/src/Makefile_Elua.am @@ -31,9 +31,7 @@ bin_elua_elua_SOURCES = \ bin/elua/main.c bin_elua_elua_CPPFLAGS = -I$(top_builddir)/src/lib/efl @ELUA_CFLAGS@ \ - -DLOCALE_DIR=\"@LOCALE_DIR@\" \ - -DPACKAGE_BIN_DIR=\"$(bindir)\" \ - -DPACKAGE_DATA_DIR=\"$(datadir)/elua\" + -DLOCALE_DIR=\"@LOCALE_DIR@\" if HAVE_OSX if HAVE_X86_64 diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index ed83b7027b..b2806be5e0 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -37,7 +37,6 @@ enum static int elua_require_ref = LUA_REFNIL; static int elua_appload_ref = LUA_REFNIL; static const char *elua_progname = NULL; -static Eina_Prefix *elua_prefix = NULL; static Elua_State *elua_state = NULL; static int _el_log_domain = -1; @@ -224,7 +223,6 @@ void elua_bin_shutdown(Elua_State *es, int c) { INF("elua shutdown"); - if (elua_prefix) eina_prefix_free(elua_prefix); if (es) elua_state_free(es); if (_el_log_domain != EINA_LOG_DOMAIN_GLOBAL) eina_log_domain_unregister(_el_log_domain); @@ -337,17 +335,6 @@ elua_main(lua_State *L) lua_gc(L, LUA_GCSTOP, 0); - elua_prefix = eina_prefix_new(elua_progname, elua_main, "ELUA", "elua", "checkme", - PACKAGE_BIN_DIR, "", PACKAGE_DATA_DIR, - LOCALE_DIR); - - if (!elua_prefix) - { - ERR("could not find elua prefix"); - m->status = 1; - return 0; - } - elua_state_dirs_set(es, coredir, moddir, appsdir); elua_state_dirs_fill(es, noenv); From 6be9b662fbad73d76690daea3631e0d37c0ae896 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 12:02:08 +0100 Subject: [PATCH 27/91] elua lib: add APIs for include path management --- src/lib/elua/Elua.h | 3 +++ src/lib/elua/elua.c | 45 ++++++++++++++++++++++++++++++++++++- src/lib/elua/elua_private.h | 2 ++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index a05b82433c..f70af321d0 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -78,6 +78,8 @@ EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); +EAPI void elua_state_include_path_add(Elua_State *es, const char *path); + EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); @@ -89,6 +91,7 @@ EAPI void elua_io_register(const Elua_State *es); EAPI int elua_io_popen(lua_State *L); EAPI int elua_module_init(lua_State *L); +EAPI int elua_module_system_init(lua_State *L); #endif diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index ae5da0e012..9019fef565 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -80,10 +80,10 @@ elua_state_new(void) EAPI void elua_state_free(Elua_State *es) { + void *data; if (!es) return; if (es->luastate) { - void *data; EINA_LIST_FREE(es->cmods, data) { lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, (size_t)data); @@ -93,6 +93,8 @@ elua_state_free(Elua_State *es) } else if (es->cmods) eina_list_free(es->cmods); + EINA_LIST_FREE(es->lincs, data) + eina_stringshare_del(data); eina_stringshare_del(es->coredir); eina_stringshare_del(es->moddir); eina_stringshare_del(es->appsdir); @@ -168,6 +170,15 @@ elua_state_apps_dir_get(const Elua_State *es) return es->moddir; } +EAPI void +elua_state_include_path_add(Elua_State *es, const char *path) +{ + EINA_SAFETY_ON_NULL_RETURN(es); + EINA_SAFETY_ON_NULL_RETURN(path); + EINA_SAFETY_ON_FALSE_RETURN(path[0]); + es->lincs = eina_list_append(es->lincs, eina_stringshare_add(path)); +} + EAPI lua_State * elua_state_lua_state_get(const Elua_State *es) { @@ -278,3 +289,35 @@ elua_module_init(lua_State *L) } return 0; } + +EAPI int +elua_module_system_init(lua_State *L) +{ + Elua_State *es = elua_state_from_lua_get(L); + const char *corepath = es->coredir; + const char *modpath = es->moddir; + const char *appspath = es->appsdir; + Eina_Stringshare *data = NULL; + int n = 3; + if (!corepath || !modpath || !appspath) + return 0; + lua_pushvalue(L, 1); + es->requireref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + es->apploadref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushfstring(L, "%s/?.lua;", corepath); + EINA_LIST_FREE(es->lincs, data) + { + lua_pushfstring(L, "%s/?.lua;", data); + eina_stringshare_del(data); + ++n; + } + lua_pushfstring(L, "%s/?.eo.lua;", modpath); + lua_pushfstring(L, "%s/?.lua;", modpath); + lua_pushvalue(L, 3); + lua_concat(L, n + 1); + lua_pushfstring(L, "%s/?.lua;", appspath); + lua_pushvalue(L, 4); + lua_concat(L, 2); + return 2; +} diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index 722d3f2b20..aa00cd300f 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -34,6 +34,8 @@ struct _Elua_State Eina_Stringshare *moddir; Eina_Stringshare *appsdir; Eina_List *cmods; + Eina_List *lincs; + int requireref, apploadref; }; extern int _elua_log_dom; From 18da22a468dcf72fae587a4ebd029b1148130a80 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 12:05:54 +0100 Subject: [PATCH 28/91] elua lib: add APIs to retrieve refs to require/appload --- src/lib/elua/Elua.h | 3 +++ src/lib/elua/elua.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index f70af321d0..9814cf2390 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -80,6 +80,9 @@ EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); EAPI void elua_state_include_path_add(Elua_State *es, const char *path); +EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es); +EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); + EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 9019fef565..9299b8ff6e 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -179,6 +179,26 @@ elua_state_include_path_add(Elua_State *es, const char *path) es->lincs = eina_list_append(es->lincs, eina_stringshare_add(path)); } +EAPI Eina_Bool +elua_state_require_ref_push(Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(es->requireref != LUA_REFNIL, EINA_FALSE); + lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, es->requireref); + return EINA_TRUE; +} + +EAPI Eina_Bool +elua_state_appload_ref_push(Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(es->apploadref != LUA_REFNIL, EINA_FALSE); + lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, es->apploadref); + return EINA_TRUE; +} + EAPI lua_State * elua_state_lua_state_get(const Elua_State *es) { From a2d9f1af91a7dd2738415d3f9cb58b49f36d0164 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 12:10:57 +0100 Subject: [PATCH 29/91] elua: utilize the new APIs for handling require/appload --- src/bin/elua/main.c | 53 +++++++++------------------------------------ 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index b2806be5e0..ffcd17a224 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -30,14 +30,11 @@ typedef struct Arg_Data enum { ARG_CODE = 0, - ARG_LIBRARY, - ARG_LIBDIR + ARG_LIBRARY }; -static int elua_require_ref = LUA_REFNIL; -static int elua_appload_ref = LUA_REFNIL; -static const char *elua_progname = NULL; -static Elua_State *elua_state = NULL; +static const char *elua_progname = NULL; +static Elua_State *elua_state = NULL; static int _el_log_domain = -1; @@ -103,41 +100,11 @@ elua_getargs(Elua_State *es, int argc, char **argv, int n) return narg; } -static int -elua_register_require(lua_State *L) -{ - const char *corepath = elua_state_core_dir_get(elua_state); - const char *modpath = elua_state_mod_dir_get(elua_state); - const char *appspath = elua_state_apps_dir_get(elua_state); - Eina_List *largs = lua_touserdata(L, lua_upvalueindex(1)), *l = NULL; - Arg_Data *data = NULL; - int n = 3; - lua_pushvalue(L, 1); - elua_require_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 2); - elua_appload_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushfstring(L, "%s/?.lua;", corepath); - EINA_LIST_FOREACH(largs, l, data) - { - if (data->type != ARG_LIBDIR) continue; - lua_pushfstring(L, "%s/?.lua;", data->value); - ++n; - } - lua_pushfstring(L, "%s/?.eo.lua;", modpath); - lua_pushfstring(L, "%s/?.lua;", modpath); - lua_pushvalue(L, 3); - lua_concat(L, n + 1); - lua_pushfstring(L, "%s/?.lua;", appspath); - lua_pushvalue(L, 4); - lua_concat(L, 2); - return 2; -} - static int elua_dolib(Elua_State *es, const char *libname) { lua_State *L = elua_state_lua_state_get(es); - lua_rawgeti(L, LUA_REGISTRYINDEX, elua_require_ref); + elua_state_require_ref_push(es); lua_pushstring(L, libname); return elua_report_error(es, elua_progname, lua_pcall(L, 1, 0, 0)); } @@ -163,7 +130,7 @@ static Eina_Bool elua_loadapp(Elua_State *es, const char *appname) { lua_State *L = elua_state_lua_state_get(es); - lua_rawgeti(L, LUA_REGISTRYINDEX, elua_appload_ref); + elua_state_appload_ref_push(es); lua_pushstring(L, appname); lua_call(L, 1, 2); if (lua_isnil(L, -2)) @@ -317,15 +284,16 @@ elua_main(lua_State *L) break; case 'e': case 'l': - case 'I': { Arg_Data *v = malloc(sizeof(Arg_Data)); - v->type = (ch == 'e') ? ARG_CODE : ((ch == 'l') - ? ARG_LIBRARY : ARG_LIBDIR); + v->type = (ch == 'e') ? ARG_CODE : ARG_LIBRARY; v->value = optarg; largs = eina_list_append(largs, v); break; } + case 'I': + elua_state_include_path_add(es, optarg); + break; case 'E': noenv = EINA_TRUE; } @@ -355,8 +323,7 @@ elua_main(lua_State *L) m->status = 1; return 0; } - lua_pushlightuserdata(L, largs); - lua_pushcclosure(L, elua_register_require, 1); + lua_pushcfunction(L, elua_module_system_init); lua_createtable(L, 0, 0); luaL_register(L, NULL, cutillib); lua_call(L, 2, 0); From 865e2e3364e7e1506909a525ecd4c936c2f8a6f9 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 14:04:50 +0100 Subject: [PATCH 30/91] elua: remove -e param (we're not a REPL) --- src/bin/elua/main.c | 81 ++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 60 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index ffcd17a224..6497f3d62d 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -21,18 +21,6 @@ #include "Elua.h" -typedef struct Arg_Data -{ - int type; - const char *value; -} Arg_Data; - -enum -{ - ARG_CODE = 0, - ARG_LIBRARY -}; - static const char *elua_progname = NULL; static Elua_State *elua_state = NULL; @@ -224,7 +212,6 @@ elua_print_help(const char *pname, FILE *stream) " -C[COREDIR], --core-dir=[COREDIR] Elua core directory path.\n" " -M[MODDIR], --modules-dir=[MODDIR] Elua modules directory path.\n" " -A[APPDIR], --apps-dir=[APPDIR] Elua applications directory path.\n" - " -e[CODE], --execute=[CODE] Execute string 'code'.\n" " -l[LIBRARY], --library=[LIBRARY] Require library 'library'.\n" " -I[DIR], --lib-dir=[DIR] Append an additional require path.\n" " -E, --noenv Ignore environment variables.\n", pname); @@ -238,7 +225,6 @@ static struct option lopt[] = { "modules-dir", required_argument, NULL, 'M' }, { "apps-dir" , required_argument, NULL, 'A' }, - { "execute" , required_argument, NULL, 'e' }, { "library" , required_argument, NULL, 'l' }, { "lib-dir" , required_argument, NULL, 'I' }, { "noenv" , no_argument , NULL, 'E' }, @@ -251,10 +237,10 @@ elua_main(lua_State *L) { Eina_Bool noenv = EINA_FALSE, hasexec = EINA_FALSE; - Eina_List *largs = NULL, *l = NULL; - Arg_Data *data = NULL; + Eina_List *largs = NULL; const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; char modfile[PATH_MAX]; + char *data = NULL; int ch; @@ -266,12 +252,13 @@ elua_main(lua_State *L) elua_progname = (argv[0] && argv[0][0]) ? argv[0] : "elua"; - while ((ch = getopt_long(argc, argv, "+LhC:M:A:e:l:I:E", lopt, NULL)) != -1) + while ((ch = getopt_long(argc, argv, "+LhC:M:A:l:I:E", lopt, NULL)) != -1) { switch (ch) { case 'h': elua_print_help(elua_progname, stdout); + if (largs) eina_list_free(largs); return 0; case 'C': coredir = optarg; @@ -282,20 +269,17 @@ elua_main(lua_State *L) case 'A': appsdir = optarg; break; - case 'e': case 'l': - { - Arg_Data *v = malloc(sizeof(Arg_Data)); - v->type = (ch == 'e') ? ARG_CODE : ARG_LIBRARY; - v->value = optarg; - largs = eina_list_append(largs, v); - break; - } + if (!optarg[0]) continue; + largs = eina_list_append(largs, optarg); + break; case 'I': + if (!optarg[0]) continue; elua_state_include_path_add(es, optarg); break; case 'E': noenv = EINA_TRUE; + break; } } @@ -313,16 +297,12 @@ elua_main(lua_State *L) if (!coredir || !moddir || !appsdir) { ERR("could not set one or more script directories"); - m->status = 1; - return 0; + goto error; } snprintf(modfile, sizeof(modfile), "%s/module.lua", coredir); if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) - { - m->status = 1; - return 0; - } + goto error; lua_pushcfunction(L, elua_module_system_init); lua_createtable(L, 0, 0); luaL_register(L, NULL, cutillib); @@ -330,10 +310,7 @@ elua_main(lua_State *L) snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coredir); if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) - { - m->status = 1; - return 0; - } + goto error; elua_state_setup_i18n(es); lua_call(L, 1, 0); @@ -343,34 +320,13 @@ elua_main(lua_State *L) INF("elua lua state initialized"); /* load all the things */ - EINA_LIST_FOREACH(largs, l, data) + EINA_LIST_FREE(largs, data) { - switch (data->type) - { - case ARG_CODE: - if (!hasexec) hasexec = EINA_TRUE; - if (elua_dostr(es, data->value, "=(command line)")) - { - m->status = 1; - return 0; - } - break; - case ARG_LIBRARY: - if (elua_dolib(es, data->value)) - { - m->status = 1; - return 0; - } - break; - default: - break; - } + if (elua_dolib(es, data)) + goto error; } - /* cleanup */ - EINA_LIST_FREE(largs, data) free(data); - - /* run script or execute sdin as file */ + /* run script or execute stdin as file */ if (optind < argc) { int quit = 0; @@ -389,6 +345,11 @@ elua_main(lua_State *L) ecore_main_loop_begin(); return 0; + +error: + m->status = 1; + if (largs) eina_list_free(largs); + return 0; } int From 13bd7ba6533a54759df079467f9842ccb6ae4e24 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 14:36:26 +0100 Subject: [PATCH 31/91] elua lib: add progname to state --- src/bin/elua/main.c | 2 +- src/lib/elua/Elua.h | 2 +- src/lib/elua/elua.c | 4 +++- src/lib/elua/elua_private.h | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 6497f3d62d..24bd916c8d 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -376,7 +376,7 @@ main(int argc, char **argv) INF("elua logging initialized: %d", _el_log_domain); - if (!(es = elua_state_new())) + if (!(es = elua_state_new((argv[0] && argv[0][0]) ? argv[0] : "elua"))) { ERR("could not initialize elua state."); elua_bin_shutdown(es, 1); diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 9814cf2390..cc7442dd4c 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -65,7 +65,7 @@ typedef struct _Elua_State Elua_State; EAPI int elua_init(void); EAPI int elua_shutdown(void); -EAPI Elua_State *elua_state_new(void); +EAPI Elua_State *elua_state_new(const char *progname); EAPI Elua_State *elua_state_from_lua_get(lua_State *L); EAPI void elua_state_free(Elua_State *es); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 9299b8ff6e..fb85901ec2 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -63,7 +63,7 @@ elua_shutdown(void) } EAPI Elua_State * -elua_state_new(void) +elua_state_new(const char *progname) { Elua_State *ret = NULL; lua_State *L = luaL_newstate(); @@ -71,6 +71,7 @@ elua_state_new(void) return NULL; ret = calloc(1, sizeof(Elua_State)); ret->luastate = L; + if (progname) ret->progname = eina_stringshare_add(progname); luaL_openlibs(L); lua_pushlightuserdata(L, ret); lua_setfield(L, LUA_REGISTRYINDEX, "elua_ptr"); @@ -95,6 +96,7 @@ elua_state_free(Elua_State *es) eina_list_free(es->cmods); EINA_LIST_FREE(es->lincs, data) eina_stringshare_del(data); + eina_stringshare_del(es->progname); eina_stringshare_del(es->coredir); eina_stringshare_del(es->moddir); eina_stringshare_del(es->appsdir); diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index aa00cd300f..fbf7bb3ca9 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -30,6 +30,7 @@ struct _Elua_State { lua_State *luastate; + Eina_Stringshare *progname; Eina_Stringshare *coredir; Eina_Stringshare *moddir; Eina_Stringshare *appsdir; From e806ae6f5c606279897309e3ab5790d9837526fa Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 14:39:20 +0100 Subject: [PATCH 32/91] elua lib: add a way to retrieve the progname --- src/lib/elua/Elua.h | 2 ++ src/lib/elua/elua.c | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index cc7442dd4c..6d49303012 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -78,6 +78,8 @@ EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); +EAPI Eina_Stringshare *elua_state_prog_name_get(const Elua_State *es); + EAPI void elua_state_include_path_add(Elua_State *es, const char *path); EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index fb85901ec2..1827e1e878 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -172,6 +172,13 @@ elua_state_apps_dir_get(const Elua_State *es) return es->moddir; } +EAPI Eina_Stringshare * +elua_state_prog_name_get(const Elua_State *es) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); + return es->progname; +} + EAPI void elua_state_include_path_add(Elua_State *es, const char *path) { From f80999ae605911a757e0f4a53f124e048d1b4914 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 14:41:19 +0100 Subject: [PATCH 33/91] elua: use the new progname API --- src/bin/elua/main.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 24bd916c8d..1fe60456e0 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -21,10 +21,8 @@ #include "Elua.h" -static const char *elua_progname = NULL; -static Elua_State *elua_state = NULL; - -static int _el_log_domain = -1; +static Elua_State *elua_state = NULL; +static int _el_log_domain = -1; #define DBG(...) EINA_LOG_DOM_DBG(_el_log_domain, __VA_ARGS__) #define INF(...) EINA_LOG_DOM_INFO(_el_log_domain, __VA_ARGS__) @@ -94,13 +92,14 @@ elua_dolib(Elua_State *es, const char *libname) lua_State *L = elua_state_lua_state_get(es); elua_state_require_ref_push(es); lua_pushstring(L, libname); - return elua_report_error(es, elua_progname, lua_pcall(L, 1, 0, 0)); + return elua_report_error(es, elua_state_prog_name_get(es), + lua_pcall(L, 1, 0, 0)); } static int elua_dofile(Elua_State *es, const char *fname) { - return elua_report_error(es, elua_progname, + return elua_report_error(es, elua_state_prog_name_get(es), elua_io_loadfile(es, fname) || elua_docall(es, 0, 1)); } @@ -108,9 +107,9 @@ elua_dofile(Elua_State *es, const char *fname) static int elua_dostr(Elua_State *es, const char *chunk, const char *chname) { - return elua_report_error(es, elua_progname, - luaL_loadbuffer(elua_state_lua_state_get(es), chunk, strlen(chunk), - chname) + return elua_report_error(es, elua_state_prog_name_get(es), + luaL_loadbuffer(elua_state_lua_state_get(es), + chunk, strlen(chunk), chname) || elua_docall(es, 0, 0)); } @@ -171,7 +170,7 @@ elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) *quit = lua_toboolean(elua_state_lua_state_get(es), -1); lua_pop(elua_state_lua_state_get(es), 1); } - return elua_report_error(es, elua_progname, status); + return elua_report_error(es, elua_state_prog_name_get(es), status); } void @@ -250,14 +249,12 @@ elua_main(lua_State *L) int argc = m->argc; char **argv = m->argv; - elua_progname = (argv[0] && argv[0][0]) ? argv[0] : "elua"; - while ((ch = getopt_long(argc, argv, "+LhC:M:A:l:I:E", lopt, NULL)) != -1) { switch (ch) { case 'h': - elua_print_help(elua_progname, stdout); + elua_print_help(elua_state_prog_name_get(es), stdout); if (largs) eina_list_free(largs); return 0; case 'C': @@ -301,7 +298,8 @@ elua_main(lua_State *L) } snprintf(modfile, sizeof(modfile), "%s/module.lua", coredir); - if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) + if (elua_report_error(es, elua_state_prog_name_get(es), + elua_io_loadfile(es, modfile))) goto error; lua_pushcfunction(L, elua_module_system_init); lua_createtable(L, 0, 0); @@ -309,7 +307,8 @@ elua_main(lua_State *L) lua_call(L, 2, 0); snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coredir); - if (elua_report_error(es, elua_progname, elua_io_loadfile(es, modfile))) + if (elua_report_error(es, elua_state_prog_name_get(es), + elua_io_loadfile(es, modfile))) goto error; elua_state_setup_i18n(es); lua_call(L, 1, 0); From 5aca5e13a18ef06a8fcb1f4e6f93624e309a93f2 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 16:36:26 +0100 Subject: [PATCH 34/91] elua lib: add elua_util_ APIs --- src/lib/elua/Elua.h | 8 +++ src/lib/elua/elua.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 6d49303012..cb1edc13ee 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -98,6 +98,14 @@ EAPI int elua_io_popen(lua_State *L); EAPI int elua_module_init(lua_State *L); EAPI int elua_module_system_init(lua_State *L); +EAPI int elua_util_require(Elua_State *es, const char *libname); +EAPI int elua_util_file_run(Elua_State *es, const char *fname); +EAPI int elua_util_string_run(Elua_State *es, const char *chunk, + const char *chname); +EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); +EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, + int *quit); + #endif #ifdef __cplusplus diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 1827e1e878..df96e1d00c 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -350,3 +350,151 @@ elua_module_system_init(lua_State *L) lua_concat(L, 2); return 2; } + +/* Utility functions - these could be written using the other APIs */ + +static int +_elua_traceback(lua_State *L) +{ + lua_getglobal(L, "debug"); + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) + { + lua_pop(L, 2); + return 1; + } + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); + lua_call(L, 2, 1); + return 1; +} + +static int +_elua_docall(Elua_State *es, int narg, int nret) +{ + int status; + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + int bs = lua_gettop(es->luastate) - narg; + lua_pushcfunction(es->luastate, _elua_traceback); + lua_insert(es->luastate, bs); + status = lua_pcall(es->luastate, narg, nret, bs); + lua_remove(es->luastate, bs); + if (status) + lua_gc(es->luastate, LUA_GCCOLLECT, 0); + return status; +} + +static int +_elua_getargs(Elua_State *es, int argc, char **argv, int n) +{ + int i; + int narg = argc - (n + 1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + luaL_checkstack(es->luastate, narg + 3, "too many arguments to script"); + for (i = n + 1; i < argc; ++i) + { + lua_pushstring(es->luastate, argv[i]); + } + lua_createtable(es->luastate, narg, n + 1); + for (i = 0; i < argc; ++i) + { + lua_pushstring(es->luastate, argv[i]); + lua_rawseti(es->luastate, -2, i - n); + } + return narg; +} + +EAPI int +elua_util_require(Elua_State *es, const char *libname) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + EINA_SAFETY_ON_FALSE_RETURN_VAL(es->requireref, -1); + lua_pushstring(es->luastate, libname); + return elua_report_error(es, es->progname, + lua_pcall(es->luastate, 1, 0, 0)); +} + +EAPI int +elua_util_file_run(Elua_State *es, const char *fname) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + return elua_report_error(es, es->progname, + elua_io_loadfile(es, fname) + || _elua_docall(es, 0, 1)); +} + +EAPI int +elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + return elua_report_error(es, es->progname, + luaL_loadbuffer(es->luastate, chunk, strlen(chunk), + chname) + || _elua_docall(es, 0, 0)); +} + +EAPI Eina_Bool +elua_util_app_load(Elua_State *es, const char *appname) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(es->apploadref, EINA_FALSE); + lua_pushstring(es->luastate, appname); + lua_call(es->luastate, 1, 2); + if (lua_isnil(es->luastate, -2)) + { + lua_remove(es->luastate, -2); + return EINA_FALSE; + } + lua_pop(es->luastate, 1); + return EINA_TRUE; +} + +EAPI int +elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) +{ + int status, narg; + const char *fname; + EINA_SAFETY_ON_FALSE_RETURN_VAL(n < argc, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + fname = argv[n]; + narg = _elua_getargs(es, argc, argv, n); + lua_setglobal(es->luastate, "arg"); + if (fname[0] == '-' && !fname[1]) fname = NULL; + if (fname) + { + /* check if there is a file of that name */ + FILE *f = fopen(fname, "r"); + if (f) + { + fclose(f); + status = elua_io_loadfile(es, fname); + } + else + status = !elua_util_app_load(es, fname); + } + else + status = elua_io_loadfile(es, fname); + lua_insert(es->luastate, -(narg + 1)); + if (!status) + status = _elua_docall(es, narg, 1); + else + lua_pop(es->luastate, narg); + if (!status) + { + *quit = lua_toboolean(es->luastate, -1); + lua_pop(es->luastate, 1); + } + return elua_report_error(es, es->progname, status); +} From 0496762057e055fd227382cb1be81578240e5577 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 16:45:05 +0100 Subject: [PATCH 35/91] elua: utilize the new elua_util APIs --- src/bin/elua/main.c | 150 ++------------------------------------------ src/lib/elua/elua.c | 4 +- 2 files changed, 6 insertions(+), 148 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 1fe60456e0..b335ce469d 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -30,149 +30,6 @@ static int _el_log_domain = -1; #define ERR(...) EINA_LOG_DOM_ERR(_el_log_domain, __VA_ARGS__) #define CRT(...) EINA_LOG_DOM_CRITICAL(_el_log_domain, __VA_ARGS__) -static int -elua_traceback(lua_State *L) -{ - lua_getglobal(L, "debug"); - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - return 1; - } - lua_getfield(L, -1, "traceback"); - if (!lua_isfunction(L, -1)) - { - lua_pop(L, 2); - return 1; - } - lua_pushvalue(L, 1); - lua_pushinteger(L, 2); - lua_call(L, 2, 1); - return 1; -} - -static int -elua_docall(Elua_State *es, int narg, int nret) -{ - int status; - lua_State *L = elua_state_lua_state_get(es); - int bs = lua_gettop(L) - narg; - lua_pushcfunction(L, elua_traceback); - lua_insert(L, bs); - status = lua_pcall(L, narg, nret, bs); - lua_remove(L, bs); - if (status) - lua_gc(L, LUA_GCCOLLECT, 0); - return status; -} - -static int -elua_getargs(Elua_State *es, int argc, char **argv, int n) -{ - lua_State *L = elua_state_lua_state_get(es); - int i; - int narg = argc - (n + 1); - luaL_checkstack(L, narg + 3, "too many arguments to script"); - for (i = n + 1; i < argc; ++i) - { - lua_pushstring(L, argv[i]); - } - lua_createtable(L, narg, n + 1); - for (i = 0; i < argc; ++i) - { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - n); - } - return narg; -} - -static int -elua_dolib(Elua_State *es, const char *libname) -{ - lua_State *L = elua_state_lua_state_get(es); - elua_state_require_ref_push(es); - lua_pushstring(L, libname); - return elua_report_error(es, elua_state_prog_name_get(es), - lua_pcall(L, 1, 0, 0)); -} - -static int -elua_dofile(Elua_State *es, const char *fname) -{ - return elua_report_error(es, elua_state_prog_name_get(es), - elua_io_loadfile(es, fname) - || elua_docall(es, 0, 1)); -} - -static int -elua_dostr(Elua_State *es, const char *chunk, const char *chname) -{ - return elua_report_error(es, elua_state_prog_name_get(es), - luaL_loadbuffer(elua_state_lua_state_get(es), - chunk, strlen(chunk), chname) - || elua_docall(es, 0, 0)); -} - -static Eina_Bool -elua_loadapp(Elua_State *es, const char *appname) -{ - lua_State *L = elua_state_lua_state_get(es); - elua_state_appload_ref_push(es); - lua_pushstring(L, appname); - lua_call(L, 1, 2); - if (lua_isnil(L, -2)) - { - lua_remove(L, -2); - return EINA_FALSE; - } - lua_pop(L, 1); - return EINA_TRUE; -} - -static int -elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) -{ - int status; - const char *fname = argv[n]; - int narg = elua_getargs(es, argc, argv, n); - lua_setglobal(elua_state_lua_state_get(es), "arg"); - if (fname[0] == '-' && !fname[1]) - { - fname = NULL; - } - if (fname) - { - /* check if there is a file of that name */ - FILE *f = fopen(fname, "r"); - if (f) - { - fclose(f); - status = elua_io_loadfile(es, fname); - } - else - status = !elua_loadapp(es, fname); - } - else - { - status = elua_io_loadfile(es, fname); - } - lua_insert(elua_state_lua_state_get(es), -(narg + 1)); - if (!status) - { - status = elua_docall(es, narg, 1); - } - else - { - lua_pop(elua_state_lua_state_get(es), narg); - } - if (!status) - { - *quit = lua_toboolean(elua_state_lua_state_get(es), -1); - lua_pop(elua_state_lua_state_get(es), 1); - } - return elua_report_error(es, elua_state_prog_name_get(es), status); -} - void elua_bin_shutdown(Elua_State *es, int c) { @@ -321,7 +178,7 @@ elua_main(lua_State *L) /* load all the things */ EINA_LIST_FREE(largs, data) { - if (elua_dolib(es, data)) + if (elua_util_require(es, data)) goto error; } @@ -329,13 +186,14 @@ elua_main(lua_State *L) if (optind < argc) { int quit = 0; - if ((m->status = elua_doscript(es, argc, argv, optind, &quit))) return 0; + if ((m->status = elua_util_script_run(es, argc, argv, optind, &quit))) + return 0; if (quit) return 0; } else if (!hasexec) { int quit; - if ((m->status = elua_dofile(es, NULL))) return 0; + if ((m->status = elua_util_file_run(es, NULL))) return 0; quit = lua_toboolean(L, -1); lua_pop(L, 1); if (quit) return 0; diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index df96e1d00c..32f203cc6b 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -416,7 +416,7 @@ elua_util_require(Elua_State *es, const char *libname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - EINA_SAFETY_ON_FALSE_RETURN_VAL(es->requireref, -1); + EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); lua_pushstring(es->luastate, libname); return elua_report_error(es, es->progname, lua_pcall(es->luastate, 1, 0, 0)); @@ -448,7 +448,7 @@ elua_util_app_load(Elua_State *es, const char *appname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); - EINA_SAFETY_ON_FALSE_RETURN_VAL(es->apploadref, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_appload_ref_push(es), EINA_FALSE); lua_pushstring(es->luastate, appname); lua_call(es->luastate, 1, 2); if (lua_isnil(es->luastate, -2)) From 8041b042fe8e105fd82dee860a8a91b822307915 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 16:58:27 +0100 Subject: [PATCH 36/91] elua lib: move i18n initialization there --- src/bin/elua/main.c | 6 +----- src/lib/elua/Elua.h | 2 +- src/lib/elua/elua.c | 12 +++++++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index b335ce469d..cf499aa174 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -163,12 +163,8 @@ elua_main(lua_State *L) luaL_register(L, NULL, cutillib); lua_call(L, 2, 0); - snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coredir); - if (elua_report_error(es, elua_state_prog_name_get(es), - elua_io_loadfile(es, modfile))) + if (!elua_state_setup_i18n(es)) goto error; - elua_state_setup_i18n(es); - lua_call(L, 1, 0); elua_io_register(es); lua_gc(L, LUA_GCRESTART, 0); diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index cb1edc13ee..5100f512e4 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -89,7 +89,7 @@ EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); -EAPI void elua_state_setup_i18n(const Elua_State *es); +EAPI Eina_Bool elua_state_setup_i18n(const Elua_State *es); EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); EAPI void elua_io_register(const Elua_State *es); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 32f203cc6b..3bda98b30f 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -283,7 +283,7 @@ const luaL_reg gettextlib[] = { NULL, NULL } }; -EAPI void +EAPI Eina_Bool elua_state_setup_i18n(const Elua_State *es) { #ifdef ENABLE_NLS @@ -291,6 +291,14 @@ elua_state_setup_i18n(const Elua_State *es) char *(*dngettextp)(const char*, const char*, const char*, unsigned long) = dngettext; #endif + char buf[PATH_MAX]; + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); + snprintf(buf, sizeof(buf), "%s/gettext.lua", es->coredir); + if (elua_report_error(es, elua_state_prog_name_get(es), + elua_io_loadfile(es, buf))) + return EINA_FALSE; lua_createtable(es->luastate, 0, 0); luaL_register(es->luastate, NULL, gettextlib); #ifdef ENABLE_NLS @@ -299,6 +307,8 @@ elua_state_setup_i18n(const Elua_State *es) lua_pushlightuserdata(es->luastate, *((void**)&dngettextp)); lua_setfield(es->luastate, -2, "dngettext"); #endif + lua_call(es->luastate, 1, 0); + return EINA_TRUE; } EAPI int From 24a694026a77170d1e8b14b9d6b23411f0e832c5 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 17:04:07 +0100 Subject: [PATCH 37/91] elua lib: add elua_state_setup_modules + use --- src/bin/elua/main.c | 29 +---------------------------- src/lib/elua/Elua.h | 1 + src/lib/elua/elua.c | 29 +++++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index cf499aa174..062ddf678f 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -49,13 +49,6 @@ struct Main_Data int status; }; -const luaL_reg cutillib[] = -{ - { "init_module", elua_module_init }, - { "popenv" , elua_io_popen }, - { NULL , NULL } -}; - static void elua_print_help(const char *pname, FILE *stream) { @@ -95,7 +88,6 @@ elua_main(lua_State *L) hasexec = EINA_FALSE; Eina_List *largs = NULL; const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; - char modfile[PATH_MAX]; char *data = NULL; int ch; @@ -144,26 +136,7 @@ elua_main(lua_State *L) elua_state_dirs_set(es, coredir, moddir, appsdir); elua_state_dirs_fill(es, noenv); - coredir = elua_state_core_dir_get(es); - moddir = elua_state_mod_dir_get(es); - appsdir = elua_state_apps_dir_get(es); - - if (!coredir || !moddir || !appsdir) - { - ERR("could not set one or more script directories"); - goto error; - } - - snprintf(modfile, sizeof(modfile), "%s/module.lua", coredir); - if (elua_report_error(es, elua_state_prog_name_get(es), - elua_io_loadfile(es, modfile))) - goto error; - lua_pushcfunction(L, elua_module_system_init); - lua_createtable(L, 0, 0); - luaL_register(L, NULL, cutillib); - lua_call(L, 2, 0); - - if (!elua_state_setup_i18n(es)) + if (!elua_state_setup_modules(es) || !elua_state_setup_i18n(es)) goto error; elua_io_register(es); diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 5100f512e4..e9bd6eeee9 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -90,6 +90,7 @@ EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); EAPI Eina_Bool elua_state_setup_i18n(const Elua_State *es); +EAPI Eina_Bool elua_state_setup_modules(const Elua_State *es); EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); EAPI void elua_io_register(const Elua_State *es); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 3bda98b30f..c124b20afc 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -295,9 +295,9 @@ elua_state_setup_i18n(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/gettext.lua", es->coredir); - if (elua_report_error(es, elua_state_prog_name_get(es), - elua_io_loadfile(es, buf))) + if (elua_report_error(es, es->progname, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_createtable(es->luastate, 0, 0); luaL_register(es->luastate, NULL, gettextlib); @@ -311,6 +311,31 @@ elua_state_setup_i18n(const Elua_State *es) return EINA_TRUE; } +const luaL_reg _elua_cutillib[] = +{ + { "init_module", elua_module_init }, + { "popenv" , elua_io_popen }, + { NULL , NULL } +}; + +EAPI Eina_Bool +elua_state_setup_modules(const Elua_State *es) +{ + char buf[PATH_MAX]; + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); + snprintf(buf, sizeof(buf), "%s/module.lua", es->coredir); + if (elua_report_error(es, es->progname, elua_io_loadfile(es, buf))) + return EINA_FALSE; + lua_pushcfunction(es->luastate, elua_module_system_init); + lua_createtable(es->luastate, 0, 0); + luaL_register(es->luastate, NULL, _elua_cutillib); + lua_call(es->luastate, 2, 0); + return EINA_TRUE; +} + EAPI int elua_module_init(lua_State *L) { From 02de415312b185aa79ef9cc4f80c7f98932579e6 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 10 Apr 2015 17:10:08 +0100 Subject: [PATCH 38/91] elua: cleanup main runtime --- src/bin/elua/main.c | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 062ddf678f..be4748653d 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -2,8 +2,6 @@ # include #endif -/* The Lua runtime component of the EFL */ - #include #include #include @@ -21,8 +19,7 @@ #include "Elua.h" -static Elua_State *elua_state = NULL; -static int _el_log_domain = -1; +static int _el_log_domain = -1; #define DBG(...) EINA_LOG_DOM_DBG(_el_log_domain, __VA_ARGS__) #define INF(...) EINA_LOG_DOM_INFO(_el_log_domain, __VA_ARGS__) @@ -84,20 +81,19 @@ static struct option lopt[] = static int elua_main(lua_State *L) { - Eina_Bool noenv = EINA_FALSE, - hasexec = EINA_FALSE; + Eina_Bool noenv = EINA_FALSE; Eina_List *largs = NULL; const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; char *data = NULL; - int ch; - - struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1); - Elua_State *es = m->es; + struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1); + Elua_State *es = m->es; int argc = m->argc; char **argv = m->argv; + int ch; + while ((ch = getopt_long(argc, argv, "+LhC:M:A:l:I:E", lopt, NULL)) != -1) { switch (ch) @@ -140,32 +136,26 @@ elua_main(lua_State *L) goto error; elua_io_register(es); + lua_gc(L, LUA_GCRESTART, 0); INF("elua lua state initialized"); - /* load all the things */ EINA_LIST_FREE(largs, data) - { - if (elua_util_require(es, data)) - goto error; - } + if (elua_util_require(es, data)) goto error; - /* run script or execute stdin as file */ if (optind < argc) { int quit = 0; - if ((m->status = elua_util_script_run(es, argc, argv, optind, &quit))) + if (elua_util_script_run(es, argc, argv, optind, &quit)) + goto error; + if (quit) return 0; - if (quit) return 0; } - else if (!hasexec) + else { - int quit; - if ((m->status = elua_util_file_run(es, NULL))) return 0; - quit = lua_toboolean(L, -1); - lua_pop(L, 1); - if (quit) return 0; + ERR("nothing to run"); + goto error; } ecore_main_loop_begin(); @@ -208,8 +198,6 @@ main(int argc, char **argv) elua_bin_shutdown(es, 1); } - elua_state = es; - INF("elua lua state created"); m.es = es; From eea537ded38c546d5c3500c711e5f6d61c968355 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 13 Apr 2015 15:42:02 +0100 Subject: [PATCH 39/91] elua: cleanup launcher --- src/bin/elua/main.c | 80 ++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index be4748653d..48665699f9 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -21,22 +21,8 @@ static int _el_log_domain = -1; -#define DBG(...) EINA_LOG_DOM_DBG(_el_log_domain, __VA_ARGS__) #define INF(...) EINA_LOG_DOM_INFO(_el_log_domain, __VA_ARGS__) -#define WRN(...) EINA_LOG_DOM_WARN(_el_log_domain, __VA_ARGS__) #define ERR(...) EINA_LOG_DOM_ERR(_el_log_domain, __VA_ARGS__) -#define CRT(...) EINA_LOG_DOM_CRITICAL(_el_log_domain, __VA_ARGS__) - -void -elua_bin_shutdown(Elua_State *es, int c) -{ - INF("elua shutdown"); - if (es) elua_state_free(es); - if (_el_log_domain != EINA_LOG_DOMAIN_GLOBAL) - eina_log_domain_unregister(_el_log_domain); - elua_shutdown(); - exit(c); -} struct Main_Data { @@ -95,35 +81,27 @@ elua_main(lua_State *L) int ch; while ((ch = getopt_long(argc, argv, "+LhC:M:A:l:I:E", lopt, NULL)) != -1) - { - switch (ch) - { - case 'h': - elua_print_help(elua_state_prog_name_get(es), stdout); - if (largs) eina_list_free(largs); - return 0; - case 'C': - coredir = optarg; - break; - case 'M': - moddir = optarg; - break; - case 'A': - appsdir = optarg; - break; - case 'l': - if (!optarg[0]) continue; - largs = eina_list_append(largs, optarg); - break; - case 'I': - if (!optarg[0]) continue; - elua_state_include_path_add(es, optarg); - break; - case 'E': - noenv = EINA_TRUE; - break; - } - } + switch (ch) + { + case 'h': + elua_print_help(elua_state_prog_name_get(es), stdout); goto success; + case 'C': + coredir = optarg; break; + case 'M': + moddir = optarg; break; + case 'A': + appsdir = optarg; break; + case 'l': + case 'I': + if (!optarg[0]) continue; + if (ch == 'l') + largs = eina_list_append(largs, optarg); + else + elua_state_include_path_add(es, optarg); + break; + case 'E': + noenv = EINA_TRUE; break; + } INF("arguments parsed"); @@ -150,7 +128,7 @@ elua_main(lua_State *L) if (elua_util_script_run(es, argc, argv, optind, &quit)) goto error; if (quit) - return 0; + goto success; } else { @@ -160,14 +138,26 @@ elua_main(lua_State *L) ecore_main_loop_begin(); - return 0; + goto success; error: m->status = 1; +success: if (largs) eina_list_free(largs); return 0; } +void +elua_bin_shutdown(Elua_State *es, int c) +{ + INF("elua shutdown"); + if (es) elua_state_free(es); + if (_el_log_domain != EINA_LOG_DOMAIN_GLOBAL) + eina_log_domain_unregister(_el_log_domain); + elua_shutdown(); + exit(c); +} + int main(int argc, char **argv) { From d8890209afc702643e1a066279eb8eda09b1119b Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 13 Apr 2015 16:21:25 +0100 Subject: [PATCH 40/91] elua lib: unify API conventions --- src/bin/elua/main.c | 5 ++--- src/lib/elua/Elua.h | 10 +++++----- src/lib/elua/cache.c | 7 ++++--- src/lib/elua/elua.c | 18 +++++++++--------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 48665699f9..ca7ccc1ab2 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -110,11 +110,10 @@ elua_main(lua_State *L) elua_state_dirs_set(es, coredir, moddir, appsdir); elua_state_dirs_fill(es, noenv); - if (!elua_state_setup_modules(es) || !elua_state_setup_i18n(es)) + if (!elua_state_modules_setup(es) || !elua_state_i18n_setup(es) + || !elua_state_io_setup(es)) goto error; - elua_io_register(es); - lua_gc(L, LUA_GCRESTART, 0); INF("elua lua state initialized"); diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index e9bd6eeee9..8d00e67464 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -87,15 +87,15 @@ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); -EAPI int elua_report_error(const Elua_State *es, const char *pname, int status); +EAPI int elua_error_report(const Elua_State *es, const char *pname, int status); -EAPI Eina_Bool elua_state_setup_i18n(const Elua_State *es); -EAPI Eina_Bool elua_state_setup_modules(const Elua_State *es); +EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); +EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); +EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); -EAPI void elua_io_register(const Elua_State *es); - EAPI int elua_io_popen(lua_State *L); + EAPI int elua_module_init(lua_State *L); EAPI int elua_module_system_init(lua_State *L); diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index 8cd1617f61..ae5cf93716 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -208,10 +208,11 @@ loadfile(lua_State *L) return 2; } -EAPI void -elua_io_register(const Elua_State *es) +EAPI Eina_Bool +elua_state_io_setup(const Elua_State *es) { - EINA_SAFETY_ON_FALSE_RETURN(es && es->luastate); + EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, EINA_FALSE); lua_pushcfunction(es->luastate, loadfile); lua_setglobal(es->luastate, "loadfile"); + return EINA_TRUE; } diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index c124b20afc..0207681947 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -237,7 +237,7 @@ _elua_errmsg(const char *pname, const char *msg) } EAPI int -elua_report_error(const Elua_State *es, const char *pname, int status) +elua_error_report(const Elua_State *es, const char *pname, int status) { EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); if (status && !lua_isnil(es->luastate, -1)) @@ -284,7 +284,7 @@ const luaL_reg gettextlib[] = }; EAPI Eina_Bool -elua_state_setup_i18n(const Elua_State *es) +elua_state_i18n_setup(const Elua_State *es) { #ifdef ENABLE_NLS char *(*dgettextp)(const char*, const char*) = dgettext; @@ -297,7 +297,7 @@ elua_state_setup_i18n(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/gettext.lua", es->coredir); - if (elua_report_error(es, es->progname, elua_io_loadfile(es, buf))) + if (elua_error_report(es, es->progname, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_createtable(es->luastate, 0, 0); luaL_register(es->luastate, NULL, gettextlib); @@ -319,7 +319,7 @@ const luaL_reg _elua_cutillib[] = }; EAPI Eina_Bool -elua_state_setup_modules(const Elua_State *es) +elua_state_modules_setup(const Elua_State *es) { char buf[PATH_MAX]; EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); @@ -327,7 +327,7 @@ elua_state_setup_modules(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/module.lua", es->coredir); - if (elua_report_error(es, es->progname, elua_io_loadfile(es, buf))) + if (elua_error_report(es, es->progname, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_pushcfunction(es->luastate, elua_module_system_init); lua_createtable(es->luastate, 0, 0); @@ -453,7 +453,7 @@ elua_util_require(Elua_State *es, const char *libname) EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); lua_pushstring(es->luastate, libname); - return elua_report_error(es, es->progname, + return elua_error_report(es, es->progname, lua_pcall(es->luastate, 1, 0, 0)); } @@ -462,7 +462,7 @@ elua_util_file_run(Elua_State *es, const char *fname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_report_error(es, es->progname, + return elua_error_report(es, es->progname, elua_io_loadfile(es, fname) || _elua_docall(es, 0, 1)); } @@ -472,7 +472,7 @@ elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_report_error(es, es->progname, + return elua_error_report(es, es->progname, luaL_loadbuffer(es->luastate, chunk, strlen(chunk), chname) || _elua_docall(es, 0, 0)); @@ -531,5 +531,5 @@ elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) *quit = lua_toboolean(es->luastate, -1); lua_pop(es->luastate, 1); } - return elua_report_error(es, es->progname, status); + return elua_error_report(es, es->progname, status); } From f8f9cc14a798a4e760e09c6c3a2d54eea02e40ee Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Tue, 14 Apr 2015 14:14:50 +0100 Subject: [PATCH 41/91] elua lib: API rename --- src/lib/elua/Elua.h | 2 +- src/lib/elua/cache.c | 2 +- src/lib/elua/elua.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 8d00e67464..1d45e1c5cb 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -66,7 +66,7 @@ EAPI int elua_init(void); EAPI int elua_shutdown(void); EAPI Elua_State *elua_state_new(const char *progname); -EAPI Elua_State *elua_state_from_lua_get(lua_State *L); +EAPI Elua_State *elua_state_from_lua_state_get(lua_State *L); EAPI void elua_state_free(Elua_State *es); diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index ae5cf93716..43ef3d7d19 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -190,7 +190,7 @@ elua_io_loadfile(const Elua_State *es, const char *fname) static int loadfile(lua_State *L) { - Elua_State *es = elua_state_from_lua_get(L); + Elua_State *es = elua_state_from_lua_state_get(L); const char *fname = luaL_optstring(L, 1, NULL); int status = elua_io_loadfile(es, fname), hasenv = (lua_gettop(L) >= 3); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 0207681947..229eb65a12 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -216,7 +216,7 @@ elua_state_lua_state_get(const Elua_State *es) } EAPI Elua_State * -elua_state_from_lua_get(lua_State *L) +elua_state_from_lua_state_get(lua_State *L) { EINA_SAFETY_ON_NULL_RETURN_VAL(L, NULL); lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr"); @@ -339,7 +339,7 @@ elua_state_modules_setup(const Elua_State *es) EAPI int elua_module_init(lua_State *L) { - Elua_State *es = elua_state_from_lua_get(L); + Elua_State *es = elua_state_from_lua_state_get(L); if (!lua_isnoneornil(L, 1)) { lua_pushvalue(L, 1); @@ -357,7 +357,7 @@ elua_module_init(lua_State *L) EAPI int elua_module_system_init(lua_State *L) { - Elua_State *es = elua_state_from_lua_get(L); + Elua_State *es = elua_state_from_lua_state_get(L); const char *corepath = es->coredir; const char *modpath = es->moddir; const char *appspath = es->appsdir; From 7f05cfc0e6795f4dee66c0b9175c658f40d14ab4 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Tue, 14 Apr 2015 16:14:51 +0100 Subject: [PATCH 42/91] elua lib: error_report is now in util namespace --- src/lib/elua/Elua.h | 5 +++-- src/lib/elua/elua.c | 50 ++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 1d45e1c5cb..880c7736a2 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -87,8 +87,6 @@ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); -EAPI int elua_error_report(const Elua_State *es, const char *pname, int status); - EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); @@ -107,6 +105,9 @@ EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit); +EAPI int elua_util_error_report(const Elua_State *es, const char *pname, + int status); + #endif #ifdef __cplusplus diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 229eb65a12..799dd07ee2 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -230,25 +230,6 @@ elua_state_from_lua_state_get(lua_State *L) return NULL; } -static void -_elua_errmsg(const char *pname, const char *msg) -{ - ERR("%s%s%s", pname ? pname : "", pname ? ": " : "", msg); -} - -EAPI int -elua_error_report(const Elua_State *es, const char *pname, int status) -{ - EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); - if (status && !lua_isnil(es->luastate, -1)) - { - const char *msg = lua_tostring(es->luastate, -1); - _elua_errmsg(pname, msg ? msg : "(non-string error)"); - lua_pop(es->luastate, 1); - } - return status; -} - static int _elua_gettext_bind_textdomain(lua_State *L) { @@ -297,7 +278,7 @@ elua_state_i18n_setup(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/gettext.lua", es->coredir); - if (elua_error_report(es, es->progname, elua_io_loadfile(es, buf))) + if (elua_util_error_report(es, es->progname, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_createtable(es->luastate, 0, 0); luaL_register(es->luastate, NULL, gettextlib); @@ -327,7 +308,7 @@ elua_state_modules_setup(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/module.lua", es->coredir); - if (elua_error_report(es, es->progname, elua_io_loadfile(es, buf))) + if (elua_util_error_report(es, es->progname, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_pushcfunction(es->luastate, elua_module_system_init); lua_createtable(es->luastate, 0, 0); @@ -453,7 +434,7 @@ elua_util_require(Elua_State *es, const char *libname) EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); lua_pushstring(es->luastate, libname); - return elua_error_report(es, es->progname, + return elua_util_error_report(es, es->progname, lua_pcall(es->luastate, 1, 0, 0)); } @@ -462,7 +443,7 @@ elua_util_file_run(Elua_State *es, const char *fname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_error_report(es, es->progname, + return elua_util_error_report(es, es->progname, elua_io_loadfile(es, fname) || _elua_docall(es, 0, 1)); } @@ -472,7 +453,7 @@ elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_error_report(es, es->progname, + return elua_util_error_report(es, es->progname, luaL_loadbuffer(es->luastate, chunk, strlen(chunk), chname) || _elua_docall(es, 0, 0)); @@ -531,5 +512,24 @@ elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) *quit = lua_toboolean(es->luastate, -1); lua_pop(es->luastate, 1); } - return elua_error_report(es, es->progname, status); + return elua_util_error_report(es, es->progname, status); +} + +static void +_elua_errmsg(const char *pname, const char *msg) +{ + ERR("%s%s%s", pname ? pname : "", pname ? ": " : "", msg); +} + +EAPI int +elua_util_error_report(const Elua_State *es, const char *pname, int status) +{ + EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); + if (status && !lua_isnil(es->luastate, -1)) + { + const char *msg = lua_tostring(es->luastate, -1); + _elua_errmsg(pname, msg ? msg : "(non-string error)"); + lua_pop(es->luastate, 1); + } + return status; } From dbe34d803c37a1e9b85b787fba4f6e015c66c134 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 10:55:27 +0100 Subject: [PATCH 43/91] elua lib: add test suite --- src/Makefile_Elua.am | 29 ++++++++++ src/tests/elua/elua_lib.c | 21 +++++++ src/tests/elua/elua_suite.c | 108 ++++++++++++++++++++++++++++++++++++ src/tests/elua/elua_suite.h | 9 +++ 4 files changed, 167 insertions(+) create mode 100644 src/tests/elua/elua_lib.c create mode 100644 src/tests/elua/elua_suite.c create mode 100644 src/tests/elua/elua_suite.h diff --git a/src/Makefile_Elua.am b/src/Makefile_Elua.am index 49ba426711..fa2f078428 100644 --- a/src/Makefile_Elua.am +++ b/src/Makefile_Elua.am @@ -102,4 +102,33 @@ eluacore_DATA = \ EXTRA_DIST += $(eluacore_DATA) +if EFL_ENABLE_TESTS + +check_PROGRAMS += tests/elua/elua_suite + +tests_elua_elua_suite_SOURCES = \ +tests/elua/elua_lib.c \ +tests/elua/elua_suite.c \ +tests/elua/elua_suite.h + +tests_elua_elua_suite_CPPFLAGS = -I$(top_builddir)/src/lib/efl \ +-DTESTS_BUILD_DIR=\"$(top_builddir)/src/tests/elua\" \ +-DPACKAGE_DATA_DIR=\"$(top_srcdir)/src/tests/elua\" \ +-DPACKAGE_BUILD_DIR=\"$(abs_top_builddir)\" \ +@CHECK_CFLAGS@ \ +@ELUA_CFLAGS@ + +TESTS += tests/elua/elua_suite + +if HAVE_OSX +if HAVE_X86_64 +tests_elua_elua_suite_LDFLAGS = -pagezero_size 10000 -image_base 100000000 +endif +endif + +tests_elua_elua_suite_LDADD = @CHECK_LIBS@ @USE_ELUA_LIBS@ +tests_elua_elua_suite_DEPENDENCIES = @USE_ELUA_INTERNAL_LIBS@ + +endif + endif diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c new file mode 100644 index 0000000000..702aed50fe --- /dev/null +++ b/src/tests/elua/elua_lib.c @@ -0,0 +1,21 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +#include +#include "Elua.h" +#include "elua_suite.h" + +/*START_TEST(test_name) +{ +} +END_TEST*/ + +void elua_lib_test(TCase *tc) +{ + //tcase_add_test(tc, test_name); +} + diff --git a/src/tests/elua/elua_suite.c b/src/tests/elua/elua_suite.c new file mode 100644 index 0000000000..13a6df8f85 --- /dev/null +++ b/src/tests/elua/elua_suite.c @@ -0,0 +1,108 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +#include +#include "elua_suite.h" + +typedef struct _Elua_Test_Case Elua_Test_Case; + +struct _Elua_Test_Case +{ + const char *test_case; + void (*build)(TCase *tc); +}; + +static const Elua_Test_Case etc[] = { + { "Elua Library", elua_lib_test}, + { NULL, NULL } +}; + +static void +_list_tests(void) +{ + const Elua_Test_Case *itr; + + itr = etc; + fputs("Available Test Cases:\n", stderr); + for (; itr->test_case; itr++) + fprintf(stderr, "\t%s\n", itr->test_case); +} + +static Eina_Bool +_use_test(int argc, const char **argv, const char *test_case) +{ + if (argc < 1) + return 1; + + for (; argc > 0; argc--, argv++) + if (strcmp(test_case, *argv) == 0) + return 1; + return 0; +} + +static Suite * +elua_suite_build(int argc, const char **argv) +{ + TCase *tc; + Suite *s; + int i; + + s = suite_create("Elua"); + + for (i = 0; etc[i].test_case; ++i) + { + if (!_use_test(argc, argv, etc[i].test_case)) continue; + tc = tcase_create(etc[i].test_case); + + etc[i].build(tc); + + suite_add_tcase(s, tc); +#ifndef _WIN32 + tcase_set_timeout(tc, 0); +#endif + } + + return s; +} + +int +main(int argc, char **argv) +{ + Suite *s; + SRunner *sr; + int i, failed_count; + setenv("CK_FORK", "no", 0); + + for (i = 1; i < argc; i++) + if ((strcmp(argv[i], "-h" ) == 0) || + (strcmp(argv[i], "--help") == 0)) + { + fprintf(stderr, "Usage:\n\t%s [test_case1 .. [test_caseN]]\n", + argv[0]); + _list_tests(); + return 0; + } + else if ((strcmp(argv[i], "-l" ) == 0) || + (strcmp(argv[i], "--list") == 0)) + { + _list_tests(); + return 0; + } + + putenv("EFL_RUN_IN_TREE=1"); + + s = elua_suite_build(argc - 1, (const char **)argv + 1); + sr = srunner_create(s); + + srunner_set_xml(sr, TESTS_BUILD_DIR "/check-results.xml"); + + srunner_run_all(sr, CK_ENV); + failed_count = srunner_ntests_failed(sr); + srunner_free(sr); + + return (failed_count == 0) ? 0 : 255; +} diff --git a/src/tests/elua/elua_suite.h b/src/tests/elua/elua_suite.h new file mode 100644 index 0000000000..0d48d02617 --- /dev/null +++ b/src/tests/elua/elua_suite.h @@ -0,0 +1,9 @@ +#ifndef _ELUA_SUITE_H +#define _ELUA_SUITE_H + +#include + +void elua_lib_test(TCase *tc); + +#endif /* _ELUA_SUITE_H */ + From 1f35cdd473fec6c17bb42843ec11fc0b8ca4c67a Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 13:45:08 +0100 Subject: [PATCH 44/91] elua: initial test (state creation/deletion) --- src/tests/elua/elua_lib.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 702aed50fe..cfa29097b9 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -9,13 +9,21 @@ #include "Elua.h" #include "elua_suite.h" -/*START_TEST(test_name) +START_TEST(elua_api) { + fail_if(!elua_init()); + + Elua_State *st = elua_state_new("test"); + fail_if(!st); + + elua_state_free(st); + + elua_shutdown(); } -END_TEST*/ +END_TEST void elua_lib_test(TCase *tc) { - //tcase_add_test(tc, test_name); + tcase_add_test(tc, elua_api); } From a2e2eb7f89a672b9b01f2fe5e74224d0998856e8 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 14:02:21 +0100 Subject: [PATCH 45/91] elua: test state retrieval --- src/tests/elua/elua_lib.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index cfa29097b9..1b7af87b37 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -11,11 +11,19 @@ START_TEST(elua_api) { + Elua_State *st; + lua_State *lst; + fail_if(!elua_init()); - Elua_State *st = elua_state_new("test"); + st = elua_state_new("test"); fail_if(!st); + lst = elua_state_lua_state_get(st); + fail_if(!lst); + + fail_if(elua_state_from_lua_state_get(lst) != st); + elua_state_free(st); elua_shutdown(); From 8a3a32b6fac458d71c9d6054db80da659990848b Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 14:08:28 +0100 Subject: [PATCH 46/91] elua lib: test progname --- src/tests/elua/elua_lib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 1b7af87b37..600ef5f695 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -24,6 +24,8 @@ START_TEST(elua_api) fail_if(elua_state_from_lua_state_get(lst) != st); + fail_if(strcmp(elua_state_prog_name_get(st), "test")); + elua_state_free(st); elua_shutdown(); From e77ba60a5829136bd393f3b434c91427640ac20e Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 14:58:42 +0100 Subject: [PATCH 47/91] elua lib: tests for core dirs + fix core dirs --- src/Makefile_Elua.am | 9 +++++++++ src/Makefile_Elua_Helper.am | 7 ++----- src/lib/elua/elua.c | 2 +- src/tests/elua/elua_lib.c | 6 ++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Makefile_Elua.am b/src/Makefile_Elua.am index fa2f078428..a053053321 100644 --- a/src/Makefile_Elua.am +++ b/src/Makefile_Elua.am @@ -46,6 +46,11 @@ bin_elua_elua_LDADD = @ELUA_LIBS@ @USE_ELUA_LIBS@ endif bin_elua_elua_DEPENDENCIES = @ELUA_INTERNAL_LIBS@ lib/elua/libelua.la +ELUA_BINDINGS_DIR = $(abs_top_srcdir)/src/bindings/luajit +ELUA_CORE_DIR = $(abs_top_srcdir)/src/scripts/elua/core +ELUA_MODULES_DIR = $(abs_top_srcdir)/src/scripts/elua/modules +ELUA_APPS_DIR = $(abs_top_srcdir)/src/scripts/elua/apps + ### Helper for other modules using Elua include Makefile_Elua_Helper.am @@ -115,6 +120,10 @@ tests_elua_elua_suite_CPPFLAGS = -I$(top_builddir)/src/lib/efl \ -DTESTS_BUILD_DIR=\"$(top_builddir)/src/tests/elua\" \ -DPACKAGE_DATA_DIR=\"$(top_srcdir)/src/tests/elua\" \ -DPACKAGE_BUILD_DIR=\"$(abs_top_builddir)\" \ +-DELUA_BINDINGS_DIR=\"$(ELUA_BINDINGS_DIR)\" \ +-DELUA_CORE_DIR=\"$(ELUA_CORE_DIR)\" \ +-DELUA_MODULES_DIR=\"$(ELUA_MODULES_DIR)\" \ +-DELUA_APPS_DIR=\"$(ELUA_APPS_DIR)\" \ @CHECK_CFLAGS@ \ @ELUA_CFLAGS@ diff --git a/src/Makefile_Elua_Helper.am b/src/Makefile_Elua_Helper.am index 479a31ddb6..10b15541fc 100644 --- a/src/Makefile_Elua_Helper.am +++ b/src/Makefile_Elua_Helper.am @@ -4,11 +4,8 @@ _ELUA_GEN_DEP = @elua_bin@ else ELUA_GEN = ELUA_EOLIAN_LIBRARY_PATH=$(top_builddir)/src/lib/eolian/.libs \ $(top_builddir)/src/bin/elua/elua${EXEEXT} \ - -I$(abs_top_srcdir)/src/bindings/luajit \ - -C$(abs_top_srcdir)/src/scripts/elua/core \ - -M$(abs_top_srcdir)/src/scripts/elua/modules \ - -A$(abs_top_srcdir)/src/scripts/elua/apps \ - lualian + -I$(ELUA_BINDINGS_DIR) -C$(ELUA_CORE_DIR) -M$(ELUA_MODULES_DIR) \ + -A$(ELUA_APPS_DIR) lualian _ELUA_GEN_DEP = bin/elua/elua${EXEEXT} scripts/elua/apps/lualian.lua \ scripts/elua/modules/lualian.lua \ scripts/elua/modules/getopt.lua scripts/elua/core/util.lua \ diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 799dd07ee2..d60f1a3963 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -169,7 +169,7 @@ EAPI Eina_Stringshare * elua_state_apps_dir_get(const Elua_State *es) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, NULL); - return es->moddir; + return es->appsdir; } EAPI Eina_Stringshare * diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 600ef5f695..cfe123657c 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -19,6 +19,12 @@ START_TEST(elua_api) st = elua_state_new("test"); fail_if(!st); + elua_state_dirs_set(st, ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR); + + fail_if(strcmp(elua_state_core_dir_get(st), ELUA_CORE_DIR)); + fail_if(strcmp(elua_state_mod_dir_get(st), ELUA_MODULES_DIR)); + fail_if(strcmp(elua_state_apps_dir_get(st), ELUA_APPS_DIR)); + lst = elua_state_lua_state_get(st); fail_if(!lst); From 75dd60d62d90efde12e5067d08df816e13de203a Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:08:02 +0100 Subject: [PATCH 48/91] elua lib: test elua_state_dirs_fill --- src/tests/elua/elua_lib.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index cfe123657c..60e068cbc9 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -19,8 +19,17 @@ START_TEST(elua_api) st = elua_state_new("test"); fail_if(!st); - elua_state_dirs_set(st, ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR); + /* test env vars */ + setenv("ELUA_CORE_DIR", "foo", 1); + setenv("ELUA_MODULES_DIR", "bar", 1); + setenv("ELUA_APPS_DIR", "baz", 1); + elua_state_dirs_fill(st, EINA_FALSE); + fail_if(strcmp(elua_state_core_dir_get(st), "foo")); + fail_if(strcmp(elua_state_mod_dir_get(st), "bar")); + fail_if(strcmp(elua_state_apps_dir_get(st), "baz")); + /* now fill it properly */ + elua_state_dirs_set(st, ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR); fail_if(strcmp(elua_state_core_dir_get(st), ELUA_CORE_DIR)); fail_if(strcmp(elua_state_mod_dir_get(st), ELUA_MODULES_DIR)); fail_if(strcmp(elua_state_apps_dir_get(st), ELUA_APPS_DIR)); From a6c5ec55a7462aad831cb82ca96ee3c1d4da6eb9 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:11:43 +0100 Subject: [PATCH 49/91] elua lib: setup funcs tests --- src/tests/elua/elua_lib.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 60e068cbc9..e93707322c 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -9,6 +9,11 @@ #include "Elua.h" #include "elua_suite.h" +/* Untestable APIs: + * + * void elua_state_include_path_add(Elua_State *, const char *); + */ + START_TEST(elua_api) { Elua_State *st; @@ -41,6 +46,10 @@ START_TEST(elua_api) fail_if(strcmp(elua_state_prog_name_get(st), "test")); + fail_if(!elua_state_modules_setup(st)); + fail_if(!elua_state_i18n_setup(st)); + fail_if(!elua_state_io_setup(st)); + elua_state_free(st); elua_shutdown(); From 259f9067c2fa562e6f7d92f5bbb4018a769c0a07 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:16:15 +0100 Subject: [PATCH 50/91] elua lib: tests for require/appload refs --- src/tests/elua/elua_lib.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index e93707322c..98fca1a321 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -50,6 +50,14 @@ START_TEST(elua_api) fail_if(!elua_state_i18n_setup(st)); fail_if(!elua_state_io_setup(st)); + fail_if(!elua_state_require_ref_push(st)); + fail_if(lua_type(lst, -1) != LUA_TFUNCTION); + lua_pop(lst, 1); + + fail_if(!elua_state_appload_ref_push(st)); + fail_if(lua_type(lst, -1) != LUA_TFUNCTION); + lua_pop(lst, 1); + elua_state_free(st); elua_shutdown(); From 3b17064c105d3f52d737e95a2e082e2bba09ea34 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:23:33 +0100 Subject: [PATCH 51/91] elua lib: test some util APIs --- src/tests/elua/elua_lib.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 98fca1a321..5a87929467 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -9,11 +9,6 @@ #include "Elua.h" #include "elua_suite.h" -/* Untestable APIs: - * - * void elua_state_include_path_add(Elua_State *, const char *); - */ - START_TEST(elua_api) { Elua_State *st; @@ -39,6 +34,9 @@ START_TEST(elua_api) fail_if(strcmp(elua_state_mod_dir_get(st), ELUA_MODULES_DIR)); fail_if(strcmp(elua_state_apps_dir_get(st), ELUA_APPS_DIR)); + /* needed for later setup, but untestable alone */ + elua_state_include_path_add(st, ELUA_BINDINGS_DIR); + lst = elua_state_lua_state_get(st); fail_if(!lst); @@ -58,6 +56,10 @@ START_TEST(elua_api) fail_if(lua_type(lst, -1) != LUA_TFUNCTION); lua_pop(lst, 1); + fail_if(elua_util_require(st, "util")); + fail_if(elua_util_string_run(st, "return 1337", "foo")); + fail_if(!elua_util_string_run(st, "foo bar", "foo")); /* invalid code */ + elua_state_free(st); elua_shutdown(); From e8a4f47f93605c75700d826a8339ca60bc7d32b4 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:25:37 +0100 Subject: [PATCH 52/91] elua lib: test more utility APIs --- src/tests/elua/elua_lib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 5a87929467..6d261c8574 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -59,6 +59,8 @@ START_TEST(elua_api) fail_if(elua_util_require(st, "util")); fail_if(elua_util_string_run(st, "return 1337", "foo")); fail_if(!elua_util_string_run(st, "foo bar", "foo")); /* invalid code */ + fail_if(!elua_util_app_load(st, "lualian")); + fail_if(elua_util_app_load(st, "non_existent_app")); elua_state_free(st); From 07133df0b59a176c6ad895c4c3010d33fcad8a71 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:33:10 +0100 Subject: [PATCH 53/91] elua lib: test error reporting + lua stack state --- src/tests/elua/elua_lib.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 6d261c8574..4bf77962d1 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -60,7 +60,17 @@ START_TEST(elua_api) fail_if(elua_util_string_run(st, "return 1337", "foo")); fail_if(!elua_util_string_run(st, "foo bar", "foo")); /* invalid code */ fail_if(!elua_util_app_load(st, "lualian")); + fail_if(lua_type(lst, -1) != LUA_TFUNCTION); + lua_pop(lst, 1); fail_if(elua_util_app_load(st, "non_existent_app")); + fail_if(lua_type(lst, -1) != LUA_TSTRING); + lua_pop(lst, 1); + + /* halfassed testing here, but not possible otherwise */ + fail_if(elua_util_error_report(st, "foo", 0)); + lua_pushliteral(lst, "msg"); + fail_if(!elua_util_error_report(st, "foo", 5)); + fail_if(lua_gettop(lst) > 0); elua_state_free(st); From 5bfa8e8b142ae010b9557f1025c2ecf15c97e750 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:36:17 +0100 Subject: [PATCH 54/91] elua lib: test raw file load --- src/tests/elua/elua_lib.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 4bf77962d1..aa4e1be340 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -65,6 +65,12 @@ START_TEST(elua_api) fail_if(elua_util_app_load(st, "non_existent_app")); fail_if(lua_type(lst, -1) != LUA_TSTRING); lua_pop(lst, 1); + fail_if(elua_io_loadfile(st, ELUA_CORE_DIR "/util.lua")); + fail_if(lua_type(lst, -1) != LUA_TFUNCTION); + lua_pop(lst, 1); + fail_if(!elua_io_loadfile(st, ELUA_CORE_DIR "/non_existent_file.lua")); + fail_if(lua_type(lst, -1) != LUA_TSTRING); + lua_pop(lst, 1); /* halfassed testing here, but not possible otherwise */ fail_if(elua_util_error_report(st, "foo", 0)); From 9e28b90010aff31e9dbbd6fb539048893ebfbb2c Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 15:38:28 +0100 Subject: [PATCH 55/91] elua lib: unset environment vars in tests --- src/tests/elua/elua_lib.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index aa4e1be340..ffa99f52e6 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -27,6 +27,9 @@ START_TEST(elua_api) fail_if(strcmp(elua_state_core_dir_get(st), "foo")); fail_if(strcmp(elua_state_mod_dir_get(st), "bar")); fail_if(strcmp(elua_state_apps_dir_get(st), "baz")); + unsetenv("ELUA_CORE_DIR"); + unsetenv("ELUA_MODULES_DIR"); + unsetenv("ELUA_APPS_DIR"); /* now fill it properly */ elua_state_dirs_set(st, ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR); From 4da4f7a534451fce42ab1cd75f2bbb7346bea538 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 16:06:48 +0100 Subject: [PATCH 56/91] elua lib: test elua_util_file_run --- src/tests/elua/elua_lib.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index ffa99f52e6..6c8952c731 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -13,6 +13,9 @@ START_TEST(elua_api) { Elua_State *st; lua_State *lst; + char buf[] = "tmpXXXXXX"; + FILE *f; + int fd; fail_if(!elua_init()); @@ -75,6 +78,17 @@ START_TEST(elua_api) fail_if(lua_type(lst, -1) != LUA_TSTRING); lua_pop(lst, 1); + fd = mkstemp(buf); + fail_if(fd < 0); + f = fdopen(fd, "w"); + fail_if(!f); + fprintf(f, "return 5\n"); + fclose(f); + fail_if(elua_util_file_run(st, buf)); + fail_if(lua_tointeger(lst, -1) != 5); + lua_pop(lst, 1); + fail_if(remove(buf)); + /* halfassed testing here, but not possible otherwise */ fail_if(elua_util_error_report(st, "foo", 0)); lua_pushliteral(lst, "msg"); From 145cd1a615b6060805a5ce2aa197b77b523c1e5b Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 16:20:55 +0100 Subject: [PATCH 57/91] elua lib: test elua_util_script_run --- src/tests/elua/elua_lib.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 6c8952c731..0d670e56d8 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -16,6 +16,12 @@ START_TEST(elua_api) char buf[] = "tmpXXXXXX"; FILE *f; int fd; + char *cargv[2]; + char arg1[] = "test"; + char arg2[] = "lualian"; + int quit = 0; + cargv[0] = arg1; + cargv[1] = arg2; fail_if(!elua_init()); @@ -95,6 +101,18 @@ START_TEST(elua_api) fail_if(!elua_util_error_report(st, "foo", 5)); fail_if(lua_gettop(lst) > 0); + fail_if(elua_util_script_run(st, 2, cargv, 1, &quit)); + fail_if(quit != 1); + + f = fopen(buf, "w"); + fail_if(!f); + fprintf(f, "return false"); + fclose(f); + cargv[1] = buf; + fail_if(elua_util_script_run(st, 2, cargv, 1, &quit)); + fail_if(quit != 0); + fail_if(remove(buf)); + elua_state_free(st); elua_shutdown(); From 7cb8d13980150e8a2f99377b8f2987b3c7aa9ff7 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 16 Apr 2015 16:30:05 +0100 Subject: [PATCH 58/91] elua lib: hide some APIs that should not be public --- src/lib/elua/Elua.h | 4 ---- src/lib/elua/elua.c | 19 +++++++++++-------- src/lib/elua/elua_private.h | 2 ++ src/lib/elua/io.c | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 880c7736a2..81a6421b6e 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -92,10 +92,6 @@ EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); -EAPI int elua_io_popen(lua_State *L); - -EAPI int elua_module_init(lua_State *L); -EAPI int elua_module_system_init(lua_State *L); EAPI int elua_util_require(Elua_State *es, const char *libname); EAPI int elua_util_file_run(Elua_State *es, const char *fname); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index d60f1a3963..488438ab0d 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -292,11 +292,14 @@ elua_state_i18n_setup(const Elua_State *es) return EINA_TRUE; } +int _elua_module_init(lua_State *L); +int _elua_module_system_init(lua_State *L); + const luaL_reg _elua_cutillib[] = { - { "init_module", elua_module_init }, - { "popenv" , elua_io_popen }, - { NULL , NULL } + { "init_module", _elua_module_init }, + { "popenv" , _elua_io_popen }, + { NULL , NULL } }; EAPI Eina_Bool @@ -310,15 +313,15 @@ elua_state_modules_setup(const Elua_State *es) snprintf(buf, sizeof(buf), "%s/module.lua", es->coredir); if (elua_util_error_report(es, es->progname, elua_io_loadfile(es, buf))) return EINA_FALSE; - lua_pushcfunction(es->luastate, elua_module_system_init); + lua_pushcfunction(es->luastate, _elua_module_system_init); lua_createtable(es->luastate, 0, 0); luaL_register(es->luastate, NULL, _elua_cutillib); lua_call(es->luastate, 2, 0); return EINA_TRUE; } -EAPI int -elua_module_init(lua_State *L) +int +_elua_module_init(lua_State *L) { Elua_State *es = elua_state_from_lua_state_get(L); if (!lua_isnoneornil(L, 1)) @@ -335,8 +338,8 @@ elua_module_init(lua_State *L) return 0; } -EAPI int -elua_module_system_init(lua_State *L) +int +_elua_module_system_init(lua_State *L) { Elua_State *es = elua_state_from_lua_state_get(L); const char *corepath = es->coredir; diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index fbf7bb3ca9..cf24e1e661 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -47,4 +47,6 @@ extern int _elua_log_dom; #define ERR(...) EINA_LOG_DOM_ERR(_elua_log_dom, __VA_ARGS__) #define CRT(...) EINA_LOG_DOM_CRITICAL(_elua_log_dom, __VA_ARGS__) +int _elua_io_popen(lua_State *L); + #endif diff --git a/src/lib/elua/io.c b/src/lib/elua/io.c index 4b73a3b789..91eef6f372 100644 --- a/src/lib/elua/io.c +++ b/src/lib/elua/io.c @@ -341,8 +341,8 @@ elua_newfile(lua_State *L) return f; } -EAPI int -elua_io_popen(lua_State *L) +int +_elua_io_popen(lua_State *L) { const char *fname = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); From 81e3f753da5ab2edc3a0cb2ba7576dffbb0863ed Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 14:38:18 +0100 Subject: [PATCH 59/91] elua lib: start adding documentation --- src/lib/elua/Elua.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 81a6421b6e..bd4b20ba1c 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -52,6 +52,50 @@ extern "C" { # include #endif +/** + * @page elua_main Elua library (BETA) + * + * @date 2015 (created) + * + * @section toc Table of Contents + * + * @li @ref elua_main_intro + * @li @ref elua_main_compiling + * @li @ref elua_main_next_steps + * + * @section elua_main_intro Introduction + * + * The Elua library provides all necessary infrastructure required to set up + * a fully functional Lua state able of running Elua scripts. This is provided + * as a library in order to encourage reuse from different libraries and apps. + * + * @section elua_main_compiling How to compile + * + * As Elua is a library, compiling is very simple. + * + * Compiling C or C++ files into object files: + * + * @verbatim + gcc -c -o main.o main.c `pkg-config --cflags elua` + @endverbatim + * + * Linking object files into a binary executable: + * + * @verbatim + gcc -o my_application main.o `pkg-config --libs elua` + @endverbatim + * + * See @ref pkgconfig + * + * @section elua_main_next_steps Next Steps + * + * There is a comperehensive API reference available that should get you up + * and running. + * + * @addtogroup Elua + * @{ + */ + #ifdef EFL_BETA_API_SUPPORT #include From f16f21f37d926cc87b3ea8dcc03538e6f9aa4a7c Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 14:41:50 +0100 Subject: [PATCH 60/91] elua lib: add init/shutdown doc --- src/lib/elua/Elua.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index bd4b20ba1c..4588bcb8ab 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -104,9 +104,35 @@ extern "C" { #include #include +/** Opaque Elua state + * + * @ingroup Elua + */ typedef struct _Elua_State Elua_State; +/** + * @brief Initialize the Elua library. + * + * This initializes the Elua library for usage. It maintains an internal + * counter so that multiple calls will only increment/decrement correctly. + * + * @see elua_shutdown + * + * @ingroup Elua + */ EAPI int elua_init(void); + +/** + * @brief Shutdown the Elua library. + * + * Depending on the internal initialization counter, this either decrements + * or completely shuts down the Elua library. In any case, call this once for + * each init call. + * + * @see elua_init + * + * @ingroup Elua + */ EAPI int elua_shutdown(void); EAPI Elua_State *elua_state_new(const char *progname); From 8b6dd5a417d67ae4842bf0de50a974881214bf08 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 14:45:08 +0100 Subject: [PATCH 61/91] elua lib: add state_new doc --- src/lib/elua/Elua.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 4588bcb8ab..c88d20997a 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -135,6 +135,20 @@ EAPI int elua_init(void); */ EAPI int elua_shutdown(void); +/** + * @brief Create a new Elua state. + * + * This creates a new Elua state. An Elua state is externally opaque, but + * it contains a LuaJIT state as well as some additional information that + * is mostly initialized by other APIs. + * + * @param[in] progname The program name that holds the Elua state. This will + * be used for stuff like error reporting. Typically the same as the binary + * name of the application (argv[0]). + * @return A new Elua state or NULL. + * + * @ingroup Elua + */ EAPI Elua_State *elua_state_new(const char *progname); EAPI Elua_State *elua_state_from_lua_state_get(lua_State *L); From 8f7c9a3b2606f3a582a284286bb77e743f02c1e1 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 14:47:47 +0100 Subject: [PATCH 62/91] elua lib: add state_from_lua_state_get doc --- src/lib/elua/Elua.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index c88d20997a..e764750c1c 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -150,6 +150,20 @@ EAPI int elua_shutdown(void); * @ingroup Elua */ EAPI Elua_State *elua_state_new(const char *progname); + +/** + * @brief Retrieve an Elua state from a Lua state. + * + * This doesn't create a new Elua state. Instead it just retrieves an existing + * Elua state given a Lua state. If no Elua state could be found (for example + * when the Lua state was created independently of Elua), this function returns + * NULL. + * + * @param[in] L The Lua state. + * @return An Elua state or NULL. + * + * @ingroup Elua + */ EAPI Elua_State *elua_state_from_lua_state_get(lua_State *L); EAPI void elua_state_free(Elua_State *es); From 002dc5a6b97a742bce0035c9ed33919df06c2779 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 14:49:10 +0100 Subject: [PATCH 63/91] elua lib: add state_free doc --- src/lib/elua/Elua.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index e764750c1c..84d243baa9 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -166,6 +166,16 @@ EAPI Elua_State *elua_state_new(const char *progname); */ EAPI Elua_State *elua_state_from_lua_state_get(lua_State *L); +/** + * @brief Destroy an Elua state. + * + * Given an Elua state, this destroys its internal Lua state as well as all + * other data its holding and then frees the Elua state itself. + * + * @param[in] es The Elua state. + * + * @ingroup Elua + */ EAPI void elua_state_free(Elua_State *es); EAPI void elua_state_dirs_set(Elua_State *es, const char *core, From fcf865f7ae26460aa624a2be12595aee263d55f0 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 14:57:43 +0100 Subject: [PATCH 64/91] elua lib: add docs for elua_state_dirs_set --- src/lib/elua/Elua.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 84d243baa9..b824b75e59 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -178,6 +178,26 @@ EAPI Elua_State *elua_state_from_lua_state_get(lua_State *L); */ EAPI void elua_state_free(Elua_State *es); +/** + * @brief Set the Elua directory paths. + * + * Every Elua state needs three paths - the core script path, the module + * path and the apps path. The core path refers to from where core scripts + * will be loaded (such as the module system), the module path refers to from + * where extra modules will be loaded and the apps path refers to from where + * Elua applications will be loaded (this is not a module path). + * + * @param[in] es The Elua state. + * @param[in] core The core path. + * @param[in] mods The modules path. + * @param[in] apps The apps path. + * + * @see elua_state_core_dir_get + * @see elua_state_mod_dir_get + * @see elua_state_apps_dir_get + * + * @ingroup Elua + */ EAPI void elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, const char *apps); EAPI void elua_state_dirs_fill(Elua_State *es, Eina_Bool ignore_env); From eab576db611e3f067f5c4e4659b61eefb7a52ced Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 15:03:53 +0100 Subject: [PATCH 65/91] elua lib: add doc for elua_state_dirs_fill --- src/lib/elua/Elua.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index b824b75e59..7c478f8516 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -187,6 +187,10 @@ EAPI void elua_state_free(Elua_State *es); * where extra modules will be loaded and the apps path refers to from where * Elua applications will be loaded (this is not a module path). * + * If you provide NULL for any path, it will not be set. This allows you to + * split the setting into multiple calls. By the time of state use all need + * to be set. + * * @param[in] es The Elua state. * @param[in] core The core path. * @param[in] mods The modules path. @@ -200,6 +204,23 @@ EAPI void elua_state_free(Elua_State *es); */ EAPI void elua_state_dirs_set(Elua_State *es, const char *core, const char *mods, const char *apps); + +/** + * @brief Fill the currently unset Elua dirs. + * + * This checks if any of the three main paths are unset and tries to fill + * them from the environment. It first tries environment variables to fill + * them (ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR) unless the ignore_env + * param is EINA_TRUE. If it is (or if the environment vars weren't set right) + * it uses eina prefix of the library to determine the paths. In that case + * they will expand to DATADIR/core, DATADIR/modules and DATADIR/apps, where + * DATADIR is typically something like /usr/share/elua. + * + * @param[in] es The Elua state. + * @param[in] ignore_env If set to EINA_TRUE, this ignores the env vars. + * + * @ingroup Elua + */ EAPI void elua_state_dirs_fill(Elua_State *es, Eina_Bool ignore_env); EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); From 4d6f254885f53ed545f7cf9d0cd6b19e4d5bb234 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 15:06:01 +0100 Subject: [PATCH 66/91] elua lib: add docs for the dir retrieval APIs. --- src/lib/elua/Elua.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 7c478f8516..7f27ba8df8 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -223,8 +223,31 @@ EAPI void elua_state_dirs_set(Elua_State *es, const char *core, */ EAPI void elua_state_dirs_fill(Elua_State *es, Eina_Bool ignore_env); +/** + * @brief Retrieve the Elua core dir. + * + * @param[in] es The Elua state. + * + * @ingroup Elua + */ EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); + +/** + * @brief Retrieve the Elua module dir. + * + * @param[in] es The Elua state. + * + * @ingroup Elua + */ EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); + +/** + * @brief Retrieve the Elua apps dir. + * + * @param[in] es The Elua state. + * + * @ingroup Elua + */ EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); EAPI Eina_Stringshare *elua_state_prog_name_get(const Elua_State *es); From a54fa7abb9c0450bd2d6567f139cfd006f50e1b4 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:08:36 +0100 Subject: [PATCH 67/91] elua lib: add docs for elua_state_prog_name_get --- src/lib/elua/Elua.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 7f27ba8df8..c2a9d12bb6 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -227,6 +227,7 @@ EAPI void elua_state_dirs_fill(Elua_State *es, Eina_Bool ignore_env); * @brief Retrieve the Elua core dir. * * @param[in] es The Elua state. + * @return The path. * * @ingroup Elua */ @@ -236,6 +237,7 @@ EAPI Eina_Stringshare *elua_state_core_dir_get(const Elua_State *es); * @brief Retrieve the Elua module dir. * * @param[in] es The Elua state. + * @return The path. * * @ingroup Elua */ @@ -245,11 +247,20 @@ EAPI Eina_Stringshare *elua_state_mod_dir_get(const Elua_State *es); * @brief Retrieve the Elua apps dir. * * @param[in] es The Elua state. + * @return The path. * * @ingroup Elua */ EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); +/** + * @brief Retrieve the prog name set on state creation. + * + * @param[in] es The Elua state. + * @return The name. + * + * @ingroup Elua + */ EAPI Eina_Stringshare *elua_state_prog_name_get(const Elua_State *es); EAPI void elua_state_include_path_add(Elua_State *es, const char *path); From 6a8c807f81ca454a8c185a426e10d746e9cdaa9c Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:09:37 +0100 Subject: [PATCH 68/91] elua lib: docs for elua_state_include_path_add --- src/lib/elua/Elua.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index c2a9d12bb6..32c7af184d 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -263,6 +263,13 @@ EAPI Eina_Stringshare *elua_state_apps_dir_get(const Elua_State *es); */ EAPI Eina_Stringshare *elua_state_prog_name_get(const Elua_State *es); +/** + * @brief Add another path to look up modules in to the state. + * + * @param[in] es The Elua state. + * + * @ingroup Elua + */ EAPI void elua_state_include_path_add(Elua_State *es, const char *path); EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es); From 9d46c234030dce854f8e762ff15e47be6dd96e87 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:10:31 +0100 Subject: [PATCH 69/91] elua lib: docs for elua_state_require_ref_push --- src/lib/elua/Elua.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 32c7af184d..3f9a114bef 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -272,6 +272,14 @@ EAPI Eina_Stringshare *elua_state_prog_name_get(const Elua_State *es); */ EAPI void elua_state_include_path_add(Elua_State *es, const char *path); +/** + * @brief Push the Elua "require" function onto the Lua stack. + * + * @param[in] es The Elua state. + * @return EINA_TRUE if the push was successful, EINA_FALSE otherwise. + * + * @ingroup Elua + */ EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es); EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); From a27fe217df1e5ce2d7cf62b316ca2cf3367aa4b4 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:10:49 +0100 Subject: [PATCH 70/91] elua lib: docs for elua_state_appload_ref_push --- src/lib/elua/Elua.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 3f9a114bef..ffc318416c 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -281,6 +281,15 @@ EAPI void elua_state_include_path_add(Elua_State *es, const char *path); * @ingroup Elua */ EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es); + +/** + * @brief Push the Elua app loader function onto the Lua stack. + * + * @param[in] es The Elua state. + * @return EINA_TRUE if the push was successful, EINA_FALSE otherwise. + * + * @ingroup Elua + */ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); From 8bd25cc966e2f2ab179d562c1f7845b744e475fd Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:12:22 +0100 Subject: [PATCH 71/91] elua lib: docs for elua_state_lua_state_get --- src/lib/elua/Elua.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index ffc318416c..4d3e1c9255 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -292,6 +292,18 @@ EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es); */ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); +/** + * @brief Retrieve the Lua state from an Elua state. + * + * This function retrieves the Lua state from a valid Elua state. As an + * Elua state is always initialized, this will return a valid state, unless + * the given Elua state is NULL, in which case it will also return NULL. + * + * @param[in] es The Elua state. + * @return The Lua state or NULL. + * + * @ingroup Elua + */ EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); From 373ad4e9f60cf11b4bcfb2fb3a33b54954a813cc Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:33:38 +0100 Subject: [PATCH 72/91] elua lib: docs for elua_state_i18n_setup --- src/lib/elua/Elua.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 4d3e1c9255..23ec100b1a 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -306,6 +306,19 @@ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); */ EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); +/** + * @brief Set up internationalization support for an Elua state. + * + * This function sets up correct i18n for an Elua state. That means loading + * the gettext bindings and making Lua aware of them. This also works when + * i18n support is disabled at compilation time, so you can just call it + * unconditionally. + * + * @param[in] es The Elua state. + * @return EINA_TRUE on success, EINA_FALSE on failure. + * + * @ingroup Elua + */ EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); From 123955994b05bb3bef3c32e299b95f51107b7b34 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:34:35 +0100 Subject: [PATCH 73/91] elua lib: docs for elua_state_modules_setup --- src/lib/elua/Elua.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 23ec100b1a..6feb2dd79e 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -320,6 +320,18 @@ EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); * @ingroup Elua */ EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); + +/** + * @brief Set up module support for an Elua state. + * + * This loads the Elua module system and makes Lua aware of it. It also + * registers the Elua C utility library module. + * + * @param[in] es The Elua state. + * @return EINA_TRUE on success, EINA_FALSE on failure. + * + * @ingroup Elua + */ EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); From 098b135799ce3516258024f62e6f5b3bd2ff7d18 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:35:55 +0100 Subject: [PATCH 74/91] elua lib: docs for elua_state_io_setup --- src/lib/elua/Elua.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 6feb2dd79e..83441ea7bd 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -333,6 +333,18 @@ EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); * @ingroup Elua */ EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); + +/** + * @brief Set up IO support for an Elua state. + * + * Elua provides its own loadfile based around mmap to replace the less + * efficient Lua version. This function takes care of the setup. + * + * @param[in] es The Elua state. + * @return EINA_TRUE on success, EINA_FALSE on failure. + * + * @ingroup Elua + */ EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); From 2a8eb85dff62684b80fdea6dc5d609b9723b8e49 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:38:52 +0100 Subject: [PATCH 75/91] elua lib: docs for elua_io_loadfile --- src/lib/elua/Elua.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 83441ea7bd..0a165678df 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -347,6 +347,18 @@ EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); */ EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); +/** + * @brief Loads a file using Elua's own mmap-based IO. + * + * This function behaves identically to luaL_loadfile when it comes to + * semantics. The loaded file remains on the Lua stack. + * + * @param[in] es The Elua state. + * @param[in] fname The file name. + * @return 0 for no errors, a non-zero value for errors (-1 for NULL es). + * + * @ingroup Elua + */ EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); EAPI int elua_util_require(Elua_State *es, const char *libname); From 1bc450402f9125e2138eb5d29ce014510b910494 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:49:53 +0100 Subject: [PATCH 76/91] elua lib: docs for elua_util_require --- src/lib/elua/Elua.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 0a165678df..c4c965fd79 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -361,6 +361,19 @@ EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); */ EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); +/** + * @brief Requires a module. + * + * Requires a Lua module. Leaves the Lua stack clean. Returns 0 on success + * or non-zero value on failure (see lua_pcall). + * + * @param[in] es The Elua state. + * @param[in] libname The library name. + * @return 0 for no errors, a non-zero value for errors (-1 for NULL es or + * NULL require). + * + * @ingroup Elua + */ EAPI int elua_util_require(Elua_State *es, const char *libname); EAPI int elua_util_file_run(Elua_State *es, const char *fname); EAPI int elua_util_string_run(Elua_State *es, const char *chunk, From f9520f7974424eaa3c5a33d7c81f555cfc82fca1 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:51:25 +0100 Subject: [PATCH 77/91] elua lib: docs for elua_util_file_run --- src/lib/elua/Elua.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index c4c965fd79..a1ba8a7dac 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -375,6 +375,19 @@ EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); * @ingroup Elua */ EAPI int elua_util_require(Elua_State *es, const char *libname); + +/** + * @brief Runs a file. + * + * Runs a file. Uses the Elua mmapped file IO to load the file. Returns zero + * on success or non-zero value on failure. + * + * @param[in] es The Elua state. + * @param[in] fname The file name. + * @return 0 for no errors, a non-zero value for errors (-1 for NULL es). + * + * @ingroup Elua + */ EAPI int elua_util_file_run(Elua_State *es, const char *fname); EAPI int elua_util_string_run(Elua_State *es, const char *chunk, const char *chname); From ca056e51ef499dc386e28d99bcb8f0acd998dc49 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:52:21 +0100 Subject: [PATCH 78/91] elua lib: docs for elua_util_string_run --- src/lib/elua/Elua.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index a1ba8a7dac..5b9981969b 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -389,6 +389,19 @@ EAPI int elua_util_require(Elua_State *es, const char *libname); * @ingroup Elua */ EAPI int elua_util_file_run(Elua_State *es, const char *fname); + +/** + * @brief Runs a string. + * + * Runs a string. Returns zero on success or non-zero value on failure. + * + * @param[in] es The Elua state. + * @param[in] chunk The string to run. + * @param[in] chname The chunk name to use for traceback/debug. + * @return 0 for no errors, a non-zero value for errors (-1 for NULL es). + * + * @ingroup Elua + */ EAPI int elua_util_string_run(Elua_State *es, const char *chunk, const char *chname); EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); From a5486874d5a54032f1e46afff03c5cf0c11062fb Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 16:53:34 +0100 Subject: [PATCH 79/91] elua lib: docs for elua_util_app_load --- src/lib/elua/Elua.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 5b9981969b..58cb104a70 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -404,6 +404,19 @@ EAPI int elua_util_file_run(Elua_State *es, const char *fname); */ EAPI int elua_util_string_run(Elua_State *es, const char *chunk, const char *chname); + +/** + * @brief Loads an application. + * + * This loads an app, respecting the app path set on state initialization. + * Leaves the Lua stack clean. Actually runs the app. + * + * @param[in] es The Elua state. + * @param[in] appname The application name. + * @return EINA_TRUE on success, EINA_FALSE on failure. + * + * @ingroup Elua + */ EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit); From a02bb195d9aabc9726dcac1345f8404d60aefaec Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 17:00:16 +0100 Subject: [PATCH 80/91] elua lib: docs for elua_util_script_run --- src/lib/elua/Elua.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 58cb104a70..08bfc0c3aa 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -418,6 +418,34 @@ EAPI int elua_util_string_run(Elua_State *es, const char *chunk, * @ingroup Elua */ EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); + +/** + * @brief Runs a script. + * + * This is a more complicated function that runs a script. It's a combination + * of the previously mentioned util functions. It takes argc and argv, which + * are typically given to the program, and an index of the first positional + * arg in argv (i.e. not options). The value on this index is then used as + * the potential name. + * + * If this name is either a dash or empty, the script is loaded from stdin. + * If it's a value and a file with this name exists, the script is loaded from + * the file. Otherwise, the name is treated to be an application name, and + * is loaded from the application path. + * + * If all succeeds, this is then run, and a quit value is written into the + * quit arg; if it's true (1), it means the app wants to exit immediately. + * If it's false (0), it means the app likely wants to execute a main loop. + * + * @param[in] es The Elua state. + * @param[in] argc The argument count. + * @param[in] argv The arguments. + * @param[in] n The index of the first positional argument. + * @param[out] quit Whether to quit or run a main loop. + * @return 0 on success, non-zero value on failure (-1 for NULL inputs). + * + * @ingroup Elua + */ EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit); From 660a4d7e1c752164aadafcdadb740745f2b123f7 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 20 Apr 2015 17:04:33 +0100 Subject: [PATCH 81/91] elua lib: docs for elua_util_error_report + remove unnecessary arg --- src/lib/elua/Elua.h | 17 +++++++++++++++-- src/lib/elua/elua.c | 25 +++++++++++-------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 08bfc0c3aa..3a172515c5 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -449,8 +449,21 @@ EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit); -EAPI int elua_util_error_report(const Elua_State *es, const char *pname, - int status); +/** + * @brief Reports an error using Eina logging. + * + * If the given status is 0, this function just returns it. Otherwise, it takes + * the topmost item on the Lua stack, converts it to string (if it cannot be + * converted, a "(non-string error)" placeholder is used) and logs it out + * as an error, together with the program name set on Elua state init. + * + * @param[in] es The Elua state. + * @param[in] status The status code. + * @return The status code. + * + * @ingroup Elua + */ +EAPI int elua_util_error_report(const Elua_State *es, int status); #endif diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 488438ab0d..234674dfd9 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -278,7 +278,7 @@ elua_state_i18n_setup(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/gettext.lua", es->coredir); - if (elua_util_error_report(es, es->progname, elua_io_loadfile(es, buf))) + 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); @@ -311,7 +311,7 @@ elua_state_modules_setup(const Elua_State *es) EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/module.lua", es->coredir); - if (elua_util_error_report(es, es->progname, elua_io_loadfile(es, buf))) + if (elua_util_error_report(es, elua_io_loadfile(es, buf))) return EINA_FALSE; lua_pushcfunction(es->luastate, _elua_module_system_init); lua_createtable(es->luastate, 0, 0); @@ -437,8 +437,7 @@ elua_util_require(Elua_State *es, const char *libname) EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); lua_pushstring(es->luastate, libname); - return elua_util_error_report(es, es->progname, - lua_pcall(es->luastate, 1, 0, 0)); + return elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0)); } EAPI int @@ -446,9 +445,8 @@ elua_util_file_run(Elua_State *es, const char *fname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_util_error_report(es, es->progname, - elua_io_loadfile(es, fname) - || _elua_docall(es, 0, 1)); + return elua_util_error_report(es, elua_io_loadfile(es, fname) + || _elua_docall(es, 0, 1)); } EAPI int @@ -456,10 +454,9 @@ elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_util_error_report(es, es->progname, - luaL_loadbuffer(es->luastate, chunk, strlen(chunk), - chname) - || _elua_docall(es, 0, 0)); + return elua_util_error_report(es, luaL_loadbuffer(es->luastate, chunk, + strlen(chunk), chname) + || _elua_docall(es, 0, 0)); } EAPI Eina_Bool @@ -515,7 +512,7 @@ elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) *quit = lua_toboolean(es->luastate, -1); lua_pop(es->luastate, 1); } - return elua_util_error_report(es, es->progname, status); + return elua_util_error_report(es, status); } static void @@ -525,13 +522,13 @@ _elua_errmsg(const char *pname, const char *msg) } EAPI int -elua_util_error_report(const Elua_State *es, const char *pname, int status) +elua_util_error_report(const Elua_State *es, int status) { EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); if (status && !lua_isnil(es->luastate, -1)) { const char *msg = lua_tostring(es->luastate, -1); - _elua_errmsg(pname, msg ? msg : "(non-string error)"); + _elua_errmsg(es->progname, msg ? msg : "(non-string error)"); lua_pop(es->luastate, 1); } return status; From 5cd8e4e67bba83ef7cb8589910c47aba7de8e008 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 23 Apr 2015 15:30:12 +0100 Subject: [PATCH 82/91] elua: remove long opts (not necessary or useful) --- src/bin/elua/main.c | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index ca7ccc1ab2..71f8033acb 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -39,30 +39,16 @@ elua_print_help(const char *pname, FILE *stream) "A main entry for all EFL/LuaJIT powered applications.\n\n" "The following options are supported:\n\n" "" - " -h, --help Show this message.\n" - " -l, --license Show a license message.\n" - " -C[COREDIR], --core-dir=[COREDIR] Elua core directory path.\n" - " -M[MODDIR], --modules-dir=[MODDIR] Elua modules directory path.\n" - " -A[APPDIR], --apps-dir=[APPDIR] Elua applications directory path.\n" - " -l[LIBRARY], --library=[LIBRARY] Require library 'library'.\n" - " -I[DIR], --lib-dir=[DIR] Append an additional require path.\n" - " -E, --noenv Ignore environment variables.\n", pname); + " -h Show this message.\n" + " -l Show a license message.\n" + " -C[COREDIR] Elua core directory path.\n" + " -M[MODDIR] Elua modules directory path.\n" + " -A[APPDIR] Elua applications directory path.\n" + " -l[LIBRARY] Require library 'library'.\n" + " -I[DIR], Append an additional require path.\n" + " -E, Ignore environment variables.\n", pname); } -static struct option lopt[] = -{ - { "help" , no_argument , NULL, 'h' }, - - { "core-dir" , required_argument, NULL, 'C' }, - { "modules-dir", required_argument, NULL, 'M' }, - { "apps-dir" , required_argument, NULL, 'A' }, - - { "library" , required_argument, NULL, 'l' }, - { "lib-dir" , required_argument, NULL, 'I' }, - { "noenv" , no_argument , NULL, 'E' }, - { NULL , 0 , NULL, 0 } -}; - /* protected main */ static int elua_main(lua_State *L) @@ -80,7 +66,7 @@ elua_main(lua_State *L) int ch; - while ((ch = getopt_long(argc, argv, "+LhC:M:A:l:I:E", lopt, NULL)) != -1) + while ((ch = getopt(argc, argv, "+LhC:M:A:l:I:E")) != -1) switch (ch) { case 'h': From 0d8b38a2acd8fc52a87826f30f56d025794ecf21 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 23 Apr 2015 15:39:37 +0100 Subject: [PATCH 83/91] elua: include fix --- src/bin/elua/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 71f8033acb..c24951c64c 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -17,7 +17,7 @@ #include #include -#include "Elua.h" +#include static int _el_log_domain = -1; From 88faba813c7c0369f4e5c1fd521d41f439c8cfe0 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 23 Apr 2015 16:02:58 +0100 Subject: [PATCH 84/91] elua lib: merge the 3 setup funcs into one This allows simpler initialization and elua_util_require can now queue up modules before full initialization is done. --- src/bin/elua/main.c | 33 ++++++++++------------- src/lib/elua/Elua.h | 34 +++++++----------------- src/lib/elua/cache.c | 4 +-- src/lib/elua/elua.c | 53 +++++++++++++++++++++++++++++++++---- src/lib/elua/elua_private.h | 2 ++ 5 files changed, 75 insertions(+), 51 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index c24951c64c..6b55120067 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -54,9 +54,7 @@ static int elua_main(lua_State *L) { Eina_Bool noenv = EINA_FALSE; - Eina_List *largs = NULL; const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; - char *data = NULL; struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1); Elua_State *es = m->es; @@ -70,7 +68,7 @@ elua_main(lua_State *L) switch (ch) { case 'h': - elua_print_help(elua_state_prog_name_get(es), stdout); goto success; + elua_print_help(elua_state_prog_name_get(es), stdout); return 0; case 'C': coredir = optarg; break; case 'M': @@ -81,7 +79,7 @@ elua_main(lua_State *L) case 'I': if (!optarg[0]) continue; if (ch == 'l') - largs = eina_list_append(largs, optarg); + elua_util_require(es, optarg); else elua_state_include_path_add(es, optarg); break; @@ -96,39 +94,36 @@ elua_main(lua_State *L) elua_state_dirs_set(es, coredir, moddir, appsdir); elua_state_dirs_fill(es, noenv); - if (!elua_state_modules_setup(es) || !elua_state_i18n_setup(es) - || !elua_state_io_setup(es)) - goto error; + if (!elua_state_setup(es)) + { + m->status = 1; + return 0; + } lua_gc(L, LUA_GCRESTART, 0); INF("elua lua state initialized"); - EINA_LIST_FREE(largs, data) - if (elua_util_require(es, data)) goto error; - if (optind < argc) { int quit = 0; if (elua_util_script_run(es, argc, argv, optind, &quit)) - goto error; + { + m->status = 1; + return 0; + } if (quit) - goto success; + return 0; } else { ERR("nothing to run"); - goto error; + m->status = 1; + return 0; } ecore_main_loop_begin(); - goto success; - -error: - m->status = 1; -success: - if (largs) eina_list_free(largs); return 0; } diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 3a172515c5..93814a359d 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -307,45 +307,29 @@ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es); EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); /** - * @brief Set up internationalization support for an Elua state. + * @brief Set up the Elua state. + * + * This API function sets up 3 things, module system, i18n and I/O. After that + * it requires all modules not yet required (i.e. those queued in before the + * state was fully initialized). * * This function sets up correct i18n for an Elua state. That means loading * the gettext bindings and making Lua aware of them. This also works when * i18n support is disabled at compilation time, so you can just call it * unconditionally. * - * @param[in] es The Elua state. - * @return EINA_TRUE on success, EINA_FALSE on failure. - * - * @ingroup Elua - */ -EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es); - -/** - * @brief Set up module support for an Elua state. - * - * This loads the Elua module system and makes Lua aware of it. It also + * This also loads the Elua module system and makes Lua aware of it. It also * registers the Elua C utility library module. * - * @param[in] es The Elua state. - * @return EINA_TRUE on success, EINA_FALSE on failure. - * - * @ingroup Elua - */ -EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es); - -/** - * @brief Set up IO support for an Elua state. - * - * Elua provides its own loadfile based around mmap to replace the less - * efficient Lua version. This function takes care of the setup. + * Finally, Elua provides its own loadfile based around mmap to replace the + * less efficient Lua version. This function takes care of the setup. * * @param[in] es The Elua state. * @return EINA_TRUE on success, EINA_FALSE on failure. * * @ingroup Elua */ -EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); +EAPI Eina_Bool elua_state_setup(Elua_State *es); /** * @brief Loads a file using Elua's own mmap-based IO. diff --git a/src/lib/elua/cache.c b/src/lib/elua/cache.c index 43ef3d7d19..b856b0bde9 100644 --- a/src/lib/elua/cache.c +++ b/src/lib/elua/cache.c @@ -208,8 +208,8 @@ loadfile(lua_State *L) return 2; } -EAPI Eina_Bool -elua_state_io_setup(const Elua_State *es) +Eina_Bool +_elua_state_io_setup(const Elua_State *es) { EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, EINA_FALSE); lua_pushcfunction(es->luastate, loadfile); diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 234674dfd9..9ad15d17f0 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -94,6 +94,8 @@ elua_state_free(Elua_State *es) } else if (es->cmods) eina_list_free(es->cmods); + EINA_LIST_FREE(es->lmods, data) + eina_stringshare_del(data); EINA_LIST_FREE(es->lincs, data) eina_stringshare_del(data); eina_stringshare_del(es->progname); @@ -264,8 +266,8 @@ const luaL_reg gettextlib[] = { NULL, NULL } }; -EAPI Eina_Bool -elua_state_i18n_setup(const Elua_State *es) +static Eina_Bool +_elua_state_i18n_setup(const Elua_State *es) { #ifdef ENABLE_NLS char *(*dgettextp)(const char*, const char*) = dgettext; @@ -302,8 +304,8 @@ const luaL_reg _elua_cutillib[] = { NULL , NULL } }; -EAPI Eina_Bool -elua_state_modules_setup(const Elua_State *es) +static Eina_Bool +_elua_state_modules_setup(const Elua_State *es) { char buf[PATH_MAX]; EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); @@ -370,6 +372,42 @@ _elua_module_system_init(lua_State *L) return 2; } +EAPI Eina_Bool +elua_state_setup(Elua_State *es) +{ + Eina_Stringshare *data; + Eina_Bool failed = EINA_FALSE; + + if (!_elua_state_modules_setup(es)) + return EINA_FALSE; + if (!_elua_state_i18n_setup(es)) + return EINA_FALSE; + if (!_elua_state_io_setup(es)) + return EINA_FALSE; + + /* finally require the necessary modules */ + EINA_LIST_FREE(es->lmods, data) + { + if (!failed) + { + if (!elua_state_require_ref_push(es)) + { + failed = EINA_TRUE; + break; + } + lua_pushstring(es->luastate, data); + if (elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0))) + { + failed = EINA_TRUE; + break; + } + } + eina_stringshare_del(data); + } + + return EINA_TRUE; +} + /* Utility functions - these could be written using the other APIs */ static int @@ -435,7 +473,12 @@ elua_util_require(Elua_State *es, const char *libname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); + if (!elua_state_require_ref_push(es)) + { + /* store stuff until things are correctly set up */ + es->lmods = eina_list_append(es->lmods, eina_stringshare_add(libname)); + return 0; + } lua_pushstring(es->luastate, libname); return elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0)); } diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index cf24e1e661..53b514e627 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -34,6 +34,7 @@ struct _Elua_State Eina_Stringshare *coredir; Eina_Stringshare *moddir; Eina_Stringshare *appsdir; + Eina_List *lmods; Eina_List *cmods; Eina_List *lincs; int requireref, apploadref; @@ -48,5 +49,6 @@ extern int _elua_log_dom; #define CRT(...) EINA_LOG_DOM_CRITICAL(_elua_log_dom, __VA_ARGS__) int _elua_io_popen(lua_State *L); +Eina_Bool _elua_state_io_setup(const Elua_State *es); #endif From e9aadea402f2f533e6d9137171f5bff4eeb4bafc Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 23 Apr 2015 16:13:27 +0100 Subject: [PATCH 85/91] elua lib: fix tests --- src/tests/elua/elua_lib.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 0d670e56d8..445a89d9d3 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -56,9 +56,7 @@ START_TEST(elua_api) fail_if(strcmp(elua_state_prog_name_get(st), "test")); - fail_if(!elua_state_modules_setup(st)); - fail_if(!elua_state_i18n_setup(st)); - fail_if(!elua_state_io_setup(st)); + fail_if(!elua_state_setup(st)); fail_if(!elua_state_require_ref_push(st)); fail_if(lua_type(lst, -1) != LUA_TFUNCTION); @@ -96,9 +94,9 @@ START_TEST(elua_api) fail_if(remove(buf)); /* halfassed testing here, but not possible otherwise */ - fail_if(elua_util_error_report(st, "foo", 0)); + fail_if(elua_util_error_report(st, 0)); lua_pushliteral(lst, "msg"); - fail_if(!elua_util_error_report(st, "foo", 5)); + fail_if(!elua_util_error_report(st, 5)); fail_if(lua_gettop(lst) > 0); fail_if(elua_util_script_run(st, 2, cargv, 1, &quit)); From e73f7f7a40ba3832d743ae4b893940440a38e3d4 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 24 Apr 2015 16:01:24 +0100 Subject: [PATCH 86/91] elua lib: use Eina_Bool as return val in some utils --- src/bin/elua/main.c | 2 +- src/lib/elua/Elua.h | 31 ++++++++++++++----------------- src/lib/elua/elua.c | 34 +++++++++++++++++----------------- src/tests/elua/elua_lib.c | 12 ++++++------ 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/bin/elua/main.c b/src/bin/elua/main.c index 6b55120067..ed1b4074a4 100644 --- a/src/bin/elua/main.c +++ b/src/bin/elua/main.c @@ -107,7 +107,7 @@ elua_main(lua_State *L) if (optind < argc) { int quit = 0; - if (elua_util_script_run(es, argc, argv, optind, &quit)) + if (!elua_util_script_run(es, argc, argv, optind, &quit)) { m->status = 1; return 0; diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 93814a359d..623179fecb 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -348,46 +348,43 @@ EAPI int elua_io_loadfile(const Elua_State *es, const char *fname); /** * @brief Requires a module. * - * Requires a Lua module. Leaves the Lua stack clean. Returns 0 on success - * or non-zero value on failure (see lua_pcall). + * Requires a Lua module. Leaves the Lua stack clean. * * @param[in] es The Elua state. * @param[in] libname The library name. - * @return 0 for no errors, a non-zero value for errors (-1 for NULL es or - * NULL require). + * @return EINA_TRUE on success, EINA_FALSE on failure. * * @ingroup Elua */ -EAPI int elua_util_require(Elua_State *es, const char *libname); +EAPI Eina_Bool elua_util_require(Elua_State *es, const char *libname); /** * @brief Runs a file. * - * Runs a file. Uses the Elua mmapped file IO to load the file. Returns zero - * on success or non-zero value on failure. + * Runs a file. Uses the Elua mmapped file IO to load the file. * * @param[in] es The Elua state. * @param[in] fname The file name. - * @return 0 for no errors, a non-zero value for errors (-1 for NULL es). + * @return EINA_TRUE on success, EINA_FALSE on failure. * * @ingroup Elua */ -EAPI int elua_util_file_run(Elua_State *es, const char *fname); +EAPI Eina_Bool elua_util_file_run(Elua_State *es, const char *fname); /** * @brief Runs a string. * - * Runs a string. Returns zero on success or non-zero value on failure. + * Runs a string. * * @param[in] es The Elua state. * @param[in] chunk The string to run. * @param[in] chname The chunk name to use for traceback/debug. - * @return 0 for no errors, a non-zero value for errors (-1 for NULL es). + * @return EINA_TRUE on success, EINA_FALSE on failure. * * @ingroup Elua */ -EAPI int elua_util_string_run(Elua_State *es, const char *chunk, - const char *chname); +EAPI Eina_Bool elua_util_string_run(Elua_State *es, const char *chunk, + const char *chname); /** * @brief Loads an application. @@ -424,14 +421,14 @@ EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); * @param[in] es The Elua state. * @param[in] argc The argument count. * @param[in] argv The arguments. - * @param[in] n The index of the first positional argument. + * @param[in] n The index of the first positional argt. * @param[out] quit Whether to quit or run a main loop. - * @return 0 on success, non-zero value on failure (-1 for NULL inputs). + * @return EINA_TRUE on success, EINA_FALSE on failure. * * @ingroup Elua */ -EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, - int *quit); +EAPI Eina_Bool elua_util_script_run(Elua_State *es, int argc, char **argv, + int n, int *quit); /** * @brief Reports an error using Eina logging. diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 9ad15d17f0..6cb2e13d71 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -468,11 +468,11 @@ _elua_getargs(Elua_State *es, int argc, char **argv, int n) return narg; } -EAPI int +EAPI Eina_Bool elua_util_require(Elua_State *es, const char *libname) { - EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); if (!elua_state_require_ref_push(es)) { /* store stuff until things are correctly set up */ @@ -480,26 +480,26 @@ elua_util_require(Elua_State *es, const char *libname) return 0; } lua_pushstring(es->luastate, libname); - return elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0)); + return !elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0)); } -EAPI int +EAPI Eina_Bool elua_util_file_run(Elua_State *es, const char *fname) { - EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_util_error_report(es, elua_io_loadfile(es, fname) - || _elua_docall(es, 0, 1)); + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + return !elua_util_error_report(es, elua_io_loadfile(es, fname) + || _elua_docall(es, 0, 1)); } -EAPI int +EAPI Eina_Bool elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) { - EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); - return elua_util_error_report(es, luaL_loadbuffer(es->luastate, chunk, - strlen(chunk), chname) - || _elua_docall(es, 0, 0)); + EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); + return !elua_util_error_report(es, luaL_loadbuffer(es->luastate, chunk, + strlen(chunk), chname) + || _elua_docall(es, 0, 0)); } EAPI Eina_Bool @@ -519,7 +519,7 @@ elua_util_app_load(Elua_State *es, const char *appname) return EINA_TRUE; } -EAPI int +EAPI Eina_Bool elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) { int status, narg; @@ -555,7 +555,7 @@ elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) *quit = lua_toboolean(es->luastate, -1); lua_pop(es->luastate, 1); } - return elua_util_error_report(es, status); + return !elua_util_error_report(es, status); } static void diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 445a89d9d3..7ee3f2b0a1 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -66,9 +66,9 @@ START_TEST(elua_api) fail_if(lua_type(lst, -1) != LUA_TFUNCTION); lua_pop(lst, 1); - fail_if(elua_util_require(st, "util")); - fail_if(elua_util_string_run(st, "return 1337", "foo")); - fail_if(!elua_util_string_run(st, "foo bar", "foo")); /* invalid code */ + fail_if(!elua_util_require(st, "util")); + fail_if(!elua_util_string_run(st, "return 1337", "foo")); + fail_if(elua_util_string_run(st, "foo bar", "foo")); /* invalid code */ fail_if(!elua_util_app_load(st, "lualian")); fail_if(lua_type(lst, -1) != LUA_TFUNCTION); lua_pop(lst, 1); @@ -88,7 +88,7 @@ START_TEST(elua_api) fail_if(!f); fprintf(f, "return 5\n"); fclose(f); - fail_if(elua_util_file_run(st, buf)); + fail_if(!elua_util_file_run(st, buf)); fail_if(lua_tointeger(lst, -1) != 5); lua_pop(lst, 1); fail_if(remove(buf)); @@ -99,7 +99,7 @@ START_TEST(elua_api) fail_if(!elua_util_error_report(st, 5)); fail_if(lua_gettop(lst) > 0); - fail_if(elua_util_script_run(st, 2, cargv, 1, &quit)); + fail_if(!elua_util_script_run(st, 2, cargv, 1, &quit)); fail_if(quit != 1); f = fopen(buf, "w"); @@ -107,7 +107,7 @@ START_TEST(elua_api) fprintf(f, "return false"); fclose(f); cargv[1] = buf; - fail_if(elua_util_script_run(st, 2, cargv, 1, &quit)); + fail_if(!elua_util_script_run(st, 2, cargv, 1, &quit)); fail_if(quit != 0); fail_if(remove(buf)); From db66fe62551c170da81b071c0055b3b3dd4159da Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 27 Apr 2015 11:30:33 +0100 Subject: [PATCH 87/91] elua lib: fix elua_util_app_load + docs --- src/lib/elua/Elua.h | 14 ++++++++++---- src/lib/elua/elua.c | 14 +++++++------- src/tests/elua/elua_lib.c | 4 ++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/lib/elua/Elua.h b/src/lib/elua/Elua.h index 623179fecb..3b087790b8 100644 --- a/src/lib/elua/Elua.h +++ b/src/lib/elua/Elua.h @@ -335,7 +335,10 @@ EAPI Eina_Bool elua_state_setup(Elua_State *es); * @brief Loads a file using Elua's own mmap-based IO. * * This function behaves identically to luaL_loadfile when it comes to - * semantics. The loaded file remains on the Lua stack. + * semantics. The loaded file remains on the Lua stack. If the input + * state is NULL, the return value is -1 and nothing is left on the stack. + * On any different error, the error object is left on the stack and this + * returns a value larger than zero (LUA_ERR*). On success, zero is returned. * * @param[in] es The Elua state. * @param[in] fname The file name. @@ -390,15 +393,18 @@ EAPI Eina_Bool elua_util_string_run(Elua_State *es, const char *chunk, * @brief Loads an application. * * This loads an app, respecting the app path set on state initialization. - * Leaves the Lua stack clean. Actually runs the app. + * Actually runs the app. If the input state is NULL, the return value is -1 + * nd nothing is left on the stack. On any different error, the error object + * is left on the stack and this returns 1. On success, zero is returned + * (and the return value from the app is left on the stack). * * @param[in] es The Elua state. * @param[in] appname The application name. - * @return EINA_TRUE on success, EINA_FALSE on failure. + * @return 0 for no errors, 1 on errors, -1 on null input. * * @ingroup Elua */ -EAPI Eina_Bool elua_util_app_load(Elua_State *es, const char *appname); +EAPI int elua_util_app_load(Elua_State *es, const char *appname); /** * @brief Runs a script. diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 6cb2e13d71..76cafb1ea6 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -502,21 +502,21 @@ elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) || _elua_docall(es, 0, 0)); } -EAPI Eina_Bool +EAPI int elua_util_app_load(Elua_State *es, const char *appname) { - EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); - EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_appload_ref_push(es), EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); + EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_appload_ref_push(es), -1); lua_pushstring(es->luastate, appname); lua_call(es->luastate, 1, 2); if (lua_isnil(es->luastate, -2)) { lua_remove(es->luastate, -2); - return EINA_FALSE; + return 1; } lua_pop(es->luastate, 1); - return EINA_TRUE; + return 0; } EAPI Eina_Bool @@ -541,7 +541,7 @@ elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) status = elua_io_loadfile(es, fname); } else - status = !elua_util_app_load(es, fname); + status = elua_util_app_load(es, fname); } else status = elua_io_loadfile(es, fname); diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index 7ee3f2b0a1..82857270db 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -69,10 +69,10 @@ START_TEST(elua_api) fail_if(!elua_util_require(st, "util")); fail_if(!elua_util_string_run(st, "return 1337", "foo")); fail_if(elua_util_string_run(st, "foo bar", "foo")); /* invalid code */ - fail_if(!elua_util_app_load(st, "lualian")); + fail_if(elua_util_app_load(st, "lualian")); fail_if(lua_type(lst, -1) != LUA_TFUNCTION); lua_pop(lst, 1); - fail_if(elua_util_app_load(st, "non_existent_app")); + fail_if(!elua_util_app_load(st, "non_existent_app")); fail_if(lua_type(lst, -1) != LUA_TSTRING); lua_pop(lst, 1); fail_if(elua_io_loadfile(st, ELUA_CORE_DIR "/util.lua")); From 093c7aa596122177cda128236dd42df0650075f6 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 27 Apr 2015 11:34:29 +0100 Subject: [PATCH 88/91] elua lib: remove unnecessary checks (always initialized) --- src/lib/elua/elua.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 76cafb1ea6..be85d94d71 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -194,7 +194,6 @@ EAPI Eina_Bool elua_state_require_ref_push(Elua_State *es) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); EINA_SAFETY_ON_FALSE_RETURN_VAL(es->requireref != LUA_REFNIL, EINA_FALSE); lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, es->requireref); return EINA_TRUE; @@ -204,7 +203,6 @@ EAPI Eina_Bool elua_state_appload_ref_push(Elua_State *es) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); EINA_SAFETY_ON_FALSE_RETURN_VAL(es->apploadref != LUA_REFNIL, EINA_FALSE); lua_rawgeti(es->luastate, LUA_REGISTRYINDEX, es->apploadref); return EINA_TRUE; @@ -276,7 +274,6 @@ _elua_state_i18n_setup(const Elua_State *es) #endif char buf[PATH_MAX]; EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/gettext.lua", es->coredir); @@ -309,7 +306,6 @@ _elua_state_modules_setup(const Elua_State *es) { char buf[PATH_MAX]; EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->coredir, EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(es->progname, EINA_FALSE); snprintf(buf, sizeof(buf), "%s/module.lua", es->coredir); @@ -436,7 +432,6 @@ _elua_docall(Elua_State *es, int narg, int nret) { int status; EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); int bs = lua_gettop(es->luastate) - narg; lua_pushcfunction(es->luastate, _elua_traceback); lua_insert(es->luastate, bs); @@ -453,7 +448,6 @@ _elua_getargs(Elua_State *es, int argc, char **argv, int n) int i; int narg = argc - (n + 1); EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); luaL_checkstack(es->luastate, narg + 3, "too many arguments to script"); for (i = n + 1; i < argc; ++i) { @@ -472,7 +466,6 @@ EAPI Eina_Bool elua_util_require(Elua_State *es, const char *libname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); if (!elua_state_require_ref_push(es)) { /* store stuff until things are correctly set up */ @@ -487,7 +480,6 @@ EAPI Eina_Bool elua_util_file_run(Elua_State *es, const char *fname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); return !elua_util_error_report(es, elua_io_loadfile(es, fname) || _elua_docall(es, 0, 1)); } @@ -496,7 +488,6 @@ EAPI Eina_Bool elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); return !elua_util_error_report(es, luaL_loadbuffer(es->luastate, chunk, strlen(chunk), chname) || _elua_docall(es, 0, 0)); @@ -506,7 +497,6 @@ EAPI int elua_util_app_load(Elua_State *es, const char *appname) { EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_appload_ref_push(es), -1); lua_pushstring(es->luastate, appname); lua_call(es->luastate, 1, 2); @@ -526,7 +516,6 @@ elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) const char *fname; EINA_SAFETY_ON_FALSE_RETURN_VAL(n < argc, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); - EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); fname = argv[n]; narg = _elua_getargs(es, argc, argv, n); lua_setglobal(es->luastate, "arg"); @@ -567,7 +556,7 @@ _elua_errmsg(const char *pname, const char *msg) EAPI int elua_util_error_report(const Elua_State *es, int status) { - EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, status); + EINA_SAFETY_ON_FALSE_RETURN_VAL(es, status); if (status && !lua_isnil(es->luastate, -1)) { const char *msg = lua_tostring(es->luastate, -1); From 3a685d40c0280f930e959b32dedf1b3270e37a6e Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 30 Apr 2015 11:15:48 +0100 Subject: [PATCH 89/91] elua lib: add a func to retrieve the current translation lang --- src/lib/elua/elua.c | 20 ++++++++++++++++++++ src/scripts/elua/core/gettext.lua | 1 - 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index be85d94d71..3e1a715a98 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -258,9 +258,29 @@ _elua_gettext_bind_textdomain(lua_State *L) #endif } +static int +_elua_get_message_language(lua_State *L) +{ + const char *e; + e = getenv("LANGUAGE"); + if (e && e[0]) goto success; + e = getenv("LC_ALL"); + if (e && e[0]) goto success; + e = getenv("LC_MESSAGES"); + if (e && e[0]) goto success; + e = getenv("LANG"); + if (e && e[0]) goto success; + lua_pushnil(L); + return 1; +success: + lua_pushstring(L, e); + return 1; +}; + const luaL_reg gettextlib[] = { { "bind_textdomain", _elua_gettext_bind_textdomain }, + { "get_message_language", _elua_get_message_language }, { NULL, NULL } }; diff --git a/src/scripts/elua/core/gettext.lua b/src/scripts/elua/core/gettext.lua index a77d50abc8..96af7d3941 100644 --- a/src/scripts/elua/core/gettext.lua +++ b/src/scripts/elua/core/gettext.lua @@ -7,7 +7,6 @@ local M = {} local gettext = ... local bind_textdomain = gettext.bind_textdomain -local bind_textdomain_codeset = gettext.bind_textdomain_codeset local dgettext = gettext.dgettext local dngettext = gettext.dngettext From 490deb93288d8401bff4f442d67f4df6a054f7c9 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 30 Apr 2015 11:50:09 +0100 Subject: [PATCH 90/91] elua lib: add API to retrieve lconv from lua --- src/lib/elua/elua.c | 47 +++++++++++++++++++++++++++++++++++++ src/lib/elua/elua_private.h | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index 3e1a715a98..ed5a4e3786 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -277,10 +277,57 @@ success: return 1; }; +static int +_elua_get_localeconv(lua_State *L) +{ + struct lconv *lc = localeconv(); + lua_createtable(L, 0, 24); + +#define ELUA_LCF_S(name) \ + lua_pushstring(L, lc->name); \ + lua_setfield(L, -2, #name); + +#define ELUA_LCF_C(name) \ + lua_pushinteger(L, (int)lc->name); \ + lua_setfield(L, -2, #name); + + ELUA_LCF_S(decimal_point); + ELUA_LCF_S(thousands_sep); + ELUA_LCF_S(grouping); + ELUA_LCF_S(int_curr_symbol); + ELUA_LCF_S(currency_symbol); + ELUA_LCF_S(mon_decimal_point); + ELUA_LCF_S(mon_thousands_sep); + ELUA_LCF_S(mon_grouping); + ELUA_LCF_S(positive_sign); + ELUA_LCF_S(negative_sign); + + ELUA_LCF_C(frac_digits); + ELUA_LCF_C(p_cs_precedes); + ELUA_LCF_C(n_cs_precedes); + ELUA_LCF_C(p_sep_by_space); + ELUA_LCF_C(n_sep_by_space); + ELUA_LCF_C(p_sign_posn); + ELUA_LCF_C(n_sign_posn); + ELUA_LCF_C(int_frac_digits); + ELUA_LCF_C(int_p_cs_precedes); + ELUA_LCF_C(int_n_cs_precedes); + ELUA_LCF_C(int_p_sep_by_space); + ELUA_LCF_C(int_n_sep_by_space); + ELUA_LCF_C(int_p_sign_posn); + ELUA_LCF_C(int_n_sign_posn); + +#undef ELUA_LCF_S +#undef ELUA_LCF_C + + return 1; +}; + const luaL_reg gettextlib[] = { { "bind_textdomain", _elua_gettext_bind_textdomain }, { "get_message_language", _elua_get_message_language }, + { "get_localeconv", _elua_get_localeconv }, { NULL, NULL } }; diff --git a/src/lib/elua/elua_private.h b/src/lib/elua/elua_private.h index 53b514e627..01d8e4b1ac 100644 --- a/src/lib/elua/elua_private.h +++ b/src/lib/elua/elua_private.h @@ -5,8 +5,9 @@ # include #endif +#include + #ifdef ENABLE_NLS -# include # include # define _(x) dgettext(PACKAGE, x) #else From 7cc295ae9a539f2b0e98786d6a6ddb8176dcbd49 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 30 Apr 2015 13:35:11 +0100 Subject: [PATCH 91/91] elua lib: use -1 for default values in localeconv --- src/lib/elua/elua.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c index ed5a4e3786..3d34b1f9b1 100644 --- a/src/lib/elua/elua.c +++ b/src/lib/elua/elua.c @@ -288,7 +288,7 @@ _elua_get_localeconv(lua_State *L) lua_setfield(L, -2, #name); #define ELUA_LCF_C(name) \ - lua_pushinteger(L, (int)lc->name); \ + lua_pushinteger(L, (lc->name == CHAR_MAX) ? -1 : (int)lc->name); \ lua_setfield(L, -2, #name); ELUA_LCF_S(decimal_point);