Files: 78df5549b3d8b084519490f3fb698ff93970c907 / index.js
3470 bytesRaw
1 | const ReferanceMap = require('reference-map') |
2 | const AbstractContainer = require('primea-abstract-container') |
3 | const ContainerTable = require('primea-container-table') |
4 | |
5 | module.exports = class WasmContainer extends AbstractContainer { |
6 | /** |
7 | * The wasm container runs wasm code and provides a basic API for wasm |
8 | * interfaces for interacting with the kernel |
9 | * @param {object} kernel - the kernel instance |
10 | * @param {object} interfaces - a map of interfaces to expose to the wasm binary |
11 | */ |
12 | constructor (kernel, interfaces) { |
13 | super() |
14 | this.kernel = kernel |
15 | this.imports = interfaces |
16 | this.referanceMap = new ReferanceMap() |
17 | } |
18 | |
19 | async onCreation (message) { |
20 | let code = this.kernel.code |
21 | if (!WebAssembly.validate(code)) { |
22 | throw new Error('invalid wasm binary') |
23 | } else { |
24 | for (const name in this.imports) { |
25 | const interf = this.imports[name] |
26 | if (interf.initialize) { |
27 | code = await interf.initialize(code) |
28 | } |
29 | } |
30 | this.kernel.state.code = code |
31 | } |
32 | return this._run(message, 'onCreation') |
33 | } |
34 | |
35 | /** |
36 | * Runs the wasm VM given a message |
37 | * @param {object} message |
38 | * @returns {Promise} a promise that resolves once the compuation is finished |
39 | */ |
40 | onMessage (message) { |
41 | return this._run(message, 'onMessage') |
42 | } |
43 | |
44 | async _run (message, method) { |
45 | // Builds a import map with an array of given interfaces |
46 | const importMap = {} |
47 | for (const name in this.imports) { |
48 | importMap[name] = {} |
49 | const Import = this.imports[name] |
50 | const newInterface = new Import(this) |
51 | const props = Object.getOwnPropertyNames(Import.prototype) |
52 | |
53 | // bind the methods to the correct 'this' |
54 | for (const prop of props) { |
55 | if (prop !== 'constructor') { |
56 | importMap[name][prop] = newInterface[prop].bind(newInterface) |
57 | } |
58 | } |
59 | } |
60 | |
61 | const result = await WebAssembly.instantiate(this.kernel.code, importMap) |
62 | this.instance = result.instance |
63 | |
64 | // add the message and ports to the refereance map |
65 | const messageRef = this.referanceMap.add(message) |
66 | |
67 | // runs the wasm code |
68 | this.instance.exports[method](messageRef) |
69 | return this.onDone() |
70 | } |
71 | |
72 | /** |
73 | * returns a promise that resolves when the wasm instance is done running |
74 | * @returns {Promise} |
75 | */ |
76 | async onDone () { |
77 | let prevOps |
78 | while (prevOps !== this._opsQueue) { |
79 | prevOps = this._opsQueue |
80 | await prevOps |
81 | } |
82 | this.referanceMap.clear() |
83 | } |
84 | |
85 | /** |
86 | * Pushed an async operation to the a promise queue that |
87 | * @returns {Promise} the returned promise resolves in the order the intail |
88 | * operation was pushed to the queue |
89 | */ |
90 | pushOpsQueue (promise) { |
91 | this._opsQueue = Promise.all([this._opsQueue, promise]) |
92 | return this._opsQueue |
93 | } |
94 | |
95 | /** |
96 | * executes a callback given an index in the exported callback container |
97 | * @param {integer} cb |
98 | * @param {*} val - a value to return to the callback function |
99 | */ |
100 | execute (cb, val) { |
101 | this.instance.exports.callbacks.get(cb)(val) |
102 | } |
103 | |
104 | /** |
105 | * returns a section of memory from the wasm instance |
106 | * @param {integer} offset |
107 | * @param {integer} length |
108 | * @returns {Uint8Array} |
109 | */ |
110 | getMemory (offset, length) { |
111 | return new Uint8Array(this.instance.exports.memory.buffer, offset, length) |
112 | } |
113 | |
114 | setMemory (offset, val) { |
115 | const mem = this.getMemory(offset, val.length) |
116 | mem.set(val) |
117 | } |
118 | |
119 | static get typeId () { |
120 | return ContainerTable.WebAssembly |
121 | } |
122 | } |
123 |
Built with git-ssb-web