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 end
52255225
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-form
5229+ # 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"]; end
5232+ if t == "sym_str_hash"; return ["string", "const char *", "sp_SymStrHash_has_key", "sp_SymStrHash_get"]; end
5233+ if t == "sym_poly_hash"; return ["poly", "sp_RbVal", "sp_SymPolyHash_has_key", "sp_SymPolyHash_get"]; end
5234+ if t == "str_int_hash"; return ["int", "mrb_int", "sp_StrIntHash_has_key", "sp_StrIntHash_get"]; end
5235+ if t == "str_str_hash"; return ["string", "const char *", "sp_StrStrHash_has_key", "sp_StrStrHash_get"]; end
5236+ if t == "str_poly_hash"; return ["poly", "sp_RbVal", "sp_StrPolyHash_has_key", "sp_StrPolyHash_get"]; end
5237+ nil
5238+ end
5239+
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_h
2248822502 end
2248922503 end
22504+ # Hash#fetch(k) { |k| default } — block form. `compile_body_into`
22505+ # walks the block body emitting all statements; the final
22506+ # expression assigns into a temp that we return. The non-block
22507+ # 2-arg fetch (default value) stays in the per-variant arms
22508+ # below; this arm is for the block-default form which they
22509+ # didn't cover.
22510+ if mname == "fetch" && @nd_block[nid] >= 0
22511+ fi = hash_fetch_block_info(recv_type)
22512+ if fi != nil
22513+ args_id_fb = @nd_arguments[nid]
22514+ if args_id_fb >= 0
22515+ aargs_fb = get_args(args_id_fb)
22516+ if aargs_fb.length >= 1
22517+ key_fb = compile_expr(aargs_fb[0])
22518+ tmp_fb = new_temp
22519+ 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 + 1
22524+ push_scope
22525+ compile_body_into(@nd_body[@nd_block[nid]], tmp_fb, fi[0])
22526+ pop_scope
22527+ @indent = @indent - 1
22528+ emit(" }")
22529+ return tmp_fb
22530+ end
22531+ end
22532+ end
22533+ end
2249022534 if recv_type == "sym_int_hash"
2249122535 if mname == "map" && @nd_block[nid] >= 0
2249222536 # 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 = 1
4321343257 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 end
4321543261 emit(" " + ret_tmp + " = " + expr + ";")
4321643262 end