Class: ScriptCore::Engine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_path = ScriptCore::DEFAULT_BIN_PATH, executable_name: "enterprise_script_service", instructions_name: "enterprise_script_service.mrb") ⇒ Engine

Returns a new instance of Engine.

Raises:

  • (Errno::ENOENT)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/script_core/engine.rb', line 7

def initialize(bin_path = ScriptCore::DEFAULT_BIN_PATH, executable_name: "enterprise_script_service",
               instructions_name: "enterprise_script_service.mrb")
  raise Errno::ENOENT, "No such directory - #{bin_path}" unless File.directory?(bin_path)
  @bin_path = bin_path

  @executable = ScriptCore::Executable.new(@bin_path.join(executable_name))
  @timeout = 1
  @instruction_quota = 100_000
  @instruction_quota_start = 0
  @memory_quota = 8 << 20

  preload_instructions_path = @bin_path.join(instructions_name)
  @instructions = File.exist?(preload_instructions_path) ? File.binread(preload_instructions_path) : nil
end

Instance Attribute Details

#instruction_quotaObject

Returns the value of attribute instruction_quota.



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

def instruction_quota
  @instruction_quota
end

#instruction_quota_startObject

Returns the value of attribute instruction_quota_start.



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

def instruction_quota_start
  @instruction_quota_start
end

#instructionsObject

Returns the value of attribute instructions.



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

def instructions
  @instructions
end

#memory_quotaObject

Returns the value of attribute memory_quota.



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

def memory_quota
  @memory_quota
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#eval(sources, payload: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/script_core/engine.rb', line 22

def eval(sources, payload: nil)
  @executable.run(
    input: payload || {},
    sources: sources || [],
    instructions: @instructions,
    timeout: @timeout,
    instruction_quota: @instruction_quota,
    instruction_quota_start: @instruction_quota_start,
    memory_quota: @memory_quota
  )
end