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:
- 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.
- 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. - 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
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;}}") 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;}") 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;}") # Hash inspect — Ruby's modern shorthand `{k: v, ...}`. All keys # in a sym_int_hash are valid identifier symbols (the parser only # routes literal symbol keys here), so the bare-name form always # round-trips correctly. 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;}") # sym_array.tally — emitted alongside sp_SymIntHash because the # helper depends on the typedef. sym_array storage is sp_IntArray # (sym ids stored as mrb_int); cast each element to sp_sym for the @needs_int_array = 1 return "sp_SymIntHash_values(" + rc + ")" end # transform_values { |v| ... } — same key set, block-transformed # int values. Keeps the result a sym_int_hash; the block's last # expression is set as the new value (truncated to mrb_int by # sp_SymIntHash_set's signature when the block returns int). if mname == "transform_values" if @nd_block[nid] >= 0 blk = @nd_block[nid] bp = get_block_param(nid, 0) tmp = new_temp emit(" sp_SymIntHash *" + tmp + " = sp_SymIntHash_new();") emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {") emit(" mrb_int lv_" + bp + " = sp_SymIntHash_get(" + rc + ", " + rc + "->order[_i]);") push_scope declare_var(bp, "int") bbody = @nd_body[blk] bexpr = "0" if bbody >= 0 bs = get_stmts(bbody) if bs.length > 0 bexpr = compile_expr(bs.last) end end emit(" sp_SymIntHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");") pop_scope emit(" }") return tmp end end end if recv_type == "sym_str_hash" if mname == "[]" if at == "tuple:float,float" return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))" end if at == "sym_int_hash" @needs_sym_int_hash = 1 return "sp_SymIntHash_inspect(" + val + ")" end "" endtest/hash_transform_values.rbaddedView file
Loading diff…
test/hash_transform_values.rb.expectedaddedView file
Loading diff…
test/hash_transform_values_int_str.rbdeleted
Loading diff…
test/hash_transform_values_int_str.rb.expecteddeleted
Loading diff…