From ff14b4a939511d42aa0ca46ea2139637b74e6e8a Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 4 Oct 2022 15:22:22 -0500 Subject: add garbage collector canaries --- demo/minimal/main.lua | 1 + src/main.c | 3 ++- src/util/util.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/util/util.h | 1 + 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/demo/minimal/main.lua b/demo/minimal/main.lua index 8eaa5c1..28a8cbe 100644 --- a/demo/minimal/main.lua +++ b/demo/minimal/main.lua @@ -1 +1,2 @@ print('hello, world!') +local canary = honey.util.gc_canary(function() print('collecting canary') end) diff --git a/src/main.c b/src/main.c index 2a2880a..b825ff4 100644 --- a/src/main.c +++ b/src/main.c @@ -34,8 +34,9 @@ int main(int argc, char **argv) setup_image(L, honey_index); setup_import(L, honey_index); setup_logging(L, honey_index); - setup_window(L, honey_index); setup_ode(L, honey_index); + setup_util(L, honey_index); + setup_window(L, honey_index); lua_setglobal(L, "honey"); /* load main script */ diff --git a/src/util/util.c b/src/util/util.c index 84edd27..89e9d38 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -1,4 +1,6 @@ #include +#include +#include void append_table(lua_State *L, int tbl_a, int tbl_b) { @@ -10,3 +12,46 @@ void append_table(lua_State *L, int tbl_a, int tbl_b) lua_pop(L, 1); } } + + +const char *canary_tname = "honey.canary"; + +int gc_canary(lua_State *L); +int gc_canary_collect(lua_State *L); + +void setup_util(lua_State *L, int honey_tbl) +{ + /* create gc_canary metatable */ + luaL_newmetatable(L, canary_tname); + lua_pushcfunction(L, gc_canary_collect); + lua_setfield(L, -2, "__gc"); + lua_pop(L, 1); + + hs_create_table(L, + hs_str_cfunc("gc_canary", gc_canary), + ); + + lua_setfield(L, honey_tbl, "util"); +} + + +int gc_canary(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TFUNCTION); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + int *canary = lua_newuserdata(L, sizeof(int)); + *canary = ref; + luaL_getmetatable(L, canary_tname); + lua_setmetatable(L, -2); + return 1; +} + + +int gc_canary_collect(lua_State *L) +{ + int *canary = luaL_checkudata(L, 1, canary_tname); + lua_rawgeti(L, LUA_REGISTRYINDEX, *canary); + lua_call(L, 0, 0); + luaL_unref(L, LUA_REGISTRYINDEX, *canary); + return 0; +} diff --git a/src/util/util.h b/src/util/util.h index 3ef4e0b..231bcc9 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -3,6 +3,7 @@ #include +void setup_util(lua_State *L, int honey_tbl); void append_table(lua_State *L, int tbl_a, int tbl_b); #endif -- cgit v1.2.1