Skip to content

Commit d85b0ec

Browse files
authored
Revert part of #15000, allow mixing imports/exports (#15238)
The validation added in #15000 was unfortunately a bit too eager in the classification of sync/async and disallowed valid configurations. An example is in spinframework/spin#3773 there are async imports and sync exports, but the exports are all bindgen'd with `async`. This commit reverts the validation/error from #15000 and adds a small test to ensure that this keeps working in the future.
1 parent c839c07 commit d85b0ec

2 files changed

Lines changed: 94 additions & 64 deletions

File tree

‎crates/component-macro/src/bindgen.rs‎

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syn::{Token, braced, token};
1010
use wasmtime_wit_bindgen::{
1111
FunctionConfig, FunctionFilter, FunctionFlags, Opts, Ownership, TrappableError,
1212
};
13-
use wit_parser::{Function, IndexMap, PackageId, Resolve, WorldId, WorldItem, WorldKey};
13+
use wit_parser::{PackageId, Resolve, WorldId};
1414

1515
pub struct Config {
1616
opts: Opts,
@@ -30,8 +30,6 @@ pub fn expand(input: &mut Config) -> Result<TokenStream> {
3030
return Ok(quote::quote!(#src));
3131
}
3232

33-
check_for_sync_exports_and_async_imports(input)?;
34-
3533
// If debugging is enabled then place a formatted version of the expanded
3634
// code into a file. This file will then show up in rustc error messages for
3735
// any codegen issues and can be inspected manually.
@@ -140,67 +138,6 @@ impl Config {
140138
}
141139
}
142140

143-
fn check_for_sync_exports_and_async_imports(input: &Config) -> Result<()> {
144-
let world = &input.resolve.worlds[input.world];
145-
let async_imports =
146-
filter_function_names(&input.resolve, &world.imports, |f| f.kind.is_async());
147-
let sync_exports =
148-
filter_function_names(&input.resolve, &world.exports, |f| !f.kind.is_async());
149-
150-
if !sync_exports.is_empty() && !async_imports.is_empty() {
151-
let format_list = |names: &[String]| {
152-
names
153-
.iter()
154-
.map(|n| format!(" - {n}\n"))
155-
.collect::<String>()
156-
};
157-
158-
return Err(Error::new(
159-
Span::call_site(),
160-
format!(
161-
"World `{world}` mixes synchronous exports with asynchronous imports, which is not allowed.\n\
162-
\n\
163-
synchronous exports:\n{sync_exports}\n\
164-
asynchronous imports:\n{async_imports}\n\
165-
help: This error will resolve if either all imports are non-async or all exports \
166-
are async. For more details, see the async bindgen example:\n\
167-
https://docs.wasmtime.dev/api/wasmtime/component/bindgen_examples/_7_async/index.html",
168-
world = world.name,
169-
sync_exports = format_list(&sync_exports),
170-
async_imports = format_list(&async_imports),
171-
),
172-
));
173-
}
174-
175-
Ok(())
176-
}
177-
178-
/// Get a list of world and interface-level function names that match some predicate
179-
fn filter_function_names(
180-
resolve: &Resolve,
181-
items: &IndexMap<WorldKey, WorldItem>,
182-
pred: impl Fn(&Function) -> bool,
183-
) -> Vec<String> {
184-
let mut names = vec![];
185-
for (key, item) in items {
186-
match item {
187-
WorldItem::Function(f) if pred(f) => names.push(f.name.clone()),
188-
WorldItem::Interface { id, .. } => {
189-
let iface = resolve.name_world_key(key);
190-
names.extend(
191-
resolve.interfaces[*id]
192-
.functions
193-
.values()
194-
.filter(|f| pred(f))
195-
.map(|f| format!("{iface}.{}", f.name)),
196-
);
197-
}
198-
_ => {}
199-
}
200-
}
201-
names
202-
}
203-
204141
impl Parse for Config {
205142
fn parse(input: ParseStream<'_>) -> Result<Self> {
206143
let call_site = Span::call_site();

‎tests/all/component_model/bindgen.rs‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,99 @@ mod one_import_concurrent {
292292
}
293293
}
294294

295+
mod sync_export_async_import {
296+
use super::*;
297+
use wasmtime::Trap;
298+
use wasmtime::component::{Accessor, HasData};
299+
300+
wasmtime::component::bindgen!({
301+
inline: "
302+
package foo:foo;
303+
304+
world sync-export-async-import {
305+
import foo: interface {
306+
foo: async func(block: bool);
307+
}
308+
309+
export bar: func(block: bool);
310+
}
311+
",
312+
exports: { default: async },
313+
});
314+
315+
const COMPONENT: &str = r#"
316+
(component
317+
(import "foo" (instance $foo-instance
318+
(export "foo" (func async (param "block" bool)))
319+
))
320+
(core func $foo (canon lower (func $foo-instance "foo")))
321+
(core module $m
322+
(import "" "foo" (func $foo (param i32)))
323+
(func (export "bar") (param i32)
324+
local.get 0
325+
call $foo
326+
)
327+
)
328+
(core instance $i (instantiate $m
329+
(with "" (instance (export "foo" (func $foo))))
330+
))
331+
(func (export "bar") (param "block" bool)
332+
(canon lift (core func $i "bar"))
333+
)
334+
)
335+
"#;
336+
337+
#[derive(Default)]
338+
struct MyImports {
339+
hit: bool,
340+
}
341+
342+
impl HasData for MyImports {
343+
type Data<'a> = &'a mut MyImports;
344+
}
345+
346+
impl<T> foo::HostWithStore<T> for MyImports {
347+
async fn foo(accessor: &Accessor<T, Self>, block: bool) {
348+
if block {
349+
std::future::pending::<()>().await;
350+
}
351+
accessor.with(|mut view| view.get().hit = true);
352+
}
353+
}
354+
355+
impl foo::Host for MyImports {}
356+
357+
async fn setup() -> Result<(Store<MyImports>, SyncExportAsyncImport)> {
358+
let mut config = Config::new();
359+
config.wasm_component_model_async(true);
360+
let engine = Engine::new(&config)?;
361+
let component = Component::new(&engine, COMPONENT)?;
362+
let mut linker = Linker::new(&engine);
363+
foo::add_to_linker::<_, MyImports>(&mut linker, |x| x)?;
364+
let mut store = Store::new(&engine, MyImports::default());
365+
let instance =
366+
SyncExportAsyncImport::instantiate_async(&mut store, &component, &linker).await?;
367+
Ok((store, instance))
368+
}
369+
370+
#[tokio::test]
371+
async fn non_blocking() -> Result<()> {
372+
let (mut store, instance) = setup().await?;
373+
instance.call_bar(&mut store, false).await?;
374+
assert!(store.data().hit);
375+
Ok(())
376+
}
377+
378+
#[tokio::test]
379+
async fn blocking_traps() -> Result<()> {
380+
let (mut store, instance) = setup().await?;
381+
let err = instance.call_bar(&mut store, true).await.unwrap_err();
382+
assert_eq!(err.downcast_ref::<Trap>(), Some(&Trap::CannotBlockSyncTask));
383+
assert!(!store.data().hit);
384+
Ok(())
385+
}
386+
}
387+
295388
mod resources_at_world_level {
296389
use super::*;
297390
use wasmtime::component::{HasSelf, Resource};

0 commit comments

Comments
 (0)