git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 35dfa5477c5c02ad53d5eef194860e48f9677897

Files: 35dfa5477c5c02ad53d5eef194860e48f9677897 / vm.js

1341 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 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 return this.onDone()
21 }
22
23 /**
24 * returns a promise that resolves when the wasm instance is done running
25 */
26 async onDone () {
27 let prevOps
28 while (prevOps !== this._opsQueue) {
29 prevOps = this._opsQueue
30 await this._opsQueue
31 }
32 }
33
34 /**
35 * addes an aync operation to the operations queue
36 */
37 pushOpsQueue (promise, callbackIndex, intefaceCallback) {
38 this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
39 const result = intefaceCallback(values.pop())
40 this._instance.exports[callbackIndex.toString()](result)
41 })
42 }
43
44 sendMessage (message) {
45
46 }
47
48 get environment () {
49 return this._environment
50 }
51
52 get memory () {
53 return this._instance.exports.memory.buffer
54 }
55}
56

Built with git-ssb-web