vm.c: allow Hash subclasses to override []; close #6675

The OP_GETIDX optimization now only applies to instances of the Hash
class itself. Subclasses fall back to method dispatch, allowing them
to override the [] method. This fixes compatibility with libraries
like mruby-hashie that rely on aliasing/overriding [] in subclasses.

Trade-off: Hash#[] cannot be overridden on the Hash class itself
(only on subclasses). This is a reasonable semantic for mruby since
subclassing is the proper pattern for customization.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-12-25 01:24:58 +09:00
parent d1aa722a98
commit 35af869d7d
+2
View File
@@ -1892,6 +1892,8 @@ RETRY_TRY_BLOCK:
}
break;
case MRB_TT_HASH:
/* optimize only for Hash class; subclasses may override [] */
if (mrb_obj_class(mrb, va) != mrb->hash_class) goto getidx_fallback;
va = mrb_hash_get(mrb, va, vb);
ci = mrb->c->ci;
regs[a] = va;