Commit 3c77e34e
3c77e34ed8e6Merged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): poly to_i unboxes SP_TAG_INT payload directly
compile_poly_method_call lacked a to_i arm, so (poly).to_i
fell through to emit_poly_dispatch → emit_poly_builtin_dispatch,
which only handles [] / length / size. Result: the SP_TAG_OBJ
branch emitted no arms (none matched to_i), and the result temp
stayed at its 0 default.
Concretely, optcarrot's (@fetch[a][a]).to_i lowered to:
sp_RbVal _t = <dispatch result>;
mrb_int _t2 = 0;
if (_t.tag == SP_TAG_OBJ) {
// empty
}
// _t2 is always 0 — the dispatch chain produced int values
// boxed back into SP_TAG_INT polys, but to_i never read them.
Add a special-case for to_i early in compile_poly_method_call:
extract the int payload via recv.tag == SP_TAG_INT ? recv.v.i : 0.
For the typical case (poly with int payload after a [] dispatch
that boxed via sp_box_int), this returns the actual int. For
non-int-payload polys, the fallback 0 matches Ruby's "no to_i
defined → 0-or-undefined" shape (none of optcarrot's call sites
hit this path).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 3c77e34ed8e6
- Parent
- ef05cb1aa590
spinel_codegen.rbmodifiedView file
if mname == "to_s" return "sp_poly_to_s(" + rc + ")" end if mname == "to_i" # Unbox the poly value's int payload. For SP_TAG_INT it's the # raw `v.i`. For SP_TAG_OBJ branches the runtime values are # always pointers — `.to_i` on a class that doesn't define # `to_i` is the obj-id in Ruby; we approximate as 0 since # spinel doesn't expose a stable obj-id and the only practical # use is unboxing a poly-with-int payload (e.g. the result of # `arr[i][j]` over a heterogeneous IntArray + Method array, # whose dispatch arms all box back to SP_TAG_INT). tmp = new_temp recv_tmp = new_temp emit(" sp_RbVal " + recv_tmp + " = " + rc + ";") emit(" mrb_int " + tmp + " = (" + recv_tmp + ".tag == SP_TAG_INT) ? " + recv_tmp + ".v.i : 0;") return tmp end # For object method calls, dispatch based on cls_id. Two namespaces # of cls_id share SP_TAG_OBJ: # - non-negative: index into @cls_names (user-defined classes)test/poly_to_i_via_str_poly_hash.rbaddedView file
Loading diff…
test/poly_to_i_via_str_poly_hash.rb.expectedaddedView file
Loading diff…