git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: bc67c2749ae9d68f54ef3d51c504dd4722172ec7

Files: bc67c2749ae9d68f54ef3d51c504dd4722172ec7 / tests / wasmContainer.js

4435 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const Message = require('../message.js')
4const Hypervisor = require('../')
5const WasmContainer = require('../wasmContainer.js')
6
7const level = require('level-browserify')
8const RadixTree = require('dfinity-radix-tree')
9const db = level('./testdb')
10
11let tester
12
13class TestWasmContainer extends WasmContainer {
14 constructor (actor) {
15 super(actor)
16 this._storage = new Map()
17 }
18 getInterface (funcRef) {
19 const orginal = super.getInterface(funcRef)
20 return Object.assign(orginal, {
21 test: {
22 check: (a, b) => {
23 tester.equals(a, b)
24 }
25 }
26 })
27 }
28}
29
30tape('basic', async t => {
31 t.plan(2)
32 tester = t
33 const expectedState = {
34 '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
35 }
36
37 const tree = new RadixTree({
38 db
39 })
40
41 const wasm = fs.readFileSync('./wasm/reciever.wasm')
42
43 const hypervisor = new Hypervisor(tree)
44 hypervisor.registerContainer(TestWasmContainer)
45
46 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
47
48 const message = new Message({
49 funcRef: module.getFuncRef('receive'),
50 funcArguments: [5]
51 })
52 hypervisor.send(message)
53 const stateRoot = await hypervisor.createStateRoot()
54 t.deepEquals(stateRoot, expectedState, 'expected root!')
55})
56
57tape('two communicating actors', async t => {
58 t.plan(2)
59 tester = t
60 const expectedState = {
61 '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
62 }
63
64 const tree = new RadixTree({db})
65
66 const recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
67 const callerWasm = fs.readFileSync('./wasm/caller.wasm')
68
69 const hypervisor = new Hypervisor(tree)
70 hypervisor.registerContainer(TestWasmContainer)
71
72 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
73 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
74 const message = new Message({
75 funcRef: callerMod.getFuncRef('call'),
76 funcArguments: [receiverMod.getFuncRef('receive')]
77 })
78
79 hypervisor.send(message)
80 const stateRoot = await hypervisor.createStateRoot()
81 t.deepEquals(stateRoot, expectedState, 'expected root!')
82})
83
84tape('two communicating actors with callback', async t => {
85 t.plan(2)
86 tester = t
87 const expectedState = {
88 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
89 }
90
91 const tree = new RadixTree({
92 db
93 })
94
95 const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm')
96 const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm')
97
98 const hypervisor = new Hypervisor(tree)
99 hypervisor.registerContainer(TestWasmContainer)
100
101 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
102 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
103
104 const message = new Message({
105 funcRef: callerMod.getFuncRef('call'),
106 funcArguments: [receiverMod.getFuncRef('receive')]
107 })
108
109 hypervisor.send(message)
110 const stateRoot = await hypervisor.createStateRoot()
111 t.deepEquals(stateRoot, expectedState, 'expected root!')
112})
113
114tape('externalize/internalize memory', async t => {
115 t.plan(1)
116 tester = t
117 const tree = new RadixTree({
118 db
119 })
120
121 const wasm = fs.readFileSync('./wasm/memory.wasm')
122
123 const hypervisor = new Hypervisor(tree)
124 hypervisor.registerContainer(TestWasmContainer)
125
126 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
127
128 const message = new Message({
129 funcRef: module.getFuncRef('test')
130 }).on('done', actor => {
131 const a = actor.container.getMemory(0, 5)
132 const b = actor.container.getMemory(5, 5)
133 t.deepEquals(a, b, 'should copy memory correctly')
134 })
135 hypervisor.send(message)
136})
137
138tape('externalize/internalize table', async t => {
139 t.plan(1)
140 tester = t
141 const tree = new RadixTree({
142 db
143 })
144
145 const wasm = fs.readFileSync('./wasm/table.wasm')
146
147 const hypervisor = new Hypervisor(tree)
148 hypervisor.registerContainer(TestWasmContainer)
149
150 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
151
152 const message = new Message({
153 funcRef: module.getFuncRef('test')
154 }).on('done', actor => {
155 const a = actor.container.getMemory(0, 8)
156 const b = actor.container.getMemory(8, 8)
157 t.deepEquals(a, b, 'should copy memory correctly')
158 })
159 hypervisor.send(message)
160})
161

Built with git-ssb-web