Commit e139e3a8
e139e3a8b94cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #151 from ryanseys/rs-hash-transform-keys-block
feat(codegen): Hash#transform_keys block form for str_int_hash
Details
- Commit
- e139e3a8b94c
- Parent
- 8111db75af2f
- Merge parent
- b598846f7840
spinel_codegen.rbmodifiedView file
@@ -2759,6 +2759,12 @@
27592759
end27602760
return "str_int_hash"27612761
end2762+
if mname == "transform_keys"2763+
if recv >= 02764+
return infer_type(recv)2765+
end2766+
return "str_int_hash"2767+
end27622768
if mname == "zip"27632769
if recv >= 027642770
rt = infer_type(recv)@@ -17931,6 +17937,34 @@
1793117937
return tmp1793217938
end1793317939
end17940+
# transform_keys: apply the block to each key, keep the matching17941+
# value, build a new hash. Result type matches the input hash17942+
# type when the block returns the same key C-type — the common17943+
# case for `transform_keys { |k| k.upcase }` etc.17944+
if mname == "transform_keys"17945+
if @nd_block[nid] >= 017946+
blk = @nd_block[nid]17947+
bp = get_block_param(nid, 0)17948+
tmp = new_temp17949+
emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")17950+
emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")17951+
emit(" const char *lv_" + bp + " = " + rc + "->order[_i];")17952+
push_scope17953+
declare_var(bp, "string")17954+
bbody = @nd_body[blk]17955+
bexpr = "lv_" + bp17956+
if bbody >= 017957+
bs = get_stmts(bbody)17958+
if bs.length > 017959+
bexpr = compile_expr(bs.last)17960+
end17961+
end17962+
emit(" sp_StrIntHash_set(" + tmp + ", " + bexpr + ", sp_StrIntHash_get(" + rc + ", " + rc + "->order[_i]));")17963+
pop_scope17964+
emit(" }")17965+
return tmp17966+
end17967+
end1793417968
end1793517969
if recv_type == "int_str_hash"1793617970
@needs_int_str_hash = 1test/hash_transform_keys.rbaddedView file
Loading diff…