Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 14c06e7

Browse files
committed
Simplify to use new DefaultRubyVM
1 parent 50bdb3e commit 14c06e7

File tree

8 files changed

+564
-273
lines changed

8 files changed

+564
-273
lines changed

bin/build

Lines changed: 0 additions & 21 deletions
This file was deleted.

bin/build.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import esbuild from "esbuild";
2+
import url from "url";
3+
import wasmPlugin from "./wasmPlugin.mjs";
4+
5+
const entryPoint = url.fileURLToPath(new URL("../src/index.ts", import.meta.url));
6+
const outdir = url.fileURLToPath(new URL("../docs", import.meta.url));
7+
8+
const { metafile } = await esbuild.build({
9+
bundle: true,
10+
entryPoints: [entryPoint],
11+
format: "esm",
12+
metafile: true,
13+
minify: true,
14+
outdir,
15+
plugins: [wasmPlugin],
16+
sourcemap: true,
17+
splitting: true,
18+
target: "es6"
19+
});
20+
21+
const analysis = await esbuild.analyzeMetafile(metafile);
22+
console.log(analysis);

bin/serve

Lines changed: 0 additions & 25 deletions
This file was deleted.

bin/serve.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import esbuild from "esbuild";
2+
import url from "url";
3+
import wasmPlugin from "./wasmPlugin.mjs";
4+
5+
const entryPoint = url.fileURLToPath(new URL("../src/index.ts", import.meta.url));
6+
const outdir = url.fileURLToPath(new URL("../docs", import.meta.url));
7+
8+
const ctx = await esbuild.context({
9+
bundle: true,
10+
entryPoints: [entryPoint],
11+
format: "esm",
12+
outdir,
13+
plugins: [wasmPlugin],
14+
sourcemap: true,
15+
splitting: true,
16+
target: "esnext"
17+
});
18+
19+
const { host, port } = await ctx.serve({ servedir: outdir });
20+
console.log(`Listening at ${host}:${port}`);

bin/wasmPlugin.js renamed to bin/wasmPlugin.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
module.exports = {
1+
import path from 'node:path';
2+
import fs from 'node:fs';
3+
4+
export default {
25
name: 'wasm',
36
setup(build) {
4-
let path = require('path')
5-
let fs = require('fs')
6-
77
// Resolve ".wasm" files to a path with a namespace
88
build.onResolve({ filter: /\.wasm$/ }, args => {
99
// If this is the import inside the stub module, import the
@@ -37,7 +37,7 @@ module.exports = {
3737
// binary itself is imported from a second virtual module.
3838
build.onLoad({ filter: /.*/, namespace: 'wasm-stub' }, async (args) => ({
3939
contents: `import wasm from ${JSON.stringify(args.path)}
40-
export default (imports) => WebAssembly.instantiate(wasm, imports).then((result) => result.instance)`,
40+
export default () => WebAssembly.compile(wasm)`,
4141
}))
4242

4343
// Virtual modules in the "wasm-binary" namespace contain the
@@ -50,3 +50,4 @@ module.exports = {
5050
}))
5151
},
5252
}
53+

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"license": "MIT",
33
"scripts": {
4-
"build": "bin/build",
5-
"serve": "bin/serve"
4+
"build": "node bin/build.mjs",
5+
"serve": "node bin/serve.mjs"
66
},
77
"dependencies": {
88
"@monaco-editor/loader": "^1.3.2",
99
"@wasmer/wasi": "^0.12.0",
1010
"@wasmer/wasmfs": "^0.12.0",
11-
"mermaid": "^9.4.0",
11+
"mermaid": "^10.0.0",
1212
"path-browserify": "^1.0.1",
13-
"ruby-head-wasm-wasi": "^0.3.0"
13+
"ruby-head-wasm-wasi": "^0.6.0"
1414
},
1515
"devDependencies": {
16-
"@types/node": "^17.0.33",
16+
"@types/node": "^18.14.1",
1717
"@types/path-browserify": "^1.0.0",
18-
"esbuild": "^0.14.39",
19-
"monaco-editor": "^0.33.0"
18+
"esbuild": "^0.17.10",
19+
"monaco-editor": "^0.36.0"
2020
}
2121
}

src/createRuby.ts

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,17 @@
11
import { WASI } from "@wasmer/wasi";
22
import { WasmFs } from "@wasmer/wasmfs";
33
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";
55

66
import load from "./app.wasm";
77

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-
338
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())
6410

6511
// Once our virtual machine is booted, we're going to require the necessary
6612
// files to make it work. I'm not sure why I need to explicitly require
6713
// did_you_mean here, but it doesn't work without it.
68-
ruby.eval(`
14+
vm.eval(`
6915
require "rubygems"
7016
require "did_you_mean"
7117
require "json"
@@ -81,7 +27,7 @@ export default async function createRuby() {
8127
const jsonSource = JSON.stringify(JSON.stringify(source));
8228
const rubySource = `RubyVM::InstructionSequence.compile(JSON.parse(${jsonSource})).disasm`;
8329

84-
return ruby.eval(rubySource).toString();
30+
return vm.eval(rubySource).toString();
8531
},
8632
mermaid(source: string) {
8733
const jsonSource = JSON.stringify(JSON.stringify(source));
@@ -90,23 +36,23 @@ export default async function createRuby() {
9036
SyntaxTree.parse(source).to_mermaid
9137
`;
9238

93-
return ruby.eval(rubySource).toString();
39+
return vm.eval(rubySource).toString();
9440
},
9541
// A function that calls through to the SyntaxTree.format function to get
9642
// the pretty-printed version of the source.
9743
format(source: string) {
9844
const jsonSource = JSON.stringify(JSON.stringify(source));
9945
const rubySource = `SyntaxTree.format(JSON.parse(${jsonSource}))`;
10046

101-
return ruby.eval(rubySource).toString();
47+
return vm.eval(rubySource).toString();
10248
},
10349
// A function that calls through to PP to get the pretty-printed version of
10450
// the syntax tree.
10551
prettyPrint(source: string) {
10652
const jsonSource = JSON.stringify(JSON.stringify(source));
10753
const rubySource = `PP.pp(SyntaxTree.parse(JSON.parse(${jsonSource})), +"", 80)`;
10854

109-
return ruby.eval(rubySource).toString();
55+
return vm.eval(rubySource).toString();
11056
}
11157
};
11258
};

0 commit comments

Comments
 (0)