Files: f85a2907f19ae1a49abac80203e35a39f173123d / interfaceAPI.js
1196 bytesRaw
1 | module.exports = class interfaceAPI { |
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 | run (environment, imports) { |
11 | this._environment = environment |
12 | // TODO, delete the instance once done. |
13 | const instance = this._instance = WebAssembly.Instance(this._module, imports) |
14 | if (instance.exports.main) { |
15 | instance.exports.main() |
16 | } |
17 | return this.onDone() |
18 | } |
19 | |
20 | // returns a promise that resolves when the wasm instance is done running |
21 | async onDone () { |
22 | let prevOps |
23 | while (prevOps !== this._opsQueue) { |
24 | prevOps = this._opsQueue |
25 | await this._opsQueue |
26 | } |
27 | } |
28 | |
29 | pushOpsQueue (promise, callbackIndex, intefaceCallback) { |
30 | this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => { |
31 | const result = intefaceCallback(values.pop()) |
32 | this._instance.exports[callbackIndex.toString()](result) |
33 | }) |
34 | } |
35 | |
36 | sendMessage (message) { |
37 | |
38 | } |
39 | |
40 | get environment () { |
41 | return this._environment |
42 | } |
43 | |
44 | get memory () { |
45 | return this._instance.exports.memory |
46 | } |
47 | } |
48 |
Built with git-ssb-web