git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 891456b4ce75b2fdc83622388dc0ebf2837835cc

update to use new annotation lib

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 3/21/2018, 7:21:38 PM
Parent: 857f72d754b591556c19ed6bfb5896e9fea412e0

Files changed

systemObjects.jschanged
tests/wasm/globals.wasmchanged
tests/wasmContainer.jschanged
tests/wast/globals.jsonchanged
tests/wast2wasm.jschanged
wasmContainer.jschanged
customTypes.jsdeleted
systemObjects.jsView
@@ -8,9 +8,9 @@
88 }
99
1010 const DEFAULTS = {
1111 elem: [],
12- buf: Buffer.from([]),
12+ data: Buffer.from([]),
1313 id: new cbor.Tagged(TAGS.id, 0),
1414 mod: new cbor.Tagged(TAGS.mod, [{}, new cbor.Tagged(TAGS.id, 0)]),
1515 link: {'/': null},
1616 func: new cbor.Tagged(TAGS.func, 0)
tests/wasm/globals.wasmView
@@ -1,4 +1,4 @@
1-asm globals```+memory externalizememory internalize A~ A~ memoryloadstore
1+asm persistn```+memory externalizememory internalize A~ A~ memoryloadstore
22 
33 AA$ #AAA
44 A test
tests/wasmContainer.jsView
@@ -56,8 +56,10 @@
5656
5757 const message = new Message({
5858 funcRef,
5959 funcArguments: [5]
60+ }).on('execution:error', e => {
61+ console.log(e)
6062 })
6163 hypervisor.send(message)
6264 const stateRoot = await hypervisor.createStateRoot()
6365 // t.deepEquals(stateRoot, expectedState, 'expected root!')
tests/wast/globals.jsonView
@@ -1,6 +1,7 @@
11 {
2- "globals": [{
2+ "persist": [{
3+ "form": "global",
34 "index": 0,
4- "type": "buf"
5+ "type": "data"
56 }]
67 }
tests/wast2wasm.jsView
@@ -1,7 +1,7 @@
11 const wabt = require('wabt')
22 const fs = require('fs')
3-const types = require('../customTypes')
3+const annotations = require('primea-annotations')
44
55 function filesWast2wasm () {
66 const srcFiles = fs.readdirSync(`${__dirname}/wast`)
77 const wastFiles = srcFiles.filter(name => name.split('.').pop() === 'wast')
@@ -21,10 +21,9 @@
2121 const r = mod.toBinary({log: true})
2222 let binary = Buffer.from(r.buffer)
2323 if (json) {
2424 console.log(json)
25- const buf = types.encodeJSON(json)
26- binary = types.injectCustomSection(buf, binary)
25+ binary = annotations.encodeAndInject(json, binary)
2726 }
2827 fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary)
2928 } catch (e) {
3029 console.log(`failed at ${file}`)
wasmContainer.jsView
@@ -1,17 +1,18 @@
11 const {wasm2json, json2wasm} = require('wasm-json-toolkit')
2+const annotations = require('primea-annotations')
23 const wasmMetering = require('wasm-metering')
34 const ReferanceMap = require('reference-map')
45 const Message = require('./message.js')
5-const customTypes = require('./customTypes.js')
6+// const customTypes = require('./customTypes.js')
67 const injectGlobals = require('./injectGlobals.js')
78 const typeCheckWrapper = require('./typeCheckWrapper.js')
89 const {FunctionRef, ModuleRef, DEFAULTS} = require('./systemObjects.js')
910
1011 const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64'])
1112 const LANGUAGE_TYPES = {
1213 0x0: 'actor',
13- 0x1: 'buf',
14+ 0x1: 'data',
1415 0x02: 'elem',
1516 0x03: 'link',
1617 0x04: 'id',
1718 0x7f: 'i32',
@@ -67,17 +68,17 @@
6768 throw new Error('invalid wasm binary')
6869 }
6970
7071 let moduleJSON = wasm2json(wasm)
71- const json = customTypes.mergeTypeSections(moduleJSON)
72+ const json = annotations.mergeTypeSections(moduleJSON)
7273 moduleJSON = wasmMetering.meterJSON(moduleJSON, {
7374 meterType: 'i32'
7475 })
7576
7677 // initialize the globals
77- let numOfGlobals = json.globals.length
78+ let numOfGlobals = json.persist.length
7879 if (numOfGlobals) {
79- moduleJSON = injectGlobals(moduleJSON, json.globals)
80+ moduleJSON = injectGlobals(moduleJSON, json.persist)
8081 }
8182 // recompile the wasm
8283 wasm = json2wasm(moduleJSON)
8384 const modRef = ModuleRef.fromMetaJSON(json, id)
@@ -145,11 +146,11 @@
145146 new: dataRef => {
146147 const mod = this.actor.createActor(dataRef)
147148 return this.refs.add(mod, 'mod')
148149 },
149- export: (modRef, bufRef) => {
150+ export: (modRef, dataRef) => {
150151 const mod = this.refs.get(modRef, 'mod')
151- let name = this.refs.get(bufRef, 'buf')
152+ let name = this.refs.get(dataRef, 'data')
152153 name = Buffer.from(name).toString()
153154 const funcRef = mod.getFuncRef(name)
154155 return this.refs.add(funcRef, 'func')
155156 },
@@ -158,20 +159,20 @@
158159 }
159160 },
160161 memory: {
161162 externalize: (index, length) => {
162- const buf = Buffer.from(this.get8Memory(index, length))
163- return this.refs.add(buf, 'buf')
163+ const data = Buffer.from(this.get8Memory(index, length))
164+ return this.refs.add(data, 'data')
164165 },
165166 internalize: (dataRef, srcOffset, sinkOffset, length) => {
166- let buf = this.refs.get(dataRef, 'buf')
167- buf = buf.subarray(srcOffset, length)
168- const mem = this.get8Memory(sinkOffset, buf.length)
169- mem.set(buf)
167+ let data = this.refs.get(dataRef, 'data')
168+ data = data.subarray(srcOffset, length)
169+ const mem = this.get8Memory(sinkOffset, data.length)
170+ mem.set(data)
170171 },
171172 length (dataRef) {
172- let buf = this.refs.get(dataRef, 'buf')
173- return buf.length
173+ let data = this.refs.get(dataRef, 'data')
174+ return data.length
174175 }
175176 },
176177 table: {
177178 externalize: (index, length) => {
@@ -232,14 +233,14 @@
232233 }
233234 })
234235
235236 // setup globals
236- let numOfGlobals = this.json.globals.length
237+ let numOfGlobals = this.json.persist.length
237238 if (numOfGlobals) {
238239 const refs = []
239240 while (numOfGlobals--) {
240- const obj = this.actor.storage[numOfGlobals] || DEFAULTS[this.json.globals[numOfGlobals].type]
241- refs.push(this.refs.add(obj, this.json.globals[numOfGlobals].type))
241+ const obj = this.actor.storage[numOfGlobals] || DEFAULTS[this.json.persist[numOfGlobals].type]
242+ refs.push(this.refs.add(obj, this.json.persist[numOfGlobals].type))
242243 }
243244 this.instance.exports.setter_globals(...refs)
244245 }
245246
@@ -251,16 +252,16 @@
251252 }
252253 await this.onDone()
253254
254255 // store globals
255- numOfGlobals = this.json.globals.length
256+ numOfGlobals = this.json.persist.length
256257 if (numOfGlobals) {
257258 this.actor.storage = []
258259 this.instance.exports.getter_globals()
259260 const mem = this.get32Memory(0, numOfGlobals)
260261 while (numOfGlobals--) {
261262 const ref = mem[numOfGlobals]
262- this.actor.storage.push(this.refs.get(ref, this.json.globals[numOfGlobals].type))
263+ this.actor.storage.push(this.refs.get(ref, this.json.persist[numOfGlobals].type))
263264 }
264265 }
265266
266267 this.refs.clear()
customTypes.jsView
@@ -1,228 +1,0 @@
1-const Stream = require('buffer-pipe')
2-const leb = require('leb128')
3-const {findSections} = require('wasm-json-toolkit')
4-
5-const LANGUAGE_TYPES = {
6- 'actor': 0x0,
7- 'buf': 0x1,
8- 'elem': 0x2,
9- 'link': 0x3,
10- 'id': 0x4,
11- 'i32': 0x7f,
12- 'i64': 0x7e,
13- 'f32': 0x7d,
14- 'f64': 0x7c,
15- 'anyFunc': 0x70,
16- 'func': 0x60,
17- 'block_type': 0x40,
18-
19- 0x0: 'actor',
20- 0x1: 'buf',
21- 0x02: 'elem',
22- 0x03: 'link',
23- 0x04: 'id',
24- 0x7f: 'i32',
25- 0x7e: 'i64',
26- 0x7d: 'f32',
27- 0x7c: 'f64',
28- 0x70: 'anyFunc',
29- 0x60: 'func',
30- 0x40: 'block_type'
31-}
32-
33-function encodeJSON (json) {
34- const stream = new Stream()
35- encodeCustomSection('types', json, stream, encodeType)
36- encodeCustomSection('typeMap', json, stream, encodeTypeMap)
37- encodeCustomSection('globals', json, stream, encodeGlobals)
38-
39- return stream.buffer
40-}
41-
42-function encodeCustomSection (name, json, stream, encodingFunc) {
43- let payload = new Stream()
44- json = json[name]
45-
46- if (json) {
47- stream.write([0])
48- // encode type
49- leb.unsigned.write(name.length, payload)
50- payload.write(name)
51- encodingFunc(json, payload)
52- // write the size of the payload
53- leb.unsigned.write(payload.bytesWrote, stream)
54- stream.write(payload.buffer)
55- }
56- return stream
57-}
58-
59-function encodeGlobals (json, stream) {
60- leb.unsigned.write(json.length, stream)
61- for (const entry of json) {
62- leb.unsigned.write(entry.index, stream)
63- leb.unsigned.write(LANGUAGE_TYPES[entry.type], stream)
64- }
65- return stream
66-}
67-
68-function decodeGlobals (buf) {
69- const stream = new Stream(Buffer.from(buf))
70- let numOfEntries = leb.unsigned.read(stream)
71- const json = []
72- while (numOfEntries--) {
73- json.push({
74- index: leb.unsigned.readBn(stream).toNumber(),
75- type: LANGUAGE_TYPES[leb.unsigned.readBn(stream).toNumber()]
76- })
77- }
78- return json
79-}
80-
81-function encodeTypeMap (json, stream) {
82- leb.unsigned.write(json.length, stream)
83- for (let entry of json) {
84- leb.unsigned.write(entry.func, stream)
85- leb.unsigned.write(entry.type, stream)
86- }
87- return stream
88-}
89-
90-function decodeTypeMap (buf) {
91- const stream = new Stream(Buffer.from(buf))
92- let numOfEntries = leb.unsigned.read(stream)
93- const json = []
94- while (numOfEntries--) {
95- json.push({
96- func: leb.unsigned.readBn(stream).toNumber(),
97- type: leb.unsigned.readBn(stream).toNumber()
98- })
99- }
100- return json
101-}
102-
103-function encodeType (json, stream = new Stream()) {
104- let binEntries = new Stream()
105-
106- leb.unsigned.write(json.length, binEntries)
107- for (let entry of json) {
108- // a single type entry binary encoded
109- binEntries.write([LANGUAGE_TYPES[entry.form]]) // the form
110-
111- const len = entry.params.length // number of parameters
112- leb.unsigned.write(len, binEntries)
113- if (len !== 0) {
114- binEntries.write(entry.params.map(type => LANGUAGE_TYPES[type])) // the paramter types
115- }
116-
117- binEntries.write([entry.return_type ? 1 : 0]) // number of return types
118- if (entry.return_type) {
119- binEntries.write([LANGUAGE_TYPES[entry.return_type]])
120- }
121- }
122-
123- stream.write(binEntries.buffer)
124- return stream
125-}
126-
127-function decodeType (buf) {
128- const stream = new Stream(Buffer.from(buf))
129- const numberOfEntries = leb.unsigned.readBn(stream).toNumber()
130- const json = []
131- for (let i = 0; i < numberOfEntries; i++) {
132- let type = stream.read(1)[0]
133- const entry = {
134- form: LANGUAGE_TYPES[type],
135- params: []
136- }
137-
138- let paramCount = leb.unsigned.readBn(stream).toNumber()
139-
140- // parse the entries
141- while (paramCount--) {
142- const type = stream.read(1)[0]
143- entry.params.push(LANGUAGE_TYPES[type])
144- }
145- const numOfReturns = leb.unsigned.readBn(stream).toNumber()
146- if (numOfReturns) {
147- type = stream.read(1)[0]
148- entry.return_type = LANGUAGE_TYPES[type]
149- }
150-
151- json.push(entry)
152- }
153- return json
154-}
155-
156-function injectCustomSection (custom, wasm) {
157- const preramble = wasm.subarray(0, 8)
158- const body = wasm.subarray(8)
159- return Buffer.concat([preramble, custom, body])
160-}
161-
162-function inject (wasm, json) {
163- const buf = encodeJSON(json)
164- return injectCustomSection(buf, wasm)
165-}
166-
167-function mergeTypeSections (json) {
168- const result = {
169- types: [],
170- indexes: {},
171- exports: {},
172- globals: []
173- }
174-
175- const wantedSections = ['types', 'typeMap', 'globals', 'type', 'import', 'function', 'export']
176- const iterator = findSections(json, wantedSections)
177- const mappedFuncs = new Map()
178- const mappedTypes = new Map()
179- const {value: customType} = iterator.next()
180- if (customType) {
181- const type = decodeType(customType.payload)
182- result.types = type
183- }
184- let {value: typeMap} = iterator.next()
185- if (typeMap) {
186- decodeTypeMap(typeMap.payload).forEach(map => mappedFuncs.set(map.func, map.type))
187- }
188-
189- let {value: globals} = iterator.next()
190- if (globals) {
191- result.globals = decodeGlobals(globals.payload)
192- }
193-
194- const {value: type} = iterator.next()
195- const {value: imports = {entries: []}} = iterator.next()
196- const {value: functions = {entries: []}} = iterator.next()
197- functions.entries.forEach((typeIndex, funcIndex) => {
198- let customIndex = mappedFuncs.get(funcIndex)
199- if (customIndex === undefined) {
200- customIndex = mappedTypes.get(typeIndex)
201- }
202- if (customIndex === undefined) {
203- customIndex = result.types.push(type.entries[typeIndex]) - 1
204- mappedTypes.set(typeIndex, customIndex)
205- }
206- result.indexes[funcIndex + imports.entries.length] = customIndex
207- })
208-
209- const {value: exports = {entries: []}} = iterator.next()
210- exports.entries.forEach(entry => {
211- if (entry.kind === 'function') {
212- result.exports[entry.field_str] = entry.index
213- }
214- })
215- return result
216-}
217-
218-module.exports = {
219- injectCustomSection,
220- inject,
221- decodeType,
222- decodeTypeMap,
223- decodeGlobals,
224- encodeType,
225- encodeTypeMap,
226- encodeJSON,
227- mergeTypeSections
228-}

Built with git-ssb-web