Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/build-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@ wit-bindgen js \
wasm-opt --strip-debug "$ruby_root/usr/local/bin/ruby" -o "$dist_dir/ruby.wasm"
cp "$ruby_root/usr/local/bin/ruby" "$dist_dir/ruby.debug.wasm"

# Build +stdlib versions (removing files that are not used in normal use cases)
workdir="$(mktemp -d)"
cp -R "$ruby_root" "$workdir/ruby-root"
rm -rf $workdir/ruby-root/usr/local/include
rm -f $workdir/ruby-root/usr/local/lib/libruby-static.a
rm -f $workdir/ruby-root/usr/local/bin/ruby
wasi-vfs pack "$dist_dir/ruby.wasm" --mapdir /usr::$workdir/ruby-root/usr -o "$dist_dir/ruby+stdlib.wasm"
wasi-vfs pack "$dist_dir/ruby.debug.wasm" --mapdir /usr::$workdir/ruby-root/usr -o "$dist_dir/ruby.debug+stdlib.wasm"


mkdir "$dist_dir/bindgen"
cp $(find "$package_dir/src/bindgen" -name "*.js" -or -name "*.d.ts") "$dist_dir/bindgen"
1 change: 1 addition & 0 deletions packages/npm-packages/ruby-wasm-wasi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { addRbJsAbiHostToImports, JsAbiValue } from "./bindgen/rb-js-abi-host";
* const instance = await WebAssembly.instantiate(rubyModule, imports);
* await vm.setInstance(instance);
* wasi.initialize(instance);
* vm.initialize();
*
*/
export class RubyVM {
Expand Down
48 changes: 48 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/test/package.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from "fs/promises";
import path from "path";
import { WASI } from "wasi";
import { RubyVM } from "../dist/index.umd.js";

const initRubyVM = async (rubyModule: WebAssembly.Module, args: string[]) => {
const wasi = new WASI();
const vm = new RubyVM();
const imports = {
wasi_snapshot_preview1: wasi.wasiImport,
};

vm.addToImports(imports);

const instance = await WebAssembly.instantiate(rubyModule, imports);

await vm.setInstance(instance);

wasi.initialize(instance);
vm.initialize();

return {
vm,
wasi,
instance,
};
};

describe("Packaging validation", () => {
jest.setTimeout(20 /*sec*/ * 1000);

test.each([
{ file: "ruby+stdlib.wasm", stdlib: true },
{ file: "ruby.debug.wasm", stdlib: false },
{ file: "ruby.debug+stdlib.wasm", stdlib: true },
])("Load all variants", async ({ file, stdlib }) => {
const binary = await fs.readFile(path.join(__dirname, `./../dist/${file}`));
const mod = await WebAssembly.compile(binary.buffer);
const { vm } = await initRubyVM(mod, ["ruby.wasm", "-e_=0"]);
// Check loading ext library
vm.eval(`require "stringio"`)
if (stdlib) {
// Check loading stdlib gem
vm.eval(`require "English"`)
}
});

});