Skip to content

Commit 2283d14

Browse files
committed
forwardable/impl.rb
* lib/forwardable/impl.rb (_valid_method?, _compile_method): extract to separate implementation specific part. [ruby-core:78138] [Bug #12938] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56848 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent fb8628e commit 2283d14

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

ext/rubyvm/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
create_makefile("rubyvm")

ext/rubyvm/lib/forwardable/impl.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# :stopdoc:
2+
module Forwardable
3+
FILTER_EXCEPTION = ""
4+
5+
def self._valid_method?(method)
6+
iseq = RubyVM::InstructionSequence.compile("().#{method}", nil, nil, 0, false)
7+
rescue SyntaxError => e
8+
false
9+
else
10+
iseq.to_a.dig(-1, 1, 1, :mid) == method.to_sym
11+
end
12+
13+
def self._compile_method(src, file, line)
14+
RubyVM::InstructionSequence.compile(src, file, file, line,
15+
trace_instruction: false,
16+
tailcall_optimization: true)
17+
.eval
18+
end
19+
end

lib/forwardable.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@
110110
# +delegate.rb+.
111111
#
112112
module Forwardable
113+
require 'forwardable/impl'
114+
113115
# Version of +forwardable.rb+
114-
FORWARDABLE_VERSION = "1.1.0"
116+
FORWARDABLE_VERSION = "1.2.0"
115117

116118
@debug = nil
117119
class << self
@@ -195,14 +197,8 @@ def self._delegator_method(obj, accessor, method, ali)
195197
accessor = "#{accessor}()"
196198
end
197199

198-
vm = RubyVM::InstructionSequence
199200
method_call = ".__send__(:#{method}, *args, &block)"
200-
if begin
201-
iseq = vm.compile("().#{method}", nil, nil, 0, false)
202-
rescue SyntaxError
203-
else
204-
iseq.to_a.dig(-1, 1, 1, :mid) == method.to_sym
205-
end
201+
if _valid_method?(method)
206202
loc, = caller_locations(2,1)
207203
pre = "_ ="
208204
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
@@ -217,22 +213,17 @@ def self._delegator_method(obj, accessor, method, ali)
217213
end;
218214
end
219215

220-
line_no = __LINE__+1; str = "#{<<-"begin;"}\n#{<<-"end;"}"
216+
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
221217
begin;
222218
proc do
223219
def #{ali}(*args, &block)
224220
#{pre}
225221
begin
226222
#{accessor}
227-
end#{method_call}
223+
end#{method_call}#{FILTER_EXCEPTION}
228224
end
229225
end
230226
end;
231-
232-
vm.compile(str, __FILE__, __FILE__, line_no,
233-
trace_instruction: false,
234-
tailcall_optimization: true)
235-
.eval
236227
end
237228
end
238229

lib/forwardable/impl.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# :stopdoc:
2+
module Forwardable
3+
FILE_REGEXP = %r"#{Regexp.quote(File.dirname(__FILE__))}"
4+
FILTER_EXCEPTION = <<-'END'
5+
6+
rescue ::Exception
7+
[email protected]_if {|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug
8+
::Kernel::raise
9+
END
10+
11+
def self._valid_method?(method)
12+
catch {|tag|
13+
eval("BEGIN{throw tag}; ().#{method}", binding, __FILE__, __LINE__)
14+
}
15+
rescue SyntaxError
16+
false
17+
else
18+
true
19+
end
20+
21+
def self._compile_method(src, file, line)
22+
eval(src, nil, file, line)
23+
end
24+
end

0 commit comments

Comments
 (0)