Class: ScriptCore::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/script_core/executable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable_path) ⇒ Executable



7
8
9
10
11
12
13
# File 'lib/script_core/executable.rb', line 7

def initialize(executable_path)
  unless File.exist? executable_path
    raise ScriptCore::ExecutableNotFound.new(executable_path),
          "Executable not found, make sure you've compiled the engine and give an exists path"
  end
  @executable_path = executable_path.to_s
end

Instance Attribute Details

#executable_pathObject (readonly)

Returns the value of attribute executable_path.



5
6
7
# File 'lib/script_core/executable.rb', line 5

def executable_path
  @executable_path
end

Instance Method Details

#run(input: {}, environment_variables: {}, sources:, instructions: nil, timeout: 1, instruction_quota: 100_000, instruction_quota_start: 0, memory_quota: 8 << 20) ⇒ Object



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
43
# File 'lib/script_core/executable.rb', line 15

def run(
  input: {}, environment_variables: {}, sources:, instructions: nil,
  timeout: 1, instruction_quota: 100_000, instruction_quota_start: 0, memory_quota: 8 << 20
)
  packer = ScriptCore::Protocol.packer_factory.packer

  payload = { input: input, sources: sources }
  payload[:library] = instructions if instructions
  encoded = packer.pack(payload)

  packer = ScriptCore::Protocol.packer_factory.packer
  size = packer.pack(encoded.size)

  spawner = ScriptCore::Spawner.new
  service_process = ScriptCore::ServiceProcess.new(
    executable_path,
    spawner,
    instruction_quota,
    instruction_quota_start,
    memory_quota,
    environment_variables
  )
  runner = ScriptCore::Runner.new(
    timeout: timeout,
    service_process: service_process,
    message_processor_factory: ScriptCore::MessageProcessor
  )
  runner.run(size, encoded)
end