Commit 42dc2fb5

42dc2fb54633Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

feat(codegen): support multi-arg block.call on proc-object path

Pre-fix, compile_proc_literal hardcoded the lambda's signature as mrb_int <fn>(void *, mrb_int) — a single mrb_int slot — and the proc-call site (compile_no_recv_call_expr's vt == "proc" branch) always emitted sp_proc_call(lv_, ). Any second positional arg was silently dropped.

Reproducer:

class App
  def run(&block); block.call(10, 20); end
end
App.new.run { |a, b| puts a + b }
# CRuby:           30
# pre-fix Spinel:  10  (b stayed at 0; second arg dropped)

Fix gives every proc fn a uniform (void *cap, mrb_int *args) signature regardless of arity. The body unpacks args[i] into lv_<bp> locals at function entry, and the call site packs args into a stack-allocated array via a C99 compound literal:

sp_proc_call(lv_block, (mrb_int[]){10, 20})

This collapses the previous sp_proc_call / sp_proc_call_2 / sp_proc_call_3 family in lib/sp_runtime.h down to a single helper that handles any arity. proc_fn_args_unpack(bps) builds the per-fn unpack prelude as a returned string and is shared by the captures and no-captures branches in compile_proc_literal.

Test plan

  • make bootstrap (gen2.c == gen3.c)
  • make test passes (182 tests)
  • test/multi_arg_block_call.rb covers 1-param (regression guard), 2-param, 3-param, and 0-param-block-with-extra-args cases.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
42dc2fb54633
Parent
04000f21a009

lib/sp_runtime.hmodifiedView file

@@ -695,11 +695,10 @@
695695 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; }
696696 static const char *sp_file_basename(const char *path) { const char *s = strrchr(path, '/'); if (s) return s + 1; return path; }
697697
698-typedef mrb_int (*sp_proc_fn_t)(void *, mrb_int);
699-typedef struct sp_Proc { sp_proc_fn_t fn; void *cap; void (*cap_scan)(void *); } sp_Proc;
698+typedef struct sp_Proc { void *fn; void *cap; void (*cap_scan)(void *); } sp_Proc;
700699 static void sp_Proc_scan(void *p) { sp_Proc *pr = (sp_Proc *)p; if (pr->cap && pr->cap_scan) pr->cap_scan(pr->cap); }
701-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; }
702-static mrb_int sp_proc_call(sp_Proc *p, mrb_int arg) { return (p && p->fn) ? p->fn(p->cap, arg) : 0; }
700+static sp_Proc *sp_proc_new(void *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; }
701+static mrb_int sp_proc_call(sp_Proc *p, mrb_int *args) { return (p && p->fn) ? ((mrb_int (*)(void *, mrb_int *))p->fn)(p->cap, args) : 0; }
703702
704703 /* ---- StringIO runtime ---- */
705704 typedef struct { char *buf; int64_t len; int64_t cap; int64_t pos; int64_t lineno; int closed; } sp_StringIO;