Repository navigation
Ruby::Box support—is it possible in Ruby WASM? #645
Description
Activity
Yes, Ruby::Box can run on ruby.wasm.
Importantly, to enable Ruby::Box, you must specify RUBY_BOX=1 as an environment variable when starting the Ruby VM. Setting ENV[“RUBY_BOX”] = “1” from within Ruby code is too late.
For example, this works on Node.js:
import fs from "fs/promises";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/node";
const binary = await fs.readFile(
"./node_modules/@ruby/head-wasm-wasi/dist/ruby.wasm",
);
const module = await WebAssembly.compile(binary);
const { vm } = await DefaultRubyVM(module, {
env: {
RUBY_BOX: "1",
},
});
const result = vm.eval(`
box = Ruby::Box.new
box.eval <<~RUBY
X = 123
RUBY
puts "Ruby::Box.enabled?: #{Ruby::Box.enabled?}"
puts "box::X: #{box::X}"
`);Example output:
$ node ruby-box.node.js
(node:8080) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ruby.wasm: warning: Ruby::Box is experimental, and the behavior may change in the future!
See https://docs.ruby-lang.org/en/master/Ruby/Box.html for known issues, etc.
Ruby::Box.enabled?: true
box::X: 123The same applies in browsers if you instantiate the VM manually, e.g. by passing env to DefaultRubyVM:
const { vm } = await DefaultRubyVM(module, {
env: {
RUBY_BOX: "1",
},
});Note
Currently, browser.script.iife.js does not provide a way to pass environment variables from HTML.
Are there any plans to update browser.script.iffe.js to make it easy to pass in custom options?
This is now supported as of ruby.wasm v2.10.0, via #649.
browser.script.iife.js accepts a data-env attribute containing a JSON object, so Ruby::Box can now be enabled directly from HTML:
<script
src="https://cdn.jsdelivr.net/npm/@ruby/4.0-wasm-wasi@2.10.1/dist/browser.script.iife.js"
data-env='{"RUBY_BOX":"1"}'
></script>
<script type="text/ruby">
box = Ruby::Box.new
box.eval("X = 123")
puts "Ruby::Box.enabled?: #{Ruby::Box.enabled?}"
puts "box::X: #{box::X}"
</script>The values in data-env must be strings.
I believe this addresses your original question and the subsequent requests.
Please let me know if there’s anything else you need. Otherwise, I think it’s fine to close this issue.
Thanks I'll plan to try that and report back. (I previously just altered the JS file, which has worked out well.)
Is it possible to enable Ruby::Box in Ruby WASM?