Files: fb80c5e51c634a7cd505d71b34160b19f451cd16 / wasmDebugAgent.js
1536 bytesRaw
1 | const Wasm = require('./wasmAgent') |
2 | |
3 | module.exports = class Test extends Wasm { |
4 | /** |
5 | * Runs the core VM with a given environment and imports |
6 | */ |
7 | async run (message, kernel, imports) { |
8 | const responses = this.responses = {} |
9 | /** |
10 | * Builds a import map with an array of given interfaces |
11 | */ |
12 | async function buildImports (kernelApi, kernel, imports) { |
13 | const importMap = {} |
14 | for (const Import of imports) { |
15 | const response = responses[Import.name] = {} |
16 | const newIterface = new Import(kernelApi, message, response) |
17 | importMap[Import.name] = newIterface.exports |
18 | // initailize the import |
19 | await newIterface.initialize() |
20 | } |
21 | return importMap |
22 | } |
23 | |
24 | let instance |
25 | const interfaceApi = this.api = { |
26 | /** |
27 | * adds an aync operation to the operations queue |
28 | */ |
29 | pushOpsQueue: (promise, callbackIndex, intefaceCallback) => { |
30 | this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => { |
31 | const result = intefaceCallback(values.pop()) |
32 | instance.exports.callback.get(callbackIndex)(result) |
33 | }) |
34 | }, |
35 | memory: () => { |
36 | return instance.exports.memory.buffer |
37 | }, |
38 | kernel: kernel |
39 | } |
40 | |
41 | const initializedImports = await buildImports(interfaceApi, kernel, imports) |
42 | this.instance = instance = WebAssembly.Instance(this._module, initializedImports) |
43 | |
44 | if (instance.exports.main) { |
45 | instance.exports.main() |
46 | } |
47 | await this.onDone() |
48 | return responses |
49 | } |
50 | } |
51 |
Built with git-ssb-web