elua lib: use Eina_Bool as return val in some utils

This commit is contained in:
Daniel Kolesa 2015-04-24 16:01:24 +01:00
parent e9aadea402
commit e73f7f7a40
4 changed files with 38 additions and 41 deletions

View File

@ -107,7 +107,7 @@ elua_main(lua_State *L)
if (optind < argc) if (optind < argc)
{ {
int quit = 0; 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; m->status = 1;
return 0; return 0;

View File

@ -348,46 +348,43 @@ EAPI int elua_io_loadfile(const Elua_State *es, const char *fname);
/** /**
* @brief Requires a module. * @brief Requires a module.
* *
* Requires a Lua module. Leaves the Lua stack clean. Returns 0 on success * Requires a Lua module. Leaves the Lua stack clean.
* or non-zero value on failure (see lua_pcall).
* *
* @param[in] es The Elua state. * @param[in] es The Elua state.
* @param[in] libname The library name. * @param[in] libname The library name.
* @return 0 for no errors, a non-zero value for errors (-1 for NULL es or * @return EINA_TRUE on success, EINA_FALSE on failure.
* NULL require).
* *
* @ingroup Elua * @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. * @brief Runs a file.
* *
* Runs a file. Uses the Elua mmapped file IO to load the file. Returns zero * Runs a file. Uses the Elua mmapped file IO to load the file.
* on success or non-zero value on failure.
* *
* @param[in] es The Elua state. * @param[in] es The Elua state.
* @param[in] fname The file name. * @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 * @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. * @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] es The Elua state.
* @param[in] chunk The string to run. * @param[in] chunk The string to run.
* @param[in] chname The chunk name to use for traceback/debug. * @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 * @ingroup Elua
*/ */
EAPI int elua_util_string_run(Elua_State *es, const char *chunk, EAPI Eina_Bool elua_util_string_run(Elua_State *es, const char *chunk,
const char *chname); const char *chname);
/** /**
* @brief Loads an application. * @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] es The Elua state.
* @param[in] argc The argument count. * @param[in] argc The argument count.
* @param[in] argv The arguments. * @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. * @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 * @ingroup Elua
*/ */
EAPI int elua_util_script_run(Elua_State *es, int argc, char **argv, int n, EAPI Eina_Bool elua_util_script_run(Elua_State *es, int argc, char **argv,
int *quit); int n, int *quit);
/** /**
* @brief Reports an error using Eina logging. * @brief Reports an error using Eina logging.

View File

@ -468,11 +468,11 @@ _elua_getargs(Elua_State *es, int argc, char **argv, int n)
return narg; return narg;
} }
EAPI int EAPI Eina_Bool
elua_util_require(Elua_State *es, const char *libname) elua_util_require(Elua_State *es, const char *libname)
{ {
EINA_SAFETY_ON_NULL_RETURN_VAL(es, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es, EINA_FALSE);
EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE);
if (!elua_state_require_ref_push(es)) if (!elua_state_require_ref_push(es))
{ {
/* store stuff until things are correctly set up */ /* store stuff until things are correctly set up */
@ -480,26 +480,26 @@ elua_util_require(Elua_State *es, const char *libname)
return 0; return 0;
} }
lua_pushstring(es->luastate, libname); 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) 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, EINA_FALSE);
EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE);
return elua_util_error_report(es, elua_io_loadfile(es, fname) return !elua_util_error_report(es, elua_io_loadfile(es, fname)
|| _elua_docall(es, 0, 1)); || _elua_docall(es, 0, 1));
} }
EAPI int EAPI Eina_Bool
elua_util_string_run(Elua_State *es, const char *chunk, const char *chname) 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, EINA_FALSE);
EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, -1); EINA_SAFETY_ON_NULL_RETURN_VAL(es->luastate, EINA_FALSE);
return elua_util_error_report(es, luaL_loadbuffer(es->luastate, chunk, return !elua_util_error_report(es, luaL_loadbuffer(es->luastate, chunk,
strlen(chunk), chname) strlen(chunk), chname)
|| _elua_docall(es, 0, 0)); || _elua_docall(es, 0, 0));
} }
EAPI Eina_Bool EAPI Eina_Bool
@ -519,7 +519,7 @@ elua_util_app_load(Elua_State *es, const char *appname)
return EINA_TRUE; return EINA_TRUE;
} }
EAPI int EAPI Eina_Bool
elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit) elua_util_script_run(Elua_State *es, int argc, char **argv, int n, int *quit)
{ {
int status, narg; 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); *quit = lua_toboolean(es->luastate, -1);
lua_pop(es->luastate, 1); lua_pop(es->luastate, 1);
} }
return elua_util_error_report(es, status); return !elua_util_error_report(es, status);
} }
static void static void

View File

@ -66,9 +66,9 @@ START_TEST(elua_api)
fail_if(lua_type(lst, -1) != LUA_TFUNCTION); fail_if(lua_type(lst, -1) != LUA_TFUNCTION);
lua_pop(lst, 1); lua_pop(lst, 1);
fail_if(elua_util_require(st, "util")); fail_if(!elua_util_require(st, "util"));
fail_if(elua_util_string_run(st, "return 1337", "foo")); 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_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); fail_if(lua_type(lst, -1) != LUA_TFUNCTION);
lua_pop(lst, 1); lua_pop(lst, 1);
@ -88,7 +88,7 @@ START_TEST(elua_api)
fail_if(!f); fail_if(!f);
fprintf(f, "return 5\n"); fprintf(f, "return 5\n");
fclose(f); 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); fail_if(lua_tointeger(lst, -1) != 5);
lua_pop(lst, 1); lua_pop(lst, 1);
fail_if(remove(buf)); fail_if(remove(buf));
@ -99,7 +99,7 @@ START_TEST(elua_api)
fail_if(!elua_util_error_report(st, 5)); fail_if(!elua_util_error_report(st, 5));
fail_if(lua_gettop(lst) > 0); 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); fail_if(quit != 1);
f = fopen(buf, "w"); f = fopen(buf, "w");
@ -107,7 +107,7 @@ START_TEST(elua_api)
fprintf(f, "return false"); fprintf(f, "return false");
fclose(f); fclose(f);
cargv[1] = buf; 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(quit != 0);
fail_if(remove(buf)); fail_if(remove(buf));