Commit 6d70b4bd

6d70b4bd233fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +2 added · ~4 modified

Description

codegen,runtime: at_exit hooks run in LIFO after main returns

Previously the `at_exit { ... }` block was silently dropped (the codegen returned 1 from compile_block_iteration_stmt and the block was never installed). Add a static LIFO array sp_at_exit_hooks[] in the runtime, register the proc on at_exit { ... } via compile_proc_literal, and emit a tail loop in main() that walks the array in reverse-registration order before returning. The emit is gated on @needs_at_exit so the typical "no at_exit" build gets a zero-cost no-op.

Cap at 256 hooks (more than enough for any subset program; if ever insufficient the cap can be lifted to a malloc-backed sp_PtrArray).

Update test/warn_kernel.rb / .expected which previously asserted "block is ignored" — now reflects the new semantics.

Fixes #990

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
6d70b4bd233f
Parent
a3c671cdb248

lib/sp_runtime.hmodifiedView file

@@ -3466,6 +3466,13 @@
34663466 a->len -= n;
34673467 return r;
34683468 }
3469+/* at_exit hooks: a static LIFO of registered procs. Initialized
3470+ zero-len in BSS; main()'s tail walks it in reverse-registration
3471+ order before returning. */
3472+#define SP_AT_EXIT_MAX 256
3473+struct sp_Proc;
3474+static struct sp_Proc *sp_at_exit_hooks[SP_AT_EXIT_MAX];
3475+static mrb_int sp_at_exit_count = 0;
34693476 static sp_PtrArray *sp_PtrArray_slice_bang(sp_PtrArray *a, mrb_int from, mrb_int n) {
34703477 if (from < 0) from += a->len;
34713478 if (from < 0) from = 0;