index.jsView |
---|
| 1 | +const ReferanceMap = require('./referanceMap.js') |
| 2 | + |
1 | 3 | module.exports = class WasmContainer { |
2 | 4 | |
3 | 5 | * The interface API is the api the exposed to interfaces. All queries about |
4 | 6 | * the enviroment and call to the kernel go through this API |
5 | 7 | */ |
6 | | - constructor (code) { |
7 | | - this._module = WebAssembly.Module(code) |
| 8 | + constructor (exoInterface, imports) { |
| 9 | + this.exoInterface = exoInterface |
| 10 | + this.imports = imports |
| 11 | + this.referanceMap = new ReferanceMap() |
8 | 12 | } |
9 | 13 | |
10 | | - static get name () { |
11 | | - return 'wasm' |
12 | | - } |
13 | 14 | |
14 | 15 | * Runs the core VM with a given environment and imports |
15 | 16 | */ |
16 | | - async run (message, kernel, imports = []) { |
17 | | - const responses = {} |
| 17 | + async run (message) { |
18 | 18 | |
19 | 19 | * Builds a import map with an array of given interfaces |
20 | 20 | */ |
21 | | - function buildImports (opts, imports) { |
22 | | - const importMap = {} |
23 | | - for (const Import of imports) { |
24 | | - const name = Import.name |
25 | | - opts.response = responses[name] = {} |
26 | | - const newInterface = new Import(opts) |
27 | | - const props = Object.getOwnPropertyNames(Import.prototype) |
| 21 | + const importMap = {} |
| 22 | + for (const name in this.imports) { |
| 23 | + importMap[name] = {} |
| 24 | + const Import = this.imports[name] |
| 25 | + const newInterface = new Import(this) |
| 26 | + const props = Object.getOwnPropertyNames(Import.prototype) |
28 | 27 | |
29 | | - |
30 | | - for (const prop of props) { |
31 | | - newInterface[prop] = newInterface[prop].bind(newInterface) |
| 28 | + |
| 29 | + for (const prop of props) { |
| 30 | + if (prop !== 'constructor') { |
| 31 | + importMap[name][prop] = newInterface[prop].bind(newInterface) |
32 | 32 | } |
33 | | - importMap[name] = newInterface |
34 | 33 | } |
35 | | - return importMap |
36 | 34 | } |
37 | 35 | |
38 | | - let instance |
39 | | - |
40 | | - const opts = { |
41 | | - vm: { |
42 | | - |
43 | | - * adds an aync operation to the operations queue |
44 | | - */ |
45 | | - pushOpsQueue: (promise, callbackIndex, intefaceCallback) => { |
46 | | - this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => { |
47 | | - const result = intefaceCallback(values.pop()) |
48 | | - instance.exports.callback.get(callbackIndex)(result) |
49 | | - }) |
50 | | - }, |
51 | | - memory: () => { |
52 | | - return instance.exports.memory.buffer |
53 | | - } |
54 | | - }, |
55 | | - kernel: kernel, |
56 | | - message: message |
| 36 | + const result = await WebAssembly.instantiate(this.exoInterface.state['/'].code, importMap) |
| 37 | + this.instance = result.instance |
| 38 | + if (this.instance.exports.main) { |
| 39 | + this.instance.exports.main() |
57 | 40 | } |
58 | | - const initializedImports = buildImports(opts, imports) |
59 | | - instance = WebAssembly.Instance(this._module, initializedImports) |
60 | | - |
61 | | - if (instance.exports.main) { |
62 | | - instance.exports.main() |
63 | | - } |
64 | | - await this.onDone() |
65 | | - return responses |
| 41 | + return this.onDone() |
66 | 42 | } |
67 | 43 | |
68 | 44 | |
69 | 45 | * returns a promise that resolves when the wasm instance is done running |
71 | 47 | async onDone () { |
72 | 48 | let prevOps |
73 | 49 | while (prevOps !== this._opsQueue) { |
74 | 50 | prevOps = this._opsQueue |
75 | | - await this._opsQueue |
| 51 | + await prevOps |
76 | 52 | } |
| 53 | + this.referanceMap.clear() |
77 | 54 | } |
| 55 | + |
| 56 | + pushOpsQueue (promise) { |
| 57 | + this._opsQueue = Promise.all([this._opsQueue, promise]) |
| 58 | + return this._opsQueue |
| 59 | + } |
| 60 | + |
| 61 | + get memory () { |
| 62 | + return this.instance.exports.memory.buffer |
| 63 | + } |
| 64 | + |
| 65 | + getMemory (offset, length) { |
| 66 | + return new Uint8Array(this.memory, offset, length) |
| 67 | + } |
| 68 | + |
| 69 | + static createState (wasm) { |
| 70 | + return { |
| 71 | + nonce: [0], |
| 72 | + ports: {}, |
| 73 | + code: wasm |
| 74 | + } |
| 75 | + } |
78 | 76 | } |