Files: 6a5f857a376a1e31ae18a4fd65720456f048c611 / tests / index.js
1909 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const WasmContainer = require('../index.js') |
4 | |
5 | const dir = `${__dirname}/wasm/` |
6 | |
7 | tape('interface tests', async t => { |
8 | const code = fs.readFileSync(`${dir}testCode.wasm`) |
9 | class TestInterface { |
10 | constructor (opts) { |
11 | opts.response.test = 'response!' |
12 | } |
13 | |
14 | static get name () { |
15 | return 'test' |
16 | } |
17 | } |
18 | |
19 | t.equals(WasmContainer.name, 'wasm', 'container should have a name') |
20 | |
21 | const container = new WasmContainer(code) |
22 | t.ok(container instanceof WasmContainer, 'should be instance of a container class') |
23 | |
24 | const promise = container.run() |
25 | await promise |
26 | t.ok(promise instanceof Promise, 'run should return instance of promise') |
27 | |
28 | const results = await container.run(null, null, [TestInterface]) |
29 | t.deepEquals(results, { |
30 | 'test': { |
31 | 'test': 'response!' |
32 | } |
33 | }) |
34 | |
35 | t.end() |
36 | }) |
37 | |
38 | tape('wasm interface test', async t => { |
39 | t.plan(1) |
40 | const code = fs.readFileSync(`${dir}testMem.wasm`) |
41 | class TestInterfaceMem { |
42 | constructor (opts) { |
43 | this.opts = opts |
44 | } |
45 | |
46 | static get name () { |
47 | return 'test' |
48 | } |
49 | |
50 | memory () { |
51 | const memory = this.opts.vm.memory() |
52 | t.ok(memory instanceof ArrayBuffer, 'should have access to memory') |
53 | } |
54 | } |
55 | const container = new WasmContainer(code) |
56 | await container.run(null, null, [TestInterfaceMem]) |
57 | }) |
58 | |
59 | tape('async wasm interface test', async t => { |
60 | t.plan(1) |
61 | const code = fs.readFileSync(`${dir}asyncTest.wasm`) |
62 | class TestInterfaceMem { |
63 | constructor (opts) { |
64 | this.opts = opts |
65 | } |
66 | |
67 | static get name () { |
68 | return 'test' |
69 | } |
70 | |
71 | async (cbOffset) { |
72 | const opPromise = new Promise((resolve, reject) => { |
73 | resolve() |
74 | }) |
75 | this.opts.vm.pushOpsQueue(opPromise, cbOffset) |
76 | } |
77 | |
78 | done () { |
79 | t.ok(true) |
80 | } |
81 | } |
82 | const container = new WasmContainer(code) |
83 | await container.run(null, null, [TestInterfaceMem]) |
84 | }) |
85 |
Built with git-ssb-web