Files: e6bb7dfe86f19f0188e093e016c11a70df79bf72 / tests / wasmContainer.js
5519 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: callerMod} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) |
102 | const {module: receiverMod} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) |
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 | const hypervisor = new Hypervisor(tree) |
147 | hypervisor.registerContainer(TestWasmContainer) |
148 | |
149 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
150 | |
151 | const message = new Message({ |
152 | funcRef: module.getFuncRef('test') |
153 | }).on('done', actor => { |
154 | const a = actor.container.getMemory(0, 8) |
155 | const b = actor.container.getMemory(8, 8) |
156 | t.deepEquals(a, b, 'should copy memory correctly') |
157 | }) |
158 | hypervisor.send(message) |
159 | }) |
160 | |
161 | tape.skip('store globals', async t => { |
162 | t.plan(1) |
163 | tester = t |
164 | const tree = new RadixTree({ |
165 | db |
166 | }) |
167 | |
168 | const wasm = fs.readFileSync('./wasm/globals.wasm') |
169 | |
170 | const hypervisor = new Hypervisor(tree) |
171 | hypervisor.registerContainer(TestWasmContainer) |
172 | |
173 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
174 | |
175 | await new Promise((resolve, reject) => { |
176 | const message = new Message({ |
177 | funcRef: module.getFuncRef('store') |
178 | }).on('done', actor => { |
179 | resolve() |
180 | // const a = actor.container.getMemory(0, 8) |
181 | // const b = actor.container.getMemory(8, 8) |
182 | // t.deepEquals(a, b, 'should copy memory correctly') |
183 | }) |
184 | hypervisor.send(message) |
185 | }) |
186 | |
187 | await new Promise((resolve, reject) => { |
188 | const message = new Message({ |
189 | funcRef: module.getFuncRef('load') |
190 | }).on('done', actor => { |
191 | resolve() |
192 | // const a = actor.container.getMemory(0, 8) |
193 | // const b = actor.container.getMemory(8, 8) |
194 | // t.deepEquals(a, b, 'should copy memory correctly') |
195 | }) |
196 | hypervisor.send(message) |
197 | }) |
198 | }) |
199 |
Built with git-ssb-web