Commit 374c7b6f

374c7b6f58b0Merged

Ryan Seys <[email protected]> · 6 months ago · 5 files · +2 added · ~1 modified · -2 deleted

Description

fix(codegen): Hash#transform_values for sym_int_hash + inspect

{a:1,b:2}.transform_values { v*10 } previously fell through to the generic dispatch path and emitted 0 because there was no transform_values handler for sym_int_hash. Even with the handler, p on a sym_int_hash printed the struct pointer because compile_inspect_for had no entry for sym_int_hash.

Three coordinated pieces:

  1. compile_hash_method_expr's sym_int_hash arm gains a transform_values handler (mirroring the str_int_hash and int_str_hash arms): walk insertion order, bind block param to value, run block body, set new hash.
  2. emit_sym_int_hash_runtime emits a new sp_SymIntHash_inspect helper that formats the hash as Ruby's modern {a: 1, b: 2} shorthand. Sym keys go through sp_sym_to_s; the parser only routes valid identifier symbols here, so bare-name form always round-trips.
  3. compile_inspect_for grows a sym_int_hash branch that calls the new helper.

Test consolidation: renamed test/hash_transform_values_int_str.rb to test/hash_transform_values.rb and added a sym_int_hash variant section ahead of the existing int_str_hash content. Single file covers both hash variants now.

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

Details

Commit
374c7b6f58b0
Parent
be8666c66f8d

spinel_codegen.rbmodifiedView file

@@ -14568,6 +14568,11 @@
1456814568 emit_raw("static void sp_SymIntHash_delete(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->keys[idx]=-1;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]>=0){mrb_int nj=(mrb_int)(((mrb_int)h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=-1;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}{mrb_int oi=0;while(oi<=h->len){if(h->order[oi]==k){while(oi<h->len){h->order[oi]=h->order[oi+1];oi++;}break;}oi++;}}return;}idx=(idx+1)&h->mask;}}")
1456914569 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;}")
1457014570 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;}")
14571+ # Hash inspect — Ruby's modern shorthand `{k: v, ...}`. All keys
14572+ # in a sym_int_hash are valid identifier symbols (the parser only
14573+ # routes literal symbol keys here), so the bare-name form always
14574+ # round-trips correctly.
14575+ emit_raw("static const char*sp_SymIntHash_inspect(sp_SymIntHash*h){sp_String*s=sp_String_new(\"{\");for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,\", \");sp_String_append(s,sp_sym_to_s(h->order[i]));sp_String_append(s,\": \");sp_String_append(s,sp_int_to_s(sp_SymIntHash_get(h,h->order[i])));}sp_String_append(s,\"}\");return s->data;}")
1457114576 # sym_array.tally — emitted alongside sp_SymIntHash because the
1457214577 # helper depends on the typedef. sym_array storage is sp_IntArray
1457314578 # (sym ids stored as mrb_int); cast each element to sp_sym for the
@@ -24312,6 +24317,34 @@
2431224317 @needs_int_array = 1
2431324318 return "sp_SymIntHash_values(" + rc + ")"
2431424319 end
24320+ # transform_values { |v| ... } — same key set, block-transformed
24321+ # int values. Keeps the result a sym_int_hash; the block's last
24322+ # expression is set as the new value (truncated to mrb_int by
24323+ # sp_SymIntHash_set's signature when the block returns int).
24324+ if mname == "transform_values"
24325+ if @nd_block[nid] >= 0
24326+ blk = @nd_block[nid]
24327+ bp = get_block_param(nid, 0)
24328+ tmp = new_temp
24329+ emit(" sp_SymIntHash *" + tmp + " = sp_SymIntHash_new();")
24330+ emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")
24331+ emit(" mrb_int lv_" + bp + " = sp_SymIntHash_get(" + rc + ", " + rc + "->order[_i]);")
24332+ push_scope
24333+ declare_var(bp, "int")
24334+ bbody = @nd_body[blk]
24335+ bexpr = "0"
24336+ if bbody >= 0
24337+ bs = get_stmts(bbody)
24338+ if bs.length > 0
24339+ bexpr = compile_expr(bs.last)
24340+ end
24341+ end
24342+ emit(" sp_SymIntHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");")
24343+ pop_scope
24344+ emit(" }")
24345+ return tmp
24346+ end
24347+ end
2431524348 end
2431624349 if recv_type == "sym_str_hash"
2431724350 if mname == "[]"
@@ -32050,6 +32083,10 @@
3205032083 if at == "tuple:float,float"
3205132084 return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))"
3205232085 end
32086+ if at == "sym_int_hash"
32087+ @needs_sym_int_hash = 1
32088+ return "sp_SymIntHash_inspect(" + val + ")"
32089+ end
3205332090 ""
3205432091 end
3205532092