Commit daac6b0b

daac6b0b0e14Merged

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

Description

Carry lambda/proc/arrow .call return values back to their real type

Two sibling gaps in the proc-call return-value family (#1362):

  • lambda {} / proc {} (method-call form, the sp_proc_call mrb_int ABI) never recorded the block's return type, so g.call rendered a string/array/exception return as the raw integer-encoded pointer. scan_lambda_ret_types_node now records the CallNode form's return type (mirroring the ->{} arm), and the named-proc .call site unboxes via a new proc_call_unbox helper (pointer returns cast back, int/bool left as raw mrb_int).

  • -> {} arrow lambdas (the separate sp_Val / sp_lam_* ABI) hardcoded a string-literal body to sp_lam_int(0), dropping the value. The body now boxes the string pointer through the sp_Val slot via lam_box, which the matching lam_unbox at the .call site casts back.

Known limitation: float-returning .call has no float unbox arm yet (proc_call_unbox leaves it as raw mrb_int).

Refs #1362

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

Details

Commit
daac6b0b0e14
Parent
90e9db0d9ebb

spinel_codegen.rbmodifiedView file

@@ -20660,7 +20660,7 @@
2066020660 # one slot (the proc fn's `_unused` fallback expects args[0]
2066120661 # to be addressable).
2066220662 ca = compile_proc_call_args(nid)
20663- return "sp_proc_call(lv_" + rname + ", (mrb_int[]){" + ca + "})"
20663+ return proc_call_unbox("sp_proc_call(lv_" + rname + ", (mrb_int[]){" + ca + "})", lambda_var_ret_type(rname))
2066420664 end
2066520665 end
2066620666
@@ -42191,6 +42191,27 @@
4219142191 end
4219242192 end
4219342193 end
42194+ # `g = lambda { ... }` / `g = proc { ... }` (method-call form, distinct
42195+ # from the `->{}` LambdaNode above). The block body's last-stmt type is
42196+ # the proc's return type; record it so `g.call` unboxes the sp_proc_call
42197+ # mrb_int result back to the real type instead of printing a pointer.
42198+ if expr >= 0 && @nd_type[expr] == "CallNode" && @nd_receiver[expr] < 0 &&
42199+ (@nd_name[expr] == "lambda" || @nd_name[expr] == "proc")
42200+ blk_pl = @nd_block[expr]
42201+ if blk_pl >= 0
42202+ pbody = @nd_body[blk_pl]
42203+ if pbody >= 0
42204+ pbs = get_stmts(pbody)
42205+ if pbs.length > 0
42206+ prt = infer_type(pbs.last)
42207+ if not_in(vn, @lambda_var_ret_names) == 1
42208+ @lambda_var_ret_names.push(vn)
42209+ @lambda_var_ret_types.push(prt)
42210+ end
42211+ end
42212+ end
42213+ end
42214+ end
4219442215 scan_lambda_ret_types_node(expr)
4219542216 end
4219642217 # Scan into method bodies to find lambdas returned from methods
@@ -42264,6 +42285,25 @@
4226442285 "sp_lam_int(" + expr + ")"
4226542286 end
4226642287
42288+ # Unbox an `sp_proc_call(...)` result (declared mrb_int) back to the
42289+ # proc's recorded return type. The proc-fn ABI round-trips pointer
42290+ # bodies through `(mrb_int)(uintptr_t)`, so a string/array/hash/obj
42291+ # return arrives as an integer-encoded pointer; cast it back. int / bool
42292+ # returns need no cast. Empty vtype (unknown) is left as raw mrb_int.
42293+ def proc_call_unbox(call_expr, ret_type)
42294+ if ret_type == ""
42295+ return call_expr
42296+ end
42297+ bt = base_type(ret_type)
42298+ if bt == "string" || bt == "mutable_str"
42299+ return "(const char *)(uintptr_t)(" + call_expr + ")"
42300+ end
42301+ if type_is_pointer(ret_type) == 1
42302+ return "(" + c_type(ret_type) + ")(uintptr_t)(" + call_expr + ")"
42303+ end
42304+ call_expr
42305+ end
42306+
4226742307 def lam_unbox(expr, vtype)
4226842308 bt = base_type(vtype)
4226942309 if bt == "string"
@@ -42350,7 +42390,10 @@
4235042390 return "&sp_lam_nil_val"
4235142391 end
4235242392 if t == "StringNode"
42353- return "sp_lam_int(0)"
42393+ # Box the string pointer through the sp_Val int slot (lam_box) so a
42394+ # string-returning arrow lambda round-trips its value; the matching
42395+ # lam_unbox at the .call site casts it back. Previously stubbed to 0.
42396+ return lam_box(compile_expr(nid), "string")
4235442397 end
4235542398 if t == "ConstantReadNode"
4235642399 cname = @nd_name[nid]