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/vm/prelude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __compare(other)
end
cmp
end
private :__compare # an internal helper, not part of Comparable's public API

def <(other)
__compare(other) < 0
Expand Down Expand Up @@ -142,6 +143,9 @@ def __enum_int_arg(n)
raise RangeError, "bignum too big to convert into 'long'" if v > 9223372036854775807 || v < -9223372036854775808
v
end
# These are internal iteration/coercion helpers, not part of Enumerable's
# public API (MRI does not expose them).
private :__each_packed, :__pack, :__enum_int_arg

def to_a
r = []
Expand Down
20 changes: 14 additions & 6 deletions internal/vm/prelude_frozen_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions internal/vm/prelude_private_helpers_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestPreludeInternalHelpersPrivate checks that the prelude's internal helper
// methods (Comparable#__compare and Enumerable's __each_packed/__pack/
// __enum_int_arg) are private, so they do not leak into the modules' public
// instance_methods as MRI's do not — while still working through the operators
// and iterators that call them. Verified against ruby 4.0.6.
func TestPreludeInternalHelpersPrivate(t *testing.T) {
cases := []struct{ src, want string }{
// Public instance method lists match MRI (no __-prefixed helpers).
{`p Comparable.instance_methods(false).sort`, `[:<, :<=, :==, :>, :>=, :between?, :clamp]`},
{`p Enumerable.instance_methods(false).grep(/^__/)`, `[]`},
// The helpers are private: not answered by respond_to?, no explicit-receiver call.
{`p 5.respond_to?(:__compare)`, `false`},
{`begin; 5.__compare(3); rescue NoMethodError => e; p e.class; end`, `NoMethodError`},
// ...but still reachable through send (private) and the operators that use them.
{`p 5.send(:__compare, 3)`, `1`},
{`p 5 < 10`, `true`},
{`p 3.clamp(1, 2)`, `2`},
// Enumerable iteration still works (it goes through the now-private helpers).
{`p (1..3).to_a`, `[1, 2, 3]`},
{`p [3, 1, 2].sort`, `[1, 2, 3]`},
{`p [1, 2, 3, 4].first(2)`, `[1, 2]`},
{`p [1, 2, 3].map { |x| x * 2 }`, `[2, 4, 6]`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}