Class: Instant::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/instant/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(processor = Processor.new, timeout = 1) ⇒ Runner

Returns a new instance of Runner.



6
7
8
9
# File 'lib/instant/runner.rb', line 6

def initialize(processor = Processor.new, timeout=1)
  @processor = processor
  @timeout   = timeout
end

Instance Method Details

#run(source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/instant/runner.rb', line 11

def run(source)      
  begin
    @processed = @processor.process(source)
    context = Context.new
    return_value = nil

    begin
      thread = Thread.new do
        $SAFE = 3          
        Timeout::timeout(@timeout) do
          return_value = context.instance_eval(@processed)
        end
      end
      thread.join
    ensure
      context.close
    end
    {:status => :ok, :result => context.to_s, :return_value => return_value}
  rescue SecurityError => e
    {:status => :error, :cause => :security_error, :message => format_error(e), :result => context.to_s }
  rescue SyntaxError => e
    {:status => :error, :cause => :syntax_error, :message => format_error(e), :result => context.to_s }
  rescue Racc::ParseError => e
    {:status => :error, :cause => :parse_error, :message => format_error(e), :result => context.to_s }
  rescue Instant::LoopTooDeepError => e
    {:status => :error, :cause => :loop_too_deep, :message => "Loop too deep", :result => context.to_s }
  rescue Timeout::Error => e
    {:status => :error, :cause => :timeout, :message => "Timeout: code take more than #{@timeout}s to run.", :result => context.to_s }
  rescue StandardError => e
    {:status => :error, :cause => :unknown, :message => format_error(e), :result => context.to_s }        
  end
end