Path: blob/main/tests/all/component_model/missing_async.rs
3064 views
#![cfg(not(miri))] // not testing unsafe code12use wasmtime::component::{Component, Func, Linker, ResourceAny};3use wasmtime::{Config, Engine, Result, Store};45fn async_store() -> Store<()> {6let engine = Engine::default();7let mut store = Store::new(&engine, ());8wasmtime::Func::wrap_async(&mut store, |_, ()| Box::new(async {}));9return store;10}1112async fn func_in_store(store: &mut Store<()>) -> Result<Func> {13let component = Component::new(14store.engine(),15r#"16(component17(core module $a18(func (export "hi") )19)20(core instance $i (instantiate $a))21(func (export "hi") (canon lift (core func $i "hi")))22)23"#,24)?;25let instance = Linker::new(store.engine())26.instantiate_async(&mut *store, &component)27.await?;28let func = instance.get_func(&mut *store, "hi").unwrap();29Ok(func)30}3132fn assert_requires_async<T>(store: &mut Store<T>) {33let module = wasmtime::Module::new(store.engine(), "(module)").unwrap();34assert!(wasmtime::Instance::new(&mut *store, &module, &[]).is_err());35}3637#[tokio::test]38async fn async_disallows_func_call() -> Result<()> {39let mut store = async_store();40let func = func_in_store(&mut store).await?;41assert!(func.call(&mut store, &[], &mut []).is_err());42func.call_async(&mut store, &[], &mut []).await?;43Ok(())44}4546#[tokio::test]47async fn async_disallows_typed_func_call() -> Result<()> {48let mut store = async_store();49let func = func_in_store(&mut store).await?;50let func = func.typed::<(), ()>(&store)?;51assert!(func.call(&mut store, ()).is_err());52func.call_async(&mut store, ()).await?;53Ok(())54}5556#[tokio::test]57async fn async_disallows_instantiate() -> Result<()> {58let mut store = async_store();59let component = Component::new(store.engine(), "(component)")?;60let linker = Linker::new(store.engine());61assert!(linker.instantiate(&mut store, &component).is_err());62linker.instantiate_async(&mut store, &component).await?;63Ok(())64}6566#[tokio::test]67async fn require_async_after_linker_with_func_wrap_async() -> Result<()> {68let engine = Engine::default();69let mut store = Store::new(&engine, ());70let mut linker = Linker::new(store.engine());71linker72.root()73.func_wrap_async("hi", |_, ()| Box::new(async { Ok(()) }))?;74let module = Component::new(75store.engine(),76r#"77(component78(import "hi" (func))79(core func (canon lower (func 0)))80)81"#,82)?;83linker.instantiate_async(&mut store, &module).await?;84assert_requires_async(&mut store);85Ok(())86}8788#[tokio::test]89async fn require_async_after_linker_with_func_wrap_concurrent() -> Result<()> {90let mut config = Config::new();91config.wasm_component_model_async(true);92let engine = Engine::new(&config)?;93let mut store = Store::new(&engine, ());94let mut linker = Linker::new(store.engine());95linker96.root()97.func_wrap_concurrent("hi", |_, ()| Box::pin(async { Ok(()) }))?;98let module = Component::new(99store.engine(),100r#"101(component102(import "hi" (func async))103(core func (canon lower (func 0)))104)105"#,106)?;107linker.instantiate_async(&mut store, &module).await?;108assert_requires_async(&mut store);109Ok(())110}111112#[tokio::test]113async fn require_async_after_linker_with_func_new_async() -> Result<()> {114let engine = Engine::default();115let mut store = Store::new(&engine, ());116let mut linker = Linker::new(store.engine());117linker118.root()119.func_new_async("hi", |_, _, _, _| Box::new(async { Ok(()) }))?;120let module = Component::new(121store.engine(),122r#"123(component124(import "hi" (func))125(core func (canon lower (func 0)))126)127"#,128)?;129linker.instantiate_async(&mut store, &module).await?;130assert_requires_async(&mut store);131Ok(())132}133134#[tokio::test]135async fn require_async_after_linker_with_func_new_concurrent() -> Result<()> {136let mut config = Config::new();137config.wasm_component_model_async(true);138let engine = Engine::new(&config)?;139let mut store = Store::new(&engine, ());140let mut linker = Linker::new(store.engine());141linker142.root()143.func_new_concurrent("hi", |_, _, _, _| Box::pin(async { Ok(()) }))?;144let module = Component::new(145store.engine(),146r#"147(component148(import "hi" (func async))149(core func (canon lower (func 0)))150)151"#,152)?;153linker.instantiate_async(&mut store, &module).await?;154assert_requires_async(&mut store);155Ok(())156}157158#[tokio::test]159async fn async_disallows_resource_any_drop() -> Result<()> {160let mut store = async_store();161let component = Component::new(162store.engine(),163r#"164(component165(type $t' (resource (rep i32)))166(export $t "t" (type $t'))167168(core func $new (canon resource.new $t))169(func (export "mk") (param "r" u32) (result (own $t))170(canon lift (core func $new))171)172)173"#,174)?;175let instance = Linker::new(store.engine())176.instantiate_async(&mut store, &component)177.await?;178let func = instance.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "mk")?;179let (resource,) = func.call_async(&mut store, (42,)).await?;180181assert!(resource.resource_drop(&mut store).is_err());182resource.resource_drop_async(&mut store).await?;183184Ok(())185}186187188