Commit 48adc7489f4edabd305e1834f5393956f1801dde
added intialization
wanderer committed on 7/6/2017, 6:21:30 PMParent: 02a7b5f8fe5be06689a6dae0c6923fd0e5479817
Files changed
index.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -2,21 +2,30 @@ | ||
2 | 2 | |
3 | 3 | module.exports = class WasmContainer { |
4 | 4 | /** |
5 | 5 | * 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 | |
9 | 9 | */ |
10 | - constructor (kernel, imports) { | |
10 | + constructor (kernel, interfaces) { | |
11 | 11 | this.kernel = kernel |
12 | - this.imports = imports | |
12 | + this.imports = interfaces | |
13 | 13 | this.referanceMap = new ReferanceMap() |
14 | 14 | } |
15 | 15 | |
16 | 16 | async initialize (message) { |
17 | - if (!WebAssembly.validate(this.kernel.state.code)) { | |
17 | + let code = message.data | |
18 | + if (!WebAssembly.validate(code)) { | |
18 | 19 | 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 | |
19 | 28 | } |
20 | 29 | return this._run(message, 'init') |
21 | 30 | } |
22 | 31 | |
@@ -24,9 +33,9 @@ | ||
24 | 33 | * Runs the wasm VM given a message |
25 | 34 | * @param {object} message |
26 | 35 | * @returns {Promise} a promise that resolves once the compuation is finished |
27 | 36 | */ |
28 | - async run (message) { | |
37 | + run (message) { | |
29 | 38 | return this._run(message, 'main') |
30 | 39 | } |
31 | 40 | |
32 | 41 | async _run (message, method) { |
tests/index.js | ||
---|---|---|
@@ -116,5 +116,46 @@ | ||
116 | 116 | |
117 | 117 | await hypervisor.createInstance('wasm', message) |
118 | 118 | t.equals(rp.destPort.messages[0].data.exception, true) |
119 | 119 | }) |
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 | + }) | |
120 | 161 | }) |
Built with git-ssb-web