|
| 1 | +import crypto from "crypto"; |
| 2 | +import { WASI } from "@wasmer/wasi"; |
| 3 | +import { WasmFs } from "@wasmer/wasmfs"; |
| 4 | +import path from "path-browserify"; |
| 5 | +import { RubyVM } from "ruby-head-wasm-wasi/dist/index"; |
| 6 | + |
| 7 | +import load from "./app.wasm"; |
| 8 | + |
| 9 | +// This overwrites the default writeSync function used by the WasmFs to instead |
| 10 | +// pipe it out to the console. |
| 11 | +function createWriter(originalWriter: Function) { |
| 12 | + return function () { |
| 13 | + let text: string; |
| 14 | + |
| 15 | + if (arguments.length === 4) { |
| 16 | + text = arguments[1]; |
| 17 | + } else { |
| 18 | + text = new TextDecoder("utf-8").decode(arguments[1]); |
| 19 | + } |
| 20 | + |
| 21 | + switch (arguments[0]) { |
| 22 | + case 1: |
| 23 | + console.log(text); |
| 24 | + break; |
| 25 | + case 2: |
| 26 | + console.warn(text); |
| 27 | + break; |
| 28 | + } |
| 29 | + |
| 30 | + return originalWriter.call(arguments); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export default async function createRuby() { |
| 35 | + // First, create a new file system that we can use internally within the Ruby |
| 36 | + // WASM VM. |
| 37 | + const wasmFs = new WasmFs(); |
| 38 | + wasmFs.fs.mkdirSync("/tmp", 0o777); |
| 39 | + wasmFs.fs.writeSync = createWriter(wasmFs.fs.writeSync.bind(wasmFs.fs)); |
| 40 | + |
| 41 | + // Next, create a new WASI instance with the correct options overridden from |
| 42 | + // the defaults. |
| 43 | + const wasi = new WASI({ |
| 44 | + bindings: { |
| 45 | + ...WASI.defaultBindings, |
| 46 | + fs: wasmFs.fs, |
| 47 | + path: path, |
| 48 | + randomFillSync: crypto.randomFillSync as typeof WASI.defaultBindings.randomFillSync, |
| 49 | + }, |
| 50 | + preopens: { "/": "/tmp" } |
| 51 | + }); |
| 52 | + |
| 53 | + // Then, create a new Ruby VM instance that we can use to store the memory for |
| 54 | + // our application. |
| 55 | + const rubyVM = new RubyVM(); |
| 56 | + const imports = { wasi_snapshot_preview1: wasi.wasiImport }; |
| 57 | + rubyVM.addToImports(imports); |
| 58 | + |
| 59 | + // Set the WASI memory to use the memory for our application. |
| 60 | + const instance = await load(imports); |
| 61 | + wasi.setMemory(instance.exports.memory); |
| 62 | + |
| 63 | + // Load our application into the virtual machine. |
| 64 | + instance.exports._initialize(); |
| 65 | + await rubyVM.setInstance(instance); |
| 66 | + |
| 67 | + // Initial our virtual machine and return it. It should now be able to |
| 68 | + // evaluate and execute Ruby code. |
| 69 | + rubyVM.initialize(); |
| 70 | + |
| 71 | + // Once our virtual machine is booted, we're going to require the necessary |
| 72 | + // files to make it work. I'm not sure why I need to explicitly require |
| 73 | + // did_you_mean here, but it doesn't work without it. |
| 74 | + rubyVM.eval(` |
| 75 | + require "did_you_mean" |
| 76 | + require "json" |
| 77 | +
|
| 78 | + $:.unshift("/lib") |
| 79 | + require_relative "/lib/syntax_tree" |
| 80 | + require_relative "/lib/syntax_tree/haml" |
| 81 | + # require_relative "/lib/syntax_tree/rbs" |
| 82 | + `); |
| 83 | + |
| 84 | + return { |
| 85 | + rubyVM, |
| 86 | + formatHAML(source: string) { |
| 87 | + return format(source, ".haml"); |
| 88 | + }, |
| 89 | + formatRuby(source: string) { |
| 90 | + return format(source, ".rb"); |
| 91 | + }, |
| 92 | + // formatRBS(source: string) { |
| 93 | + // return format(source, ".rbs"); |
| 94 | + // } |
| 95 | + }; |
| 96 | + |
| 97 | + function format(source: string, kind: string) { |
| 98 | + const jsonSource = JSON.stringify(JSON.stringify(source)); |
| 99 | + const rubySource = `SyntaxTree::HANDLERS.fetch("${kind}").format(JSON.parse(${jsonSource}))`; |
| 100 | + return rubyVM.eval(rubySource).toString(); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +export type Ruby = Awaited<ReturnType<typeof createRuby>>; |
0 commit comments