Commit 71953d63
71953d6329ddMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #949 from ryanseys/rs-hash-fetch-block
codegen: Hash#fetch(k) { default } runs block on miss
Details
- Commit
- 71953d6329dd
- Parent
- 942b885a7b82
- Merge parent
- 74612b1650c6
spinel_codegen.rbmodifiedView file
@@ -5223,6 +5223,20 @@
52235223
""52245224
end52255225
5226+
# Per-variant info for the shared Hash#fetch(k) { block } arm.5227+
# Returns a 4-tuple [value_type, value_c_type, has_key_fn,5228+
# get_fn] or nil for unsupported variants. The block-form5229+
# arm uses these to emit the runtime check + dispatch.5230+
def hash_fetch_block_info(t)5231+
if t == "sym_int_hash"; return ["int", "mrb_int", "sp_SymIntHash_has_key", "sp_SymIntHash_get"]; end5232+
if t == "sym_str_hash"; return ["string", "const char *", "sp_SymStrHash_has_key", "sp_SymStrHash_get"]; end5233+
if t == "sym_poly_hash"; return ["poly", "sp_RbVal", "sp_SymPolyHash_has_key", "sp_SymPolyHash_get"]; end5234+
if t == "str_int_hash"; return ["int", "mrb_int", "sp_StrIntHash_has_key", "sp_StrIntHash_get"]; end5235+
if t == "str_str_hash"; return ["string", "const char *", "sp_StrStrHash_has_key", "sp_StrStrHash_get"]; end5236+
if t == "str_poly_hash"; return ["poly", "sp_RbVal", "sp_StrPolyHash_has_key", "sp_StrPolyHash_get"]; end5237+
nil5238+
end5239+
52265240
# CRuby returns nil for static mismatches (e.g. `{a: 1}.dig("a")`)52275241
# since no key compares equal — Hash#dig short-circuits to nil here.52285242
def hash_key_matches_recv(recv_type, key_type)@@ -22487,6 +22501,36 @@
2248722501
return ins_h2248822502
end2248922503
end22504+
# Hash#fetch(k) { |k| default } — block form. `compile_body_into`22505+
# walks the block body emitting all statements; the final22506+
# expression assigns into a temp that we return. The non-block22507+
# 2-arg fetch (default value) stays in the per-variant arms22508+
# below; this arm is for the block-default form which they22509+
# didn't cover.22510+
if mname == "fetch" && @nd_block[nid] >= 022511+
fi = hash_fetch_block_info(recv_type)22512+
if fi != nil22513+
args_id_fb = @nd_arguments[nid]22514+
if args_id_fb >= 022515+
aargs_fb = get_args(args_id_fb)22516+
if aargs_fb.length >= 122517+
key_fb = compile_expr(aargs_fb[0])22518+
tmp_fb = new_temp22519+
emit(" " + fi[1] + " " + tmp_fb + ";")22520+
emit(" if (" + fi[2] + "(" + rc + ", " + key_fb + ")) {")22521+
emit(" " + tmp_fb + " = " + fi[3] + "(" + rc + ", " + key_fb + ");")22522+
emit(" } else {")22523+
@indent = @indent + 122524+
push_scope22525+
compile_body_into(@nd_body[@nd_block[nid]], tmp_fb, fi[0])22526+
pop_scope22527+
@indent = @indent - 122528+
emit(" }")22529+
return tmp_fb22530+
end22531+
end22532+
end22533+
end2249022534
if recv_type == "sym_int_hash"2249122535
if mname == "map" && @nd_block[nid] >= 02249222536
# Hash#map { |k, v| ... } walks the hash and accumulates the@@ -43211,6 +43255,8 @@
4321143255
elsif base_type(return_type) == "int" && last_t_cbi == "bigint"4321243256
@needs_bigint = 14321343257
expr = "sp_bigint_to_int((sp_Bigint *)" + expr + ")"43258+
elsif base_type(return_type) == "poly" && last_t_cbi != "poly" && last_t_cbi != ""43259+
expr = box_value_to_poly(last_t_cbi, expr)4321443260
end4321543261
emit(" " + ret_tmp + " = " + expr + ";")4321643262
endtest/hash_fetch_block.rbaddedView file
Loading diff…
test/hash_fetch_block.rb.expectedaddedView file
Loading diff…