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

@@ -30786,6 +30786,41 @@
3078630786 emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");")
3078730787 return
3078830788 end
30789+ if rt == "poly_array"
30790+ vbox = val
30791+ if arg_ids.length >= 2
30792+ vt = infer_type(arg_ids[1])
30793+ if vt != "poly"
30794+ vbox = box_value_to_poly(vt, val)
30795+ end
30796+ end
30797+ emit(" sp_PolyArray_set(" + rc + ", " + idx + ", " + vbox + ");")
30798+ return
30799+ end
30800+ if rt == "poly"
30801+ # Slot is sp_RbVal — runtime cls_id determines which storage
30802+ # the underlying pointer points at. Optcarrot's `@fetch[a] =
30803+ # method(:peek_X)` lands here when @fetch was widened to poly
30804+ # (rather than poly_array) by the heterogeneous IntArray + Method
30805+ # writes. Dispatch by cls_id, mirroring sp_PolyArray_set's
30806+ # interface for the POLY_ARRAY case and using element-typed
30807+ # setters for the homogeneous storage variants. Extract the
30808+ # appropriate payload from the boxed value via .v.p / .v.i.
30809+ vbox = val
30810+ vt = "int"
30811+ if arg_ids.length >= 2
30812+ vt = infer_type(arg_ids[1])
30813+ if vt != "poly"
30814+ vbox = box_value_to_poly(vt, val)
30815+ end
30816+ end
30817+ vbox_tmp = new_temp
30818+ emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";")
30819+ emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");")
30820+ emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")
30821+ emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
30822+ return
30823+ end
3078930824 if rt == "str_int_hash"
3079030825 emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");")
3079130826 return