...
1;; result-offset/wasm defines Wasm functions that illustrate multiple results
2;; using a technique compatible with any WebAssembly 1.0 runtime.
3;;
4;; To return a value in WASM written to a result parameter, you have to define
5;; memory and pass a location to write the result. At the end of your function,
6;; you load that location.
7(module $result-offset/wasm
8 ;; To use result parameters, we need scratch memory. Allocate the least
9 ;; possible: 1 page (64KB).
10 (memory 1 1)
11
12 ;; get_age returns a result, while a second result is written to memory.
13 (func $get_age (param $result_offset.age i32) (result (;errno;) i32)
14 local.get 0 ;; stack = [$result_offset.age]
15 i64.const 37 ;; stack = [$result_offset.age, 37]
16 i64.store ;; stack = []
17 i32.const 0 ;; stack = [0]
18 )
19
20 ;; Now, define a function that shows the Wasm mechanics returning something
21 ;; written to a result parameter.
22 ;; The caller provides a memory offset to the callee, so that it knows where
23 ;; to write the second result.
24 (func (export "call_get_age") (result i64)
25 i32.const 8 ;; stack = [8] arbitrary $result_offset.age param to get_age
26 call $get_age ;; stack = [errno] result of get_age
27 drop ;; stack = []
28
29 i32.const 8 ;; stack = [8] same value as the $result_offset.age parameter
30 i64.load ;; stack = [age]
31 )
32)
View as plain text