Sha256: a7d6cd5a6644be43453cfe1a072cd893a6ded65b55bb8a460f24e90b106a75df

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 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,
            environment_variables: {})
      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
script_core-0.0.4 lib/script_core/executable.rb