Sha256: 2d13d1e88b5f1e17658180c92a8d92610208cfd2ef86decf430a7cdbe32f8793

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

require 'rubygems'

require 'ruby2ruby'
begin
  require 'ruby_parser' # try to load RubyParser and use it if present
rescue LoadError => e
end
# this doesn't work somehow. Maybe something changed inside
# ParseTree or sexp_processor or so.
# (the require itself works, but ParseTree doesn't play nice)
# begin
#   require 'parse_tree'
# rescue LoadError => e
# end

require 'safemode/core_ext'
require 'safemode/blankslate'
require 'safemode/exceptions'
require 'safemode/jail'
require 'safemode/core_jails'
require 'safemode/parser'
require 'safemode/scope'

module Safemode
  class << self
    def jail(obj)
      find_jail_class(obj.is_a?(Class) ? obj : obj.class).new obj
    end
    
    def find_jail_class(klass)
      while klass != Object
        return klass.const_get('Jail') if klass.const_defined?('Jail')
        klass = klass.superclass
      end
      Jail
    end
  end
    
  define_core_jail_classes
  
  class Box
    def initialize(delegate = nil, delegate_methods = [], filename = nil, line = nil)
      @scope = Scope.new(delegate, delegate_methods)
      @filename = filename
      @line = line
    end    

    def eval(code, assigns = {}, locals = {}, &block)
      code = Parser.jail(code)
      binding = @scope.bind(assigns, locals, &block)
      result = Kernel.eval(code, binding, @filename || __FILE__, @line || __LINE__)
    end
    
    def output
      @scope.output
    end 
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
safemode-1.3.7 lib/safemode.rb
safemode-1.3.6 lib/safemode.rb
safemode-1.3.5 lib/safemode.rb
safemode-1.3.4 lib/safemode.rb
safemode-1.3.3 lib/safemode.rb
safemode-1.3.2 lib/safemode.rb
safemode-1.3.1 lib/safemode.rb