-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (66 loc) · 1.88 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { WASI } from '@tybys/wasm-util'
import { Volume, createFsFromVolume } from 'memfs-browser'
async function start (buffer, fs, args) {
const wasi = new WASI({
args,
env: {},
returnOnExit: true,
preopens: {
'/': '/'
},
// print: (text) => console.log(text),
// printErr: (text) => console.error(text),
fs
})
const { instance } = await WebAssembly.instantiate(buffer, {
wasi_snapshot_preview1: wasi.wasiImport
})
console.log('%c%s', 'color: green; font-weight: bold', '$ ' + args.join(' '))
const exitCode = wasi.start(instance)
console.log('%c%s', 'color: blue', 'exit: ' + exitCode)
return exitCode
}
async function main () {
const wasmObjdumpBuffer = new Uint8Array(await (await fetch('./bin/wasm-objdump.wasm')).arrayBuffer())
const wat2wasmBuffer = new Uint8Array(await (await fetch('./bin/wat2wasm.wasm')).arrayBuffer())
const fs = createFsFromVolume(Volume.fromJSON({
'/': null
}))
const wat =
`(module
(func $fib (export "fib")
(param $n i32) (result i32)
(local $tmp i32)
local.get $n
i32.const 2
i32.lt_s
if
local.get $n
return
end
local.get $n
i32.const 1
i32.sub
call $fib
local.set $tmp
local.get $n
i32.const 2
i32.sub
call $fib
local.get $tmp
i32.add
return
)
)`
fs.writeFileSync('/fib.wat', wat, 'utf8')
console.log(wat)
await start(wat2wasmBuffer, fs, ['wat2wasm', '--version'])
await start(wat2wasmBuffer, fs, ['wat2wasm', '-o', '/fib.wasm', '/fib.wat'])
await start(wasmObjdumpBuffer, fs, ['wasm-objdump', '--help'])
await start(wasmObjdumpBuffer, fs, ['wasm-objdump', '-hx', '/fib.wasm'])
const fibwasmBuffer = fs.readFileSync('/fib.wasm')
const fibwasmInstance = await WebAssembly.instantiate(fibwasmBuffer)
const n = 10
console.log('fib(%d) => %d', n, fibwasmInstance.instance.exports.fib(n))
}
main()