git ssb

0+

wanderer🌟 / wasm-persist



Tree: 8fb216c62276f508a5fd349383be046d162baaa7

Files: 8fb216c62276f508a5fd349383be046d162baaa7 / tests / index.js

7657 bytesRaw
1const fs = require('fs')
2const tape = require('tape')
3const persit = require('../')
4
5tape('empty', async t => {
6 let wasm = fs.readFileSync(`${__dirname}/wasm/empty.wasm`)
7 wasm = persit.prepare(wasm)
8 const wasmInstance = await WebAssembly.instantiate(wasm)
9 const json = persit.hibernate(wasmInstance.instance)
10 persit.resume(wasmInstance.instance, json)
11 t.deepEquals(json, {globals: [], table: [], symbol: '_'})
12 t.equals(Object.keys(wasmInstance.instance.exports).length, 0)
13
14 t.end()
15})
16
17tape('custom', async t => {
18 let wasm = fs.readFileSync(`${__dirname}/wasm/customSections.wasm`)
19 wasm = persit.prepare(wasm)
20 const wasmInstance = await WebAssembly.instantiate(wasm)
21 const json = persit.hibernate(wasmInstance.instance)
22 persit.resume(wasmInstance.instance, json)
23 t.deepEquals(json, {globals: [], table: [], symbol: '_'})
24 t.equals(Object.keys(wasmInstance.instance.exports).length, 0)
25
26 t.end()
27})
28
29tape('single type section', async t => {
30 let wasm = fs.readFileSync(`${__dirname}/wasm/singleType.wasm`)
31 wasm = persit.prepare(wasm)
32 const wasmInstance = await WebAssembly.instantiate(wasm)
33 const json = persit.hibernate(wasmInstance.instance)
34 t.deepEquals(json, {globals: [], table: [], symbol: '_'})
35 t.equals(Object.keys(wasmInstance.instance.exports).length, 0)
36
37 t.end()
38})
39
40tape('single import section', async t => {
41 let wasm = fs.readFileSync(`${__dirname}/wasm/singleImport.wasm`)
42 wasm = persit.prepare(wasm)
43 const wasmInstance = await WebAssembly.instantiate(wasm, {foo: {bar () {}}})
44 const json = persit.hibernate(wasmInstance.instance)
45 t.deepEquals(json, {globals: [], table: [], symbol: '_'})
46 t.equals(Object.keys(wasmInstance.instance.exports).length, 0)
47
48 t.end()
49})
50
51tape('single function', async t => {
52 let wasm = fs.readFileSync(`${__dirname}/wasm/singleFunction.wasm`)
53 wasm = persit.prepare(wasm)
54 const wasmInstance = await WebAssembly.instantiate(wasm)
55 const json = persit.hibernate(wasmInstance.instance)
56 t.deepEquals(json, {globals: [], table: [], symbol: '_'})
57 t.equals(Object.keys(wasmInstance.instance.exports).length, 2)
58
59 t.end()
60})
61
62tape('single table', async t => {
63 let wasm = fs.readFileSync(`${__dirname}/wasm/singleTable.wasm`)
64 wasm = persit.prepare(wasm)
65 const wasmInstance = await WebAssembly.instantiate(wasm)
66 const json = persit.hibernate(wasmInstance.instance)
67 t.deepEquals(json, {globals: [], table: ['0'], symbol: '_'})
68 t.equals(Object.keys(wasmInstance.instance.exports).length, 3)
69
70 t.end()
71})
72
73tape('single table with foreign fuction', async t => {
74 let wasm = fs.readFileSync(`${__dirname}/wasm/singleTable.wasm`)
75 wasm = persit.prepare(wasm)
76 const wasmInstance = await WebAssembly.instantiate(wasm)
77 const wasmInstance2 = await WebAssembly.instantiate(wasm)
78 wasmInstance.instance.exports._table.set(0, wasmInstance2.instance.exports.e)
79 const json = persit.hibernate(wasmInstance.instance)
80 t.deepEquals(json, {globals: [], table: [], symbol: '_'})
81 t.equals(Object.keys(wasmInstance.instance.exports).length, 3)
82
83 t.end()
84})
85
86tape('single table resume', async t => {
87 let wasm = fs.readFileSync(`${__dirname}/wasm/singleTable.wasm`)
88 wasm = persit.prepare(wasm)
89 const wasmInstance = await WebAssembly.instantiate(wasm)
90 const json = persit.hibernate(wasmInstance.instance)
91
92 const wasmInstance2 = await WebAssembly.instantiate(wasm)
93 persit.resume(wasmInstance2.instance, json)
94 const json2 = persit.hibernate(wasmInstance2.instance)
95 t.deepEquals(json, json2)
96 t.equals(Object.keys(wasmInstance.instance.exports).length, 3)
97
98 t.end()
99})
100
101tape('single memory', async t => {
102 let wasm = fs.readFileSync(`${__dirname}/wasm/singleMemory.wasm`)
103 wasm = persit.prepare(wasm)
104 const wasmInstance = await WebAssembly.instantiate(wasm)
105 const json = persit.hibernate(wasmInstance.instance)
106 t.equals(json.memory.length, 16384)
107 t.equals(Object.keys(wasmInstance.instance.exports).length, 3)
108
109 t.end()
110})
111
112tape('skipping memory', async t => {
113 let wasm = fs.readFileSync(`${__dirname}/wasm/singleMemory.wasm`)
114 wasm = persit.prepare(wasm, '_', {memory: false})
115 const wasmInstance = await WebAssembly.instantiate(wasm)
116 const json = persit.hibernate(wasmInstance.instance)
117 t.equals(json.memory, undefined)
118 t.equals(Object.keys(wasmInstance.instance.exports).length, 2)
119
120 t.end()
121})
122
123tape('resuming memory', async t => {
124 let wasm = fs.readFileSync(`${__dirname}/wasm/singleMemory.wasm`)
125 wasm = persit.prepare(wasm)
126 const wasmInstance = await WebAssembly.instantiate(wasm)
127 const json = persit.hibernate(wasmInstance.instance)
128
129 const wasmInstance2 = await WebAssembly.instantiate(wasm)
130 persit.resume(wasmInstance2.instance, json)
131 const json2 = persit.hibernate(wasmInstance2.instance)
132 t.deepEquals(json, json2)
133 t.equals(Object.keys(wasmInstance.instance.exports).length, 3)
134
135 t.end()
136})
137
138tape('single i32 global', async t => {
139 let wasm = fs.readFileSync(`${__dirname}/wasm/singleI32Global.wasm`)
140 wasm = persit.prepare(wasm)
141 const wasmInstance = await WebAssembly.instantiate(wasm)
142 const json = persit.hibernate(wasmInstance.instance)
143 t.deepEquals(json, {globals: [-2], table: [], symbol: '_'})
144 t.equals(Object.keys(wasmInstance.instance.exports).length, 4)
145
146 t.end()
147})
148
149tape('single i32 global', async t => {
150 let wasm = fs.readFileSync(`${__dirname}/wasm/singleI32Global.wasm`)
151 wasm = persit.prepare(wasm)
152 const wasmInstance = await WebAssembly.instantiate(wasm)
153 const json = persit.hibernate(wasmInstance.instance)
154 t.deepEquals(json, {globals: [-2], table: [], symbol: '_'})
155 t.equals(Object.keys(wasmInstance.instance.exports).length, 4)
156
157 json.globals[0] = 8
158 const wasmInstance2 = await WebAssembly.instantiate(wasm)
159 persit.resume(wasmInstance2.instance, json)
160 const json2 = persit.hibernate(wasmInstance2.instance)
161 t.deepEquals(json2, {globals: [8], table: [], symbol: '_'})
162
163 t.end()
164})
165
166tape('single i64 global', async t => {
167 let wasm = fs.readFileSync(`${__dirname}/wasm/singleI64Global.wasm`)
168 wasm = persit.prepare(wasm)
169 const wasmInstance = await WebAssembly.instantiate(wasm)
170 const json = persit.hibernate(wasmInstance.instance)
171 t.deepEquals(json, {globals: [[-1, -2]], table: [], symbol: '_'})
172 t.equals(Object.keys(wasmInstance.instance.exports).length, 5)
173
174 t.end()
175})
176
177tape('resuming i64 global', async t => {
178 let wasm = fs.readFileSync(`${__dirname}/wasm/singleI64Global.wasm`)
179 wasm = persit.prepare(wasm)
180 const wasmInstance = await WebAssembly.instantiate(wasm)
181 const json = {globals: [[99, 97]], table: [], symbol: '_'}
182 persit.resume(wasmInstance.instance, json)
183 const json2 = persit.hibernate(wasmInstance.instance)
184 t.deepEquals(json, json2)
185 t.equals(Object.keys(wasmInstance.instance.exports).length, 5)
186
187 t.end()
188})
189
190tape('skipping some globals', async t => {
191 let wasm = fs.readFileSync(`${__dirname}/wasm/doubleGlobals.wasm`)
192 wasm = persit.prepare(wasm, {
193 globals: [true]
194 }, '_@')
195 const wasmInstance = await WebAssembly.instantiate(wasm)
196 const json = persit.hibernate(wasmInstance.instance, '_@')
197 t.deepEquals(json, {globals: [-2], table: [], symbol: '_@'})
198 t.equals(Object.keys(wasmInstance.instance.exports).length, 4)
199
200 t.end()
201})
202
203tape('skipping some globals', async t => {
204 let wasm = fs.readFileSync(`${__dirname}/wasm/doubleGlobals.wasm`)
205 wasm = persit.prepare(wasm, {
206 globals: [false, true]
207 }, '_@')
208 const wasmInstance = await WebAssembly.instantiate(wasm)
209 const json = persit.hibernate(wasmInstance.instance, '_@')
210 t.deepEquals(json, {globals: [[-1, -2]], table: [], symbol: '_@'})
211 t.equals(Object.keys(wasmInstance.instance.exports).length, 5)
212
213 t.end()
214})
215

Built with git-ssb-web