git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit d42f5bf9d7ad822c5a87be84d90bd79af0f4b715

add link test

wanderer committed on 4/3/2018, 5:16:16 AM
Parent: ebf9c8db29ed91f8c1969b8829b315aba47469fc

Files changed

index.jschanged
tests/index.jschanged
tests/wasm/storage.wasmchanged
tests/wasm/link.wasmadded
tests/wast/storage.wastchanged
tests/wast/link.jsonadded
tests/wast/link.wastadded
index.jsView
@@ -61,8 +61,9 @@
6161 module.exports = class WasmContainer {
6262 constructor (actor) {
6363 this.actor = actor
6464 this.refs = new ReferanceMap()
65+ this._opsQueue = Promise.resolve()
6566 }
6667
6768 static createModule (wasm, id) {
6869 if (!WebAssembly.validate(wasm)) {
@@ -131,11 +132,14 @@
131132 const link = {'/': obj}
132133 return self.refs.add(link, 'link')
133134 },
134135 unwrap: async (ref, cb) => {
135- const obj = self.refs.get(ref, 'link')
136- const promise = self.actor.tree.graph.tree(obj)
137- await self._opsQueue.push(promise)
136+ const link = self.refs.get(ref, 'link')
137+ const promise = self.actor.tree.graph.tree(link)
138+ await self.pushOpsQueue(promise)
139+ const obj = (await promise)['/']
140+ const linkRef = self.refs.add(obj, getType(obj))
141+ self.instance.exports.table.get(cb)(linkRef)
138142 }
139143 },
140144 module: {
141145 new: dataRef => {
tests/index.jsView
@@ -402,4 +402,41 @@
402402 }).on('execution:error', e => console.log(e))
403403
404404 hypervisor.send(message2)
405405 })
406+
407+tape('link', async t => {
408+ tester = t
409+ const tree = new RadixTree({db})
410+ let wasm = fs.readFileSync(WASM_PATH + '/link.wasm')
411+
412+ const egress = new EgressDriver()
413+
414+ egress.on('message', msg => {
415+ t.equals(msg.funcArguments[0].toString(), 'hello world')
416+ t.end()
417+ })
418+
419+ const hypervisor = new Hypervisor(tree, [TestWasmContainer], [egress])
420+
421+ const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
422+ const funcRef = module.getFuncRef('main')
423+ funcRef.gas = 322000
424+
425+ const message = new Message({
426+ funcRef
427+ })
428+
429+ hypervisor.send(message)
430+
431+ const funcRef2 = module.getFuncRef('load')
432+ funcRef2.gas = 322000
433+
434+ await hypervisor.createStateRoot()
435+
436+ const message2 = new Message({
437+ funcRef: funcRef2,
438+ funcArguments: [new FunctionRef({actorID: egress.id, params: ['data']})]
439+ }).on('execution:error', e => console.log(e))
440+
441+ hypervisor.send(message2)
442+})
tests/wasm/storage.wasmView
@@ -1,2 +1,2 @@
11 asm types`n typeMap``````�testchecktestprintmemory externalizestoragegetstoragesetelem externalizeelem internalizefunc internalizep memorytablemainload
2-4AAA 6AA AAAA A(  A hello world
2+4AAA 6AA AAAA A(A A hello world
tests/wasm/link.wasmView
@@ -1,0 +1,4 @@
1+asm types`n typeMap#```````�
2+testchecktestprintmemory externalizestoragegetstoragesetelem externalizeelem internalizefunc internalizelinkwraplinkunwrappA  memorytablemain
3+load A 
4+CAAA 6AA  $AAAA(A A# A A hello world
tests/wast/storage.wastView
@@ -32,10 +32,8 @@
3232 (get_local $egress))
3333
3434 (call_indirect (param i32)
3535 (i32.load (i32.const 0))
36- (get_local $egress))
37-
38- )
36+ (i32.const 0)))
3937
4038 (export "main" (func $main))
4139 (export "load" (func $load)))
tests/wast/link.jsonView
@@ -1,0 +1,12 @@
1+{
2+ "types": [{
3+ "form": "func",
4+ "params": [
5+ "func"
6+ ]
7+ }],
8+ "typeMap": [{
9+ "func": 1,
10+ "type": 0
11+ }]
12+}
tests/wast/link.wastView
@@ -1,0 +1,52 @@
1+(module
2+ (import "test" "check" (func $check (param i32 i32)))
3+ (import "test" "print" (func $print (param i32)))
4+ (import "memory" "externalize" (func $mem.externalize (param i32 i32) (result i32)))
5+ (import "storage" "get" (func $storage.get (result i32)))
6+ (import "storage" "set" (func $storage.set (param i32)))
7+ (import "elem" "externalize" (func $elem.externalize (param i32 i32) (result i32)))
8+ (import "elem" "internalize" (func $elem.internalize (param i32 i32 i32 i32)))
9+ (import "func" "internalize" (func $func.internalize (param i32 i32)))
10+ (import "link" "wrap" (func $link.wrap (param i32) (result i32)))
11+ (import "link" "unwrap" (func $link.unwrap (param i32 i32)))
12+
13+ (memory (export "memory") 1)
14+ (data (i32.const 0) "hello world")
15+ (table (export "table") 1 1 anyfunc)
16+ (elem (i32.const 0) $callback)
17+ (global $egressRef (mut i32) (i32.const 0))
18+
19+ (func $main
20+ (i32.store
21+ (i32.const 0)
22+ (call $link.wrap
23+ (call $mem.externalize (i32.const 0) (i32.const 11))))
24+
25+ (call $storage.set
26+ (call $elem.externalize (i32.const 0) (i32.const 1))))
27+
28+ (func $load (param $egress i32)
29+ (set_global $egressRef (get_local $egress))
30+
31+ (call $elem.internalize
32+ (call $storage.get)
33+ (i32.const 0)
34+ (i32.const 0)
35+ (i32.const 1))
36+
37+ (call $link.unwrap
38+ (i32.load (i32.const 0))
39+ (i32.const 0)))
40+
41+ (func $callback (param $ref i32)
42+ (call $func.internalize
43+ (i32.const 0)
44+ (get_global $egressRef))
45+
46+ (call_indirect (param i32)
47+ (get_local $ref)
48+ (i32.const 0))
49+ )
50+
51+ (export "main" (func $main))
52+ (export "load" (func $load)))

Built with git-ssb-web