Skip to content

Commit 6801aa7

Browse files
authored
Fix some minor bindgen! issues with anyhow: true (#12658)
Found when updating Spin to Wasmtime 42.0.0 where some cases weren't handled: * Trappable functions with no result. * Resource destructors. * Functions with custom trappable errors.
1 parent aa3cfe7 commit 6801aa7

18 files changed

+262
-156
lines changed

crates/component-macro/tests/codegen.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ macro_rules! gentest {
3636
imports: { default: store },
3737
});
3838
}
39+
mod with_anyhow {
40+
wasmtime::component::bindgen!({
41+
path: $path,
42+
anyhow: true,
43+
imports: { default: trappable },
44+
});
45+
}
3946
}
4047
};
4148
}
@@ -785,3 +792,31 @@ mod import_async_interface {
785792
});
786793
}
787794
}
795+
796+
mod anyhow_with_custom_error {
797+
wasmtime::component::bindgen!({
798+
inline: "
799+
package foo:foo;
800+
801+
interface i {
802+
enum error {
803+
a,
804+
b,
805+
c
806+
}
807+
x: func() -> result<_, error>;
808+
}
809+
810+
world foo {
811+
import i;
812+
}
813+
",
814+
anyhow: true,
815+
imports: { default: trappable },
816+
trappable_error_type: {
817+
"foo:foo/i.error" => MyCustomError,
818+
},
819+
});
820+
821+
struct MyCustomError;
822+
}

crates/component-macro/tests/expanded/resources-export.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ pub mod foo {
276276
wasmtime::component::ResourceType::host::<Y>(),
277277
move |mut store, rep| -> wasmtime::Result<()> {
278278
let resource = wasmtime::component::Resource::new_own(rep);
279-
HostY::drop(&mut host_getter(store.data_mut()), resource)
279+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
280+
HostY::drop(&mut host_getter(store.data_mut()), resource),
281+
)
280282
},
281283
)?;
282284
Ok(())

crates/component-macro/tests/expanded/resources-export_async.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,13 @@ pub mod foo {
276276
wasmtime::component::ResourceType::host::<Y>(),
277277
move |mut store, rep| {
278278
wasmtime::component::__internal::Box::new(async move {
279-
HostY::drop(
280-
&mut host_getter(store.data_mut()),
281-
wasmtime::component::Resource::new_own(rep),
282-
)
283-
.await
279+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
280+
HostY::drop(
281+
&mut host_getter(store.data_mut()),
282+
wasmtime::component::Resource::new_own(rep),
283+
)
284+
.await,
285+
)
284286
})
285287
},
286288
)?;

crates/component-macro/tests/expanded/resources-export_concurrent.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,13 @@ pub mod foo {
268268
move |caller: &wasmtime::component::Accessor<T>, rep| {
269269
wasmtime::component::__internal::Box::pin(async move {
270270
let accessor = &caller.with_getter(host_getter);
271-
HostYWithStore::drop(
272-
accessor,
273-
wasmtime::component::Resource::new_own(rep),
274-
)
275-
.await
271+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
272+
HostYWithStore::drop(
273+
accessor,
274+
wasmtime::component::Resource::new_own(rep),
275+
)
276+
.await,
277+
)
276278
})
277279
},
278280
)?;

crates/component-macro/tests/expanded/resources-export_tracing_async.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,13 @@ pub mod foo {
276276
wasmtime::component::ResourceType::host::<Y>(),
277277
move |mut store, rep| {
278278
wasmtime::component::__internal::Box::new(async move {
279-
HostY::drop(
280-
&mut host_getter(store.data_mut()),
281-
wasmtime::component::Resource::new_own(rep),
282-
)
283-
.await
279+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
280+
HostY::drop(
281+
&mut host_getter(store.data_mut()),
282+
wasmtime::component::Resource::new_own(rep),
283+
)
284+
.await,
285+
)
284286
})
285287
},
286288
)?;

