git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 124610c716c1ba494bca1d28f39840fff9c2fa6b

added test for memory ops

wanderer committed on 2/21/2018, 10:37:42 PM
Parent: 1ffd915b15f2f2a528bc6e5329be0be2c9cab33b

Files changed

actor.jschanged
tests/wasm/memory.wasmadded
tests/wasmContainer.jschanged
tests/wast/memory.wastadded
wasmContainer.jschanged
actor.jsView
@@ -61,9 +61,9 @@
6161 await this.container.onMessage(message)
6262 } catch (e) {
6363 message.emit('execution:error', e)
6464 }
65- message.emit('done')
65+ message.emit('done', this)
6666 }
6767
6868 /**
6969 * updates the number of ticks that the actor has run
tests/wasm/memory.wasmView
@@ -1,0 +1,3 @@
1+asm````8testcheckmemory externalizememory internalizememorytest
2+AAAAA
3+A test
tests/wasmContainer.jsView
@@ -24,16 +24,8 @@
2424 }
2525 }
2626 })
2727 }
28- setState (key, ref) {
29- const obj = this.refs.get(ref)
30- this._storage.set(key, obj)
31- }
32- getState (key) {
33- const obj = this._storage.get(key)
34- return this.refs.add(obj)
35- }
3628 }
3729
3830 tape('basic', async t => {
3931 t.plan(2)
@@ -116,63 +108,33 @@
116108
117109 hypervisor.send(message)
118110 const stateRoot = await hypervisor.createStateRoot()
119111 t.deepEquals(stateRoot, expectedState, 'expected root!')
120- // t.end()
121112 })
122113
123-tape.skip('two communicating actors with callback', async t => {
124- t.plan(2)
114+tape('externalize/internalize memory', async t => {
115+ t.plan(1)
125116 tester = t
126117 const expectedState = {
127- '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
118+ '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
128119 }
129120
130121 const tree = new RadixTree({
131122 db
132123 })
133124
134- const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm')
135- const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm')
125+ const wasm = fs.readFileSync('./wasm/memory.wasm')
136126
137127 const hypervisor = new Hypervisor(tree)
138128 hypervisor.registerContainer(TestWasmContainer)
139129
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!')
151- t.end()
152-})
153-
154-// Increment a counter.
155-tape.skip('increment', async t => {
156- const tree = new RadixTree({
157- db
158- })
159-
160- const wasm = fs.readFileSync('./wasm/counter.wasm')
161-
162- const hypervisor = new Hypervisor(tree)
163- hypervisor.registerContainer(TestWasmContainer)
164-
165130 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
166131
167132 const message = new Message({
168- funcRef: module.increment,
169- funcArguments: []
133+ funcRef: module.getFuncRef('test')
134+ }).on('done', actor => {
135+ const a = actor.container.getMemory(0, 5)
136+ const b = actor.container.getMemory(5, 5)
137+ t.deepEquals(a, b, 'should copy memory correctly')
170138 })
171139 hypervisor.send(message)
172-
173- const stateRoot = await hypervisor.createStateRoot()
174- t.end()
175-
176- console.log(stateRoot)
177-
178140 })
tests/wast/memory.wastView
@@ -1,0 +1,16 @@
1+(module
2+ (import "test" "check" (func $check (param i32 i32)))
3+ (import "memory" "externalize" (func $externalize (param i32 i32) (result i32)))
4+ (import "memory" "internalize" (func $internalize (param i32 i32 i32 i32)))
5+ (memory (export "memory") 1)
6+ (data (i32.const 0) "test")
7+ (func $test
8+ i32.const 0
9+ i32.const 4
10+ call $externalize
11+ i32.const 0
12+ i32.const 5
13+ i32.const 4
14+ call $internalize
15+ )
16+ (export "test" (func $test)))
wasmContainer.jsView
@@ -148,11 +148,11 @@
148148 this.refs = new ReferanceMap()
149149 }
150150
151151 static async onCreation (wasm, id, cachedb) {
152- if (!WebAssembly.validate(wasm)) {
153- throw new Error('invalid wasm binary')
154- }
152+ // if (!WebAssembly.validate(wasm)) {
153+ // throw new Error('invalid wasm binary')
154+ // }
155155 let moduleJSON = wasm2json(wasm)
156156 const json = customTypes.mergeTypeSections(moduleJSON)
157157 moduleJSON = wasmMetering.meterJSON(moduleJSON, {
158158 meterType: 'i32'
@@ -229,12 +229,12 @@
229229 externalize: (index, length) => {
230230 const buf = this.getMemory(index, length)
231231 return this.refs.add(buf, 'buf')
232232 },
233- internalize: (dataRef, writeOffset, readOffset, length) => {
233+ internalize: (dataRef, srcOffset, sinkOffset, length) => {
234234 let buf = this.refs.get(dataRef, 'buf')
235- buf = buf.subarray(readOffset, length)
236- const mem = this.getMemory(writeOffset, buf.length)
235+ buf = buf.subarray(srcOffset, length)
236+ const mem = this.getMemory(sinkOffset, buf.length)
237237 mem.set(buf)
238238 }
239239 },
240240 table: {
@@ -297,8 +297,11 @@
297297 this.instance.exports.table.get(funcRef.indentifier)(...args)
298298 }
299299 await this.onDone()
300300 this.refs.clear()
301+ if (this.json.globals.length) {
302+ this.instance.exports.getter_globals()
303+ }
301304 }
302305
303306 /**
304307 * returns a promise that resolves when the wasm instance is done running

Built with git-ssb-web