git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 72a22e2c56fb1973e226eac66319ef4ea3909a6f

added two communicating actor test

wanderer committed on 2/12/2018, 7:50:47 PM
Parent: 0df8aae87c8f14c08596394b83b87af8a4d64614

Files changed

tests/wasmContainer.jschanged
wasmContainer.jschanged
tests/wasmContainer.jsView
@@ -42,9 +42,9 @@
4242 '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
4343 }
4444
4545 const tree = new RadixTree({
46- db: db
46+ db
4747 })
4848
4949 const wasm = fs.readFileSync('./wasm/reciever.wasm')
5050
@@ -61,17 +61,17 @@
6161 const stateRoot = await hypervisor.createStateRoot()
6262 t.deepEquals(stateRoot, expectedState, 'expected root!')
6363 })
6464
65-tape('basic', async t => {
65+tape('two communicating actors', async t => {
6666 t.plan(2)
6767 tester = t
6868 const expectedState = {
69- '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
69+ '/': Buffer.from('f3cc5ba63d6b1737bea2c33bd1942e5488787b82', 'hex')
7070 }
7171
7272 const tree = new RadixTree({
73- db: db
73+ db
7474 })
7575
7676 const recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
7777 const callerWasm = fs.readFileSync('./wasm/caller.wasm')
@@ -82,21 +82,21 @@
8282 const {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
8383 const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
8484
8585 const message = new Message({
86- funcRef: receiverExports.receive,
87- funcArguments: [5]
86+ funcRef: callerExports.call,
87+ funcArguments: [receiverExports.receive]
8888 })
89+
8990 hypervisor.send(message)
9091 const stateRoot = await hypervisor.createStateRoot()
9192 t.deepEquals(stateRoot, expectedState, 'expected root!')
9293 })
9394
9495 // Increment a counter.
9596 tape.skip('increment', async t => {
96-
9797 const tree = new RadixTree({
98- db: db
98+ db
9999 })
100100
101101 const wasm = fs.readFileSync('./wasm/counter.wasm')
102102
wasmContainer.jsView
@@ -1,5 +1,6 @@
11 const {wasm2json, json2wasm} = require('wasm-json-toolkit')
2+const Message = require('./message.js')
23 const wasmMetering = require('wasm-metering')
34 const customTypes = require('./customTypes.js')
45 const typeCheckWrapper = require('./typeCheckWrapper.js')
56 const ReferanceMap = require('reference-map')
@@ -59,8 +60,12 @@
5960 static deserialize (serialized) {}
6061 }
6162
6263 class FunctionRef {
64+ static get type () {
65+ return 'funcRef'
66+ }
67+
6368 constructor (name, json, id) {
6469 this.name = name
6570 this.destId = id
6671 this.args = []
@@ -73,20 +78,22 @@
7378 const instance = WebAssembly.Instance(this.mod, {
7479 'env': {
7580 'checkTypes': function () {
7681 const args = [...arguments]
82+ const checkedArgs = []
7783 while (args.length) {
7884 const type = LANGUAGE_TYPES[args.shift()]
7985 let arg = args.shift()
8086 if (!nativeTypes.has(type)) {
8187 arg = self._container.refs.get(arg, type)
8288 }
83- self.args.push({
84- arg,
85- type
86- })
89+ checkedArgs.push(arg)
8790 }
88- self._container.sendMessage(instance)
91+ const message = new Message({
92+ funcRef: self,
93+ funcArguments: checkedArgs
94+ })
95+ self._container.actor.send(message)
8996 }
9097 }
9198 })
9299 this.wrapper = instance
@@ -134,14 +141,11 @@
134141 return {
135142 func: {
136143 externalize: () => {},
137144 internalize: (ref, index) => {
138- const {type, arg} = self.refs.get(ref)
139- if (type !== 'funcRef') {
140- throw new Error('invalid type')
141- }
142- arg.container = self
143- this.instance.exports.table.set(index, arg.wrapper.exports.check)
145+ const funcRef = self.refs.get(ref, 'funcRef')
146+ funcRef.container = self
147+ this.instance.exports.table.set(index, funcRef.wrapper.exports.check)
144148 },
145149 catch: (ref, catchRef) => {
146150 const {funcRef} = self.refs.get(ref, FunctionRef)
147151 const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef)
@@ -219,20 +223,20 @@
219223 const funcRef = message.funcRef
220224 const intef = this.getInteface(funcRef)
221225 this.instance = WebAssembly.Instance(this.mod, intef)
222226 if (this.instance.exports.table) {
223- this._orginalTable = this.instance.exports.table.slice()
227+ this._orginalTable = this.instance.exports.table
224228 }
225229 const args = message.funcArguments.map(arg => {
226230 if (typeof arg === 'number') {
227231 return arg
228232 } else {
229- return this.refs.add(arg)
233+ return this.refs.add(arg, arg.constructor.type)
230234 }
231235 })
232236 this.instance.exports[funcRef.name](...args)
233237 await this.onDone()
234- this.referanceMap.clear()
238+ this.refs.clear()
235239 }
236240
237241 /**
238242 * returns a promise that resolves when the wasm instance is done running

Built with git-ssb-web