|
| 1 | +import { Exit, Environment, directoryFromTree, directoryIntoTree } from './wasi-virt.js'; |
| 2 | + |
| 3 | +export { Exit } from './wasi-virt.js'; |
| 4 | + |
| 5 | +export class BaseApplication { |
| 6 | + constructor(getResources, wasmModules, instantiate) { |
| 7 | + this._resources = null; |
| 8 | + this.getResources = getResources; |
| 9 | + this.wasmModules = wasmModules; |
| 10 | + this.instantiate = instantiate; |
| 11 | + } |
| 12 | + |
| 13 | + async run(args, files, printLine = console.log) { |
| 14 | + if (this._resources === null) |
| 15 | + this._resources = await this.getResources(); |
| 16 | + |
| 17 | + const environment = new Environment(); |
| 18 | + environment.args = args; |
| 19 | + environment.root = directoryFromTree(files); |
| 20 | + for (const [dirName, resourceFiles] of Object.entries(this._resources)) |
| 21 | + environment.root.files[dirName] = directoryFromTree(resourceFiles); |
| 22 | + environment.printLine = printLine; |
| 23 | + |
| 24 | + const wasmCommand = await this.instantiate( |
| 25 | + (filename) => this.wasmModules[filename], |
| 26 | + { runtime: environment.exports }); |
| 27 | + try { |
| 28 | + wasmCommand.run.run(); |
| 29 | + } catch (e) { |
| 30 | + if (!(e instanceof Exit && e.code === 0)) |
| 31 | + throw e; |
| 32 | + } |
| 33 | + |
| 34 | + for (const dirName of Object.keys(this._resources)) |
| 35 | + delete environment.root[dirName]; |
| 36 | + return directoryIntoTree(environment.root); |
| 37 | + } |
| 38 | +} |
0 commit comments