git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: a9b5a00dafae8aebd3540837fb35491346ec2528

Files: a9b5a00dafae8aebd3540837fb35491346ec2528 / tests / index.js

4265 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const Hypervisor = require('primea-hypervisor')
4const Message = require('primea-message')
5const WasmContainer = require('../index.js')
6const testInterface = require('./testInterface.js')
7const IPFS = require('ipfs')
8const ReferanceMap = require('../referanceMap.js')
9
10const node = new IPFS({
11 start: false
12})
13
14class ContainerTestInterface {
15 constructor (wasmContainer) {
16 this.wasmContainer = wasmContainer
17 }
18
19 readMem (offset) {
20 return this.wasmContainer.getMemory(offset, 1)
21 }
22
23 async callback (cb) {
24 const promise = new Promise((resolve, reject) => {
25 resolve()
26 })
27 await this.wasmContainer.pushOpsQueue(promise)
28 this.wasmContainer.execute(cb)
29 }
30}
31
32node.on('ready', () => {
33 tape('referance mapping', t => {
34 t.plan(6)
35 const referanceMap = new ReferanceMap()
36 const obj1 = {}
37 const obj2 = {}
38 const ref1 = referanceMap.add(obj1)
39 const ref2 = referanceMap.add(obj2)
40 t.equals(ref1, 0, 'should produce correct refs')
41 t.equals(ref2, 1, 'should produce correct refs')
42
43 const foundObj1 = referanceMap.get(ref1)
44 const foundObj2 = referanceMap.get(ref2)
45
46 t.equals(foundObj1, obj1, 'should get the correct object')
47 t.equals(foundObj2, obj2, 'should get the correct object')
48
49 referanceMap.delete(ref1)
50 try {
51 referanceMap.get(ref1)
52 } catch (e) {
53 t.true(true, 'should delete refances')
54 }
55
56 referanceMap.clear()
57 try {
58 referanceMap.get(ref2)
59 } catch (e) {
60 t.true(true, 'should clear refances')
61 }
62 })
63
64 tape('wasm container - main', async t => {
65 t.plan(1)
66 const hypervisor = new Hypervisor(node.dag)
67 const main = fs.readFileSync(`${__dirname}/wasm/main.wasm`)
68 hypervisor.registerContainer('wasm', WasmContainer, {
69 test: testInterface(t)
70 })
71 const instance = await hypervisor.createInstance('wasm', new Message({
72 data: main
73 }))
74 instance.run(instance.createMessage())
75 })
76
77 tape('wasm container - mem', async t => {
78 t.plan(1)
79 const hypervisor = new Hypervisor(node.dag)
80 const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`)
81 hypervisor.registerContainer('wasm', WasmContainer, {
82 env: ContainerTestInterface,
83 test: testInterface(t)
84 })
85 await hypervisor.createInstance('wasm', new Message({
86 data: readMem
87 }))
88 })
89
90 tape('wasm container - callbacks', async t => {
91 t.plan(1)
92 const hypervisor = new Hypervisor(node.dag)
93 const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
94 hypervisor.registerContainer('wasm', WasmContainer, {
95 env: ContainerTestInterface,
96 test: testInterface(t)
97 })
98 hypervisor.createInstance('wasm', new Message({
99 data: callBackWasm
100 }))
101 })
102
103 tape('wasm container - invalid', async t => {
104 t.plan(1)
105 const hypervisor = new Hypervisor(node.dag)
106 hypervisor.registerContainer('wasm', WasmContainer, {
107 env: ContainerTestInterface,
108 test: testInterface(t)
109 })
110
111 const message = new Message({
112 data: Buffer.from([0x00])
113 })
114
115 const rp = message.responsePort = {destPort: {messages: []}}
116
117 await hypervisor.createInstance('wasm', message)
118 t.equals(rp.destPort.messages[0].data.exception, true)
119 })
120
121 tape('initailize', async t => {
122 t.plan(2)
123
124 const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`)
125
126 class ContainerTestInterface {
127 constructor (wasmContainer) {
128 this.wasmContainer = wasmContainer
129 }
130
131 readMem (offset) {
132 return this.wasmContainer.getMemory(offset, 1)
133 }
134
135 async callback (cb) {
136 const promise = new Promise((resolve, reject) => {
137 resolve()
138 })
139 await this.wasmContainer.pushOpsQueue(promise)
140 this.wasmContainer.execute(cb)
141 }
142
143 static initialize (code) {
144 t.equals(code, callBackWasm)
145 return code
146 }
147 }
148
149 const hypervisor = new Hypervisor(node.dag)
150 hypervisor.registerContainer('wasm', WasmContainer, {
151 env: ContainerTestInterface,
152 test: testInterface(t)
153 })
154
155 const message = new Message({
156 data: callBackWasm
157 })
158
159 hypervisor.createInstance('wasm', message)
160 })
161})
162

Built with git-ssb-web