binaryen 1.1.6.3 → 1.1.6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/binaryen/command.rb +78 -14
- data/lib/binaryen/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ec6cd19949163f81747301353e37912f7d2924205816df9f6620f0cb6a266ce
|
4
|
+
data.tar.gz: cfc6e449979a806440f5c893640618bd321597e1ae8848108fea8c7b9da88d72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e83c357446ac3c2e04d6de7a575d8dca7b4164cd4050416edd87d23fe864165085f25b5e3f7a7f28d46debedcc7fa146f1416aa8a529376c5ea07dfd8f1713b
|
7
|
+
data.tar.gz: 8f3e7d8a8582608436191bdea891aa8564cebc0ccceceb750bdbce38b563bd590c32e8b5025a9a63761eb6592347709e06983c023b950d377b016354da111b35
|
data/lib/binaryen/command.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "English"
|
4
|
+
require "shellwords"
|
3
5
|
require "timeout"
|
4
6
|
require "posix/spawn"
|
5
7
|
|
@@ -13,6 +15,8 @@ module Binaryen
|
|
13
15
|
# optimized_wasm = command.run("-O4", stdin: "(module)")
|
14
16
|
# ```
|
15
17
|
class Command
|
18
|
+
include POSIX::Spawn
|
19
|
+
|
16
20
|
DEFAULT_ARGS_FOR_COMMAND = {
|
17
21
|
"wasm-opt" => ["--output=-"],
|
18
22
|
}.freeze
|
@@ -40,26 +44,86 @@ module Binaryen
|
|
40
44
|
@default_args = DEFAULT_ARGS_FOR_COMMAND.fetch(cmd, [])
|
41
45
|
end
|
42
46
|
|
43
|
-
def run(*arguments, stdin: nil, stderr:
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
def run(*arguments, stdin: nil, stderr: nil)
|
48
|
+
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout
|
49
|
+
command = build_command(*arguments)
|
50
|
+
pid, iwr, ord, erd = popen4(*command)
|
51
|
+
timeout_checker = TimeoutChecker.new(end_time: end_time, pid: pid)
|
52
|
+
|
53
|
+
write_to_pipe(iwr, stdin, timeout_checker) if stdin
|
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
|
52
61
|
end
|
62
|
+
output = read_from_pipe(ord, timeout_checker)
|
63
|
+
wait_or_kill(pid, timeout_checker, erd, stderr)
|
53
64
|
|
54
|
-
|
55
|
-
rescue POSIX::Spawn::TimeoutExceeded => e
|
56
|
-
raise Timeout::Error, e.message
|
65
|
+
output
|
57
66
|
end
|
58
67
|
|
59
68
|
private
|
60
69
|
|
61
|
-
def
|
62
|
-
|
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
|
63
127
|
end
|
64
128
|
|
65
129
|
def command_path(cmd, ignore_missing)
|
data/lib/binaryen/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binaryen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.6.
|
4
|
+
version: 1.1.6.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shopify Inc.
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.3.15
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.3.15
|
27
27
|
description:
|
28
28
|
email:
|
29
29
|
- gems@shopify.com
|