Class: Binaryen::Command
- Inherits:
-
Object
- Object
- Binaryen::Command
- Includes:
- POSIX::Spawn
- Defined in:
- lib/binaryen/command.rb
Overview
Wrapper around a binaryen executable command with a timeout and streaming IO.
Defined Under Namespace
Classes: TimeoutChecker
Constant Summary collapse
- DEFAULT_ARGS_FOR_COMMAND =
{ "wasm-opt" => ["--output=-"], }.freeze
Instance Method Summary collapse
-
#initialize(cmd, timeout: 10, ignore_missing: false) ⇒ Command
constructor
A new instance of Command.
- #run(*arguments, stdin: nil, stderr: nil) ⇒ Object
Constructor Details
#initialize(cmd, timeout: 10, ignore_missing: false) ⇒ Command
Returns a new instance of Command.
41 42 43 44 45 |
# File 'lib/binaryen/command.rb', line 41 def initialize(cmd, timeout: 10, ignore_missing: false) @cmd = command_path(cmd, ignore_missing) || raise(ArgumentError, "command not found: #{cmd}") @timeout = timeout @default_args = DEFAULT_ARGS_FOR_COMMAND.fetch(cmd, []) end |
Instance Method Details
#run(*arguments, stdin: nil, stderr: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/binaryen/command.rb', line 47 def run(*arguments, stdin: nil, stderr: nil) end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout command = build_command(*arguments) pid, iwr, ord, erd = popen4(*command) timeout_checker = TimeoutChecker.new(end_time: end_time, pid: pid) write_to_pipe(iwr, stdin, timeout_checker, pid) if stdin if stderr err_output = read_from_pipe(erd, timeout_checker) write_to_pipe(stderr, err_output, timeout_checker, pid, close_write: false) end output = read_from_pipe(ord, timeout_checker) wait_or_kill(pid, timeout_checker) output end |