trynna fix click handler being garbage collected

This commit is contained in:
2026-05-19 13:33:02 +01:00
parent a692bfb2b3
commit 7bdb74e099
5 changed files with 38 additions and 12 deletions
+3 -2
View File
@@ -27,8 +27,8 @@ struct Element {
virtual Vec2<float> size(Window *window) = 0;
void click() {
if (!mrb_nil_p(on_click.proc))
on_click.call();
printf("Running click handler\n");
on_click.call();
}
};
@@ -170,6 +170,7 @@ struct Scene {
void update_scene(uint64_t delta_time) {
if (!mrb_nil_p(update.proc)) {
mrb_value dt_val = mrb_int_value(Ruby::mrb, delta_time);
printf("running: update, dt: %lu\n", delta_time);
update.call(1, &dt_val);
}
}
+1 -10
View File
@@ -60,11 +60,8 @@ struct Block {
Block() : proc(mrb_nil_value()) {}
void set_proc(mrb_value new_proc) {
if (!mrb_nil_p(proc))
mrb_gc_unregister(mrb, proc);
proc = new_proc;
if (!mrb_nil_p(proc))
mrb_gc_protect(mrb, proc);
mrb_gc_protect(mrb, proc);
}
Block(const Block &) = delete;
@@ -76,16 +73,10 @@ struct Block {
mrb_value result = mrb_funcall_argv(mrb, proc, mrb_intern_cstr(mrb, "call"), argc, argv);
return result;
}
~Block() {
mrb_gc_unregister(mrb, proc);
}
};
inline void run_string(const char *code, size_t length) {
int ai = mrb_gc_arena_save(mrb);
mrb_load_nstring(mrb, code, length);
mrb_gc_arena_restore(mrb, ai);
}
}; // namespace Ruby