Module: Stupidedi::TailCall::ClassMethods

Defined in:
lib/stupidedi/tail_call.rb

Instance Method Summary collapse

Instance Method Details

#optimize_tailcall(*names) ⇒ void

This method returns an undefined value.



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
# File 'lib/stupidedi/tail_call.rb', line 74

def optimize_tailcall(*names)
  @__tc ||= Hash.new

  for name in names
    @__tc[name.to_sym] = instance_method(name)

    module_eval <<-RUBY
      def #{name}(*args, &block)
        if Thread.current[:tc_#{name}]
          throw(:recurse_#{name}, [args, block])
        else
          Thread.current[:tc_#{name}] =
            self.class.instance_variable_get(:@__tc)[:#{name}].bind(self)

          result = catch(:done_#{name}) do
            while true
              args, block =
                catch(:recurse_#{name}) do
                  throw(:done_#{name},
                    Thread.current[:tc_#{name}].call(*args, &block))
                end
            end
          end

          Thread.current[:tc_#{name}] = nil

          result
        end
      end
    RUBY
  end
end