git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: a01401f258acd7e0b1f4ecbe82cdd1bc26048b4c

Files: a01401f258acd7e0b1f4ecbe82cdd1bc26048b4c / vm.js

1424 bytesRaw
1module.exports = class VM {
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 /**
11 * Runs the core VM with a given environment and imports
12 */
13 async run (environment, imports) {
14 this._environment = environment
15 // TODO, delete the instance once done.
16 const instance = this._instance = WebAssembly.Instance(this._module, imports)
17 if (instance.exports.main) {
18 instance.exports.main()
19 }
20 await this.onDone()
21 const env = this._environment
22 delete this._environment
23 return env
24 }
25
26 /**
27 * returns a promise that resolves when the wasm instance is done running
28 */
29 async onDone () {
30 let prevOps
31 while (prevOps !== this._opsQueue) {
32 prevOps = this._opsQueue
33 await this._opsQueue
34 }
35 }
36
37 /**
38 * addes an aync operation to the operations queue
39 */
40 pushOpsQueue (promise, callbackIndex, intefaceCallback) {
41 this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
42 const result = intefaceCallback(values.pop())
43 this._instance.exports[callbackIndex.toString()](result)
44 })
45 }
46
47 sendMessage (message) {
48
49 }
50
51 get environment () {
52 return this._environment
53 }
54
55 get memory () {
56 return this._instance.exports.memory.buffer
57 }
58}
59

Built with git-ssb-web