Commit e5f317ee074967d082a366e35035d77ef081fa84
add tests for func internalize
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 3/1/2018, 10:35:46 PM
Parent: 23e12cd33ecd2ef038fb71cc7b53d43a2dd19637
Files changed
index.js | changed |
tests/wasmContainer.js | changed |
tests/wast2wasm.js | changed |
wasmContainer.js | changed |
index.js | ||
---|---|---|
@@ -29,10 +29,12 @@ | ||
29 | 29 | } |
30 | 30 | |
31 | 31 | async loadActor (id) { |
32 | 32 | const state = await this.tree.get(id.id, true) |
33 | - let code = await this.tree.graph.get(state.root, '1') | |
34 | - let storage = await this.tree.graph.get(state.root, '2') | |
33 | + const [code, storage] = await Promise.all([ | |
34 | + this.tree.graph.get(state.root, '1'), | |
35 | + this.tree.graph.get(state.root, '2') | |
36 | + ]) | |
35 | 37 | const [type, nonce] = state.value |
36 | 38 | const Container = this._containerTypes[type] |
37 | 39 | |
38 | 40 | // create a new actor instance |
tests/wasmContainer.js | ||
---|---|---|
@@ -20,8 +20,12 @@ | ||
20 | 20 | return Object.assign(orginal, { |
21 | 21 | test: { |
22 | 22 | check: (a, b) => { |
23 | 23 | tester.equals(a, b) |
24 | + }, | |
25 | + print: (dataRef) => { | |
26 | + let buf = this.refs.get(dataRef, 'buf') | |
27 | + console.log(buf.toString()) | |
24 | 28 | } |
25 | 29 | } |
26 | 30 | }) |
27 | 31 | } |
@@ -110,8 +114,38 @@ | ||
110 | 114 | const stateRoot = await hypervisor.createStateRoot() |
111 | 115 | // t.deepEquals(stateRoot, expectedState, 'expected root!') |
112 | 116 | }) |
113 | 117 | |
118 | +tape('two communicating actors with callback', async t => { | |
119 | + t.plan(1) | |
120 | + tester = t | |
121 | + const expectedState = { | |
122 | + '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex') | |
123 | + } | |
124 | + | |
125 | + const tree = new RadixTree({ | |
126 | + db | |
127 | + }) | |
128 | + | |
129 | + const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm') | |
130 | + const callerWasm = fs.readFileSync('./wasm/private_caller.wasm') | |
131 | + | |
132 | + const hypervisor = new Hypervisor(tree) | |
133 | + hypervisor.registerContainer(TestWasmContainer) | |
134 | + | |
135 | + const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) | |
136 | + const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) | |
137 | + | |
138 | + const message = new Message({ | |
139 | + funcRef: callerMod.getFuncRef('call'), | |
140 | + funcArguments: [receiverMod.getFuncRef('receive')] | |
141 | + }).on('execution:error', (e) => {console.log(e)}) | |
142 | + | |
143 | + hypervisor.send(message) | |
144 | + const stateRoot = await hypervisor.createStateRoot() | |
145 | + // t.deepEquals(stateRoot, expectedState, 'expected root!') | |
146 | +}) | |
147 | + | |
114 | 148 | tape('externalize/internalize memory', async t => { |
115 | 149 | t.plan(1) |
116 | 150 | tester = t |
117 | 151 | const tree = new RadixTree({ |
@@ -192,4 +226,25 @@ | ||
192 | 226 | }) |
193 | 227 | hypervisor.send(message) |
194 | 228 | }) |
195 | 229 | }) |
230 | + | |
231 | +tape.skip('ben', async t => { | |
232 | + // t.plan(1) | |
233 | + tester = t | |
234 | + const tree = new RadixTree({ | |
235 | + db | |
236 | + }) | |
237 | + | |
238 | + const wasm = fs.readFileSync('./hi.wasm') | |
239 | + const hypervisor = new Hypervisor(tree) | |
240 | + hypervisor.registerContainer(TestWasmContainer) | |
241 | + | |
242 | + const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) | |
243 | + | |
244 | + const message = new Message({ | |
245 | + funcRef: module.getFuncRef('main') | |
246 | + }).on('done', () => { | |
247 | + t.end() | |
248 | + }) | |
249 | + hypervisor.send(message) | |
250 | +}) |
tests/wast2wasm.js | ||
---|---|---|
@@ -12,21 +12,25 @@ | ||
12 | 12 | try { |
13 | 13 | json = fs.readFileSync(`${__dirname}/wast/${file}.json`) |
14 | 14 | json = JSON.parse(json) |
15 | 15 | } catch (e) { |
16 | - console.log('no json') | |
16 | + console.log(`no json for ${file}`) | |
17 | 17 | } |
18 | 18 | |
19 | - console.log(wat) | |
20 | - const mod = wabt.parseWat('module.wast', wat) | |
21 | - const r = mod.toBinary({log: true}) | |
22 | - let binary = Buffer.from(r.buffer) | |
23 | - if (json) { | |
24 | - console.log(json) | |
25 | - const buf = types.encodeJSON(json) | |
26 | - binary = types.injectCustomSection(buf, binary) | |
19 | + try { | |
20 | + const mod = wabt.parseWat('module.wast', wat) | |
21 | + const r = mod.toBinary({log: true}) | |
22 | + let binary = Buffer.from(r.buffer) | |
23 | + if (json) { | |
24 | + console.log(json) | |
25 | + const buf = types.encodeJSON(json) | |
26 | + binary = types.injectCustomSection(buf, binary) | |
27 | + } | |
28 | + fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary) | |
29 | + } catch (e) { | |
30 | + console.log(`failed at ${file}`) | |
31 | + console.log(e) | |
27 | 32 | } |
28 | - fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary) | |
29 | 33 | } |
30 | 34 | } |
31 | 35 | |
32 | 36 | filesWast2wasm() |
wasmContainer.js | ||
---|---|---|
@@ -6,8 +6,10 @@ | ||
6 | 6 | const injectGlobals = require('./injectGlobals.js') |
7 | 7 | const typeCheckWrapper = require('./typeCheckWrapper.js') |
8 | 8 | const {FunctionRef, ModuleRef, DEFAULTS} = require('./systemObjects.js') |
9 | 9 | |
10 | +const FUNC_INDEX_OFFSET = 1 | |
11 | + | |
10 | 12 | const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64']) |
11 | 13 | const LANGUAGE_TYPES = { |
12 | 14 | 0x0: 'actor', |
13 | 15 | 0x1: 'buf', |
@@ -95,22 +97,20 @@ | ||
95 | 97 | externalize: index => { |
96 | 98 | const func = this.instance.exports.table.get(index) |
97 | 99 | const object = func.object |
98 | 100 | if (object) { |
101 | + // externalize a pervously internalized function | |
99 | 102 | return self.refs.add(object) |
100 | 103 | } else { |
101 | - const ref = new FunctionRef(true, object.tableIndex, self.json, self.actor.id) | |
102 | - return self.refs.add(ref) | |
104 | + const params = self.json.types[self.json.indexes[func.name - FUNC_INDEX_OFFSET]].params | |
105 | + const ref = new FunctionRef(true, func.tableIndex, params, self.actor.id) | |
106 | + return self.refs.add(ref, 'func') | |
103 | 107 | } |
104 | 108 | }, |
105 | 109 | internalize: (ref, index) => { |
106 | 110 | const funcRef = self.refs.get(ref, 'func') |
107 | - try { | |
108 | - const wrapper = generateWrapper(funcRef, self) | |
109 | - this.instance.exports.table.set(index, wrapper.exports.check) | |
110 | - } catch (e) { | |
111 | - console.log(e) | |
112 | - } | |
111 | + const wrapper = generateWrapper(funcRef, self) | |
112 | + this.instance.exports.table.set(index, wrapper.exports.check) | |
113 | 113 | }, |
114 | 114 | catch: (ref, catchRef) => { |
115 | 115 | const {funcRef} = self.refs.get(ref, FunctionRef) |
116 | 116 | const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef) |
@@ -154,8 +154,12 @@ | ||
154 | 154 | let buf = this.refs.get(dataRef, 'buf') |
155 | 155 | buf = buf.subarray(srcOffset, length) |
156 | 156 | const mem = this.getMemory(sinkOffset, buf.length) |
157 | 157 | mem.set(buf) |
158 | + }, | |
159 | + length (dataRef) { | |
160 | + let buf = this.refs.get(dataRef, 'buf') | |
161 | + return buf.length | |
158 | 162 | } |
159 | 163 | }, |
160 | 164 | table: { |
161 | 165 | externalize: (index, length) => { |
@@ -187,8 +191,9 @@ | ||
187 | 191 | } |
188 | 192 | |
189 | 193 | async onMessage (message) { |
190 | 194 | const funcRef = message.funcRef |
195 | + // console.log(funcRef) | |
191 | 196 | const intef = this.getInterface(funcRef) |
192 | 197 | this.instance = WebAssembly.Instance(this.mod, intef) |
193 | 198 | // map table indexes |
194 | 199 | const table = this.instance.exports.table |
@@ -229,8 +234,9 @@ | ||
229 | 234 | this.instance.exports[funcRef.identifier](...args) |
230 | 235 | } |
231 | 236 | await this.onDone() |
232 | 237 | |
238 | + // store globals | |
233 | 239 | numOfGlobals = this.json.globals.length |
234 | 240 | if (numOfGlobals) { |
235 | 241 | this.actor.storage = [] |
236 | 242 | this.instance.exports.getter_globals() |
@@ -293,12 +299,8 @@ | ||
293 | 299 | this.json = json |
294 | 300 | this.modSelf = ModuleRef.fromMetaJSON(json, this.actor.id) |
295 | 301 | } |
296 | 302 | |
297 | - onShutdown () { | |
298 | - | |
299 | - } | |
300 | - | |
301 | 303 | getMemory (offset, length) { |
302 | 304 | return new Uint8Array(this.instance.exports.memory.buffer, offset, length) |
303 | 305 | } |
304 | 306 |
Built with git-ssb-web