crates/component-macro/tests/expanded/resources-import.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ const _: () = {
270270
wasmtime::component::ResourceType::host::<WorldResource>(),
271271
move |mut store, rep| -> wasmtime::Result<()> {
272272
let resource = wasmtime::component::Resource::new_own(rep);
273-
HostWorldResource::drop(
274-
&mut host_getter(store.data_mut()),
275-
resource,
273+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
274+
HostWorldResource::drop(
275+
&mut host_getter(store.data_mut()),
276+
resource,
277+
),
276278
)
277279
},
278280
)?;
@@ -678,15 +680,22 @@ pub mod foo {
678680
wasmtime::component::ResourceType::host::<Bar>(),
679681
move |mut store, rep| -> wasmtime::Result<()> {
680682
let resource = wasmtime::component::Resource::new_own(rep);
681-
HostBar::drop(&mut host_getter(store.data_mut()), resource)
683+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
684+
HostBar::drop(&mut host_getter(store.data_mut()), resource),
685+
)
682686
},
683687
)?;
684688
inst.resource(
685689
"fallible",
686690
wasmtime::component::ResourceType::host::<Fallible>(),
687691
move |mut store, rep| -> wasmtime::Result<()> {
688692
let resource = wasmtime::component::Resource::new_own(rep);
689-
HostFallible::drop(&mut host_getter(store.data_mut()), resource)
693+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
694+
HostFallible::drop(
695+
&mut host_getter(store.data_mut()),
696+
resource,
697+
),
698+
)
690699
},
691700
)?;
692701
inst.func_wrap(
@@ -976,7 +985,9 @@ pub mod foo {
976985
wasmtime::component::ResourceType::host::<A>(),
977986
move |mut store, rep| -> wasmtime::Result<()> {
978987
let resource = wasmtime::component::Resource::new_own(rep);
979-
HostA::drop(&mut host_getter(store.data_mut()), resource)
988+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
989+
HostA::drop(&mut host_getter(store.data_mut()), resource),
990+
)
980991
},
981992
)?;
982993
Ok(())
@@ -1118,7 +1129,9 @@ pub mod foo {
11181129
wasmtime::component::ResourceType::host::<Foo>(),
11191130
move |mut store, rep| -> wasmtime::Result<()> {
11201131
let resource = wasmtime::component::Resource::new_own(rep);
1121-
HostFoo::drop(&mut host_getter(store.data_mut()), resource)
1132+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
1133+
HostFoo::drop(&mut host_getter(store.data_mut()), resource),
1134+
)
11221135
},
11231136
)?;
11241137
Ok(())

crates/component-macro/tests/expanded/resources-import_async.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,13 @@ const _: () = {
292292
wasmtime::component::ResourceType::host::<WorldResource>(),
293293
move |mut store, rep| {
294294
wasmtime::component::__internal::Box::new(async move {
295-
HostWorldResource::drop(
296-
&mut host_getter(store.data_mut()),
297-
wasmtime::component::Resource::new_own(rep),
298-
)
299-
.await
295+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
296+
HostWorldResource::drop(
297+
&mut host_getter(store.data_mut()),
298+
wasmtime::component::Resource::new_own(rep),
299+
)
300+
.await,
301+
)
300302
})
301303
},
302304
)?;
@@ -789,11 +791,13 @@ pub mod foo {
789791
wasmtime::component::ResourceType::host::<Bar>(),
790792
move |mut store, rep| {
791793
wasmtime::component::__internal::Box::new(async move {
792-
HostBar::drop(
793-
&mut host_getter(store.data_mut()),
794-
wasmtime::component::Resource::new_own(rep),
795-
)
796-
.await
794+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
795+
HostBar::drop(
796+
&mut host_getter(store.data_mut()),
797+
wasmtime::component::Resource::new_own(rep),
798+
)
799+
.await,
800+
)
797801
})
798802
},
799803
)?;
@@ -802,11 +806,13 @@ pub mod foo {
802806
wasmtime::component::ResourceType::host::<Fallible>(),
803807
move |mut store, rep| {
804808
wasmtime::component::__internal::Box::new(async move {
805-
HostFallible::drop(
806-
&mut host_getter(store.data_mut()),
807-
wasmtime::component::Resource::new_own(rep),
808-
)
809-
.await
809+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
810+
HostFallible::drop(
811+
&mut host_getter(store.data_mut()),
812+
wasmtime::component::Resource::new_own(rep),
813+
)
814+
.await,
815+
)
810816
})
811817
},
812818
)?;
@@ -1143,11 +1149,13 @@ pub mod foo {
11431149
wasmtime::component::ResourceType::host::<A>(),
11441150
move |mut store, rep| {
11451151
wasmtime::component::__internal::Box::new(async move {
1146-
HostA::drop(
1147-
&mut host_getter(store.data_mut()),
1148-
wasmtime::component::Resource::new_own(rep),
1149-
)
1150-
.await
1152+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
1153+
HostA::drop(
1154+
&mut host_getter(store.data_mut()),
1155+
wasmtime::component::Resource::new_own(rep),
1156+
)
1157+
.await,
1158+
)
11511159
})
11521160
},
11531161
)?;
@@ -1300,11 +1308,13 @@ pub mod foo {
13001308
wasmtime::component::ResourceType::host::<Foo>(),
13011309
move |mut store, rep| {
13021310
wasmtime::component::__internal::Box::new(async move {
1303-
HostFoo::drop(
1304-
&mut host_getter(store.data_mut()),
1305-
wasmtime::component::Resource::new_own(rep),
1306-
)
1307-
.await
1311+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
1312+
HostFoo::drop(
1313+
&mut host_getter(store.data_mut()),
1314+
wasmtime::component::Resource::new_own(rep),
1315+
)
1316+
.await,
1317+
)
13081318
})
13091319
},
13101320
)?;

