171cb94beSAlex Crichton #![allow(dead_code, reason = "lots of macro-generated code")]
267adf149SAlex Crichton 
32329ecc3SAlex Crichton macro_rules! gentest {
424785123SAlex Crichton     ($id:ident $name:tt $path:tt) => {
52329ecc3SAlex Crichton         mod $id {
6255fd6beSAlex Crichton             mod sugar {
7255fd6beSAlex Crichton                 wasmtime::component::bindgen!(in $path);
8255fd6beSAlex Crichton             }
92329ecc3SAlex Crichton             mod async_ {
102329ecc3SAlex Crichton                 wasmtime::component::bindgen!({
112329ecc3SAlex Crichton                     path: $path,
121155d6dfSAlex Crichton                     imports: { default: async },
131155d6dfSAlex Crichton                     exports: { default: async },
142329ecc3SAlex Crichton                 });
152329ecc3SAlex Crichton             }
16beca86b0SAlex Crichton             mod concurrent {
17beca86b0SAlex Crichton                 wasmtime::component::bindgen!({
18beca86b0SAlex Crichton                     path: $path,
191155d6dfSAlex Crichton                     imports: { default: async | store },
201155d6dfSAlex Crichton                     exports: { default: async | store },
21beca86b0SAlex Crichton                 });
22beca86b0SAlex Crichton             }
232329ecc3SAlex Crichton             mod tracing {
242329ecc3SAlex Crichton                 wasmtime::component::bindgen!({
252329ecc3SAlex Crichton                     path: $path,
261155d6dfSAlex Crichton                     imports: { default: tracing | verbose_tracing },
271155d6dfSAlex Crichton                     exports: { default: tracing | verbose_tracing },
28537214fdSJoel Dice                     ownership: Borrowing {
29537214fdSJoel Dice                         duplicate_if_necessary: true
30537214fdSJoel Dice                     }
312329ecc3SAlex Crichton                 });
322329ecc3SAlex Crichton             }
339f47be2eSAlex Crichton             mod imports_with_store {
349f47be2eSAlex Crichton                 wasmtime::component::bindgen!({
359f47be2eSAlex Crichton                     path: $path,
369f47be2eSAlex Crichton                     imports: { default: store },
379f47be2eSAlex Crichton                 });
389f47be2eSAlex Crichton             }
39*4a168844SAlex Crichton             mod with_anyhow {
40*4a168844SAlex Crichton                 wasmtime::component::bindgen!({
41*4a168844SAlex Crichton                     path: $path,
42*4a168844SAlex Crichton                     anyhow: true,
43*4a168844SAlex Crichton                     imports: { default: trappable },
44*4a168844SAlex Crichton                 });
45*4a168844SAlex Crichton             }
462329ecc3SAlex Crichton         }
472329ecc3SAlex Crichton     };
482329ecc3SAlex Crichton }
492329ecc3SAlex Crichton 
502329ecc3SAlex Crichton component_macro_test_helpers::foreach!(gentest);
516aca67c1SAlex Crichton 
526aca67c1SAlex Crichton mod with_key_and_resources {
5396e19700SNick Fitzgerald     use wasmtime::Result;
546aca67c1SAlex Crichton     use wasmtime::component::Resource;
556aca67c1SAlex Crichton 
566aca67c1SAlex Crichton     wasmtime::component::bindgen!({
576aca67c1SAlex Crichton         inline: "
58fd3a827eSAlex Crichton             package demo:pkg;
596aca67c1SAlex Crichton 
606aca67c1SAlex Crichton             interface bar {
61fd3a827eSAlex Crichton                 resource a;
62fd3a827eSAlex Crichton                 resource b;
636aca67c1SAlex Crichton             }
646aca67c1SAlex Crichton 
656aca67c1SAlex Crichton             world foo {
66fd3a827eSAlex Crichton                 resource a;
67fd3a827eSAlex Crichton                 resource b;
686aca67c1SAlex Crichton 
696aca67c1SAlex Crichton                 import foo: interface {
70fd3a827eSAlex Crichton                     resource a;
71fd3a827eSAlex Crichton                     resource b;
726aca67c1SAlex Crichton                 }
736aca67c1SAlex Crichton 
74fd3a827eSAlex Crichton                 import bar;
756aca67c1SAlex Crichton             }
766aca67c1SAlex Crichton         ",
776aca67c1SAlex Crichton         with: {
786aca67c1SAlex Crichton             "a": MyA,
796aca67c1SAlex Crichton             "b": MyA,
808c03849bSDan Gohman             "foo.a": MyA,
818c03849bSDan Gohman             "foo.b": MyA,
828c03849bSDan Gohman             "demo:pkg/bar.a": MyA,
838c03849bSDan Gohman             "demo:pkg/bar.b": MyA,
846aca67c1SAlex Crichton         },
856aca67c1SAlex Crichton     });
866aca67c1SAlex Crichton 
876aca67c1SAlex Crichton     pub struct MyA;
886aca67c1SAlex Crichton 
896aca67c1SAlex Crichton     struct MyComponent;
906aca67c1SAlex Crichton 
916aca67c1SAlex Crichton     impl FooImports for MyComponent {}
926aca67c1SAlex Crichton 
936aca67c1SAlex Crichton     impl HostA for MyComponent {
drop(&mut self, _: Resource<MyA>) -> Result<()>946aca67c1SAlex Crichton         fn drop(&mut self, _: Resource<MyA>) -> Result<()> {
956aca67c1SAlex Crichton             loop {}
966aca67c1SAlex Crichton         }
976aca67c1SAlex Crichton     }
986aca67c1SAlex Crichton 
996aca67c1SAlex Crichton     impl HostB for MyComponent {
drop(&mut self, _: Resource<MyA>) -> Result<()>1006aca67c1SAlex Crichton         fn drop(&mut self, _: Resource<MyA>) -> Result<()> {
1016aca67c1SAlex Crichton             loop {}
1026aca67c1SAlex Crichton         }
1036aca67c1SAlex Crichton     }
1046aca67c1SAlex Crichton 
1056aca67c1SAlex Crichton     impl foo::Host for MyComponent {}
1066aca67c1SAlex Crichton 
1076aca67c1SAlex Crichton     impl foo::HostA for MyComponent {
drop(&mut self, _: Resource<MyA>) -> Result<()>1086aca67c1SAlex Crichton         fn drop(&mut self, _: Resource<MyA>) -> Result<()> {
1096aca67c1SAlex Crichton             loop {}
1106aca67c1SAlex Crichton         }
1116aca67c1SAlex Crichton     }
1126aca67c1SAlex Crichton 
1136aca67c1SAlex Crichton     impl foo::HostB for MyComponent {
drop(&mut self, _: Resource<MyA>) -> Result<()>1146aca67c1SAlex Crichton         fn drop(&mut self, _: Resource<MyA>) -> Result<()> {
1156aca67c1SAlex Crichton             loop {}
1166aca67c1SAlex Crichton         }
1176aca67c1SAlex Crichton     }
1186aca67c1SAlex Crichton 
1196aca67c1SAlex Crichton     impl demo::pkg::bar::Host for MyComponent {}
1206aca67c1SAlex Crichton 
1216aca67c1SAlex Crichton     impl demo::pkg::bar::HostA for MyComponent {
drop(&mut self, _: Resource<MyA>) -> Result<()>1226aca67c1SAlex Crichton         fn drop(&mut self, _: Resource<MyA>) -> Result<()> {
1236aca67c1SAlex Crichton             loop {}
1246aca67c1SAlex Crichton         }
1256aca67c1SAlex Crichton     }
1266aca67c1SAlex Crichton 
1276aca67c1SAlex Crichton     impl demo::pkg::bar::HostB for MyComponent {
drop(&mut self, _: Resource<MyA>) -> Result<()>1286aca67c1SAlex Crichton         fn drop(&mut self, _: Resource<MyA>) -> Result<()> {
1296aca67c1SAlex Crichton             loop {}
1306aca67c1SAlex Crichton         }
1316aca67c1SAlex Crichton     }
1326aca67c1SAlex Crichton }
133722310a2SAlex Crichton 
13482284a36SBrian mod trappable_errors_with_versioned_and_unversioned_packages {
13582284a36SBrian     wasmtime::component::bindgen!({
136a13d7823SLoch Wansbrough         world: "foo:foo/nope",
13782284a36SBrian         inline: "
13882284a36SBrian             package foo:[email protected];
13982284a36SBrian 
14082284a36SBrian             interface a {
14182284a36SBrian                 variant error {
14282284a36SBrian                     other(string),
14382284a36SBrian                 }
14482284a36SBrian 
14582284a36SBrian                 f: func() -> result<_, error>;
14682284a36SBrian             }
14782284a36SBrian 
14882284a36SBrian             world foo {
14982284a36SBrian                 import a;
15082284a36SBrian             }
15182284a36SBrian         ",
15282284a36SBrian         path: "tests/codegen/unversioned-foo.wit",
15382284a36SBrian         trappable_error_type: {
1548c03849bSDan Gohman             "foo:foo/[email protected]" => MyX,
15582284a36SBrian         },
15682284a36SBrian     });
15782284a36SBrian 
15882284a36SBrian     type MyX = u64;
15982284a36SBrian }
16082284a36SBrian 
161722310a2SAlex Crichton mod trappable_errors {
162722310a2SAlex Crichton     wasmtime::component::bindgen!({
163722310a2SAlex Crichton         inline: "
164722310a2SAlex Crichton             package demo:pkg;
165722310a2SAlex Crichton 
166722310a2SAlex Crichton             interface a {
167722310a2SAlex Crichton                 type b = u64;
168722310a2SAlex Crichton 
169722310a2SAlex Crichton                 z1: func() -> result<_, b>;
170722310a2SAlex Crichton                 z2: func() -> result<_, b>;
171722310a2SAlex Crichton             }
172722310a2SAlex Crichton 
173722310a2SAlex Crichton             interface b {
174722310a2SAlex Crichton                 use a.{b};
175722310a2SAlex Crichton                 z: func() -> result<_, b>;
176722310a2SAlex Crichton             }
177722310a2SAlex Crichton 
178722310a2SAlex Crichton             interface c {
179722310a2SAlex Crichton                 type b = u64;
180722310a2SAlex Crichton             }
181722310a2SAlex Crichton 
182722310a2SAlex Crichton             interface d {
183722310a2SAlex Crichton                 use c.{b};
184722310a2SAlex Crichton                 z: func() -> result<_, b>;
185722310a2SAlex Crichton             }
186722310a2SAlex Crichton 
187722310a2SAlex Crichton             world foo {
188722310a2SAlex Crichton                 import a;
189722310a2SAlex Crichton                 import b;
190722310a2SAlex Crichton                 import d;
191722310a2SAlex Crichton             }
192722310a2SAlex Crichton         ",
193722310a2SAlex Crichton         trappable_error_type: {
1948c03849bSDan Gohman             "demo:pkg/a.b" => MyX,
1958c03849bSDan Gohman             "demo:pkg/c.b" => MyX,
196722310a2SAlex Crichton         },
197722310a2SAlex Crichton     });
198722310a2SAlex Crichton 
199722310a2SAlex Crichton     type MyX = u32;
200722310a2SAlex Crichton }
201e9a23180SArchisman Mridha 
202e9a23180SArchisman Mridha mod interface_name_with_rust_keyword {
203e9a23180SArchisman Mridha     wasmtime::component::bindgen!({
204e9a23180SArchisman Mridha         inline: "
205e9a23180SArchisman Mridha             package foo:foo;
206e9a23180SArchisman Mridha 
207e9a23180SArchisman Mridha             interface crate { }
208e9a23180SArchisman Mridha 
209e9a23180SArchisman Mridha             world foo {
210e9a23180SArchisman Mridha                 export crate;
211e9a23180SArchisman Mridha             }
212e9a23180SArchisman Mridha         "
213e9a23180SArchisman Mridha     });
214e9a23180SArchisman Mridha }
21577b0ae7fSAlex Crichton 
21677b0ae7fSAlex Crichton mod with_works_with_hierarchy {
21777b0ae7fSAlex Crichton     mod bindings {
21877b0ae7fSAlex Crichton         wasmtime::component::bindgen!({
21977b0ae7fSAlex Crichton             inline: "
22077b0ae7fSAlex Crichton                 package foo:foo;
22177b0ae7fSAlex Crichton 
22277b0ae7fSAlex Crichton                 interface a {
22377b0ae7fSAlex Crichton                     record t {
22477b0ae7fSAlex Crichton                         x: u32,
22577b0ae7fSAlex Crichton                     }
22677b0ae7fSAlex Crichton                     x: func() -> t;
22777b0ae7fSAlex Crichton                 }
22877b0ae7fSAlex Crichton 
22977b0ae7fSAlex Crichton                 interface b {
23077b0ae7fSAlex Crichton                     use a.{t};
23177b0ae7fSAlex Crichton                     x: func() -> t;
23277b0ae7fSAlex Crichton                 }
23377b0ae7fSAlex Crichton 
23477b0ae7fSAlex Crichton                 interface c {
23577b0ae7fSAlex Crichton                     use b.{t};
23677b0ae7fSAlex Crichton                     x: func() -> t;
23777b0ae7fSAlex Crichton                 }
23877b0ae7fSAlex Crichton 
23977b0ae7fSAlex Crichton                 world foo {
24077b0ae7fSAlex Crichton                     import c;
24177b0ae7fSAlex Crichton                 }
24277b0ae7fSAlex Crichton             "
24377b0ae7fSAlex Crichton         });
24477b0ae7fSAlex Crichton     }
24577b0ae7fSAlex Crichton 
24677b0ae7fSAlex Crichton     mod with_just_one_interface {
24777b0ae7fSAlex Crichton         wasmtime::component::bindgen!({
24877b0ae7fSAlex Crichton             inline: "
24977b0ae7fSAlex Crichton                 package foo:foo;
25077b0ae7fSAlex Crichton 
25177b0ae7fSAlex Crichton                 interface a {
25277b0ae7fSAlex Crichton                     record t {
25377b0ae7fSAlex Crichton                         x: u32,
25477b0ae7fSAlex Crichton                     }
25577b0ae7fSAlex Crichton                     x: func() -> t;
25677b0ae7fSAlex Crichton                 }
25777b0ae7fSAlex Crichton 
25877b0ae7fSAlex Crichton                 interface b {
25977b0ae7fSAlex Crichton                     use a.{t};
26077b0ae7fSAlex Crichton                     x: func() -> t;
26177b0ae7fSAlex Crichton                 }
26277b0ae7fSAlex Crichton 
26377b0ae7fSAlex Crichton                 interface c {
26477b0ae7fSAlex Crichton                     use b.{t};
26577b0ae7fSAlex Crichton                     x: func() -> t;
26677b0ae7fSAlex Crichton                 }
26777b0ae7fSAlex Crichton 
26877b0ae7fSAlex Crichton                 world foo {
26977b0ae7fSAlex Crichton                     use c.{t};
27077b0ae7fSAlex Crichton 
27177b0ae7fSAlex Crichton                     import x: func() -> t;
27277b0ae7fSAlex Crichton                 }
27377b0ae7fSAlex Crichton             ",
27477b0ae7fSAlex Crichton             with: { "foo:foo/a": super::bindings::foo::foo::a }
27577b0ae7fSAlex Crichton         });
27677b0ae7fSAlex Crichton 
27777b0ae7fSAlex Crichton         struct X;
27877b0ae7fSAlex Crichton 
27977b0ae7fSAlex Crichton         impl FooImports for X {
x(&mut self) -> super::bindings::foo::foo::a::T2801cf0060bSAlex Crichton             fn x(&mut self) -> super::bindings::foo::foo::a::T {
28177b0ae7fSAlex Crichton                 loop {}
28277b0ae7fSAlex Crichton             }
28377b0ae7fSAlex Crichton         }
28477b0ae7fSAlex Crichton     }
28577b0ae7fSAlex Crichton 
28677b0ae7fSAlex Crichton     mod with_whole_package {
28777b0ae7fSAlex Crichton         wasmtime::component::bindgen!({
28877b0ae7fSAlex Crichton             inline: "
28977b0ae7fSAlex Crichton                 package foo:foo;
29077b0ae7fSAlex Crichton 
29177b0ae7fSAlex Crichton                 interface a {
29277b0ae7fSAlex Crichton                     record t {
29377b0ae7fSAlex Crichton                         x: u32,
29477b0ae7fSAlex Crichton                     }
29577b0ae7fSAlex Crichton                     x: func() -> t;
29677b0ae7fSAlex Crichton                 }
29777b0ae7fSAlex Crichton 
29877b0ae7fSAlex Crichton                 interface b {
29977b0ae7fSAlex Crichton                     use a.{t};
30077b0ae7fSAlex Crichton                     x: func() -> t;
30177b0ae7fSAlex Crichton                 }
30277b0ae7fSAlex Crichton 
30377b0ae7fSAlex Crichton                 interface c {
30477b0ae7fSAlex Crichton                     use b.{t};
30577b0ae7fSAlex Crichton                     x: func() -> t;
30677b0ae7fSAlex Crichton                 }
30777b0ae7fSAlex Crichton 
30877b0ae7fSAlex Crichton                 world foo {
30977b0ae7fSAlex Crichton                     use c.{t};
31077b0ae7fSAlex Crichton 
31177b0ae7fSAlex Crichton                     import x: func() -> t;
31277b0ae7fSAlex Crichton                 }
31377b0ae7fSAlex Crichton             ",
31477b0ae7fSAlex Crichton             with: { "foo:foo": super::bindings::foo::foo }
31577b0ae7fSAlex Crichton         });
31677b0ae7fSAlex Crichton 
31777b0ae7fSAlex Crichton         struct X;
31877b0ae7fSAlex Crichton 
31977b0ae7fSAlex Crichton         impl FooImports for X {
x(&mut self) -> super::bindings::foo::foo::a::T3201cf0060bSAlex Crichton             fn x(&mut self) -> super::bindings::foo::foo::a::T {
32177b0ae7fSAlex Crichton                 loop {}
32277b0ae7fSAlex Crichton             }
32377b0ae7fSAlex Crichton         }
32477b0ae7fSAlex Crichton     }
32577b0ae7fSAlex Crichton 
32677b0ae7fSAlex Crichton     mod with_whole_namespace {
32777b0ae7fSAlex Crichton         wasmtime::component::bindgen!({
32877b0ae7fSAlex Crichton             inline: "
32977b0ae7fSAlex Crichton                 package foo:foo;
33077b0ae7fSAlex Crichton 
33177b0ae7fSAlex Crichton                 interface a {
33277b0ae7fSAlex Crichton                     record t {
33377b0ae7fSAlex Crichton                         x: u32,
33477b0ae7fSAlex Crichton                     }
33577b0ae7fSAlex Crichton                     x: func() -> t;
33677b0ae7fSAlex Crichton                 }
33777b0ae7fSAlex Crichton 
33877b0ae7fSAlex Crichton                 interface b {
33977b0ae7fSAlex Crichton                     use a.{t};
34077b0ae7fSAlex Crichton                     x: func() -> t;
34177b0ae7fSAlex Crichton                 }
34277b0ae7fSAlex Crichton 
34377b0ae7fSAlex Crichton                 interface c {
34477b0ae7fSAlex Crichton                     use b.{t};
34577b0ae7fSAlex Crichton                     x: func() -> t;
34677b0ae7fSAlex Crichton                 }
34777b0ae7fSAlex Crichton 
34877b0ae7fSAlex Crichton                 world foo {
34977b0ae7fSAlex Crichton                     use c.{t};
35077b0ae7fSAlex Crichton 
35177b0ae7fSAlex Crichton                     import x: func() -> t;
35277b0ae7fSAlex Crichton                 }
35377b0ae7fSAlex Crichton             ",
35477b0ae7fSAlex Crichton             with: { "foo": super::bindings::foo }
35577b0ae7fSAlex Crichton         });
35677b0ae7fSAlex Crichton 
35777b0ae7fSAlex Crichton         struct X;
35877b0ae7fSAlex Crichton 
35977b0ae7fSAlex Crichton         impl FooImports for X {
x(&mut self) -> super::bindings::foo::foo::a::T3601cf0060bSAlex Crichton             fn x(&mut self) -> super::bindings::foo::foo::a::T {
36177b0ae7fSAlex Crichton                 loop {}
36277b0ae7fSAlex Crichton             }
36377b0ae7fSAlex Crichton         }
36477b0ae7fSAlex Crichton     }
36577b0ae7fSAlex Crichton }
3661cf0060bSAlex Crichton 
3671cf0060bSAlex Crichton mod trappable_imports {
3681cf0060bSAlex Crichton     mod none {
3691cf0060bSAlex Crichton         wasmtime::component::bindgen!({
3701cf0060bSAlex Crichton             inline: "
3711cf0060bSAlex Crichton                 package foo:foo;
3721cf0060bSAlex Crichton 
3731cf0060bSAlex Crichton                 world foo {
3741cf0060bSAlex Crichton                     import foo: func();
3751cf0060bSAlex Crichton                 }
3761cf0060bSAlex Crichton             ",
3771cf0060bSAlex Crichton         });
3781cf0060bSAlex Crichton         struct X;
3791cf0060bSAlex Crichton 
3801cf0060bSAlex Crichton         impl FooImports for X {
foo(&mut self)3811cf0060bSAlex Crichton             fn foo(&mut self) {}
3821cf0060bSAlex Crichton         }
3831cf0060bSAlex Crichton     }
3841cf0060bSAlex Crichton 
3851cf0060bSAlex Crichton     mod all {
3861cf0060bSAlex Crichton         wasmtime::component::bindgen!({
3871cf0060bSAlex Crichton             inline: "
3881cf0060bSAlex Crichton                 package foo:foo;
3891cf0060bSAlex Crichton 
3901cf0060bSAlex Crichton                 world foo {
3911cf0060bSAlex Crichton                     import foo: func();
3921cf0060bSAlex Crichton                 }
3931cf0060bSAlex Crichton             ",
3941155d6dfSAlex Crichton             imports: { default: trappable },
3951cf0060bSAlex Crichton         });
3961cf0060bSAlex Crichton         struct X;
3971cf0060bSAlex Crichton 
3981cf0060bSAlex Crichton         impl FooImports for X {
foo(&mut self) -> wasmtime::Result<()>3991cf0060bSAlex Crichton             fn foo(&mut self) -> wasmtime::Result<()> {
4001cf0060bSAlex Crichton                 Ok(())
4011cf0060bSAlex Crichton             }
4021cf0060bSAlex Crichton         }
4031cf0060bSAlex Crichton     }
4041cf0060bSAlex Crichton 
4051cf0060bSAlex Crichton     mod some {
4061cf0060bSAlex Crichton         wasmtime::component::bindgen!({
4071cf0060bSAlex Crichton             inline: "
4081cf0060bSAlex Crichton                 package foo:foo;
4091cf0060bSAlex Crichton 
4101cf0060bSAlex Crichton                 world foo {
4111cf0060bSAlex Crichton                     import foo: func();
4121cf0060bSAlex Crichton                     import bar: func();
4131cf0060bSAlex Crichton                 }
4141cf0060bSAlex Crichton             ",
4151155d6dfSAlex Crichton             imports: { "foo": trappable },
4161cf0060bSAlex Crichton         });
4171cf0060bSAlex Crichton         struct X;
4181cf0060bSAlex Crichton 
4191cf0060bSAlex Crichton         impl FooImports for X {
foo(&mut self) -> wasmtime::Result<()>4201cf0060bSAlex Crichton             fn foo(&mut self) -> wasmtime::Result<()> {
4211cf0060bSAlex Crichton                 Ok(())
4221cf0060bSAlex Crichton             }
bar(&mut self)4231cf0060bSAlex Crichton             fn bar(&mut self) {}
4241cf0060bSAlex Crichton         }
4251cf0060bSAlex Crichton     }
4261cf0060bSAlex Crichton 
4271cf0060bSAlex Crichton     mod across_interfaces {
4281cf0060bSAlex Crichton         use wasmtime::component::Resource;
4291cf0060bSAlex Crichton 
4301cf0060bSAlex Crichton         wasmtime::component::bindgen!({
4311cf0060bSAlex Crichton             inline: "
4321cf0060bSAlex Crichton                 package foo:foo;
4331cf0060bSAlex Crichton 
4341cf0060bSAlex Crichton                 interface a {
4351cf0060bSAlex Crichton                     foo: func();
4361cf0060bSAlex Crichton                     bar: func();
4371cf0060bSAlex Crichton 
4381cf0060bSAlex Crichton                     resource r {
4391cf0060bSAlex Crichton                         constructor();
4401cf0060bSAlex Crichton                         foo: func();
4411cf0060bSAlex Crichton                         bar: static func();
4421cf0060bSAlex Crichton                     }
4431cf0060bSAlex Crichton                 }
4441cf0060bSAlex Crichton 
4451cf0060bSAlex Crichton                 world foo {
4461cf0060bSAlex Crichton                     import a;
4471cf0060bSAlex Crichton                     import foo: func();
4481cf0060bSAlex Crichton                     import bar: func();
4491cf0060bSAlex Crichton                     import i: interface {
4501cf0060bSAlex Crichton                         foo: func();
4511cf0060bSAlex Crichton                         bar: func();
4521cf0060bSAlex Crichton                     }
4531cf0060bSAlex Crichton 
4541cf0060bSAlex Crichton                 }
4551cf0060bSAlex Crichton             ",
4561155d6dfSAlex Crichton             imports: {
4571155d6dfSAlex Crichton                 "foo": trappable | exact,
4588c03849bSDan Gohman                 "i.foo": trappable,
4598c03849bSDan Gohman                 "foo:foo/a.foo": trappable,
4601155d6dfSAlex Crichton             },
4618c03849bSDan Gohman             with: { "foo:foo/a.r": R },
4621cf0060bSAlex Crichton         });
4631cf0060bSAlex Crichton 
4641cf0060bSAlex Crichton         struct X;
4651cf0060bSAlex Crichton         pub struct R;
4661cf0060bSAlex Crichton 
4671cf0060bSAlex Crichton         impl FooImports for X {
foo(&mut self) -> wasmtime::Result<()>4681cf0060bSAlex Crichton             fn foo(&mut self) -> wasmtime::Result<()> {
4691cf0060bSAlex Crichton                 Ok(())
4701cf0060bSAlex Crichton             }
bar(&mut self)4711cf0060bSAlex Crichton             fn bar(&mut self) {}
4721cf0060bSAlex Crichton         }
4731cf0060bSAlex Crichton 
4741cf0060bSAlex Crichton         impl i::Host for X {
foo(&mut self) -> wasmtime::Result<()>4751cf0060bSAlex Crichton             fn foo(&mut self) -> wasmtime::Result<()> {
4761cf0060bSAlex Crichton                 Ok(())
4771cf0060bSAlex Crichton             }
bar(&mut self)4781cf0060bSAlex Crichton             fn bar(&mut self) {}
4791cf0060bSAlex Crichton         }
4801cf0060bSAlex Crichton 
4811cf0060bSAlex Crichton         impl foo::foo::a::Host for X {
foo(&mut self) -> wasmtime::Result<()>4821cf0060bSAlex Crichton             fn foo(&mut self) -> wasmtime::Result<()> {
4831cf0060bSAlex Crichton                 Ok(())
4841cf0060bSAlex Crichton             }
bar(&mut self)4851cf0060bSAlex Crichton             fn bar(&mut self) {}
4861cf0060bSAlex Crichton         }
4871cf0060bSAlex Crichton 
4881cf0060bSAlex Crichton         impl foo::foo::a::HostR for X {
new(&mut self) -> Resource<R>4891cf0060bSAlex Crichton             fn new(&mut self) -> Resource<R> {
4901cf0060bSAlex Crichton                 loop {}
4911cf0060bSAlex Crichton             }
foo(&mut self, _: Resource<R>)4921cf0060bSAlex Crichton             fn foo(&mut self, _: Resource<R>) {}
bar(&mut self)4931cf0060bSAlex Crichton             fn bar(&mut self) {}
drop(&mut self, _: Resource<R>) -> wasmtime::Result<()>4941cf0060bSAlex Crichton             fn drop(&mut self, _: Resource<R>) -> wasmtime::Result<()> {
4951cf0060bSAlex Crichton                 Ok(())
4961cf0060bSAlex Crichton             }
4971cf0060bSAlex Crichton         }
4981cf0060bSAlex Crichton     }
4991cf0060bSAlex Crichton 
5001cf0060bSAlex Crichton     mod resources {
5011cf0060bSAlex Crichton         use wasmtime::component::Resource;
5021cf0060bSAlex Crichton 
5031cf0060bSAlex Crichton         wasmtime::component::bindgen!({
5041cf0060bSAlex Crichton             inline: "
5051cf0060bSAlex Crichton                 package foo:foo;
5061cf0060bSAlex Crichton 
5071cf0060bSAlex Crichton                 interface a {
5081cf0060bSAlex Crichton                     resource r {
5091cf0060bSAlex Crichton                         constructor();
5101cf0060bSAlex Crichton                         foo: func();
5111cf0060bSAlex Crichton                         bar: static func();
5121cf0060bSAlex Crichton                     }
5131cf0060bSAlex Crichton                 }
5141cf0060bSAlex Crichton 
5151cf0060bSAlex Crichton                 world foo {
5161cf0060bSAlex Crichton                     import a;
5171cf0060bSAlex Crichton 
5181cf0060bSAlex Crichton                 }
5191cf0060bSAlex Crichton             ",
5201155d6dfSAlex Crichton             imports: {
5218c03849bSDan Gohman                 "foo:foo/a.[constructor]r": trappable,
5228c03849bSDan Gohman                 "foo:foo/a.[method]r.foo": trappable,
5238c03849bSDan Gohman                 "foo:foo/a.[static]r.bar": trappable,
5241155d6dfSAlex Crichton             },
5258c03849bSDan Gohman             with: { "foo:foo/a.r": R },
5261cf0060bSAlex Crichton         });
5271cf0060bSAlex Crichton 
5281cf0060bSAlex Crichton         struct X;
5291cf0060bSAlex Crichton         pub struct R;
5301cf0060bSAlex Crichton 
5311cf0060bSAlex Crichton         impl foo::foo::a::Host for X {}
5321cf0060bSAlex Crichton 
5331cf0060bSAlex Crichton         impl foo::foo::a::HostR for X {
new(&mut self) -> wasmtime::Result<Resource<R>>5341cf0060bSAlex Crichton             fn new(&mut self) -> wasmtime::Result<Resource<R>> {
5351cf0060bSAlex Crichton                 loop {}
5361cf0060bSAlex Crichton             }
foo(&mut self, _: Resource<R>) -> wasmtime::Result<()>5371cf0060bSAlex Crichton             fn foo(&mut self, _: Resource<R>) -> wasmtime::Result<()> {
5381cf0060bSAlex Crichton                 Ok(())
5391cf0060bSAlex Crichton             }
bar(&mut self) -> wasmtime::Result<()>5401cf0060bSAlex Crichton             fn bar(&mut self) -> wasmtime::Result<()> {
5411cf0060bSAlex Crichton                 Ok(())
5421cf0060bSAlex Crichton             }
drop(&mut self, _: Resource<R>) -> wasmtime::Result<()>5431cf0060bSAlex Crichton             fn drop(&mut self, _: Resource<R>) -> wasmtime::Result<()> {
5441cf0060bSAlex Crichton                 Ok(())
5451cf0060bSAlex Crichton             }
5461cf0060bSAlex Crichton         }
5471cf0060bSAlex Crichton     }
5481cf0060bSAlex Crichton }
5497de48789SBrian 
5507de48789SBrian mod custom_derives {
55190ac295eSAlex Crichton     use std::collections::{HashSet, hash_map::RandomState};
5527de48789SBrian 
5537de48789SBrian     wasmtime::component::bindgen!({
5547de48789SBrian         inline: "
5557de48789SBrian             package my:inline;
5567de48789SBrian 
5577de48789SBrian             interface blah {
5583e4b0b9cSBrian                 variant abc {
5593e4b0b9cSBrian                     a,
5603e4b0b9cSBrian                     b,
5613e4b0b9cSBrian                     c
5623e4b0b9cSBrian                 }
5633e4b0b9cSBrian 
5647de48789SBrian                 record foo {
5657de48789SBrian                     field1: string,
5663e4b0b9cSBrian                     field2: list<u32>,
5673e4b0b9cSBrian                     field3: abc
5687de48789SBrian                 }
5697de48789SBrian 
5707de48789SBrian                 bar: func(cool: foo);
5717de48789SBrian             }
5727de48789SBrian 
5737de48789SBrian             world baz {
5747de48789SBrian                 import blah;
5757de48789SBrian             }
5767de48789SBrian         ",
5777de48789SBrian         // Clone is included by default almost everywhere, so include it here to make sure it
5787de48789SBrian         // doesn't conflict
5797de48789SBrian         additional_derives: [serde::Serialize, serde::Deserialize, Hash, Clone, PartialEq, Eq],
5807de48789SBrian     });
5817de48789SBrian 
5823e4b0b9cSBrian     use my::inline::blah::{Abc, Foo, Host};
5837de48789SBrian 
5847de48789SBrian     struct X;
5857de48789SBrian 
5867de48789SBrian     impl Host for X {
bar(&mut self, cool: Foo)5877de48789SBrian         fn bar(&mut self, cool: Foo) {
5887de48789SBrian             // Check that built in derives that I've added actually work by seeing that this hashes
5897de48789SBrian             let _blah: HashSet<Foo, RandomState> = HashSet::from_iter([Foo {
5907de48789SBrian                 field1: "hello".to_string(),
5917de48789SBrian                 field2: vec![1, 2, 3],
5923e4b0b9cSBrian                 field3: Abc::B,
5937de48789SBrian             }]);
5947de48789SBrian 
5957de48789SBrian             // Check that the attributes from an external crate actually work. If they don't work,
5967de48789SBrian             // compilation will fail here
5977de48789SBrian             let _ = serde_json::to_string(&cool);
5987de48789SBrian         }
5997de48789SBrian     }
6007de48789SBrian }
60154d36958SAlex Crichton 
60254d36958SAlex Crichton mod with_and_mixing_async {
60354d36958SAlex Crichton     mod with_async {
60454d36958SAlex Crichton         wasmtime::component::bindgen!({
60554d36958SAlex Crichton             inline: "
60654d36958SAlex Crichton                 package my:inline;
60754d36958SAlex Crichton                 interface foo {
60854d36958SAlex Crichton                     type t = u32;
60954d36958SAlex Crichton                     foo: func() -> t;
61054d36958SAlex Crichton                 }
61154d36958SAlex Crichton                 interface bar {
61254d36958SAlex Crichton                     use foo.{t};
61354d36958SAlex Crichton                     bar: func() -> t;
61454d36958SAlex Crichton                 }
61554d36958SAlex Crichton                 world x {
61654d36958SAlex Crichton                     import bar;
61754d36958SAlex Crichton                 }
61854d36958SAlex Crichton             ",
6191155d6dfSAlex Crichton             imports: {
6208c03849bSDan Gohman                 "my:inline/bar.bar": async,
62154d36958SAlex Crichton             },
62254d36958SAlex Crichton         });
62354d36958SAlex Crichton     }
62454d36958SAlex Crichton 
62554d36958SAlex Crichton     mod without_async {
62654d36958SAlex Crichton         wasmtime::component::bindgen!({
62754d36958SAlex Crichton             inline: "
62854d36958SAlex Crichton                 package my:inline;
62954d36958SAlex Crichton                 interface foo {
63054d36958SAlex Crichton                     type t = u32;
63154d36958SAlex Crichton                     foo: func() -> t;
63254d36958SAlex Crichton                 }
63354d36958SAlex Crichton                 interface bar {
63454d36958SAlex Crichton                     use foo.{t};
63554d36958SAlex Crichton                     bar: func() -> t;
63654d36958SAlex Crichton                 }
63754d36958SAlex Crichton                 world x {
63854d36958SAlex Crichton                     import bar;
63954d36958SAlex Crichton                 }
64054d36958SAlex Crichton             ",
64154d36958SAlex Crichton             with: {
64254d36958SAlex Crichton                 "my:inline/foo": super::with_async::my::inline::foo,
64354d36958SAlex Crichton             },
64454d36958SAlex Crichton             require_store_data_send: true,
64554d36958SAlex Crichton         });
64654d36958SAlex Crichton     }
64754d36958SAlex Crichton 
64854d36958SAlex Crichton     mod third {
64954d36958SAlex Crichton         wasmtime::component::bindgen!({
65054d36958SAlex Crichton             inline: "
65154d36958SAlex Crichton                 package my:inline;
65254d36958SAlex Crichton                 interface foo {
65354d36958SAlex Crichton                     type t = u32;
65454d36958SAlex Crichton                     foo: func() -> t;
65554d36958SAlex Crichton                 }
65654d36958SAlex Crichton                 interface bar {
65754d36958SAlex Crichton                     use foo.{t};
65854d36958SAlex Crichton                     bar: func() -> t;
65954d36958SAlex Crichton                 }
66054d36958SAlex Crichton                 interface baz {
66154d36958SAlex Crichton                     use bar.{t};
66254d36958SAlex Crichton                     baz: func() -> t;
66354d36958SAlex Crichton                 }
66454d36958SAlex Crichton                 world x {
66554d36958SAlex Crichton                     import baz;
66654d36958SAlex Crichton                 }
66754d36958SAlex Crichton             ",
66854d36958SAlex Crichton             with: {
66954d36958SAlex Crichton                 "my:inline/foo": super::with_async::my::inline::foo,
67054d36958SAlex Crichton                 "my:inline/bar": super::without_async::my::inline::bar,
67154d36958SAlex Crichton             },
67254d36958SAlex Crichton             require_store_data_send: true,
67354d36958SAlex Crichton         });
67454d36958SAlex Crichton     }
67554d36958SAlex Crichton }
6765f3597eaSAlex Crichton 
6775f3597eaSAlex Crichton mod trappable_error_type_and_versions {
6785f3597eaSAlex Crichton     struct MyError;
6795f3597eaSAlex Crichton 
6805f3597eaSAlex Crichton     mod package_no_version_path_no_version {
6815f3597eaSAlex Crichton         wasmtime::component::bindgen!({
6825f3597eaSAlex Crichton             inline: "
6835f3597eaSAlex Crichton                 package my:inline;
6845f3597eaSAlex Crichton                 interface i {
6855f3597eaSAlex Crichton                     enum e { a, b, c }
6865f3597eaSAlex Crichton                 }
6875f3597eaSAlex Crichton                 world foo {}
6885f3597eaSAlex Crichton             ",
6895f3597eaSAlex Crichton             trappable_error_type: {
6908c03849bSDan Gohman                 "my:inline/i.e" => super::MyError,
6915f3597eaSAlex Crichton             },
6925f3597eaSAlex Crichton         });
6935f3597eaSAlex Crichton     }
6945f3597eaSAlex Crichton     mod package_version_path_no_version {
6955f3597eaSAlex Crichton         wasmtime::component::bindgen!({
6965f3597eaSAlex Crichton             inline: "
6975f3597eaSAlex Crichton                 package my:[email protected];
6985f3597eaSAlex Crichton                 interface i {
6995f3597eaSAlex Crichton                     enum e { a, b, c }
7005f3597eaSAlex Crichton                 }
7015f3597eaSAlex Crichton                 world foo {}
7025f3597eaSAlex Crichton             ",
7035f3597eaSAlex Crichton             trappable_error_type: {
7048c03849bSDan Gohman                 "my:inline/i.e" => super::MyError,
7055f3597eaSAlex Crichton             },
7065f3597eaSAlex Crichton         });
7075f3597eaSAlex Crichton     }
7085f3597eaSAlex Crichton     mod package_version_path_version {
7095f3597eaSAlex Crichton         wasmtime::component::bindgen!({
7105f3597eaSAlex Crichton             inline: "
7115f3597eaSAlex Crichton                 package my:[email protected];
7125f3597eaSAlex Crichton                 interface i {
7135f3597eaSAlex Crichton                     enum e { a, b, c }
7145f3597eaSAlex Crichton                 }
7155f3597eaSAlex Crichton                 world foo {}
7165f3597eaSAlex Crichton             ",
7175f3597eaSAlex Crichton             trappable_error_type: {
7188c03849bSDan Gohman                 "my:inline/[email protected]" => super::MyError,
7195f3597eaSAlex Crichton             },
7205f3597eaSAlex Crichton         });
7215f3597eaSAlex Crichton     }
7225f3597eaSAlex Crichton }
723a13d7823SLoch Wansbrough 
724a13d7823SLoch Wansbrough mod paths {
725a13d7823SLoch Wansbrough     mod multiple_paths {
726a13d7823SLoch Wansbrough         wasmtime::component::bindgen!({
727a13d7823SLoch Wansbrough             world: "test:paths/test",
728a13d7823SLoch Wansbrough             inline: r#"
729a13d7823SLoch Wansbrough             package test:paths;
730a13d7823SLoch Wansbrough             world test {
731a13d7823SLoch Wansbrough                 import paths:path1/test;
732a13d7823SLoch Wansbrough                 export paths:path2/test;
733a13d7823SLoch Wansbrough             }
734a13d7823SLoch Wansbrough             "#,
735a13d7823SLoch Wansbrough             path: ["tests/codegen/path1", "tests/codegen/path2"],
736a13d7823SLoch Wansbrough         });
737a13d7823SLoch Wansbrough     }
738a13d7823SLoch Wansbrough }
7394dd4b890Sanlavandier 
7404dd4b890Sanlavandier mod import_async_interface {
7414dd4b890Sanlavandier     pub mod async_interface_implementation {
7424dd4b890Sanlavandier         wasmtime::component::bindgen!({
7434dd4b890Sanlavandier             world: "test:async-import/blah-impl",
7444dd4b890Sanlavandier             inline: r#"
7454dd4b890Sanlavandier             package test:async-import;
7464dd4b890Sanlavandier 
7474dd4b890Sanlavandier             interface blah {
7484dd4b890Sanlavandier                 foo: func();
7494dd4b890Sanlavandier             }
7504dd4b890Sanlavandier 
7514dd4b890Sanlavandier             world blah-impl {
7524dd4b890Sanlavandier                 import blah;
7534dd4b890Sanlavandier             }
7544dd4b890Sanlavandier 
7554dd4b890Sanlavandier             world bar {
7564dd4b890Sanlavandier                 import blah;
7574dd4b890Sanlavandier             }
7584dd4b890Sanlavandier             "#,
7594dd4b890Sanlavandier             imports: { default: async },
7604dd4b890Sanlavandier         });
7614dd4b890Sanlavandier 
7624dd4b890Sanlavandier         use test::async_import::blah::Host;
7634dd4b890Sanlavandier         struct X;
7644dd4b890Sanlavandier 
7654dd4b890Sanlavandier         impl Host for X {
foo(&mut self)7664dd4b890Sanlavandier             async fn foo(&mut self) {}
7674dd4b890Sanlavandier         }
7684dd4b890Sanlavandier     }
7694dd4b890Sanlavandier 
7704dd4b890Sanlavandier     mod require_t_send {
7714dd4b890Sanlavandier         wasmtime::component::bindgen!({
7724dd4b890Sanlavandier             world: "test:async-import/bar",
7734dd4b890Sanlavandier             inline: r#"
7744dd4b890Sanlavandier             package test:async-import;
7754dd4b890Sanlavandier 
7764dd4b890Sanlavandier             interface blah {
7774dd4b890Sanlavandier                 foo: func();
7784dd4b890Sanlavandier             }
7794dd4b890Sanlavandier 
7804dd4b890Sanlavandier             world blah-impl {
7814dd4b890Sanlavandier                 import blah;
7824dd4b890Sanlavandier             }
7834dd4b890Sanlavandier 
7844dd4b890Sanlavandier             world bar {
7854dd4b890Sanlavandier                 import blah;
7864dd4b890Sanlavandier             }
7874dd4b890Sanlavandier             "#,
7884dd4b890Sanlavandier             with: {
7894dd4b890Sanlavandier                 "test:async-import/blah": super::async_interface_implementation::test::async_import::blah,
7904dd4b890Sanlavandier             },
7914dd4b890Sanlavandier             imports: { default: async },
7924dd4b890Sanlavandier         });
7934dd4b890Sanlavandier     }
7944dd4b890Sanlavandier }
795*4a168844SAlex Crichton 
796*4a168844SAlex Crichton mod anyhow_with_custom_error {
797*4a168844SAlex Crichton     wasmtime::component::bindgen!({
798*4a168844SAlex Crichton         inline: "
799*4a168844SAlex Crichton             package foo:foo;
800*4a168844SAlex Crichton 
801*4a168844SAlex Crichton             interface i {
802*4a168844SAlex Crichton                 enum error {
803*4a168844SAlex Crichton                     a,
804*4a168844SAlex Crichton                     b,
805*4a168844SAlex Crichton                     c
806*4a168844SAlex Crichton                 }
807*4a168844SAlex Crichton                 x: func() -> result<_, error>;
808*4a168844SAlex Crichton             }
809*4a168844SAlex Crichton 
810*4a168844SAlex Crichton             world foo {
811*4a168844SAlex Crichton                 import i;
812*4a168844SAlex Crichton             }
813*4a168844SAlex Crichton         ",
814*4a168844SAlex Crichton         anyhow: true,
815*4a168844SAlex Crichton         imports: { default: trappable },
816*4a168844SAlex Crichton         trappable_error_type: {
817*4a168844SAlex Crichton             "foo:foo/i.error" => MyCustomError,
818*4a168844SAlex Crichton         },
819*4a168844SAlex Crichton     });
820*4a168844SAlex Crichton 
821*4a168844SAlex Crichton     struct MyCustomError;
822*4a168844SAlex Crichton }
823