-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest2.nim
80 lines (62 loc) · 1.94 KB
/
test2.nim
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
79
80
import std/[strformat, options]
import wasmtime
proc main() =
echo "Start"
let config = newConfig()
let engine = newEngine(config)
let linker = engine.newLinker()
defer: linker.delete()
let store = engine.newStore(nil, nil)
defer: store.delete()
let context = store.context()
let wasiConfig = newWasiConfig()
wasiConfig.inheritStdin()
wasiConfig.inheritStderr()
wasiConfig.inheritStdout()
context.setWasi(wasiConfig).toResult(void).okOr(err):
echo "Failed to setup wasi: ", err.msg
return
echo "Read wasm file"
let wasmBytes = readFile("tests/wasm/testm.wasm")
let module = engine.newModule(wasmBytes).okOr(err):
echo "Failed to create wasm module: ", err.msg
return
let moduleImports = module.imports
let moduleExports = module.exports
echo "Imports:"
for i, e in moduleImports:
echo &" {i}: {e}"
echo "Exports:"
for i, e in moduleExports:
echo &" {i}: {e}"
linker.defineWasi().okOr(err):
echo "Failed to create linker: ", err.msg
return
echo "Instantiate "
var trap: ptr WasmTrapT = nil
let instance = linker.instantiate(context, module, trap.addr).okOr(err):
echo "Failed to instantiate wasm module: ", err.msg
return
trap.okOr(err):
echo "[trap] Failed to instantiate wasm module: ", err.msg
return
echo "instance exports"
for i in 0..<moduleExports.len:
let mainExport = instance.getExport(context, i)
if mainExport.isNone:
echo &" {i}: none"
continue
echo &" {i}: {mainExport.get.name}"
let mainExport = instance.getExport(context, "hello")
assert mainExport.isSome
assert mainExport.get.kind.WasmExternKind == ExternFunc
echo mainExport
echo "Call hello"
mainExport.get.of_field.func_field.addr.call(context, [], [], trap.addr).toResult(void).okOr(err):
echo &"Failed to call hello: {err.msg}"
return
trap.okOr(err):
echo "[trap] Failed to call hello: ", err.msg
return
echo "Called hello"
main()