binaryen 1.1.6.5 → 1.1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/binaryen/command.rb +11 -94
- data/lib/binaryen/error.rb +1 -0
- data/lib/binaryen/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e208c99114153cd5edae8ca1125d26eeecb8f2b5f91e218ccc63f807827ee4fe
|
4
|
+
data.tar.gz: 6839fedb4b0c710fc1aa852909f3a24daf1729a2f1531e6c3c47b7594f2819d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21ec4f01eacfa0d38b08acae723f5f6bf662f42047950354c3260b9d461b774ba91749b82b162a74b83ef0e70aa4b6541fcbbbb70111cd30b11b969955a2cfec
|
7
|
+
data.tar.gz: 5dbbf03fe6f8325cdaa1e78c81bfb7feef07d1887d53a647202f56dc58e5ac1b628a2057f0e5cce083bd2868894e622054f1a999d476a568ef04a5c1558a2d72
|
data/lib/binaryen/command.rb
CHANGED
@@ -15,29 +15,11 @@ module Binaryen
|
|
15
15
|
# optimized_wasm = command.run("-O4", stdin: "(module)")
|
16
16
|
# ```
|
17
17
|
class Command
|
18
|
-
|
19
|
-
|
18
|
+
MAX_OUTPUT_SIZE = 256 * 1024 * 1024
|
20
19
|
DEFAULT_ARGS_FOR_COMMAND = {
|
21
20
|
"wasm-opt" => ["--output=-"],
|
22
21
|
}.freeze
|
23
22
|
|
24
|
-
class TimeoutChecker
|
25
|
-
def initialize(end_time:, pid:)
|
26
|
-
@end_time = end_time
|
27
|
-
@pid = pid
|
28
|
-
end
|
29
|
-
|
30
|
-
def check!
|
31
|
-
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
32
|
-
if now >= @end_time
|
33
|
-
Process.kill("KILL", @pid)
|
34
|
-
raise Timeout::Error, "Command timed out"
|
35
|
-
end
|
36
|
-
remaining_time = @end_time - now
|
37
|
-
remaining_time
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
23
|
def initialize(cmd, timeout: 10, ignore_missing: false)
|
42
24
|
@cmd = command_path(cmd, ignore_missing) || raise(ArgumentError, "command not found: #{cmd}")
|
43
25
|
@timeout = timeout
|
@@ -45,87 +27,22 @@ module Binaryen
|
|
45
27
|
end
|
46
28
|
|
47
29
|
def run(*arguments, stdin: nil, stderr: nil)
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
30
|
+
args = [@cmd] + arguments + @default_args
|
31
|
+
child = POSIX::Spawn::Child.new(*args, input: stdin, timeout: @timeout, max: MAX_OUTPUT_SIZE)
|
32
|
+
stderr&.write(child.err)
|
33
|
+
status = child.status
|
52
34
|
|
53
|
-
|
54
|
-
if stderr
|
55
|
-
err_output = read_from_pipe(erd, timeout_checker)
|
56
|
-
begin
|
57
|
-
write_to_pipe(stderr, err_output, timeout_checker, close_write: false)
|
58
|
-
rescue Errno::EPIPE
|
59
|
-
# ignore
|
60
|
-
end
|
61
|
-
end
|
62
|
-
output = read_from_pipe(ord, timeout_checker)
|
63
|
-
wait_or_kill(pid, timeout_checker, erd, stderr)
|
35
|
+
raise Binaryen::NonZeroExitStatus, "command exited with status #{status.exitstatus}" unless status.success?
|
64
36
|
|
65
|
-
|
37
|
+
child.out
|
38
|
+
rescue POSIX::Spawn::MaximumOutputExceeded => e
|
39
|
+
raise Binaryen::MaximumOutputExceeded, e.message
|
40
|
+
rescue POSIX::Spawn::TimeoutExceeded => e
|
41
|
+
raise Timeout::Error, e.message
|
66
42
|
end
|
67
43
|
|
68
44
|
private
|
69
45
|
|
70
|
-
def write_to_pipe(pipe, stdin, timeout_checker, close_write: true)
|
71
|
-
offset = 0
|
72
|
-
length = stdin.bytesize
|
73
|
-
|
74
|
-
while offset < length
|
75
|
-
remaining_time = timeout_checker.check!
|
76
|
-
|
77
|
-
if IO.select(nil, [pipe], nil, remaining_time)
|
78
|
-
written = pipe.write_nonblock(stdin.byteslice(offset, length), exception: false)
|
79
|
-
offset += written if written.is_a?(Integer)
|
80
|
-
else
|
81
|
-
raise Timeout::Error, "Command timed out"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
pipe.close_write if close_write
|
86
|
-
end
|
87
|
-
|
88
|
-
def read_from_pipe(pipe, timeout_checker)
|
89
|
-
output = +""
|
90
|
-
|
91
|
-
while (result = pipe.read_nonblock(8192, exception: false))
|
92
|
-
remaining_time = timeout_checker.check!
|
93
|
-
raise Timeout::Error, "Command timed out" if remaining_time <= 0
|
94
|
-
|
95
|
-
case result
|
96
|
-
when :wait_readable
|
97
|
-
IO.select([pipe], nil, nil, remaining_time)
|
98
|
-
when nil
|
99
|
-
break
|
100
|
-
else
|
101
|
-
output << result
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
output
|
106
|
-
end
|
107
|
-
|
108
|
-
def wait_or_kill(pid, timeout_checker, err_read, err_write)
|
109
|
-
loop do
|
110
|
-
remaining_time = timeout_checker.check!
|
111
|
-
raise Timeout::Error, "timed out waiting on process" if remaining_time <= 0
|
112
|
-
|
113
|
-
if (_, status = Process.wait2(pid, Process::WNOHANG))
|
114
|
-
|
115
|
-
raise Binaryen::NonZeroExitStatus,
|
116
|
-
"command exited with status #{status.exitstatus}" if status.exitstatus != 0
|
117
|
-
|
118
|
-
return true
|
119
|
-
else
|
120
|
-
sleep(0.1)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
def build_command(*arguments)
|
126
|
-
[@cmd] + arguments + @default_args
|
127
|
-
end
|
128
|
-
|
129
46
|
def command_path(cmd, ignore_missing)
|
130
47
|
Dir[File.join(Binaryen.bindir, cmd)].first || (ignore_missing && cmd)
|
131
48
|
end
|
data/lib/binaryen/error.rb
CHANGED
data/lib/binaryen/version.rb
CHANGED