Skip to content

Commit ac38a84

Browse files
Add RUBY_ROOT hook point in jest test
1 parent b2fa928 commit ac38a84

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
import fs from "fs/promises";
22
import path from "path";
3-
import { DefaultRubyVM } from "../src/node";
3+
import { WASI } from "wasi";
4+
import { RubyVM } from "../src/index";
45

56
const rubyModule = (async () => {
6-
const binary = await fs.readFile(
7-
path.join(__dirname, "./../dist/ruby+stdlib.wasm")
8-
);
7+
let binaryPath;
8+
if (process.env.RUBY_ROOT) {
9+
binaryPath = path.join(process.env.RUBY_ROOT, "./usr/local/bin/ruby");
10+
} else {
11+
binaryPath = path.join(__dirname, "./../dist/ruby+stdlib.wasm");
12+
}
13+
const binary = await fs.readFile(binaryPath);
914
return await WebAssembly.compile(binary.buffer);
1015
})();
1116

1217
export const initRubyVM = async () => {
13-
return (await DefaultRubyVM(await rubyModule)).vm;
18+
let preopens = {};
19+
if (process.env.RUBY_ROOT) {
20+
preopens["/usr"] = path.join(process.env.RUBY_ROOT, "./usr");
21+
}
22+
const wasi = new WASI({
23+
args: ["ruby.wasm"].concat(process.argv.slice(2)),
24+
preopens,
25+
});
26+
27+
const vm = new RubyVM();
28+
const imports = {
29+
wasi_snapshot_preview1: wasi.wasiImport,
30+
};
31+
32+
vm.addToImports(imports);
33+
34+
const instance = await WebAssembly.instantiate(await rubyModule, imports);
35+
await vm.setInstance(instance);
36+
37+
wasi.initialize(instance);
38+
39+
vm.initialize();
40+
return vm;
1441
};

packages/npm-packages/ruby-wasm-wasi/test/package.test.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "fs/promises";
22
import path from "path";
33
import { WASI } from "wasi";
44
import { RubyVM } from "../dist/index.umd.js";
5+
import { DefaultRubyVM } from "../src/node";
56

67
const initRubyVM = async (rubyModule: WebAssembly.Module, args: string[]) => {
78
const wasi = new WASI();
@@ -29,12 +30,28 @@ const initRubyVM = async (rubyModule: WebAssembly.Module, args: string[]) => {
2930
describe("Packaging validation", () => {
3031
jest.setTimeout(20 /*sec*/ * 1000);
3132

33+
const moduleCache = new Map<string, WebAssembly.Module>();
34+
const loadWasmModule = async (file: string) => {
35+
if (moduleCache.has(file)) {
36+
return moduleCache.get(file)!;
37+
}
38+
const binary = await fs.readFile(path.join(__dirname, `./../dist/${file}`));
39+
const mod = await WebAssembly.compile(binary.buffer);
40+
moduleCache.set(file, mod);
41+
return mod;
42+
};
43+
44+
test("DefaultRubyVM", async () => {
45+
const mod = await loadWasmModule(`ruby+stdlib.wasm`);
46+
const { vm } = await DefaultRubyVM(mod);
47+
vm.eval(`require "stringio"`);
48+
})
49+
3250
test.each([
3351
{ file: "ruby+stdlib.wasm", stdlib: true },
3452
{ file: "ruby.debug+stdlib.wasm", stdlib: true },
3553
])("Load all variants", async ({ file, stdlib }) => {
36-
const binary = await fs.readFile(path.join(__dirname, `./../dist/${file}`));
37-
const mod = await WebAssembly.compile(binary.buffer);
54+
const mod = await loadWasmModule(file);
3855
const { vm } = await initRubyVM(mod, ["ruby.wasm", "-e_=0"]);
3956
// Check loading ext library
4057
vm.eval(`require "stringio"`);
@@ -45,10 +62,7 @@ describe("Packaging validation", () => {
4562
});
4663

4764
test("ruby.debug+stdlib.wasm has debug info", async () => {
48-
const binary = await fs.readFile(
49-
path.join(__dirname, `./../dist/ruby.debug+stdlib.wasm`)
50-
);
51-
const mod = await WebAssembly.compile(binary.buffer);
65+
const mod = await loadWasmModule("ruby.debug+stdlib.wasm");
5266
const nameSections = WebAssembly.Module.customSections(mod, "name");
5367
expect(nameSections.length).toBe(1);
5468
});

0 commit comments

Comments
 (0)