forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
age-calculator.go
110 lines (98 loc) · 3.55 KB
/
age-calculator.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package age_calculator
import (
"context"
_ "embed"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/tetratelabs/wazero"
)
// main shows how to define, import and call a Go-defined function from a
// WebAssembly-defined function.
//
// See README.md for a full description.
func main() {
// Choose the context to use for function calls.
ctx := context.Background()
// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
// Instantiate a Go-defined module named "env" that exports functions to
// get the current year and log to the console.
//
// Note: As noted on ExportFunction documentation, function signatures are
// constrained to a subset of numeric types.
// Note: "env" is a module name conventionally used for arbitrary
// host-defined functions, but any name would do.
env, err := r.NewModuleBuilder("env").
ExportFunction("log_i32", func(v uint32) {
fmt.Println("log_i32 >>", v)
}).
ExportFunction("current_year", func() uint32 {
if envYear, err := strconv.ParseUint(os.Getenv("CURRENT_YEAR"), 10, 64); err == nil {
return uint32(envYear) // Allow env-override to prevent annual test maintenance!
}
return uint32(time.Now().Year())
}).
Instantiate(ctx)
if err != nil {
log.Fatal(err)
}
defer env.Close(ctx)
// Instantiate a WebAssembly module named "age-calculator" that imports
// functions defined in "env".
//
// Note: The import syntax in both Text and Binary format is the same
// regardless of if the function was defined in Go or WebAssembly.
ageCalculator, err := r.InstantiateModuleFromCode(ctx, []byte(`
;; Define the optional module name. '$' prefixing is a part of the text format.
(module $age-calculator
;; In WebAssembly, you don't import an entire module, rather each function.
;; This imports the functions and gives them names which are easier to read
;; than the alternative (zero-based index).
;;
;; Note: Importing unused functions is not an error in WebAssembly.
(import "env" "log_i32" (func $log (param i32)))
(import "env" "current_year" (func $year (result i32)))
;; get_age looks up the current year and subtracts the input from it.
;; Note: The stack begins empty and anything left must match the result type.
(func $get_age (param $year_born i32) (result i32)
;; stack: []
call $year ;; stack: [$year.result]
local.get 0 ;; stack: [$year.result, $year_born]
i32.sub ;; stack: [$year.result-$year_born]
)
;; export allows api.Module to return this via ExportedFunction("get_age")
(export "get_age" (func $get_age))
;; log_age
(func $log_age (param $year_born i32)
;; stack: []
local.get 0 ;; stack: [$year_born]
call $get_age ;; stack: [$get_age.result]
call $log ;; stack: []
)
(export "log_age" (func $log_age))
)`))
// ^^ Note: wazero's text compiler is incomplete #59. We are using it anyway to keep this example dependency free.
if err != nil {
log.Fatal(err)
}
defer ageCalculator.Close(ctx)
// Read the birthYear from the arguments to main
birthYear, err := strconv.ParseUint(os.Args[1], 10, 64)
if err != nil {
log.Fatalf("invalid arg %v: %v", os.Args[1], err)
}
// First, try calling the "get_age" function and printing to the console externally.
results, err := ageCalculator.ExportedFunction("get_age").Call(ctx, birthYear)
if err != nil {
log.Fatal(err)
}
fmt.Println("println >>", results[0])
// First, try calling the "log_age" function and printing to the console externally.
_, err = ageCalculator.ExportedFunction("log_age").Call(ctx, birthYear)
if err != nil {
log.Fatal(err)
}
}