lovr/src/main.c

62 lines
1.4 KiB
C
Raw Normal View History

2019-05-18 00:11:22 +00:00
#include "api/api.h"
#include "event/event.h"
#include "core/os.h"
2022-03-22 07:13:21 +00:00
#include "util.h"
2022-03-30 19:13:53 +00:00
#include "boot.lua.h"
2021-03-16 00:54:27 +00:00
#include <lua.h>
#include <lauxlib.h>
2021-02-07 23:51:31 +00:00
#include <lualib.h>
2018-09-27 04:55:54 +00:00
#include <stdbool.h>
2018-05-15 04:38:50 +00:00
#include <string.h>
2018-02-17 17:18:08 +00:00
#include <stdlib.h>
static void run(void* T) {
while (luax_resume(T, 0) == LUA_YIELD) {
os_sleep(0.);
}
}
2016-08-08 20:51:22 +00:00
int main(int argc, char** argv) {
2023-11-02 20:15:37 +00:00
os_init();
2018-02-17 17:18:08 +00:00
2023-11-02 20:15:37 +00:00
for (;;) {
2018-10-29 20:52:50 +00:00
lua_State* L = luaL_newstate();
luax_setmainthread(L);
2018-10-29 20:52:50 +00:00
luaL_openlibs(L);
2021-03-16 00:54:27 +00:00
luax_preload(L);
2018-10-29 20:52:50 +00:00
lua_newtable(L);
2023-11-02 20:15:37 +00:00
static Variant cookie;
luax_pushvariant(L, &cookie);
lua_setfield(L, -2, "restart");
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
2023-11-02 20:15:37 +00:00
lua_rawseti(L, -2, i);
}
lua_setglobal(L, "arg");
lua_pushcfunction(L, luax_getstack);
2022-03-30 19:13:53 +00:00
if (luaL_loadbuffer(L, (const char*) etc_boot_lua, etc_boot_lua_len, "@boot.lua") || lua_pcall(L, 0, 1, -2)) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
2023-11-02 20:15:37 +00:00
os_destroy();
2018-10-29 20:52:50 +00:00
return 1;
}
2018-10-29 20:52:50 +00:00
2023-11-01 21:38:04 +00:00
lua_State* T = lua_tothread(L, -1);
lovrSetLogCallback(luax_vlog, T);
lovrTry(run, T, luax_vthrow, T);
2018-10-29 20:52:50 +00:00
2023-11-02 20:15:37 +00:00
if (lua_type(T, 1) == LUA_TSTRING && !strcmp(lua_tostring(T, 1), "restart")) {
luax_checkvariant(T, 2, &cookie);
if (cookie.type == TYPE_OBJECT) memset(&cookie, 0, sizeof(cookie));
lua_close(L);
continue;
2020-12-25 19:18:57 +00:00
} else {
2023-11-02 20:15:37 +00:00
int status = lua_tointeger(T, 1);
lua_close(L);
2021-02-25 16:00:12 +00:00
os_destroy();
2023-11-02 20:15:37 +00:00
return status;
2020-12-25 19:18:57 +00:00
}
}
}