module Fruity::Baseline
Utility module for building baseline equivalents for callables.
Constants
- NOOPs
- PARAM_MAP
Public Instance Methods
[](exec)
click to toggle source
Returns the baseline for the given callable object The signature (number of arguments) and type (proc, …) will be copied as much as possible.
# File lib/fruity/baseline.rb, line 13 def [](exec) kind = callable_kind(exec) signature = callable_signature(exec) NOOPs[kind][signature] ||= build_baseline(kind, signature) end
arg_list(signature)
click to toggle source
# File lib/fruity/baseline.rb, line 51 def arg_list(signature) signature.map.with_index{|kind, i| PARAM_MAP[kind] % {:name => "p#{i}"}}.join(",") end
build_baseline(kind, signature)
click to toggle source
# File lib/fruity/baseline.rb, line 55 def build_baseline(kind, signature) args = "|#{arg_list(signature)}|" case kind when :lambda, :proc eval("#{kind}{#{args}}") when :builtin_method case signature when [] nil.method(:nil?) when [:req] nil.method(:==) else Array.method(:[]) end when :method @method_counter ||= 0 @method_counter += 1 name = "baseline_#{@method_counter}" eval("define_method(:#{name}){#{args}}") method(name) end end
callable_kind(exec)
click to toggle source
# File lib/fruity/baseline.rb, line 21 def callable_kind(exec) if exec.is_a?(Method) exec.source_location ? :method : :builtin_method elsif exec.lambda? :lambda else :proc end end
callable_signature(exec)
click to toggle source
# File lib/fruity/baseline.rb, line 31 def callable_signature(exec) if exec.respond_to?(:parameters) exec.parameters.map(&:first) else # Ruby 1.8 didn't have parameters, so rely on arity opt = exec.arity < 0 req = opt ? -1-exec.arity : exec.arity signature = [:req] * req signature << :rest if opt signature end end