summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@osg.samsung.com>2015-04-23 16:02:58 +0100
committerDaniel Kolesa <d.kolesa@osg.samsung.com>2015-05-06 15:05:23 +0100
commit88faba813c7c0369f4e5c1fd521d41f439c8cfe0 (patch)
treebcc02a18a15b7c1aea45b532023d93b2dfbe9e7b /src
parent0d8b38a2acd8fc52a87826f30f56d025794ecf21 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/bin/elua/main.c33
-rw-r--r--src/lib/elua/Elua.h34
-rw-r--r--src/lib/elua/cache.c4
-rw-r--r--src/lib/elua/elua.c53
-rw-r--r--src/lib/elua/elua_private.h2
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
54elua_main(lua_State *L) 54elua_main(lua_State *L)
55{ 55{
56 Eina_Bool noenv = EINA_FALSE; 56 Eina_Bool noenv = EINA_FALSE;
57 Eina_List *largs = NULL;
58 const char *coredir = NULL, *moddir = NULL, *appsdir = NULL; 57 const char *coredir = NULL, *moddir = NULL, *appsdir = NULL;
59 char *data = NULL;
60 58
61 struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1); 59 struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1);
62 Elua_State *es = m->es; 60 Elua_State *es = m->es;
@@ -70,7 +68,7 @@ elua_main(lua_State *L)
70 switch (ch) 68 switch (ch)
71 { 69 {
72 case 'h': 70 case 'h':
73 elua_print_help(elua_state_prog_name_get(es), stdout); goto success; 71 elua_print_help(elua_state_prog_name_get(es), stdout); return 0;
74 case 'C': 72 case 'C':
75 coredir = optarg; break; 73 coredir = optarg; break;
76 case 'M': 74 case 'M':
@@ -81,7 +79,7 @@ elua_main(lua_State *L)
81 case 'I': 79 case 'I':
82 if (!optarg[0]) continue; 80 if (!optarg[0]) continue;
83 if (ch == 'l') 81 if (ch == 'l')
84 largs = eina_list_append(largs, optarg); 82 elua_util_require(es, optarg);
85 else 83 else
86 elua_state_include_path_add(es, optarg); 84 elua_state_include_path_add(es, optarg);
87 break; 85 break;
@@ -96,39 +94,36 @@ elua_main(lua_State *L)
96 elua_state_dirs_set(es, coredir, moddir, appsdir); 94 elua_state_dirs_set(es, coredir, moddir, appsdir);
97 elua_state_dirs_fill(es, noenv); 95 elua_state_dirs_fill(es, noenv);
98 96
99 if (!elua_state_modules_setup(es) || !elua_state_i18n_setup(es) 97 if (!elua_state_setup(es))
100 || !elua_state_io_setup(es)) 98 {
101 goto error; 99 m->status = 1;
100 return 0;
101 }
102 102
103 lua_gc(L, LUA_GCRESTART, 0); 103 lua_gc(L, LUA_GCRESTART, 0);
104 104
105 INF("elua lua state initialized"); 105 INF("elua lua state initialized");
106 106
107 EINA_LIST_FREE(largs, data)
108 if (elua_util_require(es, data)) goto error;
109
110 if (optind < argc) 107 if (optind < argc)
111 { 108 {
112 int quit = 0; 109 int quit = 0;
113 if (elua_util_script_run(es, argc, argv, optind, &quit)) 110 if (elua_util_script_run(es, argc, argv, optind, &quit))
114 goto error; 111 {
112 m->status = 1;
113 return 0;
114 }
115 if (quit) 115 if (quit)
116 goto success; 116 return 0;
117 } 117 }
118 else 118 else
119 { 119 {
120 ERR("nothing to run"); 120 ERR("nothing to run");
121 goto error; 121 m->status = 1;
122 return 0;
122 } 123 }
123 124
124 ecore_main_loop_begin(); 125 ecore_main_loop_begin();
125 126
126 goto success;
127
128error:
129 m->status = 1;
130success:
131 if (largs) eina_list_free(largs);
132 return 0; 127 return 0;
133} 128}
134 129
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);
307EAPI lua_State *elua_state_lua_state_get(const Elua_State *es); 307EAPI lua_State *elua_state_lua_state_get(const Elua_State *es);
308 308
309/** 309/**
310 * @brief Set up internationalization support for an Elua state. 310 * @brief Set up the Elua state.
311 *
312 * This API function sets up 3 things, module system, i18n and I/O. After that
313 * it requires all modules not yet required (i.e. those queued in before the
314 * state was fully initialized).
311 * 315 *
312 * This function sets up correct i18n for an Elua state. That means loading 316 * This function sets up correct i18n for an Elua state. That means loading
313 * the gettext bindings and making Lua aware of them. This also works when 317 * the gettext bindings and making Lua aware of them. This also works when
314 * i18n support is disabled at compilation time, so you can just call it 318 * i18n support is disabled at compilation time, so you can just call it
315 * unconditionally. 319 * unconditionally.
316 * 320 *
317 * @param[in] es The Elua state. 321 * This also loads the Elua module system and makes Lua aware of it. It also
318 * @return EINA_TRUE on success, EINA_FALSE on failure.
319 *
320 * @ingroup Elua
321 */
322EAPI Eina_Bool elua_state_i18n_setup(const Elua_State *es);
323
324/**
325 * @brief Set up module support for an Elua state.
326 *
327 * This loads the Elua module system and makes Lua aware of it. It also
328 * registers the Elua C utility library module. 322 * registers the Elua C utility library module.
329 * 323 *
330 * @param[in] es The Elua state. 324 * Finally, Elua provides its own loadfile based around mmap to replace the
331 * @return EINA_TRUE on success, EINA_FALSE on failure. 325 * less efficient Lua version. This function takes care of the setup.
332 *
333 * @ingroup Elua
334 */
335EAPI Eina_Bool elua_state_modules_setup(const Elua_State *es);
336
337/**
338 * @brief Set up IO support for an Elua state.
339 *
340 * Elua provides its own loadfile based around mmap to replace the less
341 * efficient Lua version. This function takes care of the setup.
342 * 326 *
343 * @param[in] es The Elua state. 327 * @param[in] es The Elua state.
344 * @return EINA_TRUE on success, EINA_FALSE on failure. 328 * @return EINA_TRUE on success, EINA_FALSE on failure.
345 * 329 *
346 * @ingroup Elua 330 * @ingroup Elua
347 */ 331 */
348EAPI Eina_Bool elua_state_io_setup(const Elua_State *es); 332EAPI Eina_Bool elua_state_setup(Elua_State *es);
349 333
350/** 334/**
351 * @brief Loads a file using Elua's own mmap-based IO. 335 * @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)
208 return 2; 208 return 2;
209} 209}
210 210
211EAPI Eina_Bool 211Eina_Bool
212elua_state_io_setup(const Elua_State *es) 212_elua_state_io_setup(const Elua_State *es)
213{ 213{
214 EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, EINA_FALSE); 214 EINA_SAFETY_ON_FALSE_RETURN_VAL(es && es->luastate, EINA_FALSE);
215 lua_pushcfunction(es->luastate, loadfile); 215 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)
94 } 94 }
95 else if (es->cmods) 95 else if (es->cmods)
96 eina_list_free(es->cmods); 96 eina_list_free(es->cmods);
97 EINA_LIST_FREE(es->lmods, data)
98 eina_stringshare_del(data);
97 EINA_LIST_FREE(es->lincs, data) 99 EINA_LIST_FREE(es->lincs, data)
98 eina_stringshare_del(data); 100 eina_stringshare_del(data);
99 eina_stringshare_del(es->progname); 101 eina_stringshare_del(es->progname);
@@ -264,8 +266,8 @@ const luaL_reg gettextlib[] =
264 { NULL, NULL } 266 { NULL, NULL }
265}; 267};
266 268
267EAPI Eina_Bool 269static Eina_Bool
268elua_state_i18n_setup(const Elua_State *es) 270_elua_state_i18n_setup(const Elua_State *es)
269{ 271{
270#ifdef ENABLE_NLS 272#ifdef ENABLE_NLS
271 char *(*dgettextp)(const char*, const char*) = dgettext; 273 char *(*dgettextp)(const char*, const char*) = dgettext;
@@ -302,8 +304,8 @@ const luaL_reg _elua_cutillib[] =
302 { NULL , NULL } 304 { NULL , NULL }
303}; 305};
304 306
305EAPI Eina_Bool 307static Eina_Bool
306elua_state_modules_setup(const Elua_State *es) 308_elua_state_modules_setup(const Elua_State *es)
307{ 309{
308 char buf[PATH_MAX]; 310 char buf[PATH_MAX];
309 EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); 311 EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE);
@@ -370,6 +372,42 @@ _elua_module_system_init(lua_State *L)
370 return 2; 372 return 2;
371} 373}
372 374
375EAPI Eina_Bool
376elua_state_setup(Elua_State *es)
377{
378 Eina_Stringshare *data;
379 Eina_Bool failed = EINA_FALSE;
380
381 if (!_elua_state_modules_setup(es))
382 return EINA_FALSE;
383 if (!_elua_state_i18n_setup(es))
384 return EINA_FALSE;
385 if (!_elua_state_io_setup(es))
386 return EINA_FALSE;
387
388 /* finally require the necessary modules */
389 EINA_LIST_FREE(es->lmods, data)
390 {
391 if (!failed)
392 {
393 if (!elua_state_require_ref_push(es))
394 {
395 failed = EINA_TRUE;
396 break;
397 }
398 lua_pushstring(es->luastate, data);
399 if (elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0)))
400 {
401 failed = EINA_TRUE;
402 break;
403 }
404 }
405 eina_stringshare_del(data);
406 }
407
408 return EINA_TRUE;
409}
410
373/* Utility functions - these could be written using the other APIs */ 411/* Utility functions - these could be written using the other APIs */
374 412
375static int 413static int
@@ -435,7 +473,12 @@ elua_util_require(Elua_State *es, const char *libname)
435{ 473{
436 EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); 474 EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1);
437 EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); 475 EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1);
438 EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); 476 if (!elua_state_require_ref_push(es))
477 {
478 /* store stuff until things are correctly set up */
479 es->lmods = eina_list_append(es->lmods, eina_stringshare_add(libname));
480 return 0;
481 }
439 lua_pushstring(es->luastate, libname); 482 lua_pushstring(es->luastate, libname);
440 return elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0)); 483 return elua_util_error_report(es, lua_pcall(es->luastate, 1, 0, 0));
441} 484}
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
34 Eina_Stringshare *coredir; 34 Eina_Stringshare *coredir;
35 Eina_Stringshare *moddir; 35 Eina_Stringshare *moddir;
36 Eina_Stringshare *appsdir; 36 Eina_Stringshare *appsdir;
37 Eina_List *lmods;
37 Eina_List *cmods; 38 Eina_List *cmods;
38 Eina_List *lincs; 39 Eina_List *lincs;
39 int requireref, apploadref; 40 int requireref, apploadref;
@@ -48,5 +49,6 @@ extern int _elua_log_dom;
48#define CRT(...) EINA_LOG_DOM_CRITICAL(_elua_log_dom, __VA_ARGS__) 49#define CRT(...) EINA_LOG_DOM_CRITICAL(_elua_log_dom, __VA_ARGS__)
49 50
50int _elua_io_popen(lua_State *L); 51int _elua_io_popen(lua_State *L);
52Eina_Bool _elua_state_io_setup(const Elua_State *es);
51 53
52#endif 54#endif