git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: 885e05bd77e55f9846f83ccd30e285c5566e6f38

Files: 885e05bd77e55f9846f83ccd30e285c5566e6f38 / tests / wast2wasm.js

1110 bytesRaw
1const wabt = require('wabt')
2const fs = require('fs')
3const annotations = require('primea-annotations')
4
5function filesWast2wasm () {
6 const srcFiles = fs.readdirSync(`${__dirname}/wast`)
7 const wastFiles = srcFiles.filter(name => name.split('.').pop() === 'wast')
8 for (let file of wastFiles) {
9 const wat = fs.readFileSync(`${__dirname}/wast/${file}`).toString()
10 file = file.split('.')[0]
11 let json
12 try {
13 json = fs.readFileSync(`${__dirname}/wast/${file}.json`)
14 json = JSON.parse(json)
15 } catch (e) {
16 console.log(`no json for ${file}`)
17 }
18
19 try {
20 const mod = wabt.parseWat('module.wast', wat)
21 const r = mod.toBinary({log: true})
22 let binary = Buffer.from(r.buffer)
23 if (!WebAssembly.validate(binary)) {
24 throw new Error('invalid wasm binary')
25 }
26 if (json) {
27 console.log(json)
28 binary = annotations.encodeAndInject(json, binary)
29 }
30 fs.writeFileSync(`${__dirname}/wasm/${file}.wasm`, binary)
31 } catch (e) {
32 console.log(`failed at ${file}`)
33 console.log(e)
34 }
35 }
36}
37
38filesWast2wasm()
39

Built with git-ssb-web