forked from perlin-network/life
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
63 lines (53 loc) · 1.42 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"github.com/perlin-network/life/exec"
"github.com/perlin-network/life/gowasm"
"io/ioutil"
"time"
)
func main() {
entryFunctionFlag := flag.String("entry", "app_main", "entry function id")
jitFlag := flag.Bool("jit", false, "enable jit")
flag.Parse()
// Read WebAssembly *.wasm file.
input, err := ioutil.ReadFile(flag.Arg(0))
if err != nil {
panic(err)
}
// Instantiate a new WebAssembly VM with a few resolved imports.
vm, err := exec.NewVirtualMachine(input, exec.VMConfig{
EnableJIT: *jitFlag,
DefaultMemoryPages: 128,
DefaultTableSize: 65536,
}, gowasm.NewResolver(), nil)
if err != nil {
panic(err)
}
// Get the function ID of the entry function to be executed.
entryID, ok := vm.GetFunctionExport(*entryFunctionFlag)
if !ok {
fmt.Printf("Entry function %s not found; starting from 0.\n", *entryFunctionFlag)
entryID = 0
}
start := time.Now()
// If any function prior to the entry function was declared to be
// called by the module, run it first.
if vm.Module.Base.Start != nil {
startID := int(vm.Module.Base.Start.Index)
_, err := vm.Run(startID)
if err != nil {
vm.PrintStackTrace()
panic(err)
}
}
// Run the WebAssembly module's entry function.
ret, err := vm.Run(entryID, 0, 0)
if err != nil {
vm.PrintStackTrace()
panic(err)
}
end := time.Now()
fmt.Printf("return value = %d, duration = %v\n", ret, end.Sub(start))
}