index.jsView |
---|
| 1 … | +module.exports = class Wasm { |
| 2 … | + |
| 3 … | + * The interface API is the api the exposed to interfaces. All queries about |
| 4 … | + * the enviroment and call to the kernel go through this API |
| 5 … | + */ |
| 6 … | + constructor (code) { |
| 7 … | + this._module = WebAssembly.Module(code) |
| 8 … | + } |
| 9 … | + |
| 10 … | + * Runs the core VM with a given environment and imports |
| 11 … | + */ |
| 12 … | + async run (message, kernel, imports) { |
| 13 … | + const responses = {} |
| 14 … | + |
| 15 … | + * Builds a import map with an array of given interfaces |
| 16 … | + */ |
| 17 … | + async function buildImports (kernelApi, kernel, imports) { |
| 18 … | + const importMap = {} |
| 19 … | + for (const Import of imports) { |
| 20 … | + const response = responses[Import.name] = {} |
| 21 … | + const newIterface = new Import(kernelApi, message, response) |
| 22 … | + importMap[Import.name] = newIterface.exports |
| 23 … | + |
| 24 … | + await newIterface.initialize() |
| 25 … | + } |
| 26 … | + return importMap |
| 27 … | + } |
| 28 … | + |
| 29 … | + let instance |
| 30 … | + const interfaceApi = { |
| 31 … | + |
| 32 … | + * adds an aync operation to the operations queue |
| 33 … | + */ |
| 34 … | + pushOpsQueue: (promise, callbackIndex, intefaceCallback) => { |
| 35 … | + this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => { |
| 36 … | + const result = intefaceCallback(values.pop()) |
| 37 … | + instance.exports.callback.get(callbackIndex)(result) |
| 38 … | + }) |
| 39 … | + }, |
| 40 … | + memory: () => { |
| 41 … | + return instance.exports.memory.buffer |
| 42 … | + }, |
| 43 … | + kernel: kernel |
| 44 … | + } |
| 45 … | + |
| 46 … | + const initializedImports = await buildImports(interfaceApi, kernel, imports) |
| 47 … | + instance = WebAssembly.Instance(this._module, initializedImports) |
| 48 … | + |
| 49 … | + if (instance.exports.main) { |
| 50 … | + instance.exports.main() |
| 51 … | + } |
| 52 … | + await this.onDone() |
| 53 … | + return responses |
| 54 … | + } |
| 55 … | + |
| 56 … | + |
| 57 … | + * returns a promise that resolves when the wasm instance is done running |
| 58 … | + */ |
| 59 … | + async onDone () { |
| 60 … | + let prevOps |
| 61 … | + while (prevOps !== this._opsQueue) { |
| 62 … | + prevOps = this._opsQueue |
| 63 … | + await this._opsQueue |
| 64 … | + } |
| 65 … | + } |
| 66 … | +} |