crates/component-macro/tests/expanded/resources-import_concurrent.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,13 @@ const _: () = {
258258
move |caller: &wasmtime::component::Accessor<T>, rep| {
259259
wasmtime::component::__internal::Box::pin(async move {
260260
let accessor = &caller.with_getter(host_getter);
261-
HostWorldResourceWithStore::drop(
262-
accessor,
263-
wasmtime::component::Resource::new_own(rep),
264-
)
265-
.await
261+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
262+
HostWorldResourceWithStore::drop(
263+
accessor,
264+
wasmtime::component::Resource::new_own(rep),
265+
)
266+
.await,
267+
)
266268
})
267269
},
268270
)?;
@@ -586,11 +588,13 @@ pub mod foo {
586588
move |caller: &wasmtime::component::Accessor<T>, rep| {
587589
wasmtime::component::__internal::Box::pin(async move {
588590
let accessor = &caller.with_getter(host_getter);
589-
HostBarWithStore::drop(
590-
accessor,
591-
wasmtime::component::Resource::new_own(rep),
592-
)
593-
.await
591+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
592+
HostBarWithStore::drop(
593+
accessor,
594+
wasmtime::component::Resource::new_own(rep),
595+
)
596+
.await,
597+
)
594598
})
595599
},
596600
)?;
@@ -600,11 +604,13 @@ pub mod foo {
600604
move |caller: &wasmtime::component::Accessor<T>, rep| {
601605
wasmtime::component::__internal::Box::pin(async move {
602606
let accessor = &caller.with_getter(host_getter);
603-
HostFallibleWithStore::drop(
604-
accessor,
605-
wasmtime::component::Resource::new_own(rep),
606-
)
607-
.await
607+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
608+
HostFallibleWithStore::drop(
609+
accessor,
610+
wasmtime::component::Resource::new_own(rep),
611+
)
612+
.await,
613+
)
608614
})
609615
},
610616
)?;
@@ -947,11 +953,13 @@ pub mod foo {
947953
move |caller: &wasmtime::component::Accessor<T>, rep| {
948954
wasmtime::component::__internal::Box::pin(async move {
949955
let accessor = &caller.with_getter(host_getter);
950-
HostAWithStore::drop(
951-
accessor,
952-
wasmtime::component::Resource::new_own(rep),
953-
)
954-
.await
956+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
957+
HostAWithStore::drop(
958+
accessor,
959+
wasmtime::component::Resource::new_own(rep),
960+
)
961+
.await,
962+
)
955963
})
956964
},
957965
)?;
@@ -1084,11 +1092,13 @@ pub mod foo {
10841092
move |caller: &wasmtime::component::Accessor<T>, rep| {
10851093
wasmtime::component::__internal::Box::pin(async move {
10861094
let accessor = &caller.with_getter(host_getter);
1087-
HostFooWithStore::drop(
1088-
accessor,
1089-
wasmtime::component::Resource::new_own(rep),
1090-
)
1091-
.await
1095+
wasmtime::ToWasmtimeResult::to_wasmtime_result(
1096+
HostFooWithStore::drop(
1097+
accessor,
1098+
wasmtime::component::Resource::new_own(rep),
1099+
)
1100+
.await,
1101+
)
10921102
})
10931103
},
10941104
)?;

0 commit comments

Comments
 (0)