1
1
import { WASI } from "@wasmer/wasi" ;
2
2
import { WasmFs } from "@wasmer/wasmfs" ;
3
3
import path from "path-browserify" ;
4
- import { RubyVM } from "ruby-head-wasm-wasi/dist/index " ;
4
+ import { DefaultRubyVM } from "ruby-head-wasm-wasi/dist/browser.esm.js " ;
5
5
6
6
import load from "./app.wasm" ;
7
7
8
- // This overwrites the default writeSync function used by the WasmFs to instead
9
- // pipe it out to the console.
10
- function createWriter ( originalWriter : Function ) {
11
- return function ( ) {
12
- let text : string ;
13
-
14
- if ( arguments . length === 4 ) {
15
- text = arguments [ 1 ] ;
16
- } else {
17
- text = new TextDecoder ( "utf-8" ) . decode ( arguments [ 1 ] ) ;
18
- }
19
-
20
- switch ( arguments [ 0 ] ) {
21
- case 1 :
22
- console . log ( text ) ;
23
- break ;
24
- case 2 :
25
- console . warn ( text ) ;
26
- break ;
27
- }
28
-
29
- return originalWriter . call ( arguments ) ;
30
- }
31
- }
32
-
33
8
export default async function createRuby ( ) {
34
- // First, create a new file system that we can use internally within the Ruby
35
- // WASM VM.
36
- const wasmFs = new WasmFs ( ) ;
37
- wasmFs . fs . mkdirSync ( "/tmp" , 0o777 ) ;
38
- wasmFs . fs . writeSync = createWriter ( wasmFs . fs . writeSync . bind ( wasmFs . fs ) ) ;
39
-
40
- // Next, create a new WASI instance with the correct options overridden from
41
- // the defaults.
42
- const wasi = new WASI ( {
43
- bindings : { ...WASI . defaultBindings , fs : wasmFs . fs , path : path } ,
44
- preopens : { "/" : "/tmp" }
45
- } ) ;
46
-
47
- // Then, create a new Ruby VM instance that we can use to store the memory for
48
- // our application.
49
- const ruby = new RubyVM ( ) ;
50
- const imports = { wasi_snapshot_preview1 : wasi . wasiImport } ;
51
- ruby . addToImports ( imports ) ;
52
-
53
- // Set the WASI memory to use the memory for our application.
54
- const instance = await load ( imports ) ;
55
- wasi . setMemory ( instance . exports . memory ) ;
56
-
57
- // Load our application into the virtual machine.
58
- instance . exports . _initialize ( ) ;
59
- await ruby . setInstance ( instance ) ;
60
-
61
- // Initial our virtual machine and return it. It should now be able to
62
- // evaluate and execute Ruby code.
63
- ruby . initialize ( ) ;
9
+ const { vm } = await DefaultRubyVM ( await load ( ) )
64
10
65
11
// Once our virtual machine is booted, we're going to require the necessary
66
12
// files to make it work. I'm not sure why I need to explicitly require
67
13
// did_you_mean here, but it doesn't work without it.
68
- ruby . eval ( `
14
+ vm . eval ( `
69
15
require "rubygems"
70
16
require "did_you_mean"
71
17
require "json"
@@ -81,7 +27,7 @@ export default async function createRuby() {
81
27
const jsonSource = JSON . stringify ( JSON . stringify ( source ) ) ;
82
28
const rubySource = `RubyVM::InstructionSequence.compile(JSON.parse(${ jsonSource } )).disasm` ;
83
29
84
- return ruby . eval ( rubySource ) . toString ( ) ;
30
+ return vm . eval ( rubySource ) . toString ( ) ;
85
31
} ,
86
32
mermaid ( source : string ) {
87
33
const jsonSource = JSON . stringify ( JSON . stringify ( source ) ) ;
@@ -90,23 +36,23 @@ export default async function createRuby() {
90
36
SyntaxTree.parse(source).to_mermaid
91
37
` ;
92
38
93
- return ruby . eval ( rubySource ) . toString ( ) ;
39
+ return vm . eval ( rubySource ) . toString ( ) ;
94
40
} ,
95
41
// A function that calls through to the SyntaxTree.format function to get
96
42
// the pretty-printed version of the source.
97
43
format ( source : string ) {
98
44
const jsonSource = JSON . stringify ( JSON . stringify ( source ) ) ;
99
45
const rubySource = `SyntaxTree.format(JSON.parse(${ jsonSource } ))` ;
100
46
101
- return ruby . eval ( rubySource ) . toString ( ) ;
47
+ return vm . eval ( rubySource ) . toString ( ) ;
102
48
} ,
103
49
// A function that calls through to PP to get the pretty-printed version of
104
50
// the syntax tree.
105
51
prettyPrint ( source : string ) {
106
52
const jsonSource = JSON . stringify ( JSON . stringify ( source ) ) ;
107
53
const rubySource = `PP.pp(SyntaxTree.parse(JSON.parse(${ jsonSource } )), +"", 80)` ;
108
54
109
- return ruby . eval ( rubySource ) . toString ( ) ;
55
+ return vm . eval ( rubySource ) . toString ( ) ;
110
56
}
111
57
} ;
112
58
} ;
0 commit comments