git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit a338912f819bd6fa4333e0c982768e58f6f2219f

fix bug in custom type encoder

wanderer committed on 2/20/2018, 10:30:00 PM
Parent: b9cefb81b38fc031f3179fbfff55dfd7fa8596a6

Files changed

customTypes.jschanged
tests/wasm/caller.wasmchanged
tests/wasm/funcRef_caller.wasmchanged
tests/wasm/funcRef_reciever.wasmchanged
tests/wasmContainer.jschanged
tests/wast/caller.jsonchanged
tests/wast/funcRef_caller.jsonchanged
tests/wast/funcRef_reciever.jsonchanged
tests/wast2wasm.jschanged
wasmContainer.jschanged
customTypes.jsView
@@ -25,28 +25,50 @@
2525 }
2626
2727 function encodeJSON (json) {
2828 const stream = new Stream()
29- encodeCustomSection('type', json, stream, encodeType)
29+ encodeCustomSection('types', json, stream, encodeType)
3030 encodeCustomSection('typeMap', json, stream, encodeTypeMap)
31+ encodeCustomSection('globals', json, stream, encodeGlobals)
3132
3233 return stream.buffer
3334 }
3435
3536 function encodeCustomSection (name, json, stream, encodingFunc) {
36- stream.write([0])
3737 let payload = new Stream()
38+ json = json[name]
3839
39- // encode type
40- leb.unsigned.write(name.length, payload)
41- payload.write(name)
42- encodingFunc(json[name], payload)
43- // write the size of the payload
44- leb.unsigned.write(payload.bytesWrote, stream)
45- stream.write(payload.buffer)
40+ if (json) {
41+ stream.write([0])
42+ // encode type
43+ leb.unsigned.write(name.length, payload)
44+ payload.write(name)
45+ encodingFunc(json, payload)
46+ // write the size of the payload
47+ leb.unsigned.write(payload.bytesWrote, stream)
48+ stream.write(payload.buffer)
49+ }
4650 return stream
4751 }
4852
53+function encodeGlobals (json, stream) {
54+ leb.unsigned.write(json.length, stream)
55+ for (const entry of json) {
56+ leb.unsigned.write(entry, stream)
57+ }
58+ return stream
59+}
60+
61+function decodeGlobals (buf) {
62+ const stream = new Stream(Buffer.from(buf))
63+ let numOfEntries = leb.unsigned.read(stream)
64+ const json = []
65+ while (numOfEntries--) {
66+ json.push(leb.unsigned.readBn(stream).toNumber())
67+ }
68+ return json
69+}
70+
4971 function encodeTypeMap (json, stream) {
5072 leb.unsigned.write(json.length, stream)
5173 for (let entry of json) {
5274 leb.unsigned.write(entry.func, stream)
@@ -139,25 +161,25 @@
139161 indexes: {},
140162 exports: {}
141163 }
142164
143- const wantedSections = ['custom', 'custom', 'type', 'import', 'function', 'export']
165+ const wantedSections = ['types', 'typeMap', 'type', 'import', 'function', 'export']
144166 const iterator = findSections(json, wantedSections)
145167 const mappedFuncs = new Map()
146168 const mappedTypes = new Map()
147- const {value: customType} = iterator.next('custom')
169+ const {value: customType} = iterator.next()
148170 if (customType) {
149171 const type = decodeType(customType.payload)
150172 result.types = type
151173 }
152- let {value: typeMap} = iterator.next('custom')
174+ let {value: typeMap} = iterator.next()
153175 if (typeMap) {
154176 decodeTypeMap(typeMap.payload).forEach(map => mappedFuncs.set(map.func, map.type))
155177 }
156178
157- const {value: type} = iterator.next('type')
158- const {value: imports = {entries: []}} = iterator.next('import')
159- const {value: functions} = iterator.next('function')
179+ const {value: type} = iterator.next()
180+ const {value: imports = {entries: []}} = iterator.next()
181+ const {value: functions} = iterator.next()
160182 functions.entries.forEach((typeIndex, funcIndex) => {
161183 let customIndex = mappedFuncs.get(funcIndex)
162184 if (customIndex === undefined) {
163185 customIndex = mappedTypes.get(typeIndex)
@@ -168,9 +190,9 @@
168190 }
169191 result.indexes[funcIndex + imports.entries.length] = customIndex
170192 })
171193
172- const {value: exports = {entries: []}} = iterator.next('export')
194+ const {value: exports = {entries: []}} = iterator.next()
173195 exports.entries.forEach(entry => {
174196 if (entry.kind === 'function') {
175197 result.exports[entry.field_str] = entry.index
176198 }
@@ -182,8 +204,9 @@
182204 injectCustomSection,
183205 inject,
184206 decodeType,
185207 decodeTypeMap,
208+ decodeGlobals,
186209 encodeType,
187210 encodeTypeMap,
188211 encodeJSON,
189212 mergeTypeSections
tests/wasm/caller.wasmView
@@ -1,4 +1,3 @@
1-asm
2-type`` typeMap
1+asm types`` typeMap
32 ``func internalizeptablecall
43 A AA
tests/wasm/funcRef_caller.wasmView
@@ -1,3 +1,2 @@
1-asm
2-type`` typeMap````@func internalizetestcheckmoduleselfmoduleexportsp$memorytablecallcallback
1+asm types`` typeMap````@func internalizetestcheckmoduleselfmoduleexportsp$memorytablecallcallback
32 AAA A  A A callback
tests/wasm/funcRef_reciever.wasmView
@@ -1,4 +1,3 @@
1-asm
2-type`` typeMap
1+asm types`` typeMap
32 ``func internalizeptablereceive
43  AAA
tests/wasmContainer.jsView
@@ -65,9 +65,9 @@
6565 tape('two communicating actors', async t => {
6666 t.plan(2)
6767 tester = t
6868 const expectedState = {
69- '/': Buffer.from('123bcbf52421f0ebf0c9a28d6546a3b374f5d56d', 'hex')
69+ '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
7070 }
7171
7272 const tree = new RadixTree({db})
7373
@@ -89,12 +89,12 @@
8989 t.deepEquals(stateRoot, expectedState, 'expected root!')
9090 })
9191
9292 tape('two communicating actors with callback', async t => {
93- // t.plan(2)
93+ t.plan(2)
9494 tester = t
9595 const expectedState = {
96- '/': Buffer.from('51ded6c294314defc886b70f7f593434c8d53c95', 'hex')
96+ '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
9797 }
9898
9999 const tree = new RadixTree({
100100 db
@@ -116,8 +116,39 @@
116116
117117 hypervisor.send(message)
118118 const stateRoot = await hypervisor.createStateRoot()
119119 t.deepEquals(stateRoot, expectedState, 'expected root!')
120+ // t.end()
121+})
122+
123+tape.skip('two communicating actors with callback', async t => {
124+ t.plan(2)
125+ tester = t
126+ const expectedState = {
127+ '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
128+ }
129+
130+ const tree = new RadixTree({
131+ db
132+ })
133+
134+ const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm')
135+ const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm')
136+
137+ const hypervisor = new Hypervisor(tree)
138+ hypervisor.registerContainer(TestWasmContainer)
139+
140+ const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
141+ const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
142+
143+ const message = new Message({
144+ funcRef: callerMod.getFuncRef('call'),
145+ funcArguments: [receiverMod.getFuncRef('receive')]
146+ })
147+
148+ hypervisor.send(message)
149+ const stateRoot = await hypervisor.createStateRoot()
150+ t.deepEquals(stateRoot, expectedState, 'expected root!')
120151 t.end()
121152 })
122153
123154 // Increment a counter.
tests/wast/caller.jsonView
@@ -1,6 +1,6 @@
11 {
2- "type": [{
2+ "types": [{
33 "form": "func",
44 "params": [
55 "func"
66 ]
tests/wast/funcRef_caller.jsonView
@@ -1,6 +1,6 @@
11 {
2- "type": [{
2+ "types": [{
33 "form": "func",
44 "params": [
55 "func"
66 ]
tests/wast/funcRef_reciever.jsonView
@@ -1,6 +1,6 @@
11 {
2- "type": [{
2+ "types": [{
33 "form": "func",
44 "params": [
55 "func"
66 ]
tests/wast2wasm.jsView
@@ -20,8 +20,9 @@
2020 const mod = wabt.parseWat('module.wast', wat)
2121 const r = mod.toBinary({log: true})
2222 let binary = Buffer.from(r.buffer)
2323 if (json) {
24+ console.log(json)
2425 const buf = types.encodeJSON(json)
2526 binary = types.injectCustomSection(buf, binary)
2627 }
2728 fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary)
wasmContainer.jsView
@@ -171,9 +171,9 @@
171171 getInterface (funcRef) {
172172 const self = this
173173 return {
174174 func: {
175- externalize: (index) => {
175+ externalize: index => {
176176 const func = this.instance.exports.table.get(index)
177177 const object = func.object
178178 if (object) {
179179 return self.refs.add(object)
@@ -195,9 +195,9 @@
195195 getGasAmount: (funcRef) => {},
196196 setGasAmount: (funcRef) => {}
197197 },
198198 link: {
199- wrap: (ref) => {
199+ wrap: ref => {
200200 const obj = this.refs.get(ref)
201201 const link = new LinkRef(obj.serialize())
202202 return this.refs.add(link, 'link')
203203 },
@@ -255,9 +255,9 @@
255255 mem.set(buf)
256256 }
257257 },
258258 metering: {
259- usegas: (amount) => {
259+ usegas: amount => {
260260 funcRef.gas -= amount
261261 if (funcRef.gas < 0) {
262262 throw new Error('out of gas! :(')
263263 }

Built with git-ssb-web