Commit 9384d37c

9384d37c3bb0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Implement Hash#fetch for int_int_hash

The int->int hash specialization had #[] but not the non-block #fetch, so {1=>10}.fetch(1) raised undefined method 'fetch' for int_int_hash at runtime; the block form already worked via the shared hash_fetch_block_info arm. Add fetch(k) / fetch(k, default), mirroring the int_str_hash arm, with a KeyError raise on miss when no default is given (#914).

Found via the spinelgems harness (gem fnv).

Closes #1329

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
9384d37c3bb0
Parent
d0b372bfdb38

spinel_codegen.rbmodifiedView file

@@ -27731,6 +27731,22 @@
2773127731 if mname == "dup" || mname == "clone"
2773227732 return "sp_IntIntHash_dup(" + rc + ")"
2773327733 end
27734+ # Non-block fetch(k) / fetch(k, default). The block form is handled
27735+ # by the shared hash_fetch_block_info arm above; this mirrors the
27736+ # int_str_hash arm for the int-valued variant. Issue #914 raise on
27737+ # miss when no default is given.
27738+ if mname == "fetch"
27739+ args_id = @nd_arguments[nid]
27740+ if args_id >= 0
27741+ aargs = get_args(args_id)
27742+ key = compile_arg0_as_int(nid)
27743+ if aargs.length >= 2
27744+ defval = compile_expr(aargs[1])
27745+ return "(sp_IntIntHash_has_key(" + rc + ", " + key + ") ? sp_IntIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
27746+ end
27747+ return "(sp_IntIntHash_has_key(" + rc + ", " + key + ") ? sp_IntIntHash_get(" + rc + ", " + key + ") : (sp_raise_cls(\"KeyError\", \"key not found\"), (mrb_int)0))"
27748+ end
27749+ end
2773427750 end
2773527751 if recv_type == "int_str_hash"
2773627752 @needs_int_str_hash = 1