[ruby/mutex_m] Avoid anonymous eval

It makes it hard to locate code when profiling etc.

https://github.com/ruby/mutex_m/commit/8760ab19ec
This commit is contained in:
Jean Boussier 2023-01-11 14:56:47 +01:00 committed by git
parent 0e21c9f57e
commit a8537eae2a
2 changed files with 26 additions and 7 deletions

View File

@ -44,13 +44,11 @@ module Mutex_m
Ractor.make_shareable(VERSION) if defined?(Ractor)
def Mutex_m.define_aliases(cl) # :nodoc:
cl.module_eval %q{
alias locked? mu_locked?
alias lock mu_lock
alias unlock mu_unlock
alias try_lock mu_try_lock
alias synchronize mu_synchronize
}
cl.alias_method(:locked?, :mu_locked?)
cl.alias_method(:lock, :mu_lock)
cl.alias_method(:unlock, :mu_unlock)
cl.alias_method(:try_lock, :mu_try_lock)
cl.alias_method(:synchronize, :mu_synchronize)
end
def Mutex_m.append_features(cl) # :nodoc:

View File

@ -55,4 +55,25 @@ class TestMutexM < Test::Unit::TestCase
def test_initialize_no_args
assert NoArgInitializeChild.new
end
def test_alias_extended_object
object = Object.new
object.extend(Mutex_m)
assert object.respond_to?(:locked?)
assert object.respond_to?(:lock)
assert object.respond_to?(:unlock)
assert object.respond_to?(:try_lock)
assert object.respond_to?(:synchronize)
end
def test_alias_included_class
object = NoArgInitializeChild.new
assert object.respond_to?(:locked?)
assert object.respond_to?(:lock)
assert object.respond_to?(:unlock)
assert object.respond_to?(:try_lock)
assert object.respond_to?(:synchronize)
end
end