diff options
-rw-r--r-- | src/bin/elua/main.c | 150 | ||||
-rw-r--r-- | 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; | |||
30 | #define ERR(...) EINA_LOG_DOM_ERR(_el_log_domain, __VA_ARGS__) | 30 | #define ERR(...) EINA_LOG_DOM_ERR(_el_log_domain, __VA_ARGS__) |
31 | #define CRT(...) EINA_LOG_DOM_CRITICAL(_el_log_domain, __VA_ARGS__) | 31 | #define CRT(...) EINA_LOG_DOM_CRITICAL(_el_log_domain, __VA_ARGS__) |
32 | 32 | ||
33 | static int | ||
34 | elua_traceback(lua_State *L) | ||
35 | { | ||
36 | lua_getglobal(L, "debug"); | ||
37 | if (!lua_istable(L, -1)) | ||
38 | { | ||
39 | lua_pop(L, 1); | ||
40 | return 1; | ||
41 | } | ||
42 | lua_getfield(L, -1, "traceback"); | ||
43 | if (!lua_isfunction(L, -1)) | ||
44 | { | ||
45 | lua_pop(L, 2); | ||
46 | return 1; | ||
47 | } | ||
48 | lua_pushvalue(L, 1); | ||
49 | lua_pushinteger(L, 2); | ||
50 | lua_call(L, 2, 1); | ||
51 | return 1; | ||
52 | } | ||
53 | |||
54 | static int | ||
55 | elua_docall(Elua_State *es, int narg, int nret) | ||
56 | { | ||
57 | int status; | ||
58 | lua_State *L = elua_state_lua_state_get(es); | ||
59 | int bs = lua_gettop(L) - narg; | ||
60 | lua_pushcfunction(L, elua_traceback); | ||
61 | lua_insert(L, bs); | ||
62 | status = lua_pcall(L, narg, nret, bs); | ||
63 | lua_remove(L, bs); | ||
64 | if (status) | ||
65 | lua_gc(L, LUA_GCCOLLECT, 0); | ||
66 | return status; | ||
67 | } | ||
68 | |||
69 | static int | ||
70 | elua_getargs(Elua_State *es, int argc, char **argv, int n) | ||
71 | { | ||
72 | lua_State *L = elua_state_lua_state_get(es); | ||
73 | int i; | ||
74 | int narg = argc - (n + 1); | ||
75 | luaL_checkstack(L, narg + 3, "too many arguments to script"); | ||
76 | for (i = n + 1; i < argc; ++i) | ||
77 | { | ||
78 | lua_pushstring(L, argv[i]); | ||
79 | } | ||
80 | lua_createtable(L, narg, n + 1); | ||
81 | for (i = 0; i < argc; ++i) | ||
82 | { | ||
83 | lua_pushstring(L, argv[i]); | ||
84 | lua_rawseti(L, -2, i - n); | ||
85 | } | ||
86 | return narg; | ||
87 | } | ||
88 | |||
89 | static int | ||
90 | elua_dolib(Elua_State *es, const char *libname) | ||
91 | { | ||
92 | lua_State *L = elua_state_lua_state_get(es); | ||
93 | elua_state_require_ref_push(es); | ||
94 | lua_pushstring(L, libname); | ||
95 | return elua_report_error(es, elua_state_prog_name_get(es), | ||
96 | lua_pcall(L, 1, 0, 0)); | ||
97 | } | ||
98 | |||
99 | static int | ||
100 | elua_dofile(Elua_State *es, const char *fname) | ||
101 | { | ||
102 | return elua_report_error(es, elua_state_prog_name_get(es), | ||
103 | elua_io_loadfile(es, fname) | ||
104 | || elua_docall(es, 0, 1)); | ||
105 | } | ||
106 | |||
107 | static int | ||
108 | elua_dostr(Elua_State *es, const char *chunk, const char *chname) | ||
109 | { | ||
110 | return elua_report_error(es, elua_state_prog_name_get(es), | ||
111 | luaL_loadbuffer(elua_state_lua_state_get(es), | ||
112 | chunk, strlen(chunk), chname) | ||
113 | || elua_docall(es, 0, 0)); | ||
114 | } | ||
115 | |||
116 | static Eina_Bool | ||
117 | elua_loadapp(Elua_State *es, const char *appname) | ||
118 | { | ||
119 | lua_State *L = elua_state_lua_state_get(es); | ||
120 | elua_state_appload_ref_push(es); | ||
121 | lua_pushstring(L, appname); | ||
122 | lua_call(L, 1, 2); | ||
123 | if (lua_isnil(L, -2)) | ||
124 | { | ||
125 | lua_remove(L, -2); | ||
126 | return EINA_FALSE; | ||
127 | } | ||
128 | lua_pop(L, 1); | ||
129 | return EINA_TRUE; | ||
130 | } | ||
131 | |||
132 | static int | ||
133 | elua_doscript(Elua_State *es, int argc, char **argv, int n, int *quit) | ||
134 | { | ||
135 | int status; | ||
136 | const char *fname = argv[n]; | ||
137 | int narg = elua_getargs(es, argc, argv, n); | ||
138 | lua_setglobal(elua_state_lua_state_get(es), "arg"); | ||
139 | if (fname[0] == '-' && !fname[1]) | ||
140 | { | ||
141 | fname = NULL; | ||
142 | } | ||
143 | if (fname) | ||
144 | { | ||
145 | /* check if there is a file of that name */ | ||
146 | FILE *f = fopen(fname, "r"); | ||
147 | if (f) | ||
148 | { | ||
149 | fclose(f); | ||
150 | status = elua_io_loadfile(es, fname); | ||
151 | } | ||
152 | else | ||
153 | status = !elua_loadapp(es, fname); | ||
154 | } | ||
155 | else | ||
156 | { | ||
157 | status = elua_io_loadfile(es, fname); | ||
158 | } | ||
159 | lua_insert(elua_state_lua_state_get(es), -(narg + 1)); | ||
160 | if (!status) | ||
161 | { | ||
162 | status = elua_docall(es, narg, 1); | ||
163 | } | ||
164 | else | ||
165 | { | ||
166 | lua_pop(elua_state_lua_state_get(es), narg); | ||
167 | } | ||
168 | if (!status) | ||
169 | { | ||
170 | *quit = lua_toboolean(elua_state_lua_state_get(es), -1); | ||
171 | lua_pop(elua_state_lua_state_get(es), 1); | ||
172 | } | ||
173 | return elua_report_error(es, elua_state_prog_name_get(es), status); | ||
174 | } | ||
175 | |||
176 | void | 33 | void |
177 | elua_bin_shutdown(Elua_State *es, int c) | 34 | elua_bin_shutdown(Elua_State *es, int c) |
178 | { | 35 | { |
@@ -321,7 +178,7 @@ elua_main(lua_State *L) | |||
321 | /* load all the things */ | 178 | /* load all the things */ |
322 | EINA_LIST_FREE(largs, data) | 179 | EINA_LIST_FREE(largs, data) |
323 | { | 180 | { |
324 | if (elua_dolib(es, data)) | 181 | if (elua_util_require(es, data)) |
325 | goto error; | 182 | goto error; |
326 | } | 183 | } |
327 | 184 | ||
@@ -329,13 +186,14 @@ elua_main(lua_State *L) | |||
329 | if (optind < argc) | 186 | if (optind < argc) |
330 | { | 187 | { |
331 | int quit = 0; | 188 | int quit = 0; |
332 | if ((m->status = elua_doscript(es, argc, argv, optind, &quit))) return 0; | 189 | if ((m->status = elua_util_script_run(es, argc, argv, optind, &quit))) |
190 | return 0; | ||
333 | if (quit) return 0; | 191 | if (quit) return 0; |
334 | } | 192 | } |
335 | else if (!hasexec) | 193 | else if (!hasexec) |
336 | { | 194 | { |
337 | int quit; | 195 | int quit; |
338 | if ((m->status = elua_dofile(es, NULL))) return 0; | 196 | if ((m->status = elua_util_file_run(es, NULL))) return 0; |
339 | quit = lua_toboolean(L, -1); | 197 | quit = lua_toboolean(L, -1); |
340 | lua_pop(L, 1); | 198 | lua_pop(L, 1); |
341 | if (quit) return 0; | 199 | 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) | |||
416 | { | 416 | { |
417 | EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); | 417 | EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); |
418 | EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); | 418 | EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); |
419 | EINA_SAFETY_ON_FALSE_RETURN_VAL(es->requireref, -1); | 419 | EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_require_ref_push(es), -1); |
420 | lua_pushstring(es->luastate, libname); | 420 | lua_pushstring(es->luastate, libname); |
421 | return elua_report_error(es, es->progname, | 421 | return elua_report_error(es, es->progname, |
422 | lua_pcall(es->luastate, 1, 0, 0)); | 422 | lua_pcall(es->luastate, 1, 0, 0)); |
@@ -448,7 +448,7 @@ elua_util_app_load(Elua_State *es, const char *appname) | |||
448 | { | 448 | { |
449 | EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); | 449 | EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE); |
450 | EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); | 450 | EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE); |
451 | EINA_SAFETY_ON_FALSE_RETURN_VAL(es->apploadref, EINA_FALSE); | 451 | EINA_SAFETY_ON_FALSE_RETURN_VAL(elua_state_appload_ref_push(es), EINA_FALSE); |
452 | lua_pushstring(es->luastate, appname); | 452 | lua_pushstring(es->luastate, appname); |
453 | lua_call(es->luastate, 1, 2); | 453 | lua_call(es->luastate, 1, 2); |
454 | if (lua_isnil(es->luastate, -2)) | 454 | if (lua_isnil(es->luastate, -2)) |