Wasm Builders 🧱

Richard Zak
Richard Zak

Posted on • Updated on

Ruby & WebAssembly

Recently (3 April), Ruby 3.2 Preview 1 was released. So let's try it!

  1. Download the pre-compiled Ruby Wasm file from the Ruby.wasm releases page.
    • Decompress the file.
    • cd head-wasm32-unknown-wasi-full Change directory to the newly created directory.
    • mv usr/local/bin/ruby ruby.wasm
  2. Create a simple Ruby script. I used this: puts "Hello, Enarx!";puts RUBY_DESCRIPTION. This just says "Hello", but more importantly, shows the version information and platform of the Ruby interpreter. Let's create this file in a src/hello_enarx.rb inside the "head" directory from the prior step.
    • Test the script by running ruby src/hello_enarx.rb.
    • On my system, the version information includes "x86_64-linux-gnu".
  3. Next, download wasi-vfs (Wasi virtual filesystem) program. It's created by a member of the Ruby development team. Choose the file for your system (Linux, right??).
    • Decompress the file
    • Place the wasi-vfs binary somewhere in your $PATH. For me, it was in ~/bin/.
  4. Create the new Ruby wasm file by packing the script we wish to run, and the Ruby library of scripts.
    • wasi-vfs pack ruby.wasm --mapdir /usr::usr/ --mapdir /src::src/ -o ruby_full.wasm
    • Test that it works: wasmtime run ruby_full.wasm -- /src/hello_enarx.rb
    • This time, the version information includes "wasm32-wasi"!
    • Notice that the command to wasmtime includes a forward slash before "src". That's because we're referring to the script inside the virtual filesystem inside the Wasm file.
  5. Optional: We can embed the instruction to Ruby to run our script into the Wasm file as well! This requires wasi-preset-args from the same Ruby developer who made the wasi-vfs program.
    • Download and extract the file.
    • Put the binary in your $PATH for convenience.
    • We'll create a new Wasm file with the arguments embedded: wasi-preset-args ruby_full.wasm -o ruby_full2.wasm -- /src/hello_enarx.rb.
    • Now we test that this new "ruby_full2.wasm" works without the path to the script: wasmtime ruby_full2.wasm.
  6. Running with Enarx. Enarx runs WebAssembly in Trusted Execution Environment (TEEs) to secure the running code (and data) from prying eyes.
    • First, we'll need an Enarx.toml file. The first line contains the command line arguments. Since the first argument that Ruby (and most programs) expect is the name of the running program, we'll have two arguments: "ruby" and "/src/hello_enarx.rb".
    • To run Enarx, we'll pass the path of the Toml file with the command line option --wasmcfgfile Enarx.toml.
    • Notice that Ruby is able to run the Wasm file which doesn't have the arguments embedded.

Ruby & WebAssembly

Ruby WebAssembly

Ruby & Enarx

Ruby running in Enarx

There are some limitations. The scripts added in the wasi-vfs stage have to be plain text Ruby scripts. Looking at the pull request which added Wasi support, it seems that it might be possible to build in extra modules which must be compiled and compile time. I have not tested this though.

Credit goes to @kateinoigakukun for the Ruby Wasi code and supporting applications which made this possible.

I have not tested wasi-vfs with Python, but it could work to add some built-in scripts into a Wasm file.

Top comments (1)

cohix profile image
Connor Hicks

This is awesome! Thanks so much for the rundown.