-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
50 lines (44 loc) · 1.41 KB
/
main.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node
import * as fs from 'fs'
import * as path from 'path'
import {promisify} from 'util'
import {linkModules, NamedModules} from './link-modules'
import {parseModule} from './parse/module'
const WASM_EXTENSION = '.wasm',
ASM_EXTENSION = '.s',
HEADER_EXTENSION = '.h'
const readFile = promisify(fs.readFile),
writeFile = promisify(fs.writeFile)
interface ModuleFiles {
[module: string]: string
}
(async () => {
const modules = await Promise.all(process.argv.slice(2).map(async file => {
if (!file.endsWith(WASM_EXTENSION)) {
throw new Error(`File ${file} is not a .wasm file`)
}
const wasm = await readFile(file)
const dataView = new DataView(wasm.buffer, wasm.byteOffset, wasm.byteLength)
return {file, module: parseModule(dataView).value}
}))
const namedModules: NamedModules = {}
const files: ModuleFiles = {}
for (const {file, module} of modules) {
const baseFile = file.slice(0, -WASM_EXTENSION.length)
const name = path.basename(baseFile)
namedModules[name] = module
files[name] = baseFile
}
const compiledModules = linkModules(namedModules)
await Promise.all(Object.keys(compiledModules).map(name => {
const {assembly, header} = compiledModules[name]
const baseFile = files[name]
return Promise.all([
writeFile(baseFile + ASM_EXTENSION, assembly),
writeFile(baseFile + HEADER_EXTENSION, header)
])
}))
})().catch(e => {
console.error(e)
process.exit(1)
})