Commit 5d47ba89
5d47ba89882bMerged
Ryan Seys <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
feat(proc): return a Float from a proc/lambda through .call
A proc/lambda whose body's value is a Float silently truncated to the
int that the sp_proc_call ABI slot defaults to (or, before, deferred
with an "unsupported proc with float/range/time return" diagnostic).
Box a float return through the existing _sp_proc_poly_ret poly slot:
emit_proc_literal stores the boxed float (via the poly-result path,
sp_box_float), and the <proc>.call / .() / [] site reads it back
with sp_poly_to_f. This reuses the machinery already in place for poly
returns; range/time still defer (no fixture exercises them yet).
Covers the direct-literal, proc-variable, and method-tail .call forms.
The poly-receiver .call path (a proc stored in a poly container, e.g.
[proc { 3.5 }][0].call) still reads the raw slot and is unchanged --
that path is already lossy for non-int returns (a stored string proc
mis-prints there too); unifying it is separate work.
Adds test/proc_float_return.rb.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 5d47ba89882b
- Parent
- 34f8cd5461a2
src/codegen.cmodifiedView file
} /* The proc fn returns mrb_int (the ABI); heap-pointer values (strings, arrays, hashes, objects) are laundered through (mrb_int)(uintptr_t). TY_POLY values are stored in _sp_proc_poly_ret (file-static sp_RbVal) before return; the call site reads it back. Float/range/time don't fit the slot and defer. */ TY_POLY and float values are stored in _sp_proc_poly_ret (file-static sp_RbVal) before return -- float boxed via sp_box_float -- and the call site reads it back (unboxing float with sp_poly_to_f). Range/time don't fit the slot and defer. */ int ret_ptr = proc_slot_is_ptr(ret); int ret_void = (ret == TY_VOID); /* body's last expr has no value (e.g. puts) */ int ret_poly = (ret == TY_POLY); if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void && !ret_poly) { int ret_fbox = (ret == TY_FLOAT); /* boxed through the poly return slot */ if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void && !ret_poly && !ret_fbox) { free(params.v); free(used.v); free(locals.v); free(caps.v); unsupported(c, create, "proc with float/range/time return (later slice)"); unsupported(c, create, "proc with range/time return (later slice)"); return; } } else buf_puts(pb, " return 0;\n"); } else if (ret_poly) { /* Store the sp_RbVal result in the file-static _sp_proc_poly_ret slot; the call site reads it back after sp_proc_call returns. */ else if (ret_poly || ret_fbox) { /* Store the result in the file-static _sp_proc_poly_ret slot (boxed: a float tail becomes sp_box_float via g_result_poly); the call site reads it back after sp_proc_call returns. */ if (!g_needs_proc_poly_retslot) { g_needs_proc_poly_retslot = 1; buf_puts(&g_proc_protos, "static sp_RbVal _sp_proc_poly_ret;\n");src/codegen_call.cmodifiedView file
Loading diff…
test/proc_float_return.rbaddedView file
Loading diff…
test/proc_float_return.rb.expectedaddedView file
Loading diff…