| 3020 | } |
| 3021 | |
| 3022 | fn test_recurse(kind: RecurseKind) -> Result<()> { |
| 3023 | #[derive(Default)] |
| 3024 | struct Ctx { |
| 3025 | instances: Vec<Arc<Instance>>, |
| 3026 | } |
| 3027 | |
| 3028 | let component = r#" |
| 3029 | (component |
| 3030 | (import "import" (func $import)) |
| 3031 | (core func $import (canon lower (func $import))) |
| 3032 | (core module $m |
| 3033 | (func $import (import "" "import")) |
| 3034 | (func (export "export") call $import) |
| 3035 | ) |
| 3036 | (core instance $m (instantiate $m (with "" (instance |
| 3037 | (export "import" (func $import)) |
| 3038 | )))) |
| 3039 | (func (export "export") (canon lift (core func $m "export"))) |
| 3040 | ) |
| 3041 | "#; |
| 3042 | let engine = super::engine(); |
| 3043 | let component = Component::new(&engine, component)?; |
| 3044 | let mut store = Store::new(&engine, Ctx::default()); |
| 3045 | let mut linker = Linker::<Ctx>::new(&engine); |
| 3046 | linker.root().func_wrap("import", |mut store, (): ()| { |
| 3047 | if let Some(instance) = store.data_mut().instances.pop() { |
| 3048 | let run = instance.get_typed_func::<(), ()>(&mut store, "export")?; |
| 3049 | run.call(&mut store, ())?; |
| 3050 | store.data_mut().instances.push(instance); |
| 3051 | } |
| 3052 | Ok(()) |
| 3053 | })?; |
| 3054 | let instance = Arc::new(linker.instantiate(&mut store, &component)?); |
| 3055 | let instance = match kind { |
| 3056 | RecurseKind::AThenA => { |
| 3057 | store.data_mut().instances.push(instance.clone()); |
| 3058 | instance |
| 3059 | } |
| 3060 | RecurseKind::AThenB => { |
| 3061 | let other = Arc::new(linker.instantiate(&mut store, &component)?); |
| 3062 | store.data_mut().instances.push(other); |
| 3063 | instance |
| 3064 | } |
| 3065 | RecurseKind::AThenBThenA => { |
| 3066 | store.data_mut().instances.push(instance.clone()); |
| 3067 | let other = Arc::new(linker.instantiate(&mut store, &component)?); |
| 3068 | store.data_mut().instances.push(other); |
| 3069 | instance |
| 3070 | } |
| 3071 | }; |
| 3072 | |
| 3073 | let export = instance.get_typed_func::<(), ()>(&mut store, "export")?; |
| 3074 | export.call(&mut store, ())?; |
| 3075 | Ok(()) |
| 3076 | } |
| 3077 | |
| 3078 | #[tokio::test] |
| 3079 | async fn thread_index_via_instantiation_sync() -> Result<()> { |