Commit 9daca9d7
9daca9d7db76Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(codegen): multi-arg lambda support up to arity 4 (#400)
Fixes #400.
->(a, b) { a + b } previously emitted broken code: only the
first required param was registered as formal; 2nd+ were treated
as free variables to capture, the call site sp_lam_call(f, a)
dropped extra args, and the body fn signature was 1-arg.
Three coordinated changes:
-
lib/sp_runtime.h: add sp_lam_call2 / sp_lam_call3 / sp_lam_call4 (and matching sp_fn{2,3,4}_t typedefs) that re-cast the stored 1-arg fn ptr to the right arity at the call site.
-
spinel_codegen.rb scan_lambda_free_vars + compile_lambda_expr: collect every required param (was reqs[0] only). The lambda fn signature gets one
sp_Val *_argNper param plus aliasinglv_<name>decls inside the body. -
compile_lambda_call_expr dispatches to sp_lam_callN based on arg count; arity 1 still uses the original sp_lam_call.
compile_lambda_body_expr also learns * / - / / / % /
<>= arms so multi-arg arithmetic bodies (a * b * c,
a > b) lower correctly. Without these, those CallNodes fell
through to the catch-all &sp_lam_nil_val and the lambda
returned nil for any non-add expression.
The fn ptr stored in sp_Val.u.proc.fn keeps its 1-arg
sp_fn_t type; the multi-arg path casts on both sides
(sp_lam_proc receive + sp_lam_callN dispatch). Arities beyond 4
keep the legacy 1-arg path (caller is on its own).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 9daca9d7db76
- Parent
- 9aa3b3bbdd3b
lib/sp_runtime.hmodifiedView file
static sp_Val *sp_lam_bool(mrb_bool b) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val)); v->tag = SP_BOOL2; v->u.bval = b; return v; }static sp_Val sp_lam_nil_val = { .tag = SP_NIL2 };static sp_Val *sp_lam_call(sp_Val *f, sp_Val *arg) { return f->u.proc.fn(f, arg); }/* Multi-arg lambda dispatch (issue #400). The fn pointer is stored typed as `sp_fn_t` (1-arg) so re-cast at the call site to the right arity. The generated lambda body is declared with the matching arity so the actual ABI matches. */typedef sp_Val *(*sp_fn2_t)(sp_Val *self, sp_Val *a, sp_Val *b);typedef sp_Val *(*sp_fn3_t)(sp_Val *self, sp_Val *a, sp_Val *b, sp_Val *c);typedef sp_Val *(*sp_fn4_t)(sp_Val *self, sp_Val *a, sp_Val *b, sp_Val *c, sp_Val *d);static sp_Val *sp_lam_call2(sp_Val *f, sp_Val *a, sp_Val *b) { return ((sp_fn2_t)(uintptr_t)f->u.proc.fn)(f, a, b); }static sp_Val *sp_lam_call3(sp_Val *f, sp_Val *a, sp_Val *b, sp_Val *c) { return ((sp_fn3_t)(uintptr_t)f->u.proc.fn)(f, a, b, c); }static sp_Val *sp_lam_call4(sp_Val *f, sp_Val *a, sp_Val *b, sp_Val *c, sp_Val *d) { return ((sp_fn4_t)(uintptr_t)f->u.proc.fn)(f, a, b, c, d); }static mrb_int sp_lam_to_int(sp_Val *v) { return v->u.ival; }/* ---- Fiber runtime (ucontext) ---- */spinel_codegen.rbmodifiedView file
Loading diff…
test/multi_arg_lambda.rbaddedView file
Loading diff…
test/multi_arg_lambda.rb.expectedaddedView file
Loading diff…