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
17 changes: 17 additions & 0 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions internal/vm/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 #<Method: …> form).
vm *VM
methodValueState
}

// newBoundMethod builds a Method bound to recv, resolved under name to m.
Expand Down
77 changes: 65 additions & 12 deletions internal/vm/method_reflect2.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,31 @@ 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
}
return vm.newBoundMethod(b.recv, b.name, sm)
})
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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 #<Method: …> 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
// `#<Class:Recv>(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 "#<Class:" + cls.ToS() + ">(" + 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
// #<Method: …> rendering, a real class method (shown as `Recv.name`) from a
// module method that merely reaches the class object (shown through its
// metaclass as `#<Class:Recv>(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 #<Method: …> signature
// fragment, e.g. "(a, b=..., *c, d, e:, f: ..., **g, &blk)". A native method,
// whose parameters are unknown, renders as "(*)".
Expand Down
Loading