Commit 4529387e

4529387ebba2Merged

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

Description

fix(codegen,runtime): sym_int_hash.merge/dup/delete dispatch (#546)

Sibling of closed #510 which fixed merge/dup/delete on sym_str_hash and sym_poly_hash. The same surface was missing on sym_int_hash (Symbol-key, Int-value); compile emitted cannot resolve call to 'merge' on sym_int_hash (emitting 0) and the call silently no-op'd.

Three pieces:

  1. New sp_SymIntHash_merge runtime helper (mirror of sp_SymStrHash_merge's shape, with the #542 NULL-guard on the second-arg pointer so a NULL-cast call site doesn't segfault).

  2. sym_int_hash recv dispatch block gains merge / dup / delete arms.

  3. h.merge(c: 3) kwarg-shorthand path builds the matching sp_SymIntHash inline from the KeywordHashNode pairs. compile_arg0 on a KeywordHashNode falls through to "0", so without this special-case the runtime helper would receive NULL and (with the new NULL-guard) silently drop the kwargs. The inline-build path mirrors compile_constructor_args's kwarg-as-bundle handling.

Sam Ruby's roundhouse ActionView::ViewHelpers.link_to path (building an HTML-attrs hash via .merge where the value type ends up numeric) now compiles + runs correctly.

Closes #546

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

Details

Commit
4529387ebba2
Parent
fb1ad6cf13ff

spinel_codegen.rbmodifiedView file

@@ -6780,6 +6780,7 @@
67806780 emit_raw("static sp_IntArray*sp_SymIntHash_keys(sp_SymIntHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,(mrb_int)h->order[i]);return a;}")
67816781 emit_raw("static sp_IntArray*sp_SymIntHash_values(sp_SymIntHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,sp_SymIntHash_get(h,h->order[i]));return a;}")
67826782 emit_raw("static sp_SymIntHash*sp_SymIntHash_dup(sp_SymIntHash*h){sp_SymIntHash*r=sp_SymIntHash_new();for(mrb_int i=0;i<h->len;i++)sp_SymIntHash_set(r,h->order[i],sp_SymIntHash_get(h,h->order[i]));return r;}")
6783+ emit_raw("static sp_SymIntHash*sp_SymIntHash_merge(sp_SymIntHash*a,sp_SymIntHash*b){sp_SymIntHash*r=sp_SymIntHash_new();for(mrb_int i=0;i<a->len;i++)sp_SymIntHash_set(r,a->order[i],sp_SymIntHash_get(a,a->order[i]));if(b){for(mrb_int i=0;i<b->len;i++)sp_SymIntHash_set(r,b->order[i],sp_SymIntHash_get(b,b->order[i]));}return r;}")
67836784 # Hash inspect — Ruby's modern shorthand `{k: v, ...}`. All keys
67846785 # in a sym_int_hash are valid identifier symbols (the parser only
67856786 # routes literal symbol keys here), so the bare-name form always
@@ -16802,6 +16803,44 @@
1680216803 return tmp
1680316804 end
1680416805 end
16806+ # merge / dup / delete -- sibling of #510 (which fixed
16807+ # sym_str_hash / sym_poly_hash) but for sym_int_hash. Issue
16808+ # #546.
16809+ if mname == "merge"
16810+ args_id_m546 = @nd_arguments[nid]
16811+ if args_id_m546 >= 0
16812+ a_m546 = get_args(args_id_m546)
16813+ if a_m546.length >= 1 && @nd_type[a_m546[0]] == "KeywordHashNode"
16814+ # `h.merge(c: 3)` shorthand: the arg is a KeywordHashNode,
16815+ # not a regular HashNode. compile_expr on a KeywordHashNode
16816+ # falls through to 0 / NULL, so build the matching
16817+ # sp_SymIntHash inline from the kwarg pairs. Mirrors the
16818+ # kwarg-as-bundle path in compile_constructor_args.
16819+ tmp_kw = new_temp
16820+ emit(" sp_SymIntHash *" + tmp_kw + " = sp_SymIntHash_new();")
16821+ elems_kw = parse_id_list(@nd_elements[a_m546[0]])
16822+ ek_kw = 0
16823+ while ek_kw < elems_kw.length
16824+ if @nd_type[elems_kw[ek_kw]] == "AssocNode"
16825+ key_kw = @nd_key[elems_kw[ek_kw]]
16826+ val_kw = @nd_expression[elems_kw[ek_kw]]
16827+ if key_kw >= 0 && val_kw >= 0 && @nd_type[key_kw] == "SymbolNode"
16828+ emit(" sp_SymIntHash_set(" + tmp_kw + ", " + compile_expr(key_kw) + ", " + compile_expr(val_kw) + ");")
16829+ end
16830+ end
16831+ ek_kw = ek_kw + 1
16832+ end
16833+ return "sp_SymIntHash_merge(" + rc + ", " + tmp_kw + ")"
16834+ end
16835+ end
16836+ return "sp_SymIntHash_merge(" + rc + ", " + compile_arg0(nid) + ")"
16837+ end
16838+ if mname == "dup" || mname == "clone"
16839+ return "sp_SymIntHash_dup(" + rc + ")"
16840+ end
16841+ if mname == "delete"
16842+ return "(sp_SymIntHash_delete(" + rc + ", " + compile_arg0(nid) + "), 0)"
16843+ end
1680516844 end
1680616845 if recv_type == "sym_str_hash"
1680716846 if mname == "[]"