-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
35 lines (32 loc) · 1 KB
/
test.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { readFile } from 'fs/promises'
import { createInterface } from 'readline'
const read = createInterface(process.stdin, process.stdout)
const buffer = await readFile('test.wasm')
const valid_buffer = WebAssembly.validate(buffer)
console.log(`the compiled assembly is ${valid_buffer ? '' : 'not '}valid!`)
if (!valid_buffer) {
read.close()
process.exit(0)
}
const { instance } = await WebAssembly.instantiate(buffer)
for (const [name, func] of Object.entries(instance.exports)) {
let params
if (func.length)
params =
await Promise.all(
Array(func.length)
.fill()
.map((_, i)=>
question(`insert the param n ${i} for ${name}: `)
.then(JSON.parse)
)
)
console.log(`${name} returns ${func.apply(undefined, params && params)}`)
}
read.close()
async function question(prompt) {
let res
let promise = new Promise(_res => res = _res)
read.question(prompt, res)
return promise
}