Skip to content

Commit 2302c01

Browse files
Update ruby-wasm-wasi/README.md
1 parent 0e636fa commit 2302c01

File tree

1 file changed

+29
-83
lines changed
  • packages/npm-packages/ruby-wasm-wasi

1 file changed

+29
-83
lines changed

packages/npm-packages/ruby-wasm-wasi/README.md

Lines changed: 29 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The CRuby source code is available at [a working branch](https://github.com/ruby
66

77
## Installation
88

9-
For instaling ruby-wasm-wasi, just run this command in your shell:
9+
For instaling ruby-wasm-wasi family, just run this command in your shell:
1010

1111
### Ruby head
1212

@@ -20,33 +20,14 @@ See [the example project](./example) for more details.
2020

2121
```javascript
2222
import fs from "fs/promises";
23-
import { WASI } from "wasi";
24-
import { RubyVM } from "ruby-head-wasm-wasi";
23+
import { DefaultRubyVM } from "ruby-head-wasm-wasi/dist/node.cjs.js";
2524

2625
const main = async () => {
27-
const wasi = new WASI();
2826
const binary = await fs.readFile(
29-
"./node_modules/ruby-head-wasm-wasi/ruby.wasm"
27+
"./node_modules/ruby-head-wasm-wasi/dist/ruby.wasm"
3028
);
31-
const vm = new RubyVM();
32-
const imports = {
33-
wasi_snapshot_preview1: wasi.wasiImport,
34-
};
35-
36-
// Add imports for WebAssembly instantiation
37-
vm.addToImports(imports);
38-
39-
// Instantiate the WebAssembly module
40-
const { instance } = await WebAssembly.instantiate(binary.buffer, imports);
41-
42-
// Set instance to vm
43-
await vm.setInstance(instance);
44-
45-
// Initialize WASI application
46-
wasi.initialize(instance);
47-
48-
// Initialize Ruby VM
49-
vm.initialize();
29+
const module = await WebAssembly.compile(binary);
30+
const { vm } = await DefaultRubyVM(module);
5031

5132
vm.eval(`
5233
luckiness = ["Lucky", "Unlucky"].sample
@@ -68,67 +49,32 @@ $ node --experimental-wasi-unstable-preview1 index.node.js
6849
In browser, you need a WASI polyfill
6950
See [the example project](./example) for more details.
7051

71-
```javascript
72-
import { WASI } from "@wasmer/wasi";
73-
import { WasmFs } from "@wasmer/wasmfs";
74-
import { RubyVM } from "ruby-head-wasm-wasi";
75-
76-
const main = async () => {
77-
// Setup WASI and FileSystem emulation
78-
const wasmFs = new WasmFs();
79-
const wasi = new WASI({
80-
bindings: {
81-
...WASI.defaultBindings,
82-
fs: wasmFs.fs,
83-
},
84-
});
85-
86-
// (Optional) Forward stdout/stderr to console
87-
const originalWriteSync = wasmFs.fs.writeSync.bind(wasmFs.fs);
88-
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
89-
const text = new TextDecoder("utf-8").decode(buffer);
90-
const handlers = {
91-
1: (line) => console.log(line),
92-
2: (line) => console.warn(line),
52+
```html
53+
<html>
54+
<script src="https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/browser.umd.js"></script>
55+
<script>
56+
const { DefaultRubyVM } = window["ruby-wasm-wasi"];
57+
const main = async () => {
58+
// Fetch and instntiate WebAssembly binary
59+
const response = await fetch(
60+
"https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/ruby.wasm"
61+
);
62+
const buffer = await response.arrayBuffer();
63+
const module = await WebAssembly.compile(buffer);
64+
const { vm } = await DefaultRubyVM(module);
65+
66+
vm.printVersion();
67+
vm.eval(`
68+
require "js"
69+
luckiness = ["Lucky", "Unlucky"].sample
70+
JS::eval("document.body.innerText = '#{luckiness}'")
71+
`);
9372
};
94-
if (handlers[fd]) handlers[fd](text);
95-
return originalWriteSync(fd, buffer, offset, length, position);
96-
};
97-
98-
// Fetch and instantiate WebAssembly binary
99-
const response = await fetch(
100-
"./node_modules/ruby-head-wasm-wasi/dist/ruby.wasm"
101-
);
102-
const buffer = await response.arrayBuffer();
103-
const vm = new RubyVM();
10473
105-
const imports = {
106-
wasi_snapshot_preview1: wasi.wasiImport,
107-
};
108-
109-
// Add imports for WebAssembly instantiation
110-
vm.addToImports(imports);
111-
112-
// Instantiate the WebAssembly module
113-
const { instance } = await WebAssembly.instantiate(buffer, imports);
114-
115-
// Set instance to vm
116-
await vm.setInstance(instance);
117-
118-
// Initialize WASI application
119-
wasi.setMemory(instance.exports.memory);
120-
121-
// Initialize Ruby VM
122-
vm.initialize();
123-
124-
vm.eval(`
125-
require "js"
126-
luckiness = ["Lucky", "Unlucky"].sample
127-
JS::eval("document.body.innerText = '#{luckiness}'")
128-
`);
129-
};
130-
131-
main();
74+
main();
75+
</script>
76+
<body></body>
77+
</html>
13278
```
13379

13480
<!-- The APIs section was generated by `npx documentation readme ./dist/index.esm.js --section=APIs` -->

0 commit comments

Comments
 (0)