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
This commit is contained in:
parent
fb8628ecda
commit
2283d14cc9
1
ext/rubyvm/extconf.rb
Normal file
1
ext/rubyvm/extconf.rb
Normal file
@ -0,0 +1 @@
|
|||||||
|
create_makefile("rubyvm")
|
19
ext/rubyvm/lib/forwardable/impl.rb
Normal file
19
ext/rubyvm/lib/forwardable/impl.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# :stopdoc:
|
||||||
|
module Forwardable
|
||||||
|
FILTER_EXCEPTION = ""
|
||||||
|
|
||||||
|
def self._valid_method?(method)
|
||||||
|
iseq = RubyVM::InstructionSequence.compile("().#{method}", nil, nil, 0, false)
|
||||||
|
rescue SyntaxError => e
|
||||||
|
false
|
||||||
|
else
|
||||||
|
iseq.to_a.dig(-1, 1, 1, :mid) == method.to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
def self._compile_method(src, file, line)
|
||||||
|
RubyVM::InstructionSequence.compile(src, file, file, line,
|
||||||
|
trace_instruction: false,
|
||||||
|
tailcall_optimization: true)
|
||||||
|
.eval
|
||||||
|
end
|
||||||
|
end
|
@ -110,8 +110,10 @@
|
|||||||
# +delegate.rb+.
|
# +delegate.rb+.
|
||||||
#
|
#
|
||||||
module Forwardable
|
module Forwardable
|
||||||
|
require 'forwardable/impl'
|
||||||
|
|
||||||
# Version of +forwardable.rb+
|
# Version of +forwardable.rb+
|
||||||
FORWARDABLE_VERSION = "1.1.0"
|
FORWARDABLE_VERSION = "1.2.0"
|
||||||
|
|
||||||
@debug = nil
|
@debug = nil
|
||||||
class << self
|
class << self
|
||||||
@ -195,14 +197,8 @@ module Forwardable
|
|||||||
accessor = "#{accessor}()"
|
accessor = "#{accessor}()"
|
||||||
end
|
end
|
||||||
|
|
||||||
vm = RubyVM::InstructionSequence
|
|
||||||
method_call = ".__send__(:#{method}, *args, &block)"
|
method_call = ".__send__(:#{method}, *args, &block)"
|
||||||
if begin
|
if _valid_method?(method)
|
||||||
iseq = vm.compile("().#{method}", nil, nil, 0, false)
|
|
||||||
rescue SyntaxError
|
|
||||||
else
|
|
||||||
iseq.to_a.dig(-1, 1, 1, :mid) == method.to_sym
|
|
||||||
end
|
|
||||||
loc, = caller_locations(2,1)
|
loc, = caller_locations(2,1)
|
||||||
pre = "_ ="
|
pre = "_ ="
|
||||||
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
|
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
|
||||||
@ -217,22 +213,17 @@ module Forwardable
|
|||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
|
|
||||||
line_no = __LINE__+1; str = "#{<<-"begin;"}\n#{<<-"end;"}"
|
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
|
||||||
begin;
|
begin;
|
||||||
proc do
|
proc do
|
||||||
def #{ali}(*args, &block)
|
def #{ali}(*args, &block)
|
||||||
#{pre}
|
#{pre}
|
||||||
begin
|
begin
|
||||||
#{accessor}
|
#{accessor}
|
||||||
end#{method_call}
|
end#{method_call}#{FILTER_EXCEPTION}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
vm.compile(str, __FILE__, __FILE__, line_no,
|
|
||||||
trace_instruction: false,
|
|
||||||
tailcall_optimization: true)
|
|
||||||
.eval
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
24
lib/forwardable/impl.rb
Normal file
24
lib/forwardable/impl.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# :stopdoc:
|
||||||
|
module Forwardable
|
||||||
|
FILE_REGEXP = %r"#{Regexp.quote(File.dirname(__FILE__))}"
|
||||||
|
FILTER_EXCEPTION = <<-'END'
|
||||||
|
|
||||||
|
rescue ::Exception
|
||||||
|
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} unless ::Forwardable::debug
|
||||||
|
::Kernel::raise
|
||||||
|
END
|
||||||
|
|
||||||
|
def self._valid_method?(method)
|
||||||
|
catch {|tag|
|
||||||
|
eval("BEGIN{throw tag}; ().#{method}", binding, __FILE__, __LINE__)
|
||||||
|
}
|
||||||
|
rescue SyntaxError
|
||||||
|
false
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def self._compile_method(src, file, line)
|
||||||
|
eval(src, nil, file, line)
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user