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)
[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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, input: {}, environment_variables: {}, instruction_quota: nil, instruction_quota_start: nil, memory_quota: nil, timeout: nil) ⇒ Object

[View source]

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/script_core/engine.rb', line 24

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

#eval_mrb(binary_mrb, input: {}, environment_variables: {}, instruction_quota: nil, instruction_quota_start: nil, memory_quota: nil, timeout: nil) ⇒ Object

[View source]

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/script_core/engine.rb', line 40

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