|
1 | | -# await: *await* |
| 1 | +# await: *await*, loading |
2 | 2 |
|
3 | 3 | require 'await' |
4 | 4 |
|
@@ -58,40 +58,55 @@ def wasm_module |
58 | 58 | end |
59 | 59 | end |
60 | 60 |
|
61 | | - def run(source, writer) |
62 | | - CRubyWASI.inject_scripts.await |
63 | | - |
64 | | - %x{ |
65 | | - const $WASI = window["WASI"].WASI; |
66 | | - const $WasmFs = window["WasmFs"].WasmFs; |
67 | | - const $RubyVM = window["ruby-wasm-wasi"].RubyVM; |
68 | | -
|
69 | | - const wasmFs = new $WasmFs(); |
70 | | - const originalWriteSync = wasmFs.fs.writeSync.bind(wasmFs.fs); |
71 | | - const textDecoder = new TextDecoder("utf-8"); |
72 | | - wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => { |
73 | | - const text = textDecoder.decode(buffer); |
74 | | - if (fd == 1 || fd == 2) { |
75 | | - #{writer.print_to_output(`text`, "")}; |
76 | | - } |
77 | | - return originalWriteSync(fd, buffer, offset, length, position); |
78 | | - }; |
79 | | -
|
80 | | - const vm = new $RubyVM(); |
81 | | - const wasi = new $WASI({ |
| 61 | + def run(source) |
| 62 | + `var $WASI, $WasmFs, $RubyVM` |
| 63 | + wasmInstance, wasmModule, vm, wasi, imports = nil |
| 64 | + |
| 65 | + loading("downloading scripts") { CRubyWASI.inject_scripts.await } |
| 66 | + |
| 67 | + loading("early load") do |
| 68 | + `$WASI = window["WASI"].WASI` |
| 69 | + `$WasmFs = window["WasmFs"].WasmFs` |
| 70 | + `$RubyVM = window["ruby-wasm-wasi"].RubyVM` |
| 71 | + |
| 72 | + wasmFs = `new $WasmFs()` |
| 73 | + originalWriteSync = `wasmFs.fs.writeSync.bind(wasmFs.fs)` |
| 74 | + textDecoder = `new TextDecoder("utf-8")` |
| 75 | + %x{ |
| 76 | + wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => { |
| 77 | + const text = textDecoder.decode(buffer); |
| 78 | + if (fd == 1 || fd == 2) { |
| 79 | + #{@writer.print_to_output(`text`, "")}; |
| 80 | + } |
| 81 | + return originalWriteSync(fd, buffer, offset, length, position); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + vm = `new $RubyVM()` |
| 86 | + wasi = `new $WASI({ |
82 | 87 | bindings: { ...$WASI.defaultBindings, fs: wasmFs.fs }, |
83 | | - }); |
84 | | - const imports = { wasi_snapshot_preview1: wasi.wasiImport }; |
85 | | - vm.addToImports(imports); |
86 | | - const wasmInstance = await WebAssembly.instantiate(#{wasm_module.await}, imports); |
87 | | - await vm.setInstance(wasmInstance); |
88 | | - wasi.setMemory(wasmInstance.exports.memory); |
89 | | - vm.initialize(); |
90 | | - } |
| 88 | + })` |
| 89 | + imports = `{ wasi_snapshot_preview1: wasi.wasiImport }` |
| 90 | + `vm.addToImports(imports)` |
| 91 | + end |
| 92 | + |
| 93 | + loading("downloading ruby") do |
| 94 | + wasmModule = wasm_module.await |
| 95 | + end |
| 96 | + |
| 97 | + loading("instantiating") do |
| 98 | + wasmInstance = `WebAssembly.instantiate(wasmModule, imports)`.await |
| 99 | + end |
| 100 | + |
| 101 | + loading("initializing") do await |
| 102 | + `vm.setInstance(wasmInstance)`.await |
| 103 | + `wasi.setMemory(wasmInstance.exports.memory)` |
| 104 | + `vm.initialize()` |
| 105 | + end |
91 | 106 |
|
92 | 107 | yield `vm.eval(source).toString()` |
93 | 108 | rescue JS::Error => err |
94 | | - writer.log_error(err) |
| 109 | + @writer.log_error(err) |
95 | 110 | end |
96 | 111 | end |
97 | 112 | end |
0 commit comments