git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: e5f317ee074967d082a366e35035d77ef081fa84

Files: e5f317ee074967d082a366e35035d77ef081fa84 / tests / wasmContainer.js

6945 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 print: (dataRef) => {
26 let buf = this.refs.get(dataRef, 'buf')
27 console.log(buf.toString())
28 }
29 }
30 })
31 }
32}
33
34tape('basic', async t => {
35 t.plan(1)
36 tester = t
37 const expectedState = {
38 '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
39 }
40
41 const tree = new RadixTree({
42 db
43 })
44
45 const wasm = fs.readFileSync('./wasm/reciever.wasm')
46
47 const hypervisor = new Hypervisor(tree)
48 hypervisor.registerContainer(TestWasmContainer)
49
50 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
51
52 const message = new Message({
53 funcRef: module.getFuncRef('receive'),
54 funcArguments: [5]
55 })
56 hypervisor.send(message)
57 const stateRoot = await hypervisor.createStateRoot()
58 // t.deepEquals(stateRoot, expectedState, 'expected root!')
59})
60
61tape('two communicating actors', async t => {
62 t.plan(1)
63 tester = t
64 const expectedState = {
65 '/': Buffer.from('8c230b5f0f680199b24ecd1800c2970dfca7cfdc', 'hex')
66 }
67
68 const tree = new RadixTree({db})
69
70 const recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
71 const callerWasm = fs.readFileSync('./wasm/caller.wasm')
72
73 const hypervisor = new Hypervisor(tree)
74 hypervisor.registerContainer(TestWasmContainer)
75
76 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
77 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
78 const message = new Message({
79 funcRef: callerMod.getFuncRef('call'),
80 funcArguments: [receiverMod.getFuncRef('receive')]
81 })
82
83 hypervisor.send(message)
84 const stateRoot = await hypervisor.createStateRoot()
85 // t.deepEquals(stateRoot, expectedState, 'expected root!')
86})
87
88tape('two communicating actors with callback', async t => {
89 t.plan(1)
90 tester = t
91 const expectedState = {
92 '/': Buffer.from('9bf27cf07b75a90e0af530e2df73e3102482b24a', 'hex')
93 }
94
95 const tree = new RadixTree({
96 db
97 })
98
99 const recieverWasm = fs.readFileSync('./wasm/funcRef_reciever.wasm')
100 const callerWasm = fs.readFileSync('./wasm/funcRef_caller.wasm')
101
102 const hypervisor = new Hypervisor(tree)
103 hypervisor.registerContainer(TestWasmContainer)
104
105 const {module: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
106 const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
107
108 const message = new Message({
109 funcRef: callerMod.getFuncRef('call'),
110 funcArguments: [receiverMod.getFuncRef('receive')]
111 })
112
113 hypervisor.send(message)
114 const stateRoot = await hypervisor.createStateRoot()
115 // t.deepEquals(stateRoot, expectedState, 'expected root!')
116})
117
118tape('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
148tape('externalize/internalize memory', async t => {
149 t.plan(1)
150 tester = t
151 const tree = new RadixTree({
152 db
153 })
154
155 const wasm = fs.readFileSync('./wasm/memory.wasm')
156
157 const hypervisor = new Hypervisor(tree)
158 hypervisor.registerContainer(TestWasmContainer)
159
160 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
161
162 const message = new Message({
163 funcRef: module.getFuncRef('test')
164 }).on('done', actor => {
165 const a = actor.container.getMemory(0, 5)
166 const b = actor.container.getMemory(5, 5)
167 t.deepEquals(a, b, 'should copy memory correctly')
168 })
169 hypervisor.send(message)
170})
171
172tape('externalize/internalize table', async t => {
173 t.plan(1)
174 tester = t
175 const tree = new RadixTree({
176 db
177 })
178
179 const wasm = fs.readFileSync('./wasm/table.wasm')
180 const hypervisor = new Hypervisor(tree)
181 hypervisor.registerContainer(TestWasmContainer)
182
183 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
184
185 const message = new Message({
186 funcRef: module.getFuncRef('test')
187 }).on('done', actor => {
188 const a = actor.container.getMemory(0, 8)
189 const b = actor.container.getMemory(8, 8)
190 t.deepEquals(a, b, 'should copy memory correctly')
191 })
192 hypervisor.send(message)
193})
194
195tape('load / store globals', async t => {
196 t.plan(1)
197 tester = t
198 const tree = new RadixTree({
199 db
200 })
201
202 const wasm = fs.readFileSync('./wasm/globals.wasm')
203
204 const hypervisor = new Hypervisor(tree)
205 hypervisor.registerContainer(TestWasmContainer)
206
207 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
208
209 await new Promise((resolve, reject) => {
210 const message = new Message({
211 funcRef: module.getFuncRef('store')
212 }).on('done', actor => {
213 resolve()
214 })
215 hypervisor.send(message)
216 })
217
218 await new Promise((resolve, reject) => {
219 const message = new Message({
220 funcRef: module.getFuncRef('load')
221 }).on('done', actor => {
222 resolve()
223 const b = actor.container.getMemory(5, 4)
224 const result = Buffer.from(b).toString()
225 t.deepEquals(result, 'test', 'should copy memory correctly')
226 })
227 hypervisor.send(message)
228 })
229})
230
231tape.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})
251

Built with git-ssb-web