Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 02ac203

Browse files
committed
(simplify and) work-around 1.8 delegator.rb not passing blocks around
1 parent 9fffcb4 commit 02ac203

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

lib/thread_safe/synchronized_delegator.rb

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
class SynchronizedDelegator < SimpleDelegator
1818

1919
def initialize(obj)
20-
super # __setobj__(obj)
20+
__setobj__(obj)
2121
@mutex = Mutex.new
22-
undef_cached_methods!
2322
end
2423

2524
def method_missing(method, *args, &block)
@@ -32,44 +31,21 @@ def method_missing(method, *args, &block)
3231
end
3332
end
3433

35-
private
36-
37-
if RUBY_VERSION[0, 3] == '1.8'
38-
39-
def singleton_class
40-
class << self; self end
41-
end unless respond_to?(:singleton_class)
42-
43-
# The 1.8 delegator library does (instance) "eval" all methods
44-
# delegated on {#initialize}.
45-
# @see http://rubydoc.info/stdlib/delegate/1.8.7/Delegator
46-
# @private
47-
def undef_cached_methods!
48-
self_class = singleton_class
49-
for method in self_class.instance_methods(false)
50-
self_class.send :undef_method, method
34+
# Work-around for 1.8 std-lib not passing block around to delegate.
35+
# @private
36+
def method_missing(method, *args, &block)
37+
mutex = @mutex
38+
begin
39+
mutex.lock
40+
target = self.__getobj__
41+
if target.respond_to?(method)
42+
target.__send__(method, *args, &block)
43+
else
44+
super(method, *args, &block)
5145
end
46+
ensure
47+
mutex.unlock
5248
end
53-
54-
# JRuby 1.8 mode stdlib internals - caching generated modules
55-
# methods under `Delegator::DelegatorModules` based on class.
56-
# @private
57-
def undef_cached_methods!
58-
gen_mod = DelegatorModules[[__getobj__.class, self.class]]
59-
if gen_mod && singleton_class.include?(gen_mod)
60-
self_class = singleton_class
61-
for method in gen_mod.instance_methods(false)
62-
self_class.send :undef_method, method
63-
end
64-
end
65-
end if constants.include?('DelegatorModules')
66-
67-
else
68-
69-
# Nothing to do since 1.9 {#method_missing} will get called.
70-
# @private
71-
def undef_cached_methods!; end
72-
73-
end
49+
end if RUBY_VERSION[0, 3] == '1.8'
7450

7551
end unless defined?(SynchronizedDelegator)

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
if defined?(JRUBY_VERSION) && ENV['TEST_NO_UNSAFE']
44
# to be used like this: rake test TEST_NO_UNSAFE=true
5-
require 'test/package.jar'
5+
load 'test/package.jar'
66
java_import 'thread_safe.SecurityManager'
77
manager = SecurityManager.new
88

test/test_synchronized_delegator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_synchronizes_access
3636
sync_hash[4] = '42'
3737
end
3838

39-
sleep(0.05) # sleep some to allow threads to boot up
39+
sleep(0.05) # sleep some to allow threads to boot
4040

4141
until t2.status == 'sleep' do
4242
Thread.pass
@@ -79,6 +79,6 @@ def test_synchronizes_access_with_block
7979
t1.join; t2.join
8080

8181
assert_equal [1, 2], array
82-
end if RUBY_VERSION !~ /1\.8/
82+
end
8383

8484
end

0 commit comments

Comments
 (0)