Files: d4fe677d602e80fa12937293728c8710033f1161 / tests / wasmContainer.js
5520 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const Message = require('../message.js') |
4 | const Hypervisor = require('../') |
5 | const WasmContainer = require('../wasmContainer.js') |
6 | |
7 | const level = require('level-browserify') |
8 | const RadixTree = require('dfinity-radix-tree') |
9 | const db = level('./testdb') |
10 | |
11 | let tester |
12 | |
13 | class 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 | |
30 | tape('basic', async t => { |
31 | t.plan(1) |
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 | |
57 | tape('two communicating actors', async t => { |
58 | t.plan(1) |
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 | |
84 | tape('two communicating actors with callback', async t => { |
85 | t.plan(1) |
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 | |
114 | tape('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 | |
138 | tape('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 | |
162 | tape.skip('store globals', async t => { |
163 | t.plan(1) |
164 | tester = t |
165 | const tree = new RadixTree({ |
166 | db |
167 | }) |
168 | |
169 | const wasm = fs.readFileSync('./wasm/globals.wasm') |
170 | |
171 | const hypervisor = new Hypervisor(tree) |
172 | hypervisor.registerContainer(TestWasmContainer) |
173 | |
174 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
175 | |
176 | await new Promise((resolve, reject) => { |
177 | const message = new Message({ |
178 | funcRef: module.getFuncRef('store') |
179 | }).on('done', actor => { |
180 | resolve() |
181 | // const a = actor.container.getMemory(0, 8) |
182 | // const b = actor.container.getMemory(8, 8) |
183 | // t.deepEquals(a, b, 'should copy memory correctly') |
184 | }) |
185 | hypervisor.send(message) |
186 | }) |
187 | |
188 | await new Promise((resolve, reject) => { |
189 | const message = new Message({ |
190 | funcRef: module.getFuncRef('load') |
191 | }).on('done', actor => { |
192 | resolve() |
193 | // const a = actor.container.getMemory(0, 8) |
194 | // const b = actor.container.getMemory(8, 8) |
195 | // t.deepEquals(a, b, 'should copy memory correctly') |
196 | }) |
197 | hypervisor.send(message) |
198 | }) |
199 | }) |
200 |
Built with git-ssb-web