...
1(module $age-calculator
2
3 ;; In WebAssembly, you don't import an entire module, rather each function.
4 ;; This imports the functions and gives them names which are easier to read
5 ;; than the alternative (zero-based index).
6 ;;
7 ;; Note: Importing unused functions is not an error in WebAssembly.
8 (import "env" "log_i32" (func $log (param i32)))
9 (import "env" "current_year" (func $year (result i32)))
10
11 ;; get_age looks up the current year and subtracts the input from it.
12 (func $get_age (export "get_age") (param $year_born i32) (result i32)
13 call $year ;; stack: [$year.result]
14 local.get $year_born ;; stack: [$year.result, $year_born]
15 i32.sub ;; stack: [$year.result-$year_born]
16 )
17
18 ;; log_age calls $log with the result of $get_age
19 (func (export "log_age") (param $year_born i32)
20 ;; stack: []
21 local.get $year_born ;; stack: [$year_born]
22 call $get_age ;; stack: [$get_age.result]
23 call $log ;; stack: []
24 )
25)
View as plain text