Added error checking to lua calls

This commit is contained in:
j4nk 2023-05-31 08:58:52 -04:00
parent 32f4ee7b8f
commit f89129d82e
1 changed files with 12 additions and 6 deletions

18
main.c
View File

@ -19,7 +19,7 @@
#define OCT_VERS 5
#define OCT_INVALID_ARGUMENT 6
#define OCT_NETWORK_ERROR 7
#define OCT_LUA_FILE_SYNTAX_ERROR 8
#define OCT_LUA_FILE_ERROR 8
#define OCT_MAX_FILENAME_SIZE 1024
@ -81,8 +81,8 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Error: network\n");
deinitialize_everything();
return EXIT_FAILURE;
case OCT_LUA_FILE_SYNTAX_ERROR:
fprintf(stderr, "Error: given lua file had syntax error\n");
case OCT_LUA_FILE_ERROR:
fprintf(stderr, "Error: given lua file had error\n");
deinitialize_everything();
return EXIT_FAILURE;
}
@ -118,7 +118,10 @@ int main(int argc, char* argv[]) {
lua_getglobal(L, "oct_loop");
lua_pushinteger(L, 0);
lua_pushinteger(L, 0);
lua_call(L, 2, 0);
if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
OCT_LOG_ERROR("%s", luaL_checkstring(L, -1));
finish = 1;
}
oct_network_send_msg();
}
}
@ -227,11 +230,14 @@ int initialize_everything(char* lua_file) {
oct_log_init_lua(L);
if (luaL_dofile(L, lua_file)) {
OCT_LOG_ERROR("%s", luaL_checkstring(L, -1));
return OCT_LUA_FILE_SYNTAX_ERROR;
return OCT_LUA_FILE_ERROR;
}
OCT_LOG_INFO("Begin running oct_init()");
lua_getglobal(L, "oct_init");
lua_call(L, 0, 2);
if (lua_pcall(L, 0, 2, 0) != LUA_OK) {
OCT_LOG_ERROR("%s", luaL_checkstring(L, -1));
return OCT_LUA_FILE_ERROR;
}
config.needs_networking = lua_tointeger(L, -2);
config.needs_termbox = lua_tointeger(L, -1);
OCT_LOG_INFO("Finish running oct_init()");