Module: Sandboxed

Defined in:
lib/sandboxed.rb,
lib/sandboxed/version.rb

Defined Under Namespace

Modules: ContextHolder, Modes

Constant Summary collapse

MODES =
{:overlay => '1.8.6', :ctx_only => '1.8.7'}.reject{|m, v| v > RUBY_VERSION}
MODE_PREFERENCE =
[:overlay, :ctx_only] & MODES.keys
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.default_modeObject



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

def default_mode
  @default_mode ||= MODE_PREFERENCE.first
end

.default_mode=(mode) ⇒ Object



39
40
41
# File 'lib/sandboxed.rb', line 39

def default_mode=(mode)
  @default_mode = check_mode(mode)
end

.safe(*args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sandboxed.rb', line 43

def safe(*args, &block)
  contexts = Thread.current[:__contexts_] ||= []
  opts = args.last.is_a?(Hash) ? args.pop : {}
  
  mode = opts.delete(:mode) || default_mode
  check_mode(mode)

  level = opts.delete(:level) || 4
  ctx = opts.delete(:context)
  args << opts unless opts.empty? # be nicer about passed in hashes

  contexts << ctx
  result = Modes.send(mode, level, ctx, args, &block)
  contexts.pop if ctx

  result
end

.safe_method(mdl, *ms) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sandboxed.rb', line 61

def safe_method(mdl, *ms)
  code = ms.map do |m|
    safe_m = "#{m}__safe_"
    <<-RUBY
      alias #{safe_m} #{m}
      def #{m}(*args, &block)
        result = SAFE_FIBER.resume([self, :#{safe_m}, args, block])
        result.is_a?(Exception) ? throw(result) : result  # TODO find a better way
      end
    RUBY
  end.join("\n")
  mdl.class_eval code
end