Skip to content

Commit 04d83cb

Browse files
Introduce rbwasm pack command as an alias of wasi-vfs pack
To remove wasi-vfs CLI binary dependency
1 parent 9e66103 commit 04d83cb

File tree

8 files changed

+37
-13
lines changed

8 files changed

+37
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmarks/vm_deep_call.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#
33
# Example runs
44
# $ ruby vm_deep_call.rb
5-
# $ RUBY_EXE="wasmtime run --mapdir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
6-
# $ RUBY_EXE="wasmtime run --env RUBY_FIBER_MACHINE_STACK_SIZE=20971520 --mapdir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
5+
# $ RUBY_EXE="wasmtime run --dir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
6+
# $ RUBY_EXE="wasmtime run --env RUBY_FIBER_MACHINE_STACK_SIZE=20971520 --dir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
77

88
def vm_rec n
99
vm_rec n - 1 if n > 0

ext/ruby_wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ wizer = "3.0.0"
1515
wasmtime-wasi = "9.0.4"
1616
wasi-cap-std-sync = "9.0.4"
1717
wasi-vfs-cli = { git = "https://github.com/kateinoigakukun/wasi-vfs/", rev = "b1e4e5d9cd6322e8745e67c092b495973835a94f" }
18+
structopt = "0.3.26"

ext/ruby_wasm/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use magnus::{
77
wrap, Error, ExceptionClass, RModule, Ruby,
88
};
99
use wizer::Wizer;
10+
use structopt::StructOpt;
1011

1112
static RUBY_WASM: value::Lazy<RModule> =
1213
value::Lazy::new(|ruby| ruby.define_module("RubyWasmExt").unwrap());
@@ -35,6 +36,15 @@ struct WasiVfsInner {
3536
struct WasiVfs(std::cell::RefCell<WasiVfsInner>);
3637

3738
impl WasiVfs {
39+
fn run_cli(args: Vec<String>) -> Result<(), Error> {
40+
wasi_vfs_cli::App::from_iter(args).execute().map_err(|e| {
41+
Error::new(
42+
exception::standard_error(),
43+
format!("failed to run wasi vfs cli: {}", e),
44+
)
45+
})
46+
}
47+
3848
fn new() -> Self {
3949
Self(std::cell::RefCell::new(WasiVfsInner { map_dirs: vec![] }))
4050
}
@@ -63,6 +73,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
6373

6474
let wasi_vfs = module.define_class("WasiVfs", ruby.class_object())?;
6575
wasi_vfs.define_singleton_method("new", function!(WasiVfs::new, 0))?;
76+
wasi_vfs.define_singleton_method("run_cli", function!(WasiVfs::run_cli, 1))?;
6677
wasi_vfs.define_method("map_dir", method!(WasiVfs::map_dir, 2))?;
6778
wasi_vfs.define_method("pack", method!(WasiVfs::pack, 1))?;
6879
Ok(())

lib/ruby_wasm/cli.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(stdout:, stderr:)
99
end
1010

1111
def run(args)
12-
available_commands = %w[build]
12+
available_commands = %w[build pack]
1313
parser =
1414
OptionParser.new do |opts|
1515
opts.banner = <<~USAGE
@@ -32,6 +32,8 @@ def run(args)
3232
case command
3333
when "build"
3434
build(args)
35+
when "pack"
36+
pack(args)
3537
else
3638
@stderr.puts parser
3739
exit
@@ -141,6 +143,11 @@ def build(args)
141143
end
142144
end
143145

146+
def pack(args)
147+
self.require_extension
148+
RubyWasmExt::WasiVfs::run_cli(["pack", *args])
149+
end
150+
144151
private
145152

146153
def build_config(options)
@@ -180,13 +187,7 @@ def do_print_ruby_cache_key(packager)
180187
end
181188

182189
def do_build(executor, tmpdir, packager, options)
183-
# Tries to require the extension for the given Ruby version first
184-
begin
185-
RUBY_VERSION =~ /(\d+\.\d+)/
186-
require_relative "#{Regexp.last_match(1)}/ruby_wasm.so"
187-
rescue LoadError
188-
require_relative "ruby_wasm.so"
189-
end
190+
self.require_extension
190191
wasm_bytes = packager.package(executor, tmpdir, options)
191192
RubyWasm.logger.info "Size: #{SizeFormatter.format(wasm_bytes.size)}"
192193
case options[:output]
@@ -197,5 +198,15 @@ def do_build(executor, tmpdir, packager, options)
197198
RubyWasm.logger.debug "Wrote #{options[:output]}"
198199
end
199200
end
201+
202+
def require_extension
203+
# Tries to require the extension for the given Ruby version first
204+
begin
205+
RUBY_VERSION =~ /(\d+\.\d+)/
206+
require_relative "#{Regexp.last_match(1)}/ruby_wasm.so"
207+
rescue LoadError
208+
require_relative "ruby_wasm.so"
209+
end
210+
end
200211
end
201212
end

packages/standalone/irb/build-package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ cp -R "$ruby_root" "$workdir/ruby-root"
2222
(
2323
cd "$workdir" && \
2424
"$WASMOPT" --strip-debug ruby-root/usr/local/bin/ruby -o ./ruby-root/ruby.wasm && \
25-
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --mapdir /usr::./ruby-root/usr --mapdir /gems::$package_dir/gems -o "$dist_dir/irb.wasm" && \
25+
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --dir ./ruby-root/usr::/usr --dir $package_dir/gems::/gems -o "$dist_dir/irb.wasm" && \
2626
wasi-preset-args "$dist_dir/irb.wasm" -o "$dist_dir/irb.wasm" -- -I/gems/lib /gems/libexec/irb --prompt default
2727
)

packages/standalone/ruby/build-package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ cp -R "$ruby_root" "$workdir/ruby-root"
2222
(
2323
cd "$workdir" && \
2424
"$WASMOPT" --strip-debug ruby-root/usr/local/bin/ruby -o ./ruby-root/ruby.wasm && \
25-
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --mapdir /usr::./ruby-root/usr -o "$dist_dir/ruby.wasm"
25+
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --dir ./ruby-root/usr::/usr -o "$dist_dir/ruby.wasm"
2626
)

rakelib/packaging.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
wasi_vfs = RubyWasm::WasiVfsProduct.new(File.join(Dir.pwd, "build"))
22
wasi_sdk = TOOLCHAINS["wasi-sdk"]
33
tools = {
4-
"WASI_VFS_CLI" => wasi_vfs.cli_bin_path,
4+
"WASI_VFS_CLI" => File.expand_path(File.join(__dir__, "..", "exe", "rbwasm")),
55
"WASMOPT" => wasi_sdk.wasm_opt
66
}
77

0 commit comments

Comments
 (0)