git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 8ad1e8d0074a39fe6116a5343888dbb3e75c06d2

Files: 8ad1e8d0074a39fe6116a5343888dbb3e75c06d2 / interfaceAPI.js

1175 bytesRaw
1module.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 intefaceCallback(values.pop())
32 this._instance.exports[callbackIndex.toString()]()
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