git ssb

0+

wanderer🌟 / js-primea-hypervisor



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.jschanged
tests/wasmContainer.jschanged
tests/wast2wasm.jschanged
wasmContainer.jschanged
index.jsView
@@ -29,10 +29,12 @@
2929 }
3030
3131 async loadActor (id) {
3232 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+ ])
3537 const [type, nonce] = state.value
3638 const Container = this._containerTypes[type]
3739
3840 // create a new actor instance
tests/wasmContainer.jsView
@@ -20,8 +20,12 @@
2020 return Object.assign(orginal, {
2121 test: {
2222 check: (a, b) => {
2323 tester.equals(a, b)
24+ },
25+ print: (dataRef) => {
26+ let buf = this.refs.get(dataRef, 'buf')
27+ console.log(buf.toString())
2428 }
2529 }
2630 })
2731 }
@@ -110,8 +114,38 @@
110114 const stateRoot = await hypervisor.createStateRoot()
111115 // t.deepEquals(stateRoot, expectedState, 'expected root!')
112116 })
113117
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+
114148 tape('externalize/internalize memory', async t => {
115149 t.plan(1)
116150 tester = t
117151 const tree = new RadixTree({
@@ -192,4 +226,25 @@
192226 })
193227 hypervisor.send(message)
194228 })
195229 })
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.jsView
@@ -12,21 +12,25 @@
1212 try {
1313 json = fs.readFileSync(`${__dirname}/wast/${file}.json`)
1414 json = JSON.parse(json)
1515 } catch (e) {
16- console.log('no json')
16+ console.log(`no json for ${file}`)
1717 }
1818
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)
2732 }
28- fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary)
2933 }
3034 }
3135
3236 filesWast2wasm()
wasmContainer.jsView
@@ -6,8 +6,10 @@
66 const injectGlobals = require('./injectGlobals.js')
77 const typeCheckWrapper = require('./typeCheckWrapper.js')
88 const {FunctionRef, ModuleRef, DEFAULTS} = require('./systemObjects.js')
99
10+const FUNC_INDEX_OFFSET = 1
11+
1012 const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64'])
1113 const LANGUAGE_TYPES = {
1214 0x0: 'actor',
1315 0x1: 'buf',
@@ -95,22 +97,20 @@
9597 externalize: index => {
9698 const func = this.instance.exports.table.get(index)
9799 const object = func.object
98100 if (object) {
101+ // externalize a pervously internalized function
99102 return self.refs.add(object)
100103 } 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')
103107 }
104108 },
105109 internalize: (ref, index) => {
106110 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)
113113 },
114114 catch: (ref, catchRef) => {
115115 const {funcRef} = self.refs.get(ref, FunctionRef)
116116 const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef)
@@ -154,8 +154,12 @@
154154 let buf = this.refs.get(dataRef, 'buf')
155155 buf = buf.subarray(srcOffset, length)
156156 const mem = this.getMemory(sinkOffset, buf.length)
157157 mem.set(buf)
158+ },
159+ length (dataRef) {
160+ let buf = this.refs.get(dataRef, 'buf')
161+ return buf.length
158162 }
159163 },
160164 table: {
161165 externalize: (index, length) => {
@@ -187,8 +191,9 @@
187191 }
188192
189193 async onMessage (message) {
190194 const funcRef = message.funcRef
195+ // console.log(funcRef)
191196 const intef = this.getInterface(funcRef)
192197 this.instance = WebAssembly.Instance(this.mod, intef)
193198 // map table indexes
194199 const table = this.instance.exports.table
@@ -229,8 +234,9 @@
229234 this.instance.exports[funcRef.identifier](...args)
230235 }
231236 await this.onDone()
232237
238+ // store globals
233239 numOfGlobals = this.json.globals.length
234240 if (numOfGlobals) {
235241 this.actor.storage = []
236242 this.instance.exports.getter_globals()
@@ -293,12 +299,8 @@
293299 this.json = json
294300 this.modSelf = ModuleRef.fromMetaJSON(json, this.actor.id)
295301 }
296302
297- onShutdown () {
298-
299- }
300-
301303 getMemory (offset, length) {
302304 return new Uint8Array(this.instance.exports.memory.buffer, offset, length)
303305 }
304306

Built with git-ssb-web