git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit 48adc7489f4edabd305e1834f5393956f1801dde

added intialization

wanderer committed on 7/6/2017, 6:21:30 PM
Parent: 02a7b5f8fe5be06689a6dae0c6923fd0e5479817

Files changed

index.jschanged
tests/index.jschanged
index.jsView
@@ -2,21 +2,30 @@
22
33 module.exports = class WasmContainer {
44 /**
55 * The wasm container runs wasm code and provides a basic API for wasm
6- * interfaces for interacting with the exoInterface
7- * @param {object} exoInterface - the exoInterface instance
8- * @param {object} imports - a map of imports to expose to the wasm binary
6+ * interfaces for interacting with the kernel
7+ * @param {object} kernel - the kernel instance
8+ * @param {object} interfaces - a map of interfaces to expose to the wasm binary
99 */
10- constructor (kernel, imports) {
10+ constructor (kernel, interfaces) {
1111 this.kernel = kernel
12- this.imports = imports
12+ this.imports = interfaces
1313 this.referanceMap = new ReferanceMap()
1414 }
1515
1616 async initialize (message) {
17- if (!WebAssembly.validate(this.kernel.state.code)) {
17+ let code = message.data
18+ if (!WebAssembly.validate(code)) {
1819 throw new Error('invalid wasm binary')
20+ } else {
21+ for (const name in this.imports) {
22+ const interf = this.imports[name]
23+ if (interf.initialize) {
24+ code = await interf.initialize(code)
25+ }
26+ }
27+ this.kernel.state.code = code
1928 }
2029 return this._run(message, 'init')
2130 }
2231
@@ -24,9 +33,9 @@
2433 * Runs the wasm VM given a message
2534 * @param {object} message
2635 * @returns {Promise} a promise that resolves once the compuation is finished
2736 */
28- async run (message) {
37+ run (message) {
2938 return this._run(message, 'main')
3039 }
3140
3241 async _run (message, method) {
tests/index.jsView
@@ -116,5 +116,46 @@
116116
117117 await hypervisor.createInstance('wasm', message)
118118 t.equals(rp.destPort.messages[0].data.exception, true)
119119 })
120+
121+ tape('initailize', async t => {
122+ t.plan(2)
123+
124+ const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
125+
126+ class ContainerTestInterface {
127+ constructor (wasmContainer) {
128+ this.wasmContainer = wasmContainer
129+ }
130+
131+ readMem (offset) {
132+ return this.wasmContainer.getMemory(offset, 1)
133+ }
134+
135+ async callback (cb) {
136+ const promise = new Promise((resolve, reject) => {
137+ resolve()
138+ })
139+ await this.wasmContainer.pushOpsQueue(promise)
140+ this.wasmContainer.execute(cb)
141+ }
142+
143+ static initialize (code) {
144+ t.equals(code, callBackWasm)
145+ return code
146+ }
147+ }
148+
149+ const hypervisor = new Hypervisor(node.dag)
150+ hypervisor.registerContainer('wasm', WasmContainer, {
151+ env: ContainerTestInterface,
152+ test: testInterface(t)
153+ })
154+
155+ const message = new Message({
156+ data: callBackWasm
157+ })
158+
159+ hypervisor.createInstance('wasm', message)
160+ })
120161 })

Built with git-ssb-web