Class: RLM::Sandbox::UnsafeInProcess

Inherits:
Base
  • Object
show all
Defined in:
lib/rlm/sandbox/unsafe_in_process.rb

Defined Under Namespace

Classes: Scope

Constant Summary collapse

STREAM_CAPTURE_MUTEX =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnsafeInProcess

Returns a new instance of UnsafeInProcess.



21
22
23
24
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 21

def initialize
  super
  @prepared = false
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



19
20
21
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 19

def context
  @context
end

#runtime_bridgeObject (readonly)

Returns the value of attribute runtime_bridge.



19
20
21
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 19

def runtime_bridge
  @runtime_bridge
end

#skillsObject (readonly)

Returns the value of attribute skills.



19
20
21
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 19

def skills
  @skills
end

#toolsObject (readonly)

Returns the value of attribute tools.



19
20
21
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 19

def tools
  @tools
end

Instance Method Details

#cleanupObject



55
56
57
58
59
60
61
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 55

def cleanup
  @context = nil
  @tools = nil
  @skills = nil
  @runtime_bridge = nil
  @prepared = false
end

#exec(code) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 39

def exec(code)
  raise SandboxError, "Sandbox not prepared" unless prepared?

  execute(code)
end

#execute(code) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 45

def execute(code)
  stdout, stderr = capture_streams do
    Scope.new(runtime_bridge).instance_eval(code, "(rlm unsafe in-process sandbox)")
  end

  ExecutionResult.new(status: :ok, stdout: stdout, stderr: stderr)
rescue StandardError => e
  ExecutionResult.new(status: :error, stderr: e.message, error: e, exit_code: 1)
end

#prepare(context:, tools:, skills:, runtime_bridge:) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 30

def prepare(context:, tools:, skills:, runtime_bridge:)
  @context = context
  @tools = tools
  @skills = skills
  @runtime_bridge = runtime_bridge
  @prepared = true
  ExecutionResult.new(status: :ok)
end

#prepared?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rlm/sandbox/unsafe_in_process.rb', line 26

def prepared?
  @prepared
end