git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit e6bb7dfe86f19f0188e093e016c11a70df79bf72

move to cbor

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 2/26/2018, 11:03:29 PM
Parent: d4fe677d602e80fa12937293728c8710033f1161

Files changed

package-lock.jsonchanged
package.jsonchanged
tests/wasmContainer.jschanged
typeCheckWrapper.jschanged
wasmContainer.jschanged
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 344343 bytes
New file size: 336834 bytes
package.jsonView
@@ -33,12 +33,12 @@
3333 "binary-search-insert": "^1.0.3",
3434 "buffer-pipe": "0.0.2",
3535 "events": "^2.0.0",
3636 "leb128": "0.0.4",
37- "levelup": "^2.0.1",
37+ "levelup": "^2.0.2",
3838 "reference-map": "^1.2.1",
3939 "safe-buffer": "^5.1.1",
40- "wasm-json-toolkit": "^0.2.1",
40+ "wasm-json-toolkit": "^0.2.2",
4141 "wasm-metering": "^0.1.1"
4242 },
4343 "devDependencies": {
4444 "coveralls": "^3.0.0",
@@ -46,9 +46,9 @@
4646 "documentation": "^5.3.5",
4747 "level-browserify": "^1.1.1",
4848 "nyc": "^11.4.1",
4949 "primea-abstract-container": "0.0.6",
50- "standard": "10.0.3",
51- "tape": "^4.6.3",
50+ "standard": "11.0.0",
51+ "tape": "^4.9.0",
5252 "wabt": "^1.0.0"
5353 }
5454 }
tests/wasmContainer.jsView
@@ -97,10 +97,10 @@
9797
9898 const hypervisor = new Hypervisor(tree)
9999 hypervisor.registerContainer(TestWasmContainer)
100100
101+ const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
101102 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
102- const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
103103
104104 const message = new Message({
105105 funcRef: callerMod.getFuncRef('call'),
106106 funcArguments: [receiverMod.getFuncRef('receive')]
@@ -142,9 +142,8 @@
142142 db
143143 })
144144
145145 const wasm = fs.readFileSync('./wasm/table.wasm')
146-
147146 const hypervisor = new Hypervisor(tree)
148147 hypervisor.registerContainer(TestWasmContainer)
149148
150149 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
typeCheckWrapper.jsView
@@ -11,9 +11,9 @@
1111 'func': 0x60,
1212 'block_type': 0x40
1313 }
1414
15-module.exports = function (type) {
15+module.exports = function (params) {
1616 const module = [{
1717 'name': 'preramble',
1818 'magic': [
1919 0,
@@ -110,9 +110,9 @@
110110 const invokeType = module[1].entries[3].params
111111 const checkCode = module[7].entries[0].code
112112 const invokeCode = module[7].entries[1].code
113113
114- type.params.forEach((param, index) => {
114+ params.forEach((param, index) => {
115115 let baseType = param
116116 const typeCode = LANGUAGE_TYPES[param]
117117 // import type
118118 if (definedTypes.has(param)) {
wasmContainer.jsView
@@ -35,23 +35,15 @@
3535 0x40: 'block_type'
3636 }
3737
3838 class FunctionRef {
39- constructor (location, identifier, json, id) {
39+ constructor (location, identifier, params, id) {
4040 this.location = location
4141 this.destId = id
42- let funcIndex
43- if (location === 'export') {
44- this.indentifier = identifier
45- funcIndex = json.exports[identifier]
46- } else {
47- this.indentifier = identifier.tableIndex
48- funcIndex = Number(identifier.name) - 1
49- }
50- const typeIndex = json.indexes[funcIndex]
51- this.type = json.types[typeIndex]
42+ this.params = params
43+ this.identifier = identifier
5244
53- const wrapper = typeCheckWrapper(this.type)
45+ const wrapper = typeCheckWrapper(params)
5446 const wasm = json2wasm(wrapper)
5547 const mod = WebAssembly.Module(wasm)
5648 const self = this
5749 this.wrapper = WebAssembly.Instance(mod, {
@@ -82,21 +74,37 @@
8274 }
8375 }
8476
8577 class ModuleRef {
86- constructor (json, id) {
87- this._json = json
78+ constructor (ex, id) {
79+ this.exports = ex
8880 this.id = id
8981 }
9082
9183 getFuncRef (name) {
92- return new FunctionRef('export', name, this._json, this.id)
84+ return new FunctionRef('export', name, this.exports[name], this.id)
9385 }
9486
95- serialize () {
96- return this._json
87+ encodeCBOR (gen) {
88+ return gen.write({
89+ '#': {
90+ exports: this.exports,
91+ '@': {
92+ id: this.id
93+ }
94+ }
95+ })
9796 }
9897
98+ static fromMetaJSON (json, id) {
99+ const exports = {}
100+ for (const ex in json.exports) {
101+ const type = json.types[json.indexes[json.exports[ex].toString()]].params
102+ exports[ex] = type
103+ }
104+ return new ModuleRef(exports, id)
105+ }
106+
99107 static deserialize (serialized) {}
100108 }
101109
102110 module.exports = class WasmContainer {
@@ -125,9 +133,9 @@
125133 new Promise((resolve, reject) => {
126134 cachedb.put(id.toString() + 'code', wasm.toString('hex'), resolve)
127135 })
128136 ])
129- return new ModuleRef(json, id)
137+ return ModuleRef.fromMetaJSON(json, id)
130138 }
131139
132140 getInterface (funcRef) {
133141 const self = this
@@ -143,9 +151,9 @@
143151 return self.refs.add(ref)
144152 }
145153 },
146154 internalize: (ref, index) => {
147- const funcRef = self.refs.get(ref)
155+ const funcRef = self.refs.get(ref, 'func')
148156 funcRef.container = self
149157 this.instance.exports.table.set(index, funcRef.wrapper.exports.check)
150158 },
151159 catch: (ref, catchRef) => {
@@ -178,9 +186,9 @@
178186 const funcRef = mod.getFuncRef(name)
179187 return this.refs.add(funcRef, 'func')
180188 },
181189 self: () => {
182- return this.refs.add(this.moduleObj, 'mod')
190+ return this.refs.add(this.modSelf, 'mod')
183191 }
184192 },
185193 memory: {
186194 externalize: (index, length) => {
@@ -223,8 +231,9 @@
223231 }
224232 }
225233
226234 async onMessage (message) {
235+ try {
227236 const funcRef = message.funcRef
228237 const intef = this.getInterface(funcRef)
229238 this.instance = WebAssembly.Instance(this.mod, intef)
230239 const table = this.instance.exports.table
@@ -237,19 +246,19 @@
237246 }
238247 }
239248 }
240249 const args = message.funcArguments.map((arg, index) => {
241- const type = funcRef.type.params[index]
250+ const type = funcRef.params[index]
242251 if (nativeTypes.has(type)) {
243252 return arg
244253 } else {
245254 return this.refs.add(arg, type)
246255 }
247256 })
248257 if (funcRef.location === 'export') {
249- this.instance.exports[funcRef.indentifier](...args)
258+ this.instance.exports[funcRef.identifier](...args)
250259 } else {
251- this.instance.exports.table.get(funcRef.indentifier)(...args)
260+ this.instance.exports.table.get(funcRef.identifier)(...args)
252261 }
253262 await this.onDone()
254263
255264 let numOfGlobals = this.json.globals.length
@@ -264,8 +273,11 @@
264273 this.actor.state.set(Buffer.from([1]), storage)
265274 }
266275
267276 this.refs.clear()
277+ } catch (e) {
278+ console.log(e)
279+ }
268280 }
269281
270282 /**
271283 * returns a promise that resolves when the wasm instance is done running
@@ -313,9 +325,9 @@
313325 wasm = Buffer.from(wasm, 'hex')
314326 json = JSON.parse(json)
315327 this.mod = WebAssembly.Module(wasm)
316328 this.json = json
317- this.moduleObj = new ModuleRef(json, this.actor.id)
329+ this.modSelf = ModuleRef.fromMetaJSON(json, this.actor.id)
318330 }
319331
320332 getMemory (offset, length) {
321333 return new Uint8Array(this.instance.exports.memory.buffer, offset, length)

Built with git-ssb-web