From ae0da18e25f0092b954bfa407ec0cd421acc2ef7 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sun, 6 Oct 2024 20:14:21 +0300 Subject: [PATCH] /apps/lua: move game_buf to state, free it on game_end() --- apps/lua/game.c | 6 +++--- apps/lua/state.h | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/lua/game.c b/apps/lua/game.c index abd2043..5136cc6 100644 --- a/apps/lua/game.c +++ b/apps/lua/game.c @@ -283,9 +283,8 @@ void game_tick(void) { lua_register(state->L, "text", b_text); /* now finally get to running the code */ - unsigned char *game_buf = NULL; - int64_t game_buf_size = file_to_bytes("/scripts/game.lua", &game_buf); - if (luaL_loadbuffer(state->L, (char *)game_buf, game_buf_size, "game.lua") == LUA_OK) { + state->game_buf_size = file_to_bytes("/scripts/game.lua", &state->game_buf); + if (luaL_loadbuffer(state->L, (char *)state->game_buf, state->game_buf_size, "game.lua") == LUA_OK) { if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) { log_critical("%s", lua_tostring(state->L, -1)); lua_pop(state->L, 1); @@ -306,6 +305,7 @@ void game_tick(void) { void game_end(void) { State *state = ctx.udata; + free(state->game_buf); lua_close(state->L); free(state); } diff --git a/apps/lua/state.h b/apps/lua/state.h index 1f20844..e4823e9 100644 --- a/apps/lua/state.h +++ b/apps/lua/state.h @@ -8,6 +8,8 @@ typedef struct State { lua_State *L; + unsigned char *game_buf; + size_t game_buf_size; } State;