71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/fastruby/object.rb', line 71
def fastruby(*arguments, &blk)
if blk
method_container = Class.new
class << method_container
attr_accessor :_self
attr_accessor :fastruby_options
end
method_container._self = self
method_container.fastruby_options = arguments.inject({},&:merge)
def method_container.method_added(mname)
m = instance_method(mname)
if fastruby_options[:fastruby_only]
tree = FastRuby::FastRubySexp.parse m.source
FastRuby.set_tree(_self, tree[1], tree, fastruby_options)
else
@_self.fastruby m.source, fastruby_options
end
end
method_container.class_eval(&blk)
return nil
end
options_hashes = arguments[1..-1]
argument = arguments.first
options_hash = {:validate_lvar_types => true}
options_hashes.each do |opt|
options_hash.merge!(opt)
end
tree = nil
require "fastruby/fastruby_sexp" unless defined? FastRuby::FastRubySexp
if argument.instance_of? FastRuby::FastRubySexp
tree = argument
elsif argument.instance_of? String
unless defined? RubyParser
require "rubygems"
require "ruby_parser"
end
tree = RubyParser.new.parse(argument).to_fastruby_sexp
else
raise ArgumentError
end
return unless tree
method_name = "_anonymous_" + Digest::SHA1.hexdigest(tree.inspect)
Object.execute_tree(FastRuby.encapsulate_tree(tree,method_name), {:main => method_name, :self => self}.merge(options_hash))
end
|