@@ -6,7 +6,7 @@ The CRuby source code is available at [a working branch](https://github.com/ruby
6
6
7
7
## Installation
8
8
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:
10
10
11
11
### Ruby head
12
12
@@ -20,33 +20,14 @@ See [the example project](./example) for more details.
20
20
21
21
``` javascript
22
22
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" ;
25
24
26
25
const main = async () => {
27
- const wasi = new WASI ();
28
26
const binary = await fs .readFile (
29
- " ./node_modules/ruby-head-wasm-wasi/ruby.wasm"
27
+ " ./node_modules/ruby-head-wasm-wasi/dist/ ruby.wasm"
30
28
);
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 );
50
31
51
32
vm .eval (`
52
33
luckiness = ["Lucky", "Unlucky"].sample
@@ -68,67 +49,32 @@ $ node --experimental-wasi-unstable-preview1 index.node.js
68
49
In browser, you need a WASI polyfill
69
50
See [ the example project] ( ./example ) for more details.
70
51
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
+ ` );
93
72
};
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 ();
104
73
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 >
132
78
```
133
79
134
80
<!-- The APIs section was generated by `npx documentation readme ./dist/index.esm.js --section=APIs` -->
0 commit comments