-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
39 lines (34 loc) · 1.03 KB
/
index.js
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
import { init, WASI } from 'https://esm.sh/@wasmer/[email protected]'
import Llc from './llc.js'
import Lld from './lld.js'
await init()
export const compileAndRun = async (mainLl) => {
const llc = await Llc()
llc.FS.writeFile('main.ll', mainLl)
await llc.callMain(['-filetype=obj', 'main.ll'])
const mainO = llc.FS.readFile('main.o')
const lld = await Lld()
lld.FS.writeFile('main.o', mainO)
await lld.callMain([
'-flavor',
'wasm',
'-L/lib/wasm32-wasi',
'-lc',
'-lc++',
'-lc++abi',
'/lib/clang/16.0.4/lib/wasi/libclang_rt.builtins-wasm32.a',
'/lib/wasm32-wasi/crt1.o',
'main.o',
'-o',
'main.wasm',
])
const mainWasm = lld.FS.readFile('main.wasm')
const wasi = new WASI({})
const module = await WebAssembly.compile(mainWasm)
const instance = await WebAssembly.instantiate(module, {
...wasi.getImports(module)
})
wasi.start(instance)
const stdout = await wasi.getStdoutString()
return stdout
}