elua: if script returns true, do not start the main loop

This commit is contained in:
Daniel Kolesa 2014-04-30 10:55:35 +01:00 committed by Daniel Kolesa
parent a94c216442
commit 8c5cfce2a4
2 changed files with 34 additions and 14 deletions

View File

@ -27,7 +27,9 @@ getopt.parse {
end end
}, },
{ "v", "verbose", false, help = "Be verbose.", { "v", "verbose", false, help = "Be verbose.",
callback = function() printv = print end callback = function()
printv = print
end
}, },
{ category = "Generator" }, { category = "Generator" },
@ -38,14 +40,20 @@ getopt.parse {
end end
}, },
{ "L", "library", true, help = "Specify a C library name.", { "L", "library", true, help = "Specify a C library name.",
callback = function(d, p, v) libname = v end callback = function(d, p, v)
libname = v
end
}, },
{ "M", "module", true, help = "Specify a module name.", { "M", "module", true, help = "Specify a module name.",
callback = function(d, p, v) modname = v end callback = function(d, p, v)
modname = v
end
}, },
{ "P", "prefix", true, help = "Specify a class name prefix " { "P", "prefix", true, help = "Specify a class name prefix "
.. "to strip out for public interfaces.", .. "to strip out for public interfaces.",
callback = function(d, p, v) cprefix = v end callback = function(d, p, v)
cprefix = v
end
}, },
{ "o", "output", true, help = "Specify output file name(s), by " { "o", "output", true, help = "Specify output file name(s), by "
.. "default goes to stdout.", .. "default goes to stdout.",
@ -69,7 +77,7 @@ getopt.parse {
end end
} }
if quit then return end if quit then return true end
for i, v in ipairs(include_dirs) do for i, v in ipairs(include_dirs) do
lualian.include_dir(v) lualian.include_dir(v)
@ -88,4 +96,6 @@ for i, fname in ipairs(args) do
printv(" Output file: printing to stdout...") printv(" Output file: printing to stdout...")
end end
lualian.generate(fname, modname, libname, cprefix, fstream) lualian.generate(fname, modname, libname, cprefix, fstream)
end end
return true

View File

@ -49,12 +49,12 @@ static int traceback(lua_State *L) {
return 1; return 1;
} }
static int docall(lua_State *L, int narg) { static int docall(lua_State *L, int narg, int nret) {
int status; int status;
int bs = lua_gettop(L) - narg; int bs = lua_gettop(L) - narg;
lua_pushcfunction(L, traceback); lua_pushcfunction(L, traceback);
lua_insert(L, bs); lua_insert(L, bs);
status = lua_pcall(L, narg, 0, bs); status = lua_pcall(L, narg, nret, bs);
lua_remove(L, bs); lua_remove(L, bs);
if (status) lua_gc(L, LUA_GCCOLLECT, 0); if (status) lua_gc(L, LUA_GCCOLLECT, 0);
return status; return status;
@ -122,15 +122,15 @@ static int dolib(lua_State *L, const char *libname) {
} }
static int dofile(lua_State *L, const char *fname) { static int dofile(lua_State *L, const char *fname) {
return report(L, elua_loadfile(L, fname) || docall(L, 0)); return report(L, elua_loadfile(L, fname) || docall(L, 0, 1));
} }
static int dostr(lua_State *L, const char *chunk, const char *chname) { static int dostr(lua_State *L, const char *chunk, const char *chname) {
return report(L, luaL_loadbuffer(L, chunk, strlen(chunk), chname) return report(L, luaL_loadbuffer(L, chunk, strlen(chunk), chname)
|| docall(L, 0)); || docall(L, 0, 0));
} }
static int doscript(lua_State *L, int argc, char **argv, int n) { static int doscript(lua_State *L, int argc, char **argv, int n, int *quit) {
int status; int status;
const char *fname = argv[n]; const char *fname = argv[n];
int narg = getargs(L, argc, argv, n); int narg = getargs(L, argc, argv, n);
@ -141,10 +141,14 @@ static int doscript(lua_State *L, int argc, char **argv, int n) {
status = elua_loadfile(L, fname); status = elua_loadfile(L, fname);
lua_insert(L, -(narg + 1)); lua_insert(L, -(narg + 1));
if (!status) { if (!status) {
status = docall(L, narg); status = docall(L, narg, 1);
} else { } else {
lua_pop(L, narg); lua_pop(L, narg);
} }
if (!status) {
*quit = lua_toboolean(L, -1);
lua_pop(L, 1);
}
return report(L, status); return report(L, status);
} }
@ -368,9 +372,15 @@ static int lua_main(lua_State *L) {
/* run script or execute sdin as file */ /* run script or execute sdin as file */
if (optind < argc) { if (optind < argc) {
if ((m->status = doscript(L, argc, argv, optind))) return 0; int quit = 0;
if ((m->status = doscript(L, argc, argv, optind, &quit))) return 0;
if (quit) return 0;
} else if (!hasexec) { } else if (!hasexec) {
dofile(L, NULL); int quit;
if ((m->status = dofile(L, NULL))) return 0;
quit = lua_toboolean(L, -1);
lua_pop(L, 1);
if (quit) return 0;
} }
ecore_main_loop_begin(); ecore_main_loop_begin();