Commit 85a128e9
85a128e978abMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): dispatch poly-recv arr[i]= by runtime cls_id
compile_bracket_assign had no branch for rt == "poly" (slot
type sp_RbVal). When optcarrot's @fetch[a] = method(:peek_X)
landed against a slot widened to plain poly (rather than
poly_array), the function fell through every typed branch and
emitted nothing — the add_mappings loop body became:
while (lv_i < n) {
lv_a = sp_IntArray_get(lv_addr, lv_i);
lv_i += 1;
// @fetch[a] = peek silently dropped
// @store[a] = poke silently dropped
}
so @fetch / @store stayed at their post-init values (an empty IntArray of nils → 0s) and CPU instruction dispatch read 0 from every address.
Add a poly-recv arm that dispatches by runtime cls_id to the appropriate typed setter, extracting the matching payload from the boxed value: PolyArray gets the boxed sp_RbVal, PtrArray gets v.p, IntArray gets v.i. The boxed value is hoisted into a temp so each branch reads the same source.
This isn't a complete fix — when the runtime storage is INT_ARRAY
but writes carry pointer-typed payloads (the typical optcarrot
shape since @fetch = [nil] * 0x10000 materializes as IntArray),
the values get cast through int and reads recover them as int,
not as their original cls_id. Proper fix is runtime widening of
the underlying storage on first heterogeneous write — left as
follow-up work. The codegen path now at least runs the
assignments instead of silently dropping them.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 85a128e978ab
- Parent
- eb66995fd0cd
spinel_codegen.rbmodifiedView file
emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");") return end if rt == "poly_array" vbox = val if arg_ids.length >= 2 vt = infer_type(arg_ids[1]) if vt != "poly" vbox = box_value_to_poly(vt, val) end end emit(" sp_PolyArray_set(" + rc + ", " + idx + ", " + vbox + ");") return end if rt == "poly" # Slot is sp_RbVal — runtime cls_id determines which storage # the underlying pointer points at. Optcarrot's `@fetch[a] = # method(:peek_X)` lands here when @fetch was widened to poly # (rather than poly_array) by the heterogeneous IntArray + Method # writes. Dispatch by cls_id, mirroring sp_PolyArray_set's # interface for the POLY_ARRAY case and using element-typed # setters for the homogeneous storage variants. Extract the # appropriate payload from the boxed value via .v.p / .v.i. vbox = val vt = "int" if arg_ids.length >= 2 vt = infer_type(arg_ids[1]) if vt != "poly" vbox = box_value_to_poly(vt, val) end end vbox_tmp = new_temp emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";") emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");") emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);") emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);") return end if rt == "str_int_hash" emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");") returntest/poly_recv_bracket_assign.rbaddedView file
Loading diff…
test/poly_recv_bracket_assign.rb.expectedaddedView file
Loading diff…