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

@@ -24678,6 +24678,21 @@
2467824678 if mname == "to_s"
2467924679 return "sp_poly_to_s(" + rc + ")"
2468024680 end
24681+ if mname == "to_i"
24682+ # Unbox the poly value's int payload. For SP_TAG_INT it's the
24683+ # raw `v.i`. For SP_TAG_OBJ branches the runtime values are
24684+ # always pointers — `.to_i` on a class that doesn't define
24685+ # `to_i` is the obj-id in Ruby; we approximate as 0 since
24686+ # spinel doesn't expose a stable obj-id and the only practical
24687+ # use is unboxing a poly-with-int payload (e.g. the result of
24688+ # `arr[i][j]` over a heterogeneous IntArray + Method array,
24689+ # whose dispatch arms all box back to SP_TAG_INT).
24690+ tmp = new_temp
24691+ recv_tmp = new_temp
24692+ emit(" sp_RbVal " + recv_tmp + " = " + rc + ";")
24693+ emit(" mrb_int " + tmp + " = (" + recv_tmp + ".tag == SP_TAG_INT) ? " + recv_tmp + ".v.i : 0;")
24694+ return tmp
24695+ end
2468124696 # For object method calls, dispatch based on cls_id. Two namespaces
2468224697 # of cls_id share SP_TAG_OBJ:
2468324698 # - non-negative: index into @cls_names (user-defined classes)