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
19 changes: 15 additions & 4 deletions internal/vm/data.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package vm

import (
"fmt"
"reflect"
"strings"

"github.com/go-embedded-ruby/ruby/internal/object"
)

// anonClassRepr renders an anonymous Data subclass the way MRI does inside a
// recursive Data #inspect sentinel — "#<Class:0x…>" with the class's identity
// address — so a self-referential anonymous Data prints "#<data #<Class:0x…>:...>"
// (matching MRI's rb_class_name fallback). It is only ever called for a Data
// class, which is always a Class (never a module).
func anonClassRepr(c *RClass) string {
return fmt.Sprintf("#<Class:0x%016x>", reflect.ValueOf(c).Pointer())
}

// dataDef records the member layout of a Data subclass minted by Data.define.
// Unlike structDef there is no keyword-init tri-state: a Data class always
// accepts both positional and keyword construction. It is stored on the RClass
Expand Down Expand Up @@ -413,7 +424,7 @@ func dataToHPair(vm *VM, res object.Value) (object.Value, object.Value) {
}
}
if !ok {
raise("TypeError", "wrong element type %s (expected array)", classNameOf(res))
raise("TypeError", "wrong element type %s (expected array)", vm.classOf(res).name)
}
}
if len(arr.Elems) != 2 {
Expand All @@ -430,15 +441,15 @@ func (vm *VM) dataInspect(o *RObject) string {
names := dataDefOf(o.class).names
if !object.ReprEnter(o) {
nm := o.class.name
if nm == "" {
nm = o.class.ToS() // anonymous: #<Class …>
if !namedThroughNesting(o.class) {
nm = anonClassRepr(o.class) // anonymous / non-permanent path: #<Class:0x…>
}
return "#<data " + nm + ":...>"
}
defer object.ReprLeave(o)
var b strings.Builder
b.WriteString("#<data")
if o.class.name != "" { // the real class name, never #name (which may be overridden)
if namedThroughNesting(o.class) { // the real class name, never #name (which may be overridden)
b.WriteString(" ")
b.WriteString(o.class.name)
}
Expand Down
150 changes: 120 additions & 30 deletions internal/vm/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ func setupStruct(vm *VM) {
}
return object.NilV
}
// keyword_init unset (Ruby 3.2 auto-detect): a lone Hash argument whose keys
// are all Symbols naming members is taken as keyword initialisation. MRI
// decides this from the caller's keyword flag, which our calling convention
// does not carry to a native method, so we approximate it by the keys naming
// members — a Hash with any non-member or non-Symbol key stays a positional
// value (so Struct.new(:a).new(b: 1) still stores {b: 1} in member a).
if d.kwInit == kwUnset && len(a) == 1 && len(names) > 0 {
if h, ok := a[0].(*object.Hash); ok && structHashAllMembers(h, names) {
for i, nm := range names {
if v, present := h.Get(object.Symbol(nm)); present {
o.structVals[i] = v
} else {
o.structVals[i] = object.NilV
}
}
return object.NilV
}
}
if len(a) > len(names) {
raise("ArgumentError", "struct size differs")
}
Expand All @@ -209,6 +227,9 @@ func setupStruct(vm *VM) {
}
return object.NilV
})
// MRI keeps Struct#initialize private (Car.private_instance_methods includes
// :initialize).
vm.setInstanceVisibility(cStruct, "initialize", visPrivate)

cStruct.define("members", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return structMemberSyms(structDefOf(self.(*RObject).class).names)
Expand All @@ -231,7 +252,11 @@ func setupStruct(vm *VM) {
names := structDefOf(self.(*RObject).class).names
vals := structValues(self)
for i, nm := range names {
vm.callBlock(blk, []object.Value{object.Symbol(nm), vals[i]})
// MRI yields ONE two-element Array per pair (like Hash#each): a `|k, v|`
// block auto-splats it, while a single-parameter block (or an Enumerator
// consumer, e.g. each_pair.map { |var| var }) receives the [name, value]
// Array whole.
vm.callBlock(blk, []object.Value{object.NewArrayFromSlice([]object.Value{object.Symbol(nm), vals[i]})})
}
return self
})
Expand Down Expand Up @@ -293,14 +318,10 @@ func setupStruct(vm *VM) {
for i, nm := range names {
if blk != nil {
res := vm.callBlock(blk, []object.Value{object.Symbol(nm), vals[i]})
pair, ok := res.(*object.Array)
if !ok {
raise("TypeError", "wrong element type %s (expected array)", classNameOf(res))
}
if len(pair.Elems) != 2 {
raise("ArgumentError", "element has wrong array length (expected 2, was %d)", len(pair.Elems))
}
h.Set(pair.Elems[0], pair.Elems[1])
// MRI coerces the returned pair with #to_ary (never #to_a); a non-Array,
// non-to_ary object is a TypeError naming the object's real class.
key, val := dataToHPair(vm, res)
h.Set(key, val)
} else {
h.Set(object.Symbol(nm), vals[i])
}
Expand All @@ -323,7 +344,7 @@ func setupStruct(vm *VM) {
}
keys, ok := a[0].(*object.Array)
if !ok {
raise("TypeError", "wrong argument type %s (expected Array)", classNameOf(a[0]))
raise("TypeError", "wrong argument type %s (expected Array or nil)", classNameOf(a[0]))
}
if len(keys.Elems) > len(names) {
return h // more keys than members: MRI returns {} without inspecting them
Expand Down Expand Up @@ -373,9 +394,19 @@ func setupStruct(vm *VM) {
}
v = vals[idx]
case object.Symbol:
v = vals[structNameIndex(string(k), names)]
// Unlike #[], #dig with a name that is not a member returns nil (treating
// the missing member as nil) rather than raising NameError.
if i, found := structFind(string(k), names); found {
v = vals[i]
} else {
v = object.NilV
}
case *object.String:
v = vals[structNameIndex(k.Str(), names)]
if i, found := structFind(k.Str(), names); found {
v = vals[i]
} else {
v = object.NilV
}
default:
raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(a[0]))
}
Expand Down Expand Up @@ -413,7 +444,7 @@ func setupStruct(vm *VM) {
vals := o.structVals
var b strings.Builder
b.WriteString("#<struct")
if o.class.name != "" { // the real class name, never #name (which may be overridden)
if namedThroughNesting(o.class) { // the real class name, never #name (which may be overridden)
b.WriteString(" ")
b.WriteString(o.class.name)
}
Expand Down Expand Up @@ -495,6 +526,24 @@ func (vm *VM) newStructClass(base *RClass, names []string, kwInit int8) *RClass
return sub
}

// namedThroughNesting reports whether c has a permanent classpath — its own name
// and every enclosing lexical namespace are named. MRI's Struct/Data #inspect
// (rb_class_path_cached) shows the class name only for such classes, omitting it
// for an anonymous class and for a class nested in an anonymous namespace (e.g.
// `class Foo < Struct.new(:a)` defined inside `Class.new`): the name our object
// model records ("Foo") is not the permanent path MRI would cache.
func namedThroughNesting(c *RClass) bool {
if c.name == "" {
return false
}
for p := c.lexParent; p != nil; p = p.lexParent {
if p.name == "" {
return false
}
}
return true
}

// hasInclude reports whether module m is already in c's include list.
func hasInclude(c, m *RClass) bool {
for _, inc := range c.includes {
Expand All @@ -505,6 +554,26 @@ func hasInclude(c, m *RClass) bool {
return false
}

// structHashAllMembers reports whether every key of a non-empty Hash is a Symbol
// naming a member — the shape that Struct#initialize (keyword_init unset) treats
// as keyword initialisation.
func structHashAllMembers(h *object.Hash, names []string) bool {
if len(h.Keys) == 0 {
return false
}
member := make(map[string]bool, len(names))
for _, nm := range names {
member[nm] = true
}
for _, k := range h.Keys {
sym, ok := k.(object.Symbol)
if !ok || !member[string(sym)] {
return false
}
}
return true
}

// structMemberSyms returns a fresh Array of the member names as Symbols.
func structMemberSyms(names []string) object.Value {
out := make([]object.Value, len(names))
Expand Down Expand Up @@ -600,7 +669,7 @@ func structDeconstructIndex(vm *VM, k object.Value, names []string) (int, bool)
iv := vm.send(k, "to_int", nil, nil)
i, ok := iv.(object.Integer)
if !ok {
raise("TypeError", "can't convert %s into Integer", classNameOf(k))
raise("TypeError", "can't convert %s into Integer", vm.classOf(k).name)
}
return structPos(int(i), len(names))
}
Expand Down Expand Up @@ -678,29 +747,50 @@ func (vm *VM) structEqual(a, b object.Value, eql bool, seen map[[2]*RObject]bool
return true
}

// structHash combines the struct class identity with each member's hash. A cycle
// guard contributes a fixed sentinel for a re-entered struct so a self- or
// mutually-recursive struct hashes without looping.
func (vm *VM) structHash(o *RObject, seen map[*RObject]bool) int64 {
if seen[o] {
return 0x27d4eb2f
// structHash combines the struct class identity with each member's hash. When the
// member graph contains a cycle (a self- or mutually-recursive struct) the whole
// hash collapses to a class-only value: MRI's rb_struct_hash runs under
// rb_exec_recursive_outer, which on detecting recursion unwinds to the OUTERMOST
// struct and returns a hash computed from its class alone (members skipped). That
// makes every recursive struct of a class hash alike — necessary because such
// structs are #eql? by short-circuiting on the shared cyclic member, and #eql?
// objects must hash equal (see core/struct/hash_spec "recursive structs").
func (vm *VM) structHash(o *RObject, _ map[*RObject]bool) int64 {
if h, recursed := vm.structHashWalk(o, map[*RObject]bool{}); !recursed {
return h
}
if seen == nil {
seen = map[*RObject]bool{}
return structClassOnlyHash(o.class)
}

// structHashWalk folds the class identity with each member's hash, tracking the
// set of structs on the current path so a back-edge to one of them is reported as
// recursion (recursed=true) rather than followed into a loop. The fold value is
// meaningful only when recursed is false.
func (vm *VM) structHashWalk(o *RObject, path map[*RObject]bool) (int64, bool) {
if path[o] {
return 0, true
}
seen[o] = true
defer delete(seen, o)
h := int64(reflect.ValueOf(o.class).Pointer())
path[o] = true
defer delete(path, o)
h := structClassOnlyHash(o.class)
for _, v := range o.structVals {
var hv int64
if so, ok := v.(*RObject); ok && hasMemberDef(so.class) {
hv = vm.structHash(so, seen)
hv, recursed := vm.structHashWalk(so, path)
if recursed {
return 0, true
}
h = h*31 + hv
} else {
hv = vm.hashValue(v)
h = h*31 + vm.hashValue(v)
}
h = h*31 + hv
}
return h
return h, false
}

// structClassOnlyHash is the hash contributed by a Struct/Data class's identity —
// the base of the member fold, and also the whole hash of a recursive struct.
func structClassOnlyHash(c *RClass) int64 {
return int64(reflect.ValueOf(c).Pointer())
}

// validConstName reports whether s is a valid (ASCII) Ruby constant name: an
Expand Down
2 changes: 1 addition & 1 deletion internal/vm/struct_conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestStructConformanceErrors(t *testing.T) {

// deconstruct_keys errors.
{`S = Struct.new(:a); S.new(1).deconstruct_keys`, "wrong number of arguments (given 0, expected 1)"},
{`S = Struct.new(:a); S.new(1).deconstruct_keys(5)`, "wrong argument type Integer (expected Array)"},
{`S = Struct.new(:a); S.new(1).deconstruct_keys(5)`, "wrong argument type Integer (expected Array or nil)"},
{`S = Struct.new(:a, :b); S.new(1, 2).deconstruct_keys([0, []])`, "no implicit conversion of Array into Integer"},
{`S = Struct.new(:a, :b); k = Object.new; def k.to_int; "x"; end; S.new(1, 2).deconstruct_keys([k])`, "can't convert Object into Integer"},
}
Expand Down
83 changes: 83 additions & 0 deletions internal/vm/struct_residuals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package vm_test

import (
"strings"
"testing"
)

// TestStructDataResiduals locks in the Struct/Data conformance residuals fixed in
// this change: keyword_init-unset auto-detect, #each_pair pair yielding, #dig on a
// missing member, #to_h block coercion, #inspect naming for anonymous-namespace
// classes, and the class-only hash of a recursive struct. Every expectation is
// byte-for-byte against MRI Ruby 4.0.
func TestStructDataResiduals(t *testing.T) {
cases := []struct{ src, want string }{
// Struct#initialize is private (MRI).
{`S = Struct.new(:a); p S.private_instance_methods(true).include?(:initialize)`, "true\n"},

// #each_pair yields ONE [name, value] Array per pair, so an Enumerator
// consumer (to_a / single-parameter map) gets the pair whole.
{`S = Struct.new(:a, :b); p S.new(1, 2).each_pair.to_a`, "[[:a, 1], [:b, 2]]\n"},
{`S = Struct.new(:a, :b); p S.new(1, 2).each_pair.map { |v| v }`, "[[:a, 1], [:b, 2]]\n"},

// #dig with a missing member name returns nil (unlike #[], which raises).
{`S = Struct.new(:a); p S.new(1).dig(:zzz)`, "nil\n"},
{`S = Struct.new(:a); p S.new(1).dig("zzz")`, "nil\n"},

// keyword_init unset auto-detect (Ruby 3.2): a lone Hash whose keys all name
// members is keyword init; any non-member or non-Symbol key keeps it
// positional (matching MRI for an explicit-brace hash).
{`S = Struct.new(:a, :b); p S.new(a: 1, b: 2).to_a`, "[1, 2]\n"},
{`S = Struct.new(:a, :b); p S.new(a: 5).to_a`, "[5, nil]\n"},
{`S = Struct.new(:a); p S.new({b: 1}).a`, "{b: 1}\n"},
{`S = Struct.new(:a); p S.new({0 => 1}).a`, "{0 => 1}\n"},
{`S = Struct.new(:a); p S.new({}).a`, "{}\n"},

// #to_h block coerces the returned pair with #to_ary (never #to_a).
{`S = Struct.new(:a); class Ary1; def to_ary; [:k, 9]; end; end; p S.new(1).to_h { |k, v| Ary1.new }`, "{k: 9}\n"},

// #to_s / #inspect: name shown only for a permanent classpath — omitted for
// an anonymous class or one nested in an anonymous namespace.
{`puts Struct.new(:a).new("").to_s`, "#<struct a=\"\">\n"},
{`S = Struct.new(:a); puts S.new("x").to_s`, "#<struct S a=\"x\">\n"},
{`module NS1; B = Struct.new(:a); end; puts NS1::B.new("z").to_s`, "#<struct NS1::B a=\"z\">\n"},
{`m = Module.new; m.module_eval "Foo = Struct.new(:a)"; puts m::Foo.new("").to_s`, "#<struct a=\"\">\n"},

// Recursive struct hashes collapse to a class-only value (so #eql? recursive
// structs hash alike); non-recursive nested structs still fold member hashes.
{`R = Struct.new(:a, :b); x = R.new(1, 2); y = R.new(1, 2); x[:a] = x; y[:a] = x; p x.hash == y.hash`, "true\n"},
{`R = Struct.new(:a); o1 = R.new(R.new(1)); o2 = R.new(R.new(2)); p o1.hash == o2.hash`, "false\n"},

// Data#to_s naming and recursive sentinel.
{`M = Data.define(:x); puts M.new(x: 1).to_s`, "#<data M x=1>\n"},
{`M = Data.define(:x); k = Class.new(M); puts k.new(x: 1).to_s`, "#<data x=1>\n"},
{`M = Data.define(:x); a = M.allocate; a.send(:initialize, x: a); puts a.to_s`, "#<data M x=#<data M:...>>\n"},
// Recursive anonymous Data renders the member class as #<Class:0x…>.
{`M = Data.define(:x); k = Class.new(M); b = k.allocate; b.send(:initialize, x: b); p !!(b.to_s =~ /\A#<data x=#<data #<Class:0x[0-9a-f]+>:\.\.\.>>\z/)`, "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)
}
}
}

// TestStructDataResidualErrors covers the TypeError paths whose message wording or
// class-naming this change corrected.
func TestStructDataResidualErrors(t *testing.T) {
cases := []struct{ src, want string }{
// deconstruct_keys of a non-Array, non-nil argument names "Array or nil".
{`Struct.new(:a).new(1).deconstruct_keys("x")`, "wrong argument type String (expected Array or nil)"},
// A #to_int that returns a non-Integer names the key's real class.
{`class KK2; def to_int; "x"; end; end; Struct.new(:a, :b).new(1, 2).deconstruct_keys([KK2.new])`, "can't convert KK2 into Integer"},
// #to_h block pair that is neither Array nor #to_ary-able names the real class.
{`Struct.new(:a).new(1).to_h { |k, v| Object.new }`, "wrong element type Object (expected array)"},
{`Data.define(:a).new(a: 1).to_h { |k, v| Object.new }`, "wrong element type Object (expected array)"},
}
for _, c := range cases {
err := runErr(t, c.src)
if err == nil || !strings.Contains(err.Error(), c.want) {
t.Errorf("src=%q: got err=%v, want containing %q", c.src, err, c.want)
}
}
}