Sha256: 0b41acbc2432d3f71c36241742e39460b74b46bc94f99eb504a4a54e251164f7

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module ScriptCore
  class Executable
    attr_reader :executable_path

    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

    def run(input:, 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
      )
      runner = ScriptCore::Runner.new(
        timeout: timeout,
        service_process: service_process,
        message_processor_factory: ScriptCore::MessageProcessor
      )
      runner.run(size, encoded)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
script_core-0.0.3 lib/script_core/executable.rb
script_core-0.0.2 lib/script_core/executable.rb
script_core-0.0.1 lib/script_core/executable.rb