Using gems with Node WASI
Hey, since I needed it myself, and based on https://gist.github.com/kateinoigakukun/5caf3b83b2732b1653e91b0e75ce3390, I wrote JS code that allows you to use gems with Node WASI.
Unfortunately, as you need preopens for that (to map the gems into the WASI vfs), it's currently not possible to use DefaultRubyVM for that, as its options are limited to env.
I wrote my own small wrapper to allow me access to the full breadth of WASI options: https://github.com/primate-run/primate/blob/master/packages/ruby/src/private/wasi.ts, but it took me a bit to understand how to get the RubyVM from the package to mimic the behaviour of DefaultRubyVM. The examples in the cheatsheet are all using DefaultRubyVM.
It is possible to perhaps make DefaultRubyVM a bit more flexible, so that it's possible to use preopens with it?
Alternatively, if you want to keep the defaults simple, perhaps add this example to the readme or cheatsheet? I don't mind sending a PR.
Thank you for your proposal. I think it's a reasonable change, but the reason why I didn't make it a part of public API was because Node.js's WASI and @bjorn3/browser_wasi_shim's WASI implementations have different preopen interface and I didn't come up with an idea to abstract it at that moment. If you can figure out a way, I'm happy to introduce it.
Yep, it looks like the browser shim expects a map from directory to a list of File.
We could pass preopens as is to Node, and for the browser sort it according to directories, and then preload the files into File, but it might turn out too complex. I can see it working well for easy cases like:
{ "/app": process.cwd() } -> recursively scan all directories in process.cwd() and preload their files.
But other cases might be more work.
Would it be simpler to offer two examples in the cheatsheet that involve using RubyVM directly? What took me the longest was figuring out how to get it imported and then redo the logic. For me it also helped dispel some of the magic of DefaultRubyVM. I think it would be good to show users how to use it directly with WASI.
What do you think?
I agree, showing how to use RubyVM directly would be more flexible and clear for advanced users. If you don’t mind, could you add examples based on your experience to the cheatsheet? I think it would be really valuable for others who run into the same situation.