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
4 changes: 4 additions & 0 deletions internal/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ type Hash struct {
// Frozen marks the Hash as frozen (Hash#freeze); an in-place mutator raises
// FrozenError. Default false, so ordinary hashes are unaffected.
Frozen bool
// Ruby2Keywords flags a hash built by Hash.ruby2_keywords_hash, marking it as
// a keyword-argument hash for `*args`-style forwarding (reported by
// Hash.ruby2_keywords_hash?). Default false.
Ruby2Keywords bool
}

// strKey is the comparable map key for a Ruby String, distinct from a Symbol of
Expand Down
10 changes: 10 additions & 0 deletions internal/vm/activesupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,13 @@ func hashArg(v object.Value) *object.Hash {
}
return h
}

// hashArgOrRaise returns v as a *Hash or raises the "wrong argument type X
// (expected Hash)" TypeError that Hash.ruby2_keywords_hash(?) reports.
func hashArgOrRaise(vm *VM, v object.Value) *object.Hash {
h, ok := v.(*object.Hash)
if !ok {
raise("TypeError", "wrong argument type %s (expected Hash)", vm.classOf(v).name)
}
return h
}
29 changes: 29 additions & 0 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,24 @@ func (vm *VM) bootstrap() {
}
return h
}}
// 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.
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])
out := object.NewHash()
for _, k := range h.Keys {
v, _ := h.Get(k)
out.Set(k, v)
}
out.Ruby2Keywords = true
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)
}}
// #[] reads args[0] and returns a stored/default value; #[]= copies element
// values into the hash. Neither retains the args slice, so both take the
// no-copy OpSend fast path (defineNR).
Expand Down Expand Up @@ -3959,6 +3977,17 @@ func (vm *VM) bootstrap() {
}
vm.cHash.define("has_value?", hashHasValue)
vm.cHash.define("value?", hashHasValue)
// Hash#key(value): the first key whose value equals value (by ==), else nil.
vm.cHash.define("key", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
h := self.(*object.Hash)
for _, k := range h.Keys {
v, _ := h.Get(k)
if valueEqual(v, args[0]) {
return k
}
}
return object.NilV
})
// select/reject return a Hash (unlike Enumerable's Array forms), so they are
// native rather than inherited.
vm.cHash.define("select", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value {
Expand Down
29 changes: 29 additions & 0 deletions internal/vm/hash_residuals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,32 @@ func TestHashCompareByIdentity(t *testing.T) {
}
}
}

// TestHashKeyAndRuby2Keywords covers Hash#key (reverse value lookup) and the
// Hash.ruby2_keywords_hash / ruby2_keywords_hash? pair, asserted against MRI
// Ruby 4.0.6.
func TestHashKeyAndRuby2Keywords(t *testing.T) {
cases := []struct{ src, want string }{
{`p({a: 1, b: 2, c: 1}.key(1))`, ":a\n"},
{`p({a: 1}.key(9))`, "nil\n"},
{`p({"x" => 5}.key(5))`, "\"x\"\n"},
{`kh = Hash.ruby2_keywords_hash({a: 1}); p [kh, Hash.ruby2_keywords_hash?(kh)]`, "[{a: 1}, true]\n"},
{`p Hash.ruby2_keywords_hash?({a: 1})`, "false\n"},
// The original hash is unchanged; only the returned copy carries the flag.
{`h = {a: 1}; Hash.ruby2_keywords_hash(h); p Hash.ruby2_keywords_hash?(h)`, "false\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
errs := []struct{ src, want string }{
{`Hash.ruby2_keywords_hash?(5)`, "wrong argument type Integer (expected Hash)"},
{`Hash.ruby2_keywords_hash("x")`, "wrong argument type String (expected Hash)"},
}
for _, c := range errs {
if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) {
t.Errorf("src=%q err=%v, want substring %q", c.src, err, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15810
15820