Skip to content

Commit ea73fbf

Browse files
Allow to read/write on in-memory file system on Web browser
This change mounts an in-memory file system provided by browser_wasi_shim.
1 parent 602e02d commit ea73fbf

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/npm-packages/ruby-wasm-wasi/src/browser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fd, WASI } from "@bjorn3/browser_wasi_shim";
1+
import { Fd, File, OpenFile, PreopenDirectory, WASI } from "@bjorn3/browser_wasi_shim";
22
import { consolePrinter } from "./console.js";
33
import { RubyVM } from "./vm.js";
44

@@ -18,7 +18,12 @@ export const DefaultRubyVM = async (
1818
([k, v]) => `${k}=${v}`,
1919
);
2020

21-
const fds: Fd[] = [];
21+
const fds: Fd[] = [
22+
new OpenFile(new File([])),
23+
new OpenFile(new File([])),
24+
new OpenFile(new File([])),
25+
new PreopenDirectory("/", {}),
26+
];
2227
const wasi = new WASI(args, env, fds, { debug: false });
2328
const vm = new RubyVM();
2429

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test, expect, Page } from "@playwright/test";
2+
3+
import {
4+
setupDebugLog,
5+
setupProxy,
6+
setupUncaughtExceptionRejection,
7+
resolveBinding,
8+
} from "../support";
9+
10+
if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
11+
test.skip("skip", () => {});
12+
} else {
13+
test.beforeEach(async ({ context, page }) => {
14+
setupDebugLog(context);
15+
setupProxy(context);
16+
setupUncaughtExceptionRejection(page);
17+
});
18+
19+
test.describe('WASI browser binding', () => {
20+
test("Read/write on in-memory file system", async ({ page }) => {
21+
const resolve = await resolveBinding(page, "checkResolved");
22+
await page.setContent(`
23+
<script src="https://cdn.jsdelivr.net/npm/@ruby/head-wasm-wasi@latest/dist/browser.script.iife.js"></script>
24+
<script type="text/ruby" data-eval="async">
25+
require "js"
26+
File.write("hello.txt", "Hello, world!")
27+
JS.global.checkResolved File.read("hello.txt")
28+
</script>
29+
`);
30+
expect(await resolve()).toBe("Hello, world!");
31+
});
32+
});
33+
}

0 commit comments

Comments
 (0)