1;;! multi_memory = true
2
3;; basic function lifting
4(component
5  (core module $m
6    (func (export ""))
7  )
8  (core instance $i (instantiate $m))
9
10  (func (export "thunk")
11    (canon lift (core func $i ""))
12  )
13)
14
15;; use an aliased type
16(component $c
17  (core module $m
18    (func (export ""))
19  )
20  (core instance $i (instantiate $m))
21
22  (type $to_alias (func))
23  (alias outer $c $to_alias (type $alias))
24
25  (func (export "thunk") (type $alias)
26    (canon lift (core func $i ""))
27  )
28)
29
30;; test out some various canonical abi
31(component $c
32  (core module $m
33    (func (export "") (param i32 i32))
34    (memory (export "memory") 1)
35    (func (export "realloc") (param i32 i32 i32 i32) (result i32)
36      unreachable)
37  )
38  (core instance $i (instantiate $m))
39
40  (func (export "thunk") (param "a" string)
41    (canon lift
42      (core func $i "")
43      (memory $i "memory")
44      (realloc (func $i "realloc"))
45    )
46  )
47
48  (func (export "thunk8") (param "a" string)
49    (canon lift
50      (core func $i "")
51      string-encoding=utf8
52      (memory $i "memory")
53      (realloc (func $i "realloc"))
54    )
55  )
56
57  (func (export "thunk16") (param "a" string)
58    (canon lift
59      (core func $i "")
60      string-encoding=utf16
61      (memory $i "memory")
62      (realloc (func $i "realloc"))
63    )
64  )
65
66  (func (export "thunklatin16") (param "a" string)
67    (canon lift
68      (core func $i "")
69      string-encoding=latin1+utf16
70      (memory $i "memory")
71      (realloc (func $i "realloc"))
72    )
73  )
74)
75
76;; lower something then immediately lift it
77(component $c
78  (import "host-return-two" (func $f (result u32)))
79
80  (core func $f_lower
81    (canon lower (func $f))
82  )
83  (func $f2 (result s32)
84    (canon lift (core func $f_lower))
85  )
86  (export "f" (func $f2))
87)
88
89;; valid, but odd
90(component
91  (core module $m (func (export "")))
92  (core instance $m (instantiate $m))
93
94  (func $f1 (canon lift (core func $m "")))
95  (core func $f2 (canon lower (func $f1)))
96)
97(assert_trap
98  (component
99    (core module $m (func (export "")))
100    (core instance $m (instantiate $m))
101
102    (func $f1 (canon lift (core func $m "")))
103    (core func $f2 (canon lower (func $f1)))
104
105    (core module $m2
106      (import "" "" (func $f))
107      (func $start
108        call $f)
109      (start $start)
110    )
111    (core instance (instantiate $m2
112      (with "" (instance (export "" (func $f2))))
113    ))
114  )
115  "degenerate component adapter called")
116
117;; fiddling with 0-sized lists
118(component $c
119  (core module $m
120    (func (export "x") (param i32 i32))
121    (func (export "realloc") (param i32 i32 i32 i32) (result i32)
122      i32.const -1)
123    (memory (export "memory") 0)
124  )
125  (core instance $m (instantiate $m))
126  (type $t' (result))
127  (export $t "t" (type $t'))
128  (func $f (param "a" (list $t))
129    (canon lift
130      (core func $m "x")
131      (realloc (func $m "realloc"))
132      (memory $m "memory")
133    )
134  )
135  (export "empty-list" (func $f))
136)
137(assert_trap (invoke "empty-list" (list.const)) "realloc return: beyond end of memory")
138