Commit 192e8e23310b0b99e63703b7d840090c2b55ddfb
added basic wasm container test
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 2/6/2018, 3:54:10 AM
Parent: 6f92591fea03c67a9e18c1daab07a4ed7d693b21
Files changed
actor.js | changed |
index.js | changed |
package-lock.json | changed |
package.json | changed |
tests/index.js | changed |
tests/wasmContainer.js | added |
wasmContainer.js | changed |
actor.js | ||
---|---|---|
@@ -42,9 +42,9 @@ | ||
42 | 42 | /** |
43 | 43 | * Runs the startup routine for the actor |
44 | 44 | */ |
45 | 45 | startup () { |
46 | - return this.container.onStartup() | |
46 | + return this.container.onStartup() | |
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
50 | 50 | * run the Actor with a given message |
@@ -57,9 +57,8 @@ | ||
57 | 57 | this.ticks = message._fromTicks |
58 | 58 | } |
59 | 59 | try { |
60 | 60 | this.currentMessage = message |
61 | - // console.log(this.container.onMessage) | |
62 | 61 | await this.container.onMessage(message) |
63 | 62 | } catch (e) { |
64 | 63 | message.emit('execution:error', e) |
65 | 64 | } |
index.js | ||
---|---|---|
@@ -38,9 +38,10 @@ | ||
38 | 38 | state, |
39 | 39 | Container, |
40 | 40 | id, |
41 | 41 | nonce, |
42 | - type | |
42 | + type, | |
43 | + cachedb: this.tree.dag._dag | |
43 | 44 | }) |
44 | 45 | |
45 | 46 | await actor.startup() |
46 | 47 | return actor |
@@ -55,9 +56,9 @@ | ||
55 | 56 | async createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
56 | 57 | const Container = this._containerTypes[type] |
57 | 58 | const encoded = encodedID(id) |
58 | 59 | const idHash = await this._getHashFromObj(encoded) |
59 | - const exports = await Container.onCreation(code, idHash) | |
60 | + const exports = await Container.onCreation(code, idHash, this.tree.dag._dag) | |
60 | 61 | const metaData = Actor.serializeMetaData(type) |
61 | 62 | |
62 | 63 | // save the container in the state |
63 | 64 | this.tree.set(idHash, metaData) |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 342441 bytes New file size: 343966 bytes |
package.json | ||
---|---|---|
@@ -33,8 +33,9 @@ | ||
33 | 33 | "binary-search-insert": "^1.0.3", |
34 | 34 | "buffer-pipe": "0.0.2", |
35 | 35 | "events": "^1.1.1", |
36 | 36 | "leb128": "0.0.4", |
37 | + "levelup": "^2.0.1", | |
37 | 38 | "reference-map": "^1.1.0", |
38 | 39 | "safe-buffer": "^5.1.1", |
39 | 40 | "wasm-json-toolkit": "^0.2.0", |
40 | 41 | "wasm-metering": "^0.1.1" |
tests/index.js | ||
---|---|---|
@@ -20,18 +20,11 @@ | ||
20 | 20 | } |
21 | 21 | }) |
22 | 22 | return exp |
23 | 23 | } |
24 | - getFuncRef (name) { | |
25 | - return { | |
26 | - name, | |
27 | - destId: this.id | |
28 | - } | |
29 | - } | |
30 | 24 | onMessage (message) { |
31 | 25 | return this[message.funcRef.name](...message.funcArguments) |
32 | 26 | } |
33 | - | |
34 | 27 | static get typeId () { |
35 | 28 | return 9 |
36 | 29 | } |
37 | 30 | } |
tests/wasmContainer.js | ||
---|---|---|
@@ -1,0 +1,52 @@ | ||
1 | +const tape = require('tape') | |
2 | +const fs = require('fs') | |
3 | +const Message = require('../message.js') | |
4 | +const Hypervisor = require('../') | |
5 | +const WasmContainer = require('../wasmContainer.js') | |
6 | + | |
7 | +const level = require('level-browserify') | |
8 | +const RadixTree = require('dfinity-radix-tree') | |
9 | +const db = level('./testdb') | |
10 | + | |
11 | +let tester | |
12 | + | |
13 | +class TestWasmContainer extends WasmContainer { | |
14 | + getInteface (funcRef) { | |
15 | + const orginal = super.getInteface(funcRef) | |
16 | + return Object.assign(orginal, { | |
17 | + test: { | |
18 | + check: (a, b) => { | |
19 | + tester.equals(a, b) | |
20 | + } | |
21 | + } | |
22 | + }) | |
23 | + } | |
24 | +} | |
25 | + | |
26 | +tape('basic', async t => { | |
27 | + t.plan(2) | |
28 | + tester = t | |
29 | + const expectedState = { | |
30 | + '/': Buffer.from('926de6b7eb39cfa8d7f8a44d1ef191d3bcb765a7', 'hex') | |
31 | + } | |
32 | + | |
33 | + const tree = new RadixTree({ | |
34 | + db: db | |
35 | + }) | |
36 | + | |
37 | + const wasm = fs.readFileSync('./wasm/reciever.wasm') | |
38 | + | |
39 | + const hypervisor = new Hypervisor(tree) | |
40 | + hypervisor.registerContainer(TestWasmContainer) | |
41 | + | |
42 | + const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) | |
43 | + | |
44 | + const message = new Message({ | |
45 | + funcRef: exports.receive, | |
46 | + funcArguments: [5] | |
47 | + }) | |
48 | + hypervisor.send(message) | |
49 | + | |
50 | + const stateRoot = await hypervisor.createStateRoot() | |
51 | + t.deepEquals(stateRoot, expectedState, 'expected root!') | |
52 | +}) |
wasmContainer.js | ||
---|---|---|
@@ -26,15 +26,12 @@ | ||
26 | 26 | 0x60: 'func', |
27 | 27 | 0x40: 'block_type' |
28 | 28 | } |
29 | 29 | |
30 | -class Ref { | |
31 | - serialize () {} | |
32 | -} | |
33 | - | |
34 | 30 | class FunctionRef { |
35 | - constructor (json, name) { | |
31 | + constructor (name, json, id) { | |
36 | 32 | this.name = name |
33 | + this.destId = id | |
37 | 34 | this.args = [] |
38 | 35 | const typeIndex = json.typeMap[name] |
39 | 36 | const type = json.type[typeIndex] |
40 | 37 | const wrapper = typeCheckWrapper(type) |
@@ -70,37 +67,46 @@ | ||
70 | 67 | } |
71 | 68 | } |
72 | 69 | |
73 | 70 | module.exports = class WasmContainer { |
74 | - constructor () { | |
71 | + constructor (actor) { | |
72 | + this.actor = actor | |
75 | 73 | this.refs = new ReferanceMap() |
76 | 74 | } |
77 | - onCreation (wasm, id, cachedb) { | |
75 | + | |
76 | + static onCreation (wasm, id, cachedb) { | |
77 | + WebAssembly.validate(wasm) | |
78 | 78 | let moduleJSON = wasm2json(wasm) |
79 | - this.json = mergeTypeSections(moduleJSON) | |
79 | + const json = mergeTypeSections(moduleJSON) | |
80 | 80 | moduleJSON = wasmMetering.meterJSON(moduleJSON, { |
81 | 81 | meterType: 'i32' |
82 | 82 | }) |
83 | - this.wasm = json2wasm(moduleJSON) | |
84 | - this.mod = WebAssembly.Module(this.wasm) | |
83 | + wasm = json2wasm(moduleJSON) | |
84 | + cachedb.put(id.toString() + 'meta', json) | |
85 | + cachedb.put(id.toString() + 'code', wasm.toString('hex')) | |
86 | + const refs = {} | |
87 | + Object.keys(json.typeMap).forEach(key => { | |
88 | + refs[key] = new FunctionRef(key, json, id) | |
89 | + }) | |
90 | + return refs | |
85 | 91 | } |
86 | 92 | |
87 | 93 | sendMessage () { |
88 | 94 | console.log('send') |
89 | 95 | } |
90 | 96 | |
91 | - onMessage (funcRef) { | |
97 | + getInteface (funcRef) { | |
92 | 98 | const self = this |
93 | - const instance = WebAssembly.Instance(this.mod, { | |
99 | + return { | |
94 | 100 | func: { |
95 | 101 | externalize: () => {}, |
96 | 102 | internalize: (ref, index) => { |
97 | 103 | const {type, arg} = self.refs.get(ref) |
98 | 104 | if (type !== 'funcRef') { |
99 | 105 | throw new Error('invalid type') |
100 | 106 | } |
101 | 107 | arg.container = self |
102 | - instance.exports.table.set(index, arg.wrapper.exports.check) | |
108 | + this.instance.exports.table.set(index, arg.wrapper.exports.check) | |
103 | 109 | }, |
104 | 110 | catch: (ref, catchRef) => { |
105 | 111 | const {funcRef} = self.refs.get(ref, FunctionRef) |
106 | 112 | const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef) |
@@ -151,27 +157,66 @@ | ||
151 | 157 | throw new Error('out of gas! :(') |
152 | 158 | } |
153 | 159 | } |
154 | 160 | } |
155 | - }) | |
156 | - const args = funcRef.args.map(arg => { | |
157 | - if (nativeTypes.has(arg.type)) { | |
158 | - return arg.arg | |
161 | + } | |
162 | + } | |
163 | + | |
164 | + onMessage (message) { | |
165 | + const funcRef = message.funcRef | |
166 | + const intef = this.getInteface(funcRef) | |
167 | + this.instance = WebAssembly.Instance(this.mod, intef) | |
168 | + const args = message.funcArguments.map(arg => { | |
169 | + if (typeof arg === 'number') { | |
170 | + return arg | |
159 | 171 | } else { |
160 | 172 | return this.refs.add(arg) |
161 | 173 | } |
162 | 174 | }) |
163 | - instance.exports[funcRef.name](...args) | |
175 | + this.instance.exports[funcRef.name](...args) | |
164 | 176 | } |
165 | 177 | |
166 | 178 | getFuncRef (name, send) { |
167 | 179 | const funcRef = new FunctionRef(this.json, name, send) |
168 | 180 | return funcRef |
169 | 181 | } |
182 | + | |
183 | + async onStartup () { | |
184 | + let [json, wasm] = await Promise.all([ | |
185 | + new Promise((resolve, reject) => { | |
186 | + this.actor.cachedb.get(this.actor.id.toString() + 'meta', (err, json) => { | |
187 | + if (err) { | |
188 | + reject(err) | |
189 | + } else { | |
190 | + resolve(json) | |
191 | + } | |
192 | + }) | |
193 | + }), | |
194 | + new Promise((resolve, reject) => { | |
195 | + this.actor.cachedb.get(this.actor.id.toString() + 'code', (err, wasm) => { | |
196 | + if (err) { | |
197 | + reject(err) | |
198 | + } else { | |
199 | + resolve(wasm) | |
200 | + } | |
201 | + }) | |
202 | + }) | |
203 | + ]) | |
204 | + wasm = Buffer.from(wasm, 'hex') | |
205 | + this.mod = WebAssembly.Module(wasm) | |
206 | + this.json = json | |
207 | + } | |
208 | + | |
209 | + static get typeId () { | |
210 | + return 9 | |
211 | + } | |
170 | 212 | } |
171 | 213 | |
172 | 214 | function mergeTypeSections (json) { |
173 | - const typeInfo = {} | |
215 | + const typeInfo = { | |
216 | + typeMap: [], | |
217 | + type: [] | |
218 | + } | |
174 | 219 | let typeSection = { |
175 | 220 | 'entries': [] |
176 | 221 | } |
177 | 222 | let importSection = { |
Built with git-ssb-web