|
| 1 | +import fs from "fs/promises"; |
| 2 | +import path from "path"; |
| 3 | +import { WASI } from "wasi"; |
| 4 | +import { RubyVM } from "../dist/index.umd.js"; |
| 5 | + |
| 6 | +const initRubyVM = async (rubyModule: WebAssembly.Module, args: string[]) => { |
| 7 | + const wasi = new WASI(); |
| 8 | + const vm = new RubyVM(); |
| 9 | + const imports = { |
| 10 | + wasi_snapshot_preview1: wasi.wasiImport, |
| 11 | + }; |
| 12 | + |
| 13 | + vm.addToImports(imports); |
| 14 | + |
| 15 | + const instance = await WebAssembly.instantiate(rubyModule, imports); |
| 16 | + |
| 17 | + await vm.setInstance(instance); |
| 18 | + |
| 19 | + wasi.initialize(instance); |
| 20 | + vm.initialize(); |
| 21 | + |
| 22 | + return { |
| 23 | + vm, |
| 24 | + wasi, |
| 25 | + instance, |
| 26 | + }; |
| 27 | +}; |
| 28 | + |
| 29 | +describe("Packaging validation", () => { |
| 30 | + jest.setTimeout(20 /*sec*/ * 1000); |
| 31 | + |
| 32 | + test.each([ |
| 33 | + { file: "ruby+stdlib.wasm", stdlib: true }, |
| 34 | + { file: "ruby.debug.wasm", stdlib: false }, |
| 35 | + { file: "ruby.debug+stdlib.wasm", stdlib: true }, |
| 36 | + ])("Load all variants", async ({ file, stdlib }) => { |
| 37 | + const binary = await fs.readFile(path.join(__dirname, `./../dist/${file}`)); |
| 38 | + const mod = await WebAssembly.compile(binary.buffer); |
| 39 | + const { vm } = await initRubyVM(mod, ["ruby.wasm", "-e_=0"]); |
| 40 | + // Check loading ext library |
| 41 | + vm.eval(`require "stringio"`) |
| 42 | + if (stdlib) { |
| 43 | + // Check loading stdlib gem |
| 44 | + vm.eval(`require "English"`) |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | +}); |
0 commit comments