Commit b74e3206
b74e32068431Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 3 files · +1 added · ~2 modified
Description
feat(codegen): implement proc closure support via capture struct
Phase 2 of the proc-closure recovery, replacing the abort() runtime error introduced in Phase 1 with real closure semantics that mirror the existing Fiber capture mechanism.
Runtime ABI (lib/sp_runtime.h, emit_proc_runtime):
- sp_Proc becomes a heap struct: { fn, cap, cap_scan }
- sp_proc_fn_t signature is (void *cap, mrb_int arg)
- sp_proc_new(fn, cap, cap_scan) allocates via sp_gc_alloc and registers a scan function (sp_Proc_scan) that delegates to the per-proc cap_scan so captured cells stay reachable
- sp_proc_call(p, arg) passes p->cap to the body fn
Type system:
- c_type("proc") -> "sp_Proc *"
- c_default_val("proc") -> "NULL"
- type_is_pointer("proc") -> 1 so locals get rooted automatically
Codegen (compile_proc_literal):
- Detect captures via scan_lambda_free_vars + find_var_type, mirror of the Fiber free-var detection.
- When captures exist:
- emit a typed capture struct typedef (one ptr field per cell)
- emit a per-proc scan function that sp_gc_marks each cell
- emit the body function with
void *_cap_rawfirst parameter, immediately cast to the typed struct so body code can use _cap->VN - in the enclosing scope, heap-promote each captured local (sp_gc_alloc cell, copy current value) and register in @heap_promoted_names so subsequent accesses in that scope also go through the cell
- sp_gc_alloc the cap struct with the per-proc scan function and populate its fields, then sp_proc_new(fn, cap, scan)
- When no captures exist: file-scope body fn with unused _cap arg, sp_proc_new(fn, NULL, NULL)
- Last-statement handling: if it is a Local{Variable,Operator}Write Node, compile_stmt it (so the side effect is emitted) and use fiber_var_ref of the lvalue as the return expression. compile_expr on a write node returns "0" without emitting anything, which was silently dropping mutations from proc tails.
Scope handling (fiber_var_ref):
- Inside a proc body (@in_proc_body == 1), names that aren't in @proc_captures resolve directly to lv_NAME. Heap-promoted cells from the enclosing function's scope are not visible from inside the proc body.
- For non-proc-body lookups, also require find_var_type(name) != "" before honoring a heap-promoted cell, defending against stale entries from earlier compile_proc_literal calls in unrelated functions (a pre-existing latent issue Fiber shares).
Verified cases (test/bm_proc_closure.rb):
- Escaping closure with mutation (make_counter)
- Read-only escaping closure (make_adder)
- Non-escaping closure with multiple captured locals + mutation
- Two procs sharing the same captured local (mutation propagates)
All 76 tests pass; 55 benchmarks pass; gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b74e32068431
- Parent
- 17300e8cefc7
lib/sp_runtime.hmodifiedView file
@@ -531,10 +531,11 @@
531531
static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }532532
static const char *sp_file_basename(const char *path) { const char *s = strrchr(path, '/'); if (s) return s + 1; return path; }533533
534-
typedef mrb_int (*sp_proc_fn_t)(mrb_int);535-
typedef struct { sp_proc_fn_t fn; } sp_Proc;536-
static sp_Proc sp_proc_new(sp_proc_fn_t fn) { sp_Proc p; p.fn = fn; return p; }537-
static mrb_int sp_proc_call(sp_Proc p, mrb_int arg) { return p.fn ? p.fn(arg) : 0; }534+
typedef mrb_int (*sp_proc_fn_t)(void *, mrb_int);535+
typedef struct sp_Proc { sp_proc_fn_t fn; void *cap; void (*cap_scan)(void *); } sp_Proc;536+
static void sp_Proc_scan(void *p) { sp_Proc *pr = (sp_Proc *)p; if (pr->cap && pr->cap_scan) pr->cap_scan(pr->cap); }537+
static sp_Proc *sp_proc_new(sp_proc_fn_t fn, void *cap, void (*cap_scan)(void *)) { sp_Proc *p = (sp_Proc *)sp_gc_alloc(sizeof(sp_Proc), NULL, sp_Proc_scan); p->fn = fn; p->cap = cap; p->cap_scan = cap_scan; return p; }538+
static mrb_int sp_proc_call(sp_Proc *p, mrb_int arg) { return (p && p->fn) ? p->fn(p->cap, arg) : 0; }538539
539540
/* ---- StringIO runtime ---- */540541
typedef struct { char *buf; int64_t len; int64_t cap; int64_t pos; int64_t lineno; int closed; } sp_StringIO;spinel_codegen.rbmodifiedView file
Loading diff…
test/bm_proc_closure.rbaddedView file
Loading diff…