diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 513f5de6..3dc2f66b 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -301,6 +301,11 @@ func (vm *VM) bootstrap() { } case *RClass: o.frozen = true + default: + // Bound/UnboundMethod (and any future boxed value) freeze via state. + if b, ok := self.(boxed); ok { + b.state().frozen = true + } } return self }) @@ -9250,6 +9255,14 @@ func dupValue(v object.Value) object.Value { copy(dup.structVals, x.structVals) } return dup + case *BoundMethod: + cp := *x + cp.methodValueState = copyMethodState(x.methodValueState) + return &cp + case *UnboundMethod: + cp := *x + cp.methodValueState = copyMethodState(x.methodValueState) + return &cp default: return v } @@ -9277,6 +9290,10 @@ func isFrozen(v object.Value) bool { case *RClass: return x.frozen } + // Bound/UnboundMethod track their frozen flag in the embedded state. + if b, ok := v.(boxed); ok { + return b.state().frozen + } return false } diff --git a/internal/vm/method.go b/internal/vm/method.go index 189c0a3c..4935e3b5 100644 --- a/internal/vm/method.go +++ b/internal/vm/method.go @@ -14,6 +14,7 @@ type BoundMethod struct { // vm is the interpreter the method belongs to, kept so ToS can render the // receiver's class and the method's parameters (MRI's # form). vm *VM + methodValueState } // newBoundMethod builds a Method bound to recv, resolved under name to m. diff --git a/internal/vm/method_reflect2.go b/internal/vm/method_reflect2.go index b8841fdd..66d4a12c 100644 --- a/internal/vm/method_reflect2.go +++ b/internal/vm/method_reflect2.go @@ -53,10 +53,14 @@ func (vm *VM) registerMethodReflect2() { return object.Symbol(methodOriginalName(self.(*UnboundMethod).m)) }) - // #super_method. + // #super_method. The super search follows the method's ORIGINAL name — an + // alias (alias_method :meow, :derp) reaches super along the definition it + // copied, not its alias name — so `super` from an aliased or a + // visibility-changed inherited method finds the same ancestor MRI's `super` + // would (see methodOriginalName). vm.cMethod.define("super_method", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { b := self.(*BoundMethod) - sm := vm.superMethodAfter(vm.dispatchAncestors(b.recv), b.m.owner, b.name) + sm := vm.superMethodAfter(vm.dispatchAncestors(b.recv), b.m.owner, methodOriginalName(b.m)) if sm == nil { return object.NilV } @@ -64,11 +68,16 @@ func (vm *VM) registerMethodReflect2() { }) cUnbound.define("super_method", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { u := self.(*UnboundMethod) - sm := vm.superMethodAfter(vm.ancestors(u.owner), u.owner, u.name) + // Walk the ancestors of the class the UnboundMethod was extracted from + // (u.origin), not those of its defining module: a module owner's own + // ancestor list stops at the module, so a `super` that must cross into + // another module included in the same class (module B's derp calling into + // module A's derp, both included in class C) would otherwise be lost. + sm := vm.superMethodAfter(vm.ancestors(u.origin), u.owner, methodOriginalName(u.m)) if sm == nil { return object.NilV } - return &UnboundMethod{name: u.name, owner: sm.owner, m: sm, vm: vm} + return &UnboundMethod{name: u.name, owner: sm.owner, m: sm, vm: vm, origin: u.origin} }) // #to_s and its alias #inspect (a shared record, so @@ -289,15 +298,9 @@ func localName(is *bytecode.ISeq, slot int) string { // "(orig)" annotation appears only when the lookup name differs from the original // name, and the location suffix only when the source file is known. func (vm *VM) formatCallableString(kind string, recv object.Value, name string, m *Method) string { - head, sep := m.owner.name, "#" + head, sep := m.owner.ToS(), "#" // UnboundMethod: the defining class/module, inspected if kind == "Method" { - if m.owner.name == "" { - head, sep = vm.send(recv, "inspect", nil, nil).ToS(), "." - } else if recvClass := vm.classOf(recv); recvClass == m.owner { - head = recvClass.name - } else { - head = recvClass.name + "(" + m.owner.name + ")" - } + head, sep = vm.methodHead(recv, name, m) } disp := name if orig := methodOriginalName(m); orig != name { @@ -310,6 +313,56 @@ func (vm *VM) formatCallableString(kind string, recv object.Value, name string, return s + ">" } +// methodHead renders the "receiver#owner" (or "receiver.owner") portion of a +// bound Method's # form, and the separator (# or .) that follows it, +// matching MRI. A class/module receiver is shown through its metaclass: +// - a genuine class method (def self.m — reached through the singleton-method +// table) reads as `Recv.m`, with `(Owner)` when inherited: `Child(Parent).m`; +// - any other method reaching the class object (an included/extended module +// method such as Module#include, a Kernel method) reads as +// `#(Owner)#m`. +// +// An ordinary object shows its class, with `(Owner)` when the method is defined +// in an ancestor; a per-object singleton method (def obj.m) shows the receiver's +// own inspect with a `.` separator. +func (vm *VM) methodHead(recv object.Value, name string, m *Method) (head, sep string) { + owner := m.owner + if cls, ok := recv.(*RClass); ok { + if sm := lookupOwnClassMethod(cls, name); sm == m { + head = cls.ToS() + if owner != cls { + head += "(" + owner.ToS() + ")" + } + return head, "." + } + return "#(" + owner.ToS() + ")", "#" + } + if owner.isSingleton && owner.metaOf == nil { + // A per-object singleton method (def obj.m): head is the receiver's inspect. + return vm.send(recv, "inspect", nil, nil).ToS(), "." + } + recvClass := vm.classOf(recv) + if recvClass == owner { + return recvClass.ToS(), "#" + } + return recvClass.ToS() + "(" + owner.ToS() + ")", "#" +} + +// lookupOwnClassMethod resolves name among the genuine class (singleton) methods +// defined with `def self.name` — the smethods tables walked up the super chain — +// but NOT modules mixed into a metaclass via extend. It distinguishes, for +// # rendering, a real class method (shown as `Recv.name`) from a +// module method that merely reaches the class object (shown through its +// metaclass as `#(Owner)#name`). +func lookupOwnClassMethod(c *RClass, name string) *Method { + for ; c != nil; c = c.super { + if m, ok := c.smethods[name]; ok { + return m + } + } + return nil +} + // formatParamList renders a method's parameters as MRI's # signature // fragment, e.g. "(a, b=..., *c, d, e:, f: ..., **g, &blk)". A native method, // whose parameters are unknown, renders as "(*)". diff --git a/internal/vm/method_residuals_wave12_test.go b/internal/vm/method_residuals_wave12_test.go new file mode 100644 index 00000000..a8e7b1c3 --- /dev/null +++ b/internal/vm/method_residuals_wave12_test.go @@ -0,0 +1,278 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestMethodInspectHeads covers the receiver/owner head rendering of +// Method#inspect (vm.methodHead) across MRI's cases: a genuine class method +// (Recv.name / Child(Parent).name), a module method reaching a class object via +// its metaclass (#(Owner)#name), an ordinary instance method +// (Recv#name and Recv(Owner)#name), an anonymous class instance, and a +// per-object singleton method. +func TestMethodInspectHeads(t *testing.T) { + tests := []struct { + name, src, want string + }{ + { + "def_self_class_method", + `class WHClsP; def self.cm; end; end +puts WHClsP.method(:cm).inspect.start_with?("#(Module)#include")`, + "true\n", + }, + { + "extended_module_method_on_class_via_metaclass", + `m = Module.new { def whbar; end } +c = Class.new +c.extend(m) +s = c.method(:whbar).inspect +puts s.start_with?("#(#{m.inspect})#whbar")`, + "true\n", + }, + { + "ordinary_instance_method_same_class", + `class WHOrd; def foo; end; end +puts WHOrd.new.method(:foo).inspect.start_with?("#.foo")`, + "true\ntrue\n", + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + }) + } +} + +// TestUnboundMethodInspectHead covers UnboundMethod#inspect head rendering: a +// named module owner inspects to its name, a per-object singleton owner inspects +// to its # form. +func TestUnboundMethodInspectHead(t *testing.T) { + tests := []struct { + name, src, want string + }{ + { + "named_module_owner", + `module WHUMod; def bar; end; end +class WHUInc; include WHUMod; end +puts WHUInc.instance_method(:bar).inspect.start_with?("# form). vm *VM + methodValueState } func (u *UnboundMethod) ToS() string { @@ -40,7 +81,7 @@ func (vm *VM) registerReflection() { if m == nil || m.undefined { raise("NameError", "undefined method '%s' for class '%s'", name, mod.name) } - return &UnboundMethod{name: name, owner: m.owner, m: m, vm: vm} + return &UnboundMethod{name: name, owner: m.owner, m: m, vm: vm, origin: mod} }) // Module#public_instance_method(:m): like #instance_method, but the resolved @@ -59,7 +100,7 @@ func (vm *VM) registerReflection() { } raise("NameError", "method '%s' for class '%s' is %s", name, mod.name, kind) } - return &UnboundMethod{name: name, owner: m.owner, m: m, vm: vm} + return &UnboundMethod{name: name, owner: m.owner, m: m, vm: vm, origin: mod} }) // Module#singleton_class?: true when the receiver is a singleton class — a @@ -168,10 +209,12 @@ func (vm *VM) registerReflection() { return sc }) - // Method#unbind → UnboundMethod. - vm.cMethod.define("unbind", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + // Method#unbind → UnboundMethod. The extraction origin is the class the bound + // method dispatched on (its singleton class when it has one), so a later + // #super_method walks the same ancestor chain the original send would. + vm.cMethod.define("unbind", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { b := self.(*BoundMethod) - return &UnboundMethod{name: b.name, owner: b.m.owner, m: b.m, vm: vm} + return &UnboundMethod{name: b.name, owner: b.m.owner, m: b.m, vm: vm, origin: vm.dispatchClass(b.recv)} }) }