-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvariables.wat
55 lines (43 loc) · 1.19 KB
/
variables.wat
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
(module
;; global variables
(global $glob_a (mut i32) (i32.const 100)) ;; mutable
(global $glob_b i32 (i32.const 200)) ;; immutable
;; Immutable global variable can be exported.
(export "CONST_B" (global $glob_b))
(func (export "local32") (result i32)
;; Local variables initialized with 0 by default.
;;
;; let sum
(local $sum i32)
;; return sum
(return (get_local $sum))
)
(func (export "local64") (result i64)
;; let sum
(local $sum i64)
;; return sum
(return (get_local $sum))
)
;; Function arguments taken from stack.
(func (export "add10") (param $a i32) (result i32)
;; const _ = 10
(i32.const 10)
(get_local $a)
;; return 10 + a
(i32.add)
)
;; This example shows the mixed order of the call arguments.
(func (export "sub10") (param $a i32) (result i32)
;; const _ = 10
(i32.const 10)
;; return 10 - a
(i32.sub (get_local $a))
)
;; Usage of global and local variables together.
(func (export "add100") (param $a i32) (result i32)
;; glob_a = 300
(set_global $glob_a (i32.const 300))
;; return 300 + a
(i32.add (get_global $glob_a) (get_local $a))
)
)