binaryen 1.1.6.3 → 1.1.6.4
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 +74 -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: e0f235a249bcf6c38016c2033776997d2d636126cea0b37d47c98dc2b932dd08
|
4
|
+
data.tar.gz: 3f123ebb5bdb0f24ebf004097e02ccab417d09838e9ec663a87e43ae472cb9a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d58caa196cbc38e9178dba6fa0efdcdde270d298ef5c3f34e523f5281630ae18c32bac9bd6da379fb7b758a536b94afc2c3c3faddb68118c5ccb64255c256da
|
7
|
+
data.tar.gz: 85f826a48ba40d0e3d48e4806cc52faf265c058b53421af32efdc67eb811075f99c7e83488f6aab85b141478cac104c2d1031931374df75c650972107c8acd5a
|
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,82 @@ 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
|
+
write_to_pipe(stderr, err_output, timeout_checker, close_write: false)
|
52
57
|
end
|
58
|
+
output = read_from_pipe(ord, timeout_checker)
|
59
|
+
wait_or_kill(pid, timeout_checker, erd, stderr)
|
53
60
|
|
54
|
-
|
55
|
-
rescue POSIX::Spawn::TimeoutExceeded => e
|
56
|
-
raise Timeout::Error, e.message
|
61
|
+
output
|
57
62
|
end
|
58
63
|
|
59
64
|
private
|
60
65
|
|
61
|
-
def
|
62
|
-
|
66
|
+
def write_to_pipe(pipe, stdin, timeout_checker, close_write: true)
|
67
|
+
offset = 0
|
68
|
+
length = stdin.bytesize
|
69
|
+
|
70
|
+
while offset < length
|
71
|
+
remaining_time = timeout_checker.check!
|
72
|
+
|
73
|
+
if IO.select(nil, [pipe], nil, remaining_time)
|
74
|
+
written = pipe.write_nonblock(stdin.byteslice(offset, length), exception: false)
|
75
|
+
offset += written if written.is_a?(Integer)
|
76
|
+
else
|
77
|
+
raise Timeout::Error, "Command timed out"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
pipe.close_write if close_write
|
82
|
+
end
|
83
|
+
|
84
|
+
def read_from_pipe(pipe, timeout_checker)
|
85
|
+
output = +""
|
86
|
+
|
87
|
+
while (result = pipe.read_nonblock(8192, exception: false))
|
88
|
+
remaining_time = timeout_checker.check!
|
89
|
+
raise Timeout::Error, "Command timed out" if remaining_time <= 0
|
90
|
+
|
91
|
+
case result
|
92
|
+
when :wait_readable
|
93
|
+
IO.select([pipe], nil, nil, remaining_time)
|
94
|
+
when nil
|
95
|
+
break
|
96
|
+
else
|
97
|
+
output << result
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
output
|
102
|
+
end
|
103
|
+
|
104
|
+
def wait_or_kill(pid, timeout_checker, err_read, err_write)
|
105
|
+
loop do
|
106
|
+
remaining_time = timeout_checker.check!
|
107
|
+
raise Timeout::Error, "timed out waiting on process" if remaining_time <= 0
|
108
|
+
|
109
|
+
if (_, status = Process.wait2(pid, Process::WNOHANG))
|
110
|
+
|
111
|
+
raise Binaryen::NonZeroExitStatus,
|
112
|
+
"command exited with status #{status.exitstatus}" if status.exitstatus != 0
|
113
|
+
|
114
|
+
return true
|
115
|
+
else
|
116
|
+
sleep(0.1)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def build_command(*arguments)
|
122
|
+
[@cmd] + arguments + @default_args
|
63
123
|
end
|
64
124
|
|
65
125
|
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.4
|
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
|