wasmtime-rb icon indicating copy to clipboard operation
wasmtime-rb copied to clipboard

Map directory for instance

Open alhafoudh opened this issue 1 year ago • 6 comments

Is it possible to map directory for WASI instance?

I did not find any reference to this.

I need equivalent of wasmtime run app.wasm --mapdir /guest::/host ...

alhafoudh avatar Feb 04 '25 22:02 alhafoudh

You're right, it is not implemented. We'd need to add a wrapper around preopened_dir in ext/src/ruby_api/wasi_ctx_builder.rs.

Do you want to take a stab at it?

jbourassa avatar Feb 05 '25 00:02 jbourassa

@jbourassa I have the implementation ready but I don't know how to test it. The rake pkg:ruby command produces unusable gem.

I get error:

cannot load such file -- wasmtime/wasmtime_rb (LoadError)
cannot load such file -- wasmtime/3.4/wasmtime_rb (LoadError)

alhafoudh avatar Feb 05 '25 21:02 alhafoudh

rake compile:dev produces the compiled binary. You should be able to use it directly with either: ruby -I lib or irb -I lib, then require "wasmtime":

❯ rake compile:dev
...snip...
❯ irb -I lib
>> require "wasmtime"
=> true
>> Wasmtime::Engine.new
=> #<Wasmtime::Engine:0x0000000104f6d470>

jbourassa avatar Feb 05 '25 21:02 jbourassa

Got it, thanks! The preopened_dir works. I will write some specs and issue a pull request.

One last thing, when I run it with wasmtime-rb locally, the Wasmtime::Module.from_file takes 1-2 minutes to load. When I use wasmtime-rb from rubygems.org it takes to load 3-5 seconds. Do you have any idea why?

Could the :dev in compile:dev cause it?

alhafoudh avatar Feb 05 '25 22:02 alhafoudh

The rake pkg:ruby command produces unusable gem.

How did you install it? Was there any error on install? I don't know what's different in my env but it worked for me:

gem install --local pkg/wasmtime-29.0.0.gem && \
  ruby -e 'require "wasmtime"; Wasmtime::Module.new(Wasmtime::Engine.new, "(module)")'

Asking to see if something's broken so that we can fix it.


One last thing, when I run it with wasmtime-rb locally, the Wasmtime::Module.from_file takes 1-2 minutes to load. When I use wasmtime-rb from rubygems.org it takes to load 3-5 seconds. Do you have any idea why?

Could the :dev in compile:dev cause it?

That would be my guess -- you can try the slower rake compile:release and see if that brings it back to 3-5s?

I will write some specs and issue a pull request.

Testing WASI is tricky, looking forward to seeing the specs you'll come up with! Please keep in mind that we want the test suite to be fast, so a very small Wasm would be ideal.

jbourassa avatar Feb 06 '25 02:02 jbourassa

Yes, the compile:release helped to speed it up back to 3-5 seconds. 👌

Here is the PR without tests for now. Can you take a quick look at it? WDYT?

https://github.com/bytecodealliance/wasmtime-rb/pull/427

alhafoudh avatar Feb 06 '25 06:02 alhafoudh