git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 65cc00ee3b0f0bae67e7b56bcbaa7b252af084a1

Files: 65cc00ee3b0f0bae67e7b56bcbaa7b252af084a1 / index.js

3250 bytesRaw
1const ReferanceMap = require('reference-map')
2const AbstractContainer = require('primea-abstract-container')
3const ContainerTable = require('primea-container-table')
4
5module.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 = message.data
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.state.code, importMap)
62 this.instance = result.instance
63 // runs the wasm code
64 this.instance.exports[method]()
65 return this.onDone()
66 }
67
68 /**
69 * returns a promise that resolves when the wasm instance is done running
70 * @returns {Promise}
71 */
72 async onDone () {
73 let prevOps
74 while (prevOps !== this._opsQueue) {
75 prevOps = this._opsQueue
76 await prevOps
77 }
78 this.referanceMap.clear()
79 }
80
81 /**
82 * Pushed an async operation to the a promise queue that
83 * @returns {Promise} the returned promise resolves in the order the intail
84 * operation was pushed to the queue
85 */
86 pushOpsQueue (promise) {
87 this._opsQueue = Promise.all([this._opsQueue, promise])
88 return this._opsQueue
89 }
90
91 /**
92 * executes a callback given an index in the exported callback container
93 * @param {integer} cb
94 * @param {*} val - a value to return to the callback function
95 */
96 execute (cb, val) {
97 this.instance.exports.callbacks.get(cb)(val)
98 }
99
100 /**
101 * returns a section of memory from the wasm instance
102 * @param {integer} offset
103 * @param {integer} length
104 * @returns {Uint8Array}
105 */
106 getMemory (offset, length) {
107 return new Uint8Array(this.instance.exports.memory.buffer, offset, length)
108 }
109
110 static get typeId () {
111 return ContainerTable.WebAssembly
112 }
113}
114

Built with git-ssb-web