Commit b9fe6475

b9fe6475f7f6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): poly-recv []= with poly key dispatches via tag

#464. Sequel to #456 (poly-recv []= with string key). Pre-fix the poly-recv []= dispatcher emitted only Array setter arms (IntArray / PtrArray / PolyArray), which all expect a mrb_int index. When the key is itself sp_RbVal (the canonical real-blog shape is request[:params].each { |k, v| merged[k] = v } where the iteration yields poly k from a poly recv), passing the poly struct to those mrb_int i parameters fails C compile with "passing sp_RbVal to mrb_int parameter".

Fix: a new arm in compile_poly_method_call's []= site for poly index. Dispatches by both recv cls_id and key tag at runtime:

  • key.tag == SP_TAG_STR + recv StrInt/StrStr/StrPoly hash: unbox key via .v.s, route to the matching Str*Hash_set.
  • key.tag == SP_TAG_SYM + recv SymPolyHash: unbox key via (sp_sym).v.i, route to SymPolyHash_set.
  • key.tag == SP_TAG_INT + recv Array storage: unbox key via .v.i, route to the matching Array setter.

Sym-keyed Int/Str hash variants are typedef-gated; only SymPolyHash (always in sp_runtime.h) is dispatched. Real-blog 4 archive errors close.

Closes #464

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b9fe6475f7f6
Parent
cdcfafbd0378

spinel_codegen.rbmodifiedView file

@@ -25777,6 +25777,46 @@
2577725777 emit(" else if (" + cur_h + ".cls_id == SP_BUILTIN_STR_POLY_HASH) sp_StrPolyHash_set((sp_StrPolyHash *)" + cur_h + ".v.p, " + idx + ", " + vbox_tmp + ");")
2577825778 return
2577925779 end
25780+ # Poly index (sp_RbVal): the key's concrete type isn't known
25781+ # statically, so the dispatch must read the key's tag at
25782+ # runtime. Surfaces in `merged[k] = v` inside an `each`
25783+ # iteration over a poly-recv hash (real-blog Main.run's
25784+ # request[:params].each forwards both k and v as poly). Emit:
25785+ #
25786+ # - SP_TAG_STR keys: route to Str*Hash setters via .v.s
25787+ # - SP_TAG_SYM keys: route to Sym*Hash setters via .v.i
25788+ # - other key tags fall through to the Array arms below
25789+ # (which unbox via .v.i)
25790+ if idx_t_poly == "poly"
25791+ @needs_rb_value = 1
25792+ @needs_str_int_hash = 1
25793+ @needs_str_str_hash = 1
25794+ @needs_str_poly_hash = 1
25795+ vbox = val
25796+ if val_t_poly != "poly"
25797+ vbox = box_value_to_poly(val_t_poly, val)
25798+ end
25799+ vbox_tmp = new_temp
25800+ emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";")
25801+ cur_p = new_temp
25802+ emit(" sp_RbVal " + cur_p + " = " + rc + ";")
25803+ # String-tagged key + Str*Hash recv. The `(" + idx + ").v.s`
25804+ # cast extracts the const char * payload.
25805+ emit(" if ((" + idx + ").tag == SP_TAG_STR && " + cur_p + ".cls_id == SP_BUILTIN_STR_INT_HASH) sp_StrIntHash_set((sp_StrIntHash *)" + cur_p + ".v.p, (" + idx + ").v.s, " + vbox_tmp + ".v.i);")
25806+ emit(" else if ((" + idx + ").tag == SP_TAG_STR && " + cur_p + ".cls_id == SP_BUILTIN_STR_STR_HASH) sp_StrStrHash_set((sp_StrStrHash *)" + cur_p + ".v.p, (" + idx + ").v.s, " + vbox_tmp + ".v.s);")
25807+ emit(" else if ((" + idx + ").tag == SP_TAG_STR && " + cur_p + ".cls_id == SP_BUILTIN_STR_POLY_HASH) sp_StrPolyHash_set((sp_StrPolyHash *)" + cur_p + ".v.p, (" + idx + ").v.s, " + vbox_tmp + ");")
25808+ # Symbol-tagged key + Sym*Hash recv. sp_SymPolyHash is in
25809+ # sp_runtime.h unconditionally; the sym_int_hash / sym_str_hash
25810+ # variants are emit-gated and may not have typedefs, so only
25811+ # SymPolyHash is dispatched here.
25812+ emit(" else if ((" + idx + ").tag == SP_TAG_SYM && " + cur_p + ".cls_id == SP_BUILTIN_SYM_POLY_HASH) sp_SymPolyHash_set((sp_SymPolyHash *)" + cur_p + ".v.p, (sp_sym)(" + idx + ").v.i, " + vbox_tmp + ");")
25813+ # Int-tagged key + Array storage. Fall through to the existing
25814+ # Array arms by using `(" + idx + ").v.i` as the index.
25815+ emit(" else if ((" + idx + ").tag == SP_TAG_INT && " + cur_p + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + cur_p + ".v.p, (" + idx + ").v.i, " + vbox_tmp + ");")
25816+ emit(" else if ((" + idx + ").tag == SP_TAG_INT && " + cur_p + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + cur_p + ".v.p, (" + idx + ").v.i, " + vbox_tmp + ".v.p);")
25817+ emit(" else if ((" + idx + ").tag == SP_TAG_INT && " + cur_p + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + cur_p + ".v.p, (" + idx + ").v.i, " + vbox_tmp + ".v.i);")
25818+ return
25819+ end
2578025820 # Slot is sp_RbVal — runtime cls_id determines which storage
2578125821 # the underlying pointer points at. Optcarrot's `@fetch[a] =
2578225822 # method(:peek_X)` lands here when @fetch was widened to poly