-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvm.js
55 lines (48 loc) · 1.31 KB
/
vm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module.exports = class VM {
/**
* The interface API is the api the exposed to interfaces. All queries about
* the enviroment and call to the kernel go through this API
*/
constructor (code) {
this._module = WebAssembly.Module(code)
}
/**
* Runs the core VM with a given environment and imports
*/
run (environment, imports) {
this._environment = environment
// TODO, delete the instance once done.
const instance = this._instance = WebAssembly.Instance(this._module, imports)
if (instance.exports.main) {
instance.exports.main()
}
return this.onDone()
}
/**
* returns a promise that resolves when the wasm instance is done running
*/
async onDone () {
let prevOps
while (prevOps !== this._opsQueue) {
prevOps = this._opsQueue
await this._opsQueue
}
}
/**
* addes an aync operation to the operations queue
*/
pushOpsQueue (promise, callbackIndex, intefaceCallback) {
this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
const result = intefaceCallback(values.pop())
this._instance.exports.callback.get(callbackIndex)(result)
})
}
sendMessage (message) {
}
get environment () {
return this._environment
}
get memory () {
return this._instance.exports.memory.buffer
}
}