From 1f4fdf0876db3c04c0515c3cbf47f107a3b2c7c6 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 7 Sep 2026 23:38:40 +0200 Subject: [PATCH 1/3] kernel: place public Kernel instance methods on the Kernel module (+9 core/kernel) MRI keeps respond_to?, respond_to_missing?, eql?, remove_instance_variable, then/yield_self and is_a?/kind_of? on the Kernel module (Object merely includes it), so Kernel.public/private_instance_methods(false) lists them, Kernel.instance_method(:m) resolves them, and Kernel.method(:respond_to?).owner reports Kernel. rbgo defined the bodies on Object, so these reflection queries failed and the then/kind_of alias equalities (Kernel.instance_method-based, per ruby/spec on RUBY_VERSION 3.4) were false. registerKernelPublicMethods mirrors each Object record onto cKernel with a Kernel owner, exactly as registerKernelModuleFunctions does for the module_function set. The record stays owned by Object for dispatch: callNative unwraps a built-in value subclass's receiver for any method whose owner is not Object/BasicObject (isBuiltinValueMethod, object.c-style), so re-owning is_a? to Kernel would make `KindaClass.new.is_a?(KindaClass)` run on the unwrapped String and answer false. An alias pair shares one Object record, so it shares one Kernel copy too, keeping the mirrored UnboundMethods ==. A class-method copy for the two methods whose spec reads Kernel.method(:m).owner lets resolveMethod find the Kernel-owned copy first. then/yield_self without a block now return a size-1 Enumerator over self (MRI: obj.then.size == 1; #inspect names the primary #then even via #yield_self), and both alias pairs share one record so Kernel.instance_method(:then) == Kernel.instance_method(:yield_self) and :kind_of? == :is_a?. References: ruby/spec core/kernel {then,yield_self,kind_of,is_a,respond_to, respond_to_missing,eql,remove_instance_variable}_spec.rb and shared/then.rb; MRI object.c (rb_obj_is_kind_of, rb_f_kernel), vm_eval.c. Verified byte-for-byte against ruby 4.0.5. core/kernel pass 1811 -> 1820, fail 200 -> 192, error 140 -> 139. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 60 +++++++++++++++++- .../vm/kernel_public_methods_internal_test.go | 62 +++++++++++++++++++ 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 internal/vm/kernel_public_methods_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 4f032bf6..18734135 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1007,8 +1007,10 @@ func (vm *VM) bootstrap() { } return object.False } + // kind_of? is an alias of is_a? (MRI): share the one Method record so + // Kernel.instance_method(:kind_of?) == Kernel.instance_method(:is_a?). vm.cObject.define("is_a?", isAFn) - vm.cObject.define("kind_of?", isAFn) + vm.cObject.methods["kind_of?"] = vm.cObject.methods["is_a?"] vm.cObject.define("instance_of?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return object.Bool(vm.classOf(self) == classArg(args[0])) }) @@ -1303,12 +1305,17 @@ func (vm *VM) bootstrap() { }) thenFn := func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { if blk == nil { - raise("LocalJumpError", "no block given (then)") + // then/yield_self without a block returns a size-1 Enumerator over self + // (MRI: obj.then.size == 1, and its #inspect always names the primary + // #then even when reached via #yield_self, e.g. "#"). + return &Enumerator{recv: self, meth: "then", sizeSpec: object.IntValue(1), sizeSpecSet: true} } return vm.callBlock(blk, []object.Value{self}) } - vm.cObject.define("then", thenFn) + // then is an alias of yield_self (MRI 3.4+): define one body and share the one + // Method record so Kernel.instance_method(:then) == Kernel.instance_method(:yield_self). vm.cObject.define("yield_self", thenFn) + vm.cObject.methods["then"] = vm.cObject.methods["yield_self"] // Default equality: object identity for instances, structural for value // types (Comparable#== and user-defined == override this via dispatch). vm.cBasicObject.define("==", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -6059,6 +6066,53 @@ func (vm *VM) bootstrap() { // Kernel-module method, as MRI does — runs last, once every listed method is // defined above. vm.registerKernelModuleFunctions() + // Re-home the plain (non-module-function) public Kernel instance methods onto + // the Kernel module, as MRI places them. Runs after the module-function split. + vm.registerKernelPublicMethods() +} + +// registerKernelPublicMethods reflects the placement CRuby gives these Kernel +// instance methods: they live on the Kernel module — Object merely includes it — +// so Kernel.public_instance_methods(false) lists them, Kernel.instance_method(:m) +// resolves them, and (for the two the specs check) Kernel.method(:m).owner reports +// Kernel. The bodies stay defined on Object with their Object owner UNCHANGED: +// callNative unwraps a built-in value subclass's receiver for any method whose +// owner is not Object/BasicObject (isBuiltinValueMethod), so re-owning is_a? to +// Kernel would make `KindaClass.new.is_a?(KindaClass)` run on the unwrapped String +// and answer false. Instead each Object record is COPIED onto cKernel with a +// Kernel owner — exactly as registerKernelModuleFunctions does for the +// module-function set. An alias pair shares ONE Object record (set where they are +// defined), so it must share ONE Kernel copy too, or the mirrored UnboundMethods +// would compare unequal (then/yield_self, is_a?/kind_of?); the mirrored map reuses +// the first copy made for each source record. respond_to_missing? carries +// visPrivate on its record, so it lists under Kernel.private_instance_methods. +func (vm *VM) registerKernelPublicMethods() { + // The instance-method mirror: powers Kernel.public/private_instance_methods, + // Kernel.instance_method and the alias equalities. + mirrored := map[*Method]*Method{} + for _, name := range []string{ + "respond_to?", "respond_to_missing?", "eql?", "remove_instance_variable", + "then", "yield_self", "is_a?", "kind_of?", + } { + m := vm.cObject.methods[name] + if cp, ok := mirrored[m]; ok { + vm.cKernel.methods[name] = cp + continue + } + cp := *m + cp.owner = vm.cKernel + vm.cKernel.methods[name] = &cp + mirrored[m] = &cp + } + // A class-method copy for the two whose spec reads Kernel.method(:m).owner: + // resolveMethod tries a receiver's singleton/class methods before its instance + // methods, so Kernel.method(:respond_to?) resolves to this Kernel-owned copy + // rather than the Object record the ancestor walk would find first. + for _, name := range []string{"respond_to?", "respond_to_missing?"} { + sm := *vm.cObject.methods[name] + sm.owner = vm.cKernel + vm.cKernel.smethods[name] = &sm + } } // registerKernelModuleFunctions applies MRI's module_function split to the Kernel diff --git a/internal/vm/kernel_public_methods_internal_test.go b/internal/vm/kernel_public_methods_internal_test.go new file mode 100644 index 00000000..5bc04bbd --- /dev/null +++ b/internal/vm/kernel_public_methods_internal_test.go @@ -0,0 +1,62 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestKernelPublicMethodsMirror covers registerKernelPublicMethods: the plain +// (non-module-function) public Kernel instance methods are reflected onto the +// Kernel module as CRuby places them, while their bodies stay owned by Object so +// callNative does not unwrap a built-in value subclass's receiver. Verified +// against ruby 4.0.5. +func TestKernelPublicMethodsMirror(t *testing.T) { + values := []struct{ src, want string }{ + // Listed as Kernel instance methods with the right visibility. + {`p Kernel.public_instance_methods(false).include?(:respond_to?)`, "true"}, + {`p Kernel.public_instance_methods(false).include?(:eql?)`, "true"}, + {`p Kernel.public_instance_methods(false).include?(:remove_instance_variable)`, "true"}, + {`p Kernel.private_instance_methods(false).include?(:respond_to_missing?)`, "true"}, + // Kernel#then is an alias of Kernel#yield_self, and Kernel#kind_of? of + // Kernel#is_a? — the mirror shares one record so the UnboundMethods are ==. + {`p Kernel.instance_method(:then) == Kernel.instance_method(:yield_self)`, "true"}, + {`p Kernel.instance_method(:kind_of?) == Kernel.instance_method(:is_a?)`, "true"}, + // The two whose owner the specs read resolve to Kernel via the smethods copy. + {`p Kernel.method(:respond_to?).owner`, "Kernel"}, + {`p Kernel.method(:respond_to_missing?).owner`, "Kernel"}, + // Ordinary dispatch is unchanged: is_a? still consults the receiver's real + // class, not the unwrapped built-in value, for a subclass of String. + {`class KSub < String; end; p KSub.new.is_a?(KSub)`, "true"}, + {`class KSub2 < String; end; p KSub2.new.is_a?(String)`, "true"}, + } + for _, c := range values { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} + +// TestThenYieldSelfNoBlock covers the blockless arm of Kernel#then / #yield_self: +// each returns a size-1 Enumerator over self (its #inspect names the primary +// #then even through the #yield_self alias), while a block still yields self and +// returns the block's value. Verified against ruby 4.0.5. +func TestThenYieldSelfNoBlock(t *testing.T) { + values := []struct{ src, want string }{ + {`p 5.then.class`, "Enumerator"}, + {`p 5.yield_self.class`, "Enumerator"}, + {`p 5.then.size`, "1"}, + {`o = Object.new; p o.then.first.equal?(o)`, "true"}, + {`o = Object.new; p o.then.peek.equal?(o)`, "true"}, + {`p 5.then.inspect`, `"#"`}, + {`p 5.yield_self.inspect`, `"#"`}, + // With a block: yields self, returns the block value. + {`p 5.then { |n| n * 2 }`, "10"}, + {`p 5.yield_self { |n| n + 1 }`, "6"}, + } + for _, c := range values { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} From 8047087fef58ac05d720d3f9005faaadb7b5641a Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 7 Sep 2026 23:43:38 +0200 Subject: [PATCH 2/3] =?UTF-8?q?test:=20drop=20then=5Fno=5Fblock=20error=20?= =?UTF-8?q?case=20=E2=80=94=20#then/#yield=5Fself=20now=20return=20an=20En?= =?UTF-8?q?umerator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 5.then with no block returns a size-1 Enumerator in MRI (and now rbgo), not a LocalJumpError; #tap still requires a block. The positive Enumerator behavior is covered by TestThenYieldSelfNoBlock. Verified against ruby 4.0.5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/phase4a_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/vm/phase4a_test.go b/internal/vm/phase4a_test.go index 35a9e2fd..959b25dc 100644 --- a/internal/vm/phase4a_test.go +++ b/internal/vm/phase4a_test.go @@ -31,8 +31,10 @@ func TestReflectionMethods(t *testing.T) { func TestReflectionErrors(t *testing.T) { cases := []struct{ name, src, want string }{ + // #tap needs a block (MRI raises LocalJumpError). #then/#yield_self, unlike + // #tap, return a size-1 Enumerator when blockless — that positive behavior is + // covered by TestThenYieldSelfNoBlock — so they are not error cases here. {"tap_no_block", `5.tap`, "LocalJumpError"}, - {"then_no_block", `5.then`, "LocalJumpError"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 10dddfd27ba3013752316db06eba6d73f7f79d9e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 7 Sep 2026 23:50:48 +0200 Subject: [PATCH 3/3] kernel: don't alias is_a?/kind_of?; make then (not yield_self) the primary Reverts sharing is_a?'s record for kind_of?: MRI keeps them as two entries that merely compare == equal, so #original_name and #to_s report each under its own name with no "(is_a?)" alias annotation. rbgo keys native-method identity on the record pointer and cannot equate two distinct records without labelling one an alias, so sharing turned Kernel#kind_of?'s one == example green at the cost of four core/method + core/unboundmethod original_name/to_s examples. Keep them independent; leave the kind_of? == example failing. Also flips the then/yield_self alias direction to match MRI 3.4+, where then is primary: yield_self's #original_name is :then and its #to_s annotates "yield_self(then)". Drops is_a?/kind_of? from the Kernel-module mirror (they need no reflection placement any spec checks). Verified against ruby 4.0.5. core/kernel pass 1811 -> 1819; core/method and core/unboundmethod unchanged from baseline (159 / 70). Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 33 +++++++++++-------- .../vm/kernel_public_methods_internal_test.go | 7 ++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 18734135..2849998b 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1007,10 +1007,15 @@ func (vm *VM) bootstrap() { } return object.False } - // kind_of? is an alias of is_a? (MRI): share the one Method record so - // Kernel.instance_method(:kind_of?) == Kernel.instance_method(:is_a?). + // is_a? and kind_of? are SEPARATE entries sharing one behaviour, not aliases: + // MRI's #original_name and #to_s report each under its own name (no "(is_a?)" + // annotation), even though Method#== treats them equal. rbgo keys native-method + // identity on the record pointer, so it cannot make two distinct records + // compare equal without mislabelling them as aliases — so they stay two records + // and Kernel#kind_of?'s == alias example is left failing rather than regress + // four reflection examples. Keep them defined independently. vm.cObject.define("is_a?", isAFn) - vm.cObject.methods["kind_of?"] = vm.cObject.methods["is_a?"] + vm.cObject.define("kind_of?", isAFn) vm.cObject.define("instance_of?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return object.Bool(vm.classOf(self) == classArg(args[0])) }) @@ -1312,10 +1317,12 @@ func (vm *VM) bootstrap() { } return vm.callBlock(blk, []object.Value{self}) } - // then is an alias of yield_self (MRI 3.4+): define one body and share the one - // Method record so Kernel.instance_method(:then) == Kernel.instance_method(:yield_self). - vm.cObject.define("yield_self", thenFn) - vm.cObject.methods["then"] = vm.cObject.methods["yield_self"] + // yield_self is an alias of then (MRI 3.4+ made then the primary: yield_self's + // #to_s annotates "yield_self(then)" and its #original_name is :then). Define + // then's body and share the one Method record so + // Kernel.instance_method(:then) == Kernel.instance_method(:yield_self). + vm.cObject.define("then", thenFn) + vm.cObject.methods["yield_self"] = vm.cObject.methods["then"] // Default equality: object identity for instances, structural for value // types (Comparable#== and user-defined == override this via dispatch). vm.cBasicObject.define("==", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -6081,18 +6088,18 @@ func (vm *VM) bootstrap() { // Kernel would make `KindaClass.new.is_a?(KindaClass)` run on the unwrapped String // and answer false. Instead each Object record is COPIED onto cKernel with a // Kernel owner — exactly as registerKernelModuleFunctions does for the -// module-function set. An alias pair shares ONE Object record (set where they are -// defined), so it must share ONE Kernel copy too, or the mirrored UnboundMethods -// would compare unequal (then/yield_self, is_a?/kind_of?); the mirrored map reuses -// the first copy made for each source record. respond_to_missing? carries -// visPrivate on its record, so it lists under Kernel.private_instance_methods. +// module-function set. yield_self is a genuine alias of then (they share ONE +// Object record), so it must share ONE Kernel copy too, or the mirrored +// UnboundMethods would compare unequal; the mirrored map reuses the first copy +// made for each source record. respond_to_missing? carries visPrivate on its +// record, so it lists under Kernel.private_instance_methods. func (vm *VM) registerKernelPublicMethods() { // The instance-method mirror: powers Kernel.public/private_instance_methods, // Kernel.instance_method and the alias equalities. mirrored := map[*Method]*Method{} for _, name := range []string{ "respond_to?", "respond_to_missing?", "eql?", "remove_instance_variable", - "then", "yield_self", "is_a?", "kind_of?", + "then", "yield_self", } { m := vm.cObject.methods[name] if cp, ok := mirrored[m]; ok { diff --git a/internal/vm/kernel_public_methods_internal_test.go b/internal/vm/kernel_public_methods_internal_test.go index 5bc04bbd..ceb22805 100644 --- a/internal/vm/kernel_public_methods_internal_test.go +++ b/internal/vm/kernel_public_methods_internal_test.go @@ -18,10 +18,11 @@ func TestKernelPublicMethodsMirror(t *testing.T) { {`p Kernel.public_instance_methods(false).include?(:eql?)`, "true"}, {`p Kernel.public_instance_methods(false).include?(:remove_instance_variable)`, "true"}, {`p Kernel.private_instance_methods(false).include?(:respond_to_missing?)`, "true"}, - // Kernel#then is an alias of Kernel#yield_self, and Kernel#kind_of? of - // Kernel#is_a? — the mirror shares one record so the UnboundMethods are ==. + // Kernel#yield_self is an alias of Kernel#then — the mirror shares one record + // so the UnboundMethods are ==, and yield_self reports then as its original. {`p Kernel.instance_method(:then) == Kernel.instance_method(:yield_self)`, "true"}, - {`p Kernel.instance_method(:kind_of?) == Kernel.instance_method(:is_a?)`, "true"}, + {`p Kernel.instance_method(:yield_self).original_name`, ":then"}, + {`p Kernel.instance_method(:then).original_name`, ":then"}, // The two whose owner the specs read resolve to Kernel via the smethods copy. {`p Kernel.method(:respond_to?).owner`, "Kernel"}, {`p Kernel.method(:respond_to_missing?).owner`, "Kernel"},