Class: Sandrbox::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/sandrbox/value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, line_no) ⇒ Value

Returns a new instance of Value.



5
6
7
8
9
10
11
# File 'lib/sandrbox/value.rb', line 5

def initialize(line, line_no)
  self.unbound_methods = []
  self.unbound_constants = []
  self.line = line
  self.line_no = line_no
  evaluate
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



3
4
5
# File 'lib/sandrbox/value.rb', line 3

def line
  @line
end

#line_noObject

Returns the value of attribute line_no.



3
4
5
# File 'lib/sandrbox/value.rb', line 3

def line_no
  @line_no
end

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/sandrbox/value.rb', line 3

def result
  @result
end

#timeObject

Returns the value of attribute time.



3
4
5
# File 'lib/sandrbox/value.rb', line 3

def time
  @time
end

#unbound_constantsObject

Returns the value of attribute unbound_constants.



3
4
5
# File 'lib/sandrbox/value.rb', line 3

def unbound_constants
  @unbound_constants
end

#unbound_methodsObject

Returns the value of attribute unbound_methods.



3
4
5
# File 'lib/sandrbox/value.rb', line 3

def unbound_methods
  @unbound_methods
end

Instance Method Details

#evaluateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sandrbox/value.rb', line 13

def evaluate
  t = Thread.new do
    $SAFE = 2
    begin
      Timeout::timeout(0.5) do
        Sandrbox.config.bad_methods.each {|meth| remove_method(meth.first, meth.last)}
        Sandrbox.config.bad_constants.each {|const| remove_constant(const)}
        self.result = eval(line, TOPLEVEL_BINDING, "sandrbox", line_no)
      end
    rescue Exception => e
      self.result = "#{e.class}: #{e.to_s}"
    ensure
      restore_constants
      restore_methods
    end
  end
  
  timeout = t.join(3)
  self.result = "SandrboxError: execution expired" if timeout.nil?

  self
end

#to_sObject



36
37
38
# File 'lib/sandrbox/value.rb', line 36

def to_s
  self.result
end