Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 98 additions & 21 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4102,6 +4102,11 @@ func (vm *VM) bootstrap() {
hashInit := func(_ *VM, self object.Value, args []object.Value, blk *Proc) object.Value {
h := self.(*object.Hash)
vm.checkHashFrozen(h) // re-initialising a frozen hash raises (a fresh one is not frozen)
// Hash.new(capacity: n) / Hash.new(default, capacity: n): a trailing
// {capacity: …} keyword hash is only a preallocation hint (rbgo does not
// preallocate), so it is dropped before the positional default is read —
// Hash.new(capacity: 42).default is nil, not the keyword hash itself.
args = stripHashCapacityKwarg(args)
// initialize (re)sets the default handling: it always clears both the
// static default and the default proc first, so re-invoking it on an
// existing hash resets them (Hash#initialize is a real, if private,
Expand Down Expand Up @@ -4222,10 +4227,12 @@ func (vm *VM) bootstrap() {
}}
// Hash.ruby2_keywords_hash(hash) returns a copy of hash flagged as a keyword
// hash for `*args` forwarding; Hash.ruby2_keywords_hash? reports that flag.
// Both raise TypeError for a non-Hash argument.
// Both accept an instance of a Hash subclass and raise TypeError for a
// non-Hash argument. The copy keeps the argument's class, so calling it on a
// subclass instance returns an instance of that subclass.
vm.cHash.smethods["ruby2_keywords_hash"] = &Method{name: "ruby2_keywords_hash", owner: vm.cHash,
native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
h := hashArgOrRaise(vm, args[0])
h, src := hashOrSubclassArg(vm, args[0])
out := newHashLike(h) // preserves the compare_by_identity flag
out.Default = h.Default
out.DefaultProc = h.DefaultProc
Expand All @@ -4234,11 +4241,31 @@ func (vm *VM) bootstrap() {
out.Set(k, v)
}
out.Ruby2Keywords = true
if src != nil { // subclass instance: keep its class identity
obj := &RObject{class: src.class, ivars: map[string]object.Value{}, builtin: out}
vm.registerLiveObject(obj)
return obj
}
return out
}}
vm.cHash.smethods["ruby2_keywords_hash?"] = &Method{name: "ruby2_keywords_hash?", owner: vm.cHash,
native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
return object.Bool(hashArgOrRaise(vm, args[0]).Ruby2Keywords)
h, _ := hashOrSubclassArg(vm, args[0])
return object.Bool(h.Ruby2Keywords)
}}
// Hash.allocate returns a fully-formed, empty Hash (Class#allocate builds a
// bare RObject with no backing value, so Hash.allocate.size would fail to find
// the *object.Hash). A subclass gets a fresh Hash wrapped so its class identity
// is kept, mirroring how Hash.new builds subclass instances.
vm.cHash.smethods["allocate"] = &Method{name: "allocate", owner: vm.cHash,
native: func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
h := object.NewHash()
if recv := self.(*RClass); recv != vm.cHash {
obj := &RObject{class: recv, ivars: map[string]object.Value{}, builtin: h}
vm.registerLiveObject(obj)
return obj
}
return h
}}
// #[] reads args[0] and returns a stored/default value; #[]= copies element
// values into the hash. Neither retains the args slice, so both take the
Expand Down Expand Up @@ -4304,10 +4331,9 @@ func (vm *VM) bootstrap() {
return hashSizedEnum(self, "each")
}
h := self.(*object.Hash)
for _, k := range h.Keys {
v, _ := h.Get(k)
vm.hashEachLive(h, func(k, v object.Value) {
vm.callBlock(blk, []object.Value{hashPair(k, v)})
}
})
return h
})
vm.cHash.methods["each_pair"] = vm.cHash.methods["each"]
Expand All @@ -4317,20 +4343,19 @@ func (vm *VM) bootstrap() {
return hashSizedEnum(self, "each_key")
}
h := self.(*object.Hash)
for _, k := range h.Keys {
vm.hashEachLive(h, func(k, _ object.Value) {
vm.callBlock(blk, []object.Value{k})
}
})
return h
})
vm.cHash.define("each_value", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value {
if blk == nil {
return hashSizedEnum(self, "each_value")
}
h := self.(*object.Hash)
for _, k := range h.Keys {
v, _ := h.Get(k)
vm.hashEachLive(h, func(_, v object.Value) {
vm.callBlock(blk, []object.Value{v})
}
})
return h
})
// mergeInto folds each other hash into dst. On a key already present, a block
Expand Down Expand Up @@ -4524,20 +4549,26 @@ func (vm *VM) bootstrap() {
}
h := self.(*object.Hash)
vm.checkHashFrozen(h)
// Compute the new keys first, then rebuild in place so a new key never
// collides with an old one mid-iteration.
keys := append([]object.Value{}, h.Keys...)
newKeys := make([]object.Value, len(keys))
// Transform in place one key at a time, mirroring MRI: snapshot the pairs
// up front, then for each old key compute its replacement. A `break` in the
// block unwinds out of the method, leaving the keys processed so far
// transformed and the rest untouched — transform_keys! is not atomic. An
// old key is deleted before its replacement is stored unless it was itself
// produced as a replacement earlier, so a key rewritten onto an already
// re-keyed slot is not dropped (and the surviving order matches MRI's).
keys := append([]object.Value(nil), h.Keys...)
vals := make([]object.Value, len(keys))
for i, k := range keys {
vals[i], _ = h.Get(k)
newKeys[i] = vm.transformKey(k, mapping, blk)
}
for _, k := range keys {
h.Delete(k)
}
for i := range newKeys {
h.Set(newKeys[i], vals[i])
newKeys := object.NewHash()
for i, k := range keys {
nk := vm.transformKey(k, mapping, blk)
if _, seen := newKeys.Get(k); !seen {
h.Delete(k)
}
h.Set(nk, vals[i])
newKeys.Set(nk, object.NilV)
}
return h
})
Expand Down Expand Up @@ -7070,6 +7101,52 @@ func simplestRatBetween(lo, hi *big.Rat) *big.Rat {
// transformKey computes a replacement key for Hash#transform_keys(!): a mapping
// hash takes precedence when it contains the key, otherwise the block (if any)
// is applied, and failing both the key is left unchanged.
// hashEachLive walks a hash for Hash#each/#each_key/#each_value over a snapshot
// of the keys, so deleting the current entry (or one shifted by Delete's in-place
// compaction) during the block does not disturb the cursor. Like MRI it visits
// each key still present exactly once and skips one removed ahead of the cursor;
// the value is read fresh each step so an in-block reassignment shows through.
func (vm *VM) hashEachLive(h *object.Hash, fn func(k, v object.Value)) {
keys := append([]object.Value(nil), h.Keys...)
for _, k := range keys {
v, ok := h.Get(k)
if !ok {
continue // removed earlier in this same iteration
}
fn(k, v)
}
}

// hashOrSubclassArg unwraps v to its backing *object.Hash for the
// Hash.ruby2_keywords_hash family. It accepts a plain Hash and an instance of a
// user subclass of Hash (an RObject wrapping a Hash); for the latter it also
// returns that RObject so the result can be rebuilt with the same class. Any
// other value raises the MRI "wrong argument type X (expected Hash)" TypeError.
func hashOrSubclassArg(vm *VM, v object.Value) (*object.Hash, *RObject) {
if o, ok := v.(*RObject); ok {
if h, ok := o.builtin.(*object.Hash); ok {
return h, o
}
}
return hashArgOrRaise(vm, v), nil
}

// stripHashCapacityKwarg drops a trailing {capacity: …} keyword hash from a
// Hash.new / Hash#initialize argument list. rbgo has no keyword/positional
// separation at the native boundary, so only a hash whose sole key is the Symbol
// :capacity is treated as the capacity: hint; any other trailing hash (including
// one with additional keys) stays a positional default value.
func stripHashCapacityKwarg(args []object.Value) []object.Value {
if n := len(args); n > 0 {
if h, ok := args[n-1].(*object.Hash); ok && h.Len() == 1 {
if _, isCap := h.Get(object.Symbol("capacity")); isCap {
return args[:n-1]
}
}
}
return args
}

func (vm *VM) transformKey(k object.Value, mapping *object.Hash, blk *Proc) object.Value {
if mapping != nil {
if nk, ok := mapping.Get(k); ok {
Expand Down
159 changes: 159 additions & 0 deletions internal/vm/hash_residuals_wave13_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm_test

import (
"strings"
"testing"
)

// TestHashRuby2KeywordsHashSubclass covers Hash.ruby2_keywords_hash /
// ruby2_keywords_hash? accepting an instance of a Hash subclass: the copy keeps
// the subclass class and the keyword flag, the predicate reads the flag off
// either form, and a non-Hash still raises TypeError. Matches MRI Ruby 4.0
// (ruby/spec core/hash/ruby2_keywords_hash).
func TestHashRuby2KeywordsHashSubclass(t *testing.T) {
cases := []struct{ src, want string }{
// Subclass instance: result is an instance of the subclass, flagged, equal.
{`class MH1 < Hash; end; h = MH1.new; h[:a] = 1; kw = Hash.ruby2_keywords_hash(h)
p [kw.class == MH1, Hash.ruby2_keywords_hash?(h), Hash.ruby2_keywords_hash?(kw), kw == h]`,
"[true, false, true, true]\n"},
// Plain hash still returns a plain Hash, flagged (src == nil branch).
{`kw = Hash.ruby2_keywords_hash({a: 1}); p [kw.class, Hash.ruby2_keywords_hash?(kw)]`,
"[Hash, true]\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
}
// A non-Hash RObject (inner unwrap fails) and a non-RObject non-Hash both
// raise the MRI TypeError.
for _, src := range []string{`Hash.ruby2_keywords_hash(Object.new)`, `Hash.ruby2_keywords_hash(1)`} {
if err := runErr(t, src); err == nil || !strings.Contains(err.Error(), "expected Hash") {
t.Errorf("src=%q err=%v, want TypeError \"expected Hash\"", src, err)
}
}
}

// TestHashNewCapacityKwarg covers Hash.new / Hash#initialize accepting a
// trailing {capacity: n} keyword hash as a preallocation hint that rbgo ignores:
// it is stripped before the positional default is read, so it never becomes the
// default value and a following block still installs a default_proc. A trailing
// hash that is not exactly {capacity: …} stays a positional default value.
// Expectations match MRI Ruby 4.0 (ruby/spec core/hash/new).
func TestHashNewCapacityKwarg(t *testing.T) {
cases := []struct{ src, want string }{
// capacity: alone -> no positional default.
{`p Hash.new(capacity: 42).default`, "nil\n"},
// A positional default plus capacity: keeps the default.
{`p Hash.new(5, capacity: 42).default`, "5\n"},
// capacity: with a block installs the default_proc, no arity error.
{`p Hash.new(capacity: 42) { 1 }.default_proc.class`, "Proc\n"},
{`p Hash.new(capacity: 42) { |_h, k| k }[:z]`, ":z\n"},
// Negative capacity is ignored, not an error.
{`p Hash.new(capacity: -42).default`, "nil\n"},
// A single-key hash that is not capacity stays a positional default
// (rbgo cannot tell braces from keywords, so this mirrors the default
// hash cases already relied on elsewhere).
{`p Hash.new({foo: 1}).default`, "{foo: 1}\n"},
// A multi-key hash is a positional default (Len != 1, not stripped).
{`p Hash.new({a: 1, b: 2}).default`, "{a: 1, b: 2}\n"},
// A non-hash last argument is a positional default (not a Hash).
{`p Hash.new(5).default`, "5\n"},
// No arguments at all (n == 0): nil default.
{`p Hash.new.default`, "nil\n"},
// #initialize routes through the same stripping.
{`h = {}; h.send(:initialize, capacity: 8); p h.default`, "nil\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
}
}

// TestHashAllocate covers Hash.allocate returning a fully-formed empty Hash (its
// #size is 0 and it is mutable) and, on a subclass, an instance that keeps the
// subclass identity while backed by a real Hash. Matches MRI Ruby 4.0
// (ruby/spec core/hash/allocate).
func TestHashAllocate(t *testing.T) {
cases := []struct{ src, want string }{
{`p Hash.allocate.size`, "0\n"},
{`h = Hash.allocate; h[:a] = 1; p(h == {a: 1})`, "true\n"},
{`p Hash.allocate.instance_of?(Hash)`, "true\n"},
// Subclass: identity preserved, backed by a working Hash.
{`c = Class.new(Hash); h = c.allocate; h[:a] = 1; p [h.size, h.class == c]`, "[1, true]\n"},
{`c = Class.new(Hash); p c.allocate.instance_of?(c)`, "true\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
}
}

// TestHashEachMutateDuringIteration covers Hash#each/#each_pair/#each_key/
// #each_value walking a snapshot so a delete/shift in the block neither
// double-visits nor skips a surviving key, while a key removed ahead of the
// cursor is skipped exactly as MRI does (ruby/spec core/hash/delete, shift).
func TestHashEachMutateDuringIteration(t *testing.T) {
cases := []struct{ src, want string }{
// Plain enumeration (the three closure bodies).
{`ps = []; {a: 1, b: 2}.each { |k, v| ps << [k, v] }; p ps`, "[[:a, 1], [:b, 2]]\n"},
{`ks = []; {a: 1, b: 2}.each_key { |k| ks << k }; p ks`, "[:a, :b]\n"},
{`vs = []; {a: 1, b: 2}.each_value { |v| vs << v }; p vs`, "[1, 2]\n"},
// Deleting the current key visits every key once and empties the hash.
{`h = {a: 1, b: 2, c: 3, d: 4}; v = []; h.each_pair { |k, _| v << k; h.delete(k) }; p [v, h]`,
"[[:a, :b, :c, :d], {}]\n"},
// Shifting while iterating visits each original key once.
{`h = {a: 1, b: 2, c: 3}; v = []; s = []; h.each_pair { |k, _| v << k; s << h.shift }; p [v, s, h]`,
"[[:a, :b, :c], [[:a, 1], [:b, 2], [:c, 3]], {}]\n"},
// A key removed ahead of the cursor is skipped (exercises the presence
// check / continue in hashEachLive).
{`h = {a: 1, b: 2, c: 3, d: 4}; v = []; h.each { |k, _| v << k; h.delete(:c) if k == :a }; p v`,
"[:a, :b, :d]\n"},
{`h = {a: 1, b: 2, c: 3}; v = []; h.each_key { |k| v << k; h.delete(:c) if k == :a }; p v`,
"[:a, :b]\n"},
{`h = {a: 1, b: 2, c: 3}; v = []; h.each_value { |val| v << val; h.delete(:c) if val == 1 }; p v`,
"[1, 2]\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
}
}

// TestHashTransformKeysBangInPlace covers Hash#transform_keys! transforming one
// key at a time in place: a break leaves the already-processed keys rewritten and
// the rest untouched, a key rewritten onto an already re-keyed slot is preserved,
// and the surviving key order matches MRI (ruby/spec core/hash/transform_keys).
func TestHashTransformKeysBangInPlace(t *testing.T) {
cases := []struct{ src, want string }{
// break part-way: :a,:b rewritten, :c (break) and :d untouched.
{`h = {a: 1, b: 2, c: 3, d: 4}; h.transform_keys! { |v| break if v == :c; v.succ }; p h`,
"{b: 1, c: 2, d: 4}\n"},
// Full transform: every key succ'd; the seen-set keeps a rewritten slot
// from being deleted by a later key (exercises the "seen" true branch).
{`h = {a: 1, b: 2, c: 3, d: 4}; h.transform_keys! { |v| v.succ }; p h`,
"{b: 1, c: 2, d: 3, e: 4}\n"},
// Mapping argument moves a key in place, keeping its original position.
{`h = {a: 1, b: 2}; h.transform_keys!({a: :x}); p h`, "{x: 1, b: 2}\n"},
{`h = {a: 1, b: 2, c: 3}; h.transform_keys!({b: :z}); p h`, "{a: 1, z: 2, c: 3}\n"},
// All keys map onto one: last value wins, single entry.
{`h = {a: 1, b: 2}; h.transform_keys! { |_| :z }; p h`, "{z: 2}\n"},
// break on the very first key leaves the hash unchanged.
{`h = {a: 1, b: 2, c: 3}; h.transform_keys! { |v| break if v == :a; v.succ }; p h`,
"{a: 1, b: 2, c: 3}\n"},
// No block and no mapping yields an Enumerator.
{`p({a: 1}.transform_keys!.class)`, "Enumerator\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
}
}