1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `the-world`. 3 /// 4 /// This structure is created through [`TheWorldPre::new`] which 5 /// takes a [`InstancePre`](wasmtime::component::InstancePre) that 6 /// has been created through a [`Linker`](wasmtime::component::Linker). 7 /// 8 /// For more information see [`TheWorld`] as well. 9 pub struct TheWorldPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: TheWorldIndices, 12 } 13 impl<T: 'static> Clone for TheWorldPre<T> { 14 fn clone(&self) -> Self { 15 Self { 16 instance_pre: self.instance_pre.clone(), 17 indices: self.indices.clone(), 18 } 19 } 20 } 21 impl<_T: 'static> TheWorldPre<_T> { 22 /// Creates a new copy of `TheWorldPre` bindings which can then 23 /// be used to instantiate into a particular store. 24 /// 25 /// This method may fail if the component behind `instance_pre` 26 /// does not have the required exports. 27 pub fn new( 28 instance_pre: wasmtime::component::InstancePre<_T>, 29 ) -> wasmtime::Result<Self> { 30 let indices = TheWorldIndices::new(&instance_pre)?; 31 Ok(Self { instance_pre, indices }) 32 } 33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } 36 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37 &self.instance_pre 38 } 39 /// Instantiates a new instance of [`TheWorld`] within the 40 /// `store` provided. 41 /// 42 /// This function will use `self` as the pre-instantiated 43 /// instance to perform instantiation. Afterwards the preloaded 44 /// indices in `self` are used to lookup all exports on the 45 /// resulting instance. 46 pub fn instantiate( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<TheWorld> { 50 let mut store = store.as_context_mut(); 51 let instance = self.instance_pre.instantiate(&mut store)?; 52 self.indices.load(&mut store, &instance) 53 } 54 } 55 impl<_T: Send + 'static> TheWorldPre<_T> { 56 /// Same as [`Self::instantiate`], except with `async`. 57 pub async fn instantiate_async( 58 &self, 59 mut store: impl wasmtime::AsContextMut<Data = _T>, 60 ) -> wasmtime::Result<TheWorld> { 61 let mut store = store.as_context_mut(); 62 let instance = self.instance_pre.instantiate_async(&mut store).await?; 63 self.indices.load(&mut store, &instance) 64 } 65 } 66 /// Auto-generated bindings for index of the exports of 67 /// `the-world`. 68 /// 69 /// This is an implementation detail of [`TheWorldPre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`TheWorld`] as well. 73 #[derive(Clone)] 74 pub struct TheWorldIndices { 75 interface0: exports::foo::foo::strings::GuestIndices, 76 } 77 /// Auto-generated bindings for an instance a component which 78 /// implements the world `the-world`. 79 /// 80 /// This structure can be created through a number of means 81 /// depending on your requirements and what you have on hand: 82 /// 83 /// * The most convenient way is to use 84 /// [`TheWorld::instantiate`] which only needs a 85 /// [`Store`], [`Component`], and [`Linker`]. 86 /// 87 /// * Alternatively you can create a [`TheWorldPre`] ahead of 88 /// time with a [`Component`] to front-load string lookups 89 /// of exports once instead of per-instantiation. This 90 /// method then uses [`TheWorldPre::instantiate`] to 91 /// create a [`TheWorld`]. 92 /// 93 /// * If you've instantiated the instance yourself already 94 /// then you can use [`TheWorld::new`]. 95 /// 96 /// These methods are all equivalent to one another and move 97 /// around the tradeoff of what work is performed when. 98 /// 99 /// [`Store`]: wasmtime::Store 100 /// [`Component`]: wasmtime::component::Component 101 /// [`Linker`]: wasmtime::component::Linker 102 pub struct TheWorld { 103 interface0: exports::foo::foo::strings::Guest, 104 } 105 const _: () = { 106 #[allow(unused_imports)] 107 use wasmtime::component::__internal::anyhow; 108 impl TheWorldIndices { 109 /// Creates a new copy of `TheWorldIndices` bindings which can then 110 /// be used to instantiate into a particular store. 111 /// 112 /// This method may fail if the component does not have the 113 /// required exports. 114 pub fn new<_T>( 115 _instance_pre: &wasmtime::component::InstancePre<_T>, 116 ) -> wasmtime::Result<Self> { 117 let _component = _instance_pre.component(); 118 let _instance_type = _instance_pre.instance_type(); 119 let interface0 = exports::foo::foo::strings::GuestIndices::new( 120 _instance_pre, 121 )?; 122 Ok(TheWorldIndices { interface0 }) 123 } 124 /// Uses the indices stored in `self` to load an instance 125 /// of [`TheWorld`] from the instance provided. 126 /// 127 /// Note that at this time this method will additionally 128 /// perform type-checks of all exports. 129 pub fn load( 130 &self, 131 mut store: impl wasmtime::AsContextMut, 132 instance: &wasmtime::component::Instance, 133 ) -> wasmtime::Result<TheWorld> { 134 let _ = &mut store; 135 let _instance = instance; 136 let interface0 = self.interface0.load(&mut store, &_instance)?; 137 Ok(TheWorld { interface0 }) 138 } 139 } 140 impl TheWorld { 141 /// Convenience wrapper around [`TheWorldPre::new`] and 142 /// [`TheWorldPre::instantiate`]. 143 pub fn instantiate<_T>( 144 store: impl wasmtime::AsContextMut<Data = _T>, 145 component: &wasmtime::component::Component, 146 linker: &wasmtime::component::Linker<_T>, 147 ) -> wasmtime::Result<TheWorld> { 148 let pre = linker.instantiate_pre(component)?; 149 TheWorldPre::new(pre)?.instantiate(store) 150 } 151 /// Convenience wrapper around [`TheWorldIndices::new`] and 152 /// [`TheWorldIndices::load`]. 153 pub fn new( 154 mut store: impl wasmtime::AsContextMut, 155 instance: &wasmtime::component::Instance, 156 ) -> wasmtime::Result<TheWorld> { 157 let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; 158 indices.load(&mut store, instance) 159 } 160 /// Convenience wrapper around [`TheWorldPre::new`] and 161 /// [`TheWorldPre::instantiate_async`]. 162 pub async fn instantiate_async<_T>( 163 store: impl wasmtime::AsContextMut<Data = _T>, 164 component: &wasmtime::component::Component, 165 linker: &wasmtime::component::Linker<_T>, 166 ) -> wasmtime::Result<TheWorld> 167 where 168 _T: Send, 169 { 170 let pre = linker.instantiate_pre(component)?; 171 TheWorldPre::new(pre)?.instantiate_async(store).await 172 } 173 pub fn add_to_linker<T, D>( 174 linker: &mut wasmtime::component::Linker<T>, 175 host_getter: fn(&mut T) -> D::Data<'_>, 176 ) -> wasmtime::Result<()> 177 where 178 D: foo::foo::strings::HostWithStore, 179 for<'a> D::Data<'a>: foo::foo::strings::Host, 180 T: 'static, 181 { 182 foo::foo::strings::add_to_linker::<T, D>(linker, host_getter)?; 183 Ok(()) 184 } 185 pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { 186 &self.interface0 187 } 188 } 189 }; 190 pub mod foo { 191 pub mod foo { 192 #[allow(clippy::all)] 193 pub mod strings { 194 #[allow(unused_imports)] 195 use wasmtime::component::__internal::{anyhow, Box}; 196 pub trait HostWithStore: wasmtime::component::HasData {} 197 impl<_T: ?Sized> HostWithStore for _T 198 where 199 _T: wasmtime::component::HasData, 200 {} 201 pub trait Host { 202 fn a(&mut self, x: wasmtime::component::__internal::String) -> (); 203 fn b(&mut self) -> wasmtime::component::__internal::String; 204 fn c( 205 &mut self, 206 a: wasmtime::component::__internal::String, 207 b: wasmtime::component::__internal::String, 208 ) -> wasmtime::component::__internal::String; 209 } 210 impl<_T: Host + ?Sized> Host for &mut _T { 211 fn a(&mut self, x: wasmtime::component::__internal::String) -> () { 212 Host::a(*self, x) 213 } 214 fn b(&mut self) -> wasmtime::component::__internal::String { 215 Host::b(*self) 216 } 217 fn c( 218 &mut self, 219 a: wasmtime::component::__internal::String, 220 b: wasmtime::component::__internal::String, 221 ) -> wasmtime::component::__internal::String { 222 Host::c(*self, a, b) 223 } 224 } 225 pub fn add_to_linker<T, D>( 226 linker: &mut wasmtime::component::Linker<T>, 227 host_getter: fn(&mut T) -> D::Data<'_>, 228 ) -> wasmtime::Result<()> 229 where 230 D: HostWithStore, 231 for<'a> D::Data<'a>: Host, 232 T: 'static, 233 { 234 let mut inst = linker.instance("foo:foo/strings")?; 235 inst.func_wrap( 236 "a", 237 move | 238 mut caller: wasmtime::StoreContextMut<'_, T>, 239 (arg0,): (wasmtime::component::__internal::String,)| 240 { 241 let host = &mut host_getter(caller.data_mut()); 242 let r = Host::a(host, arg0); 243 Ok(r) 244 }, 245 )?; 246 inst.func_wrap( 247 "b", 248 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 249 let host = &mut host_getter(caller.data_mut()); 250 let r = Host::b(host); 251 Ok((r,)) 252 }, 253 )?; 254 inst.func_wrap( 255 "c", 256 move | 257 mut caller: wasmtime::StoreContextMut<'_, T>, 258 ( 259 arg0, 260 arg1, 261 ): ( 262 wasmtime::component::__internal::String, 263 wasmtime::component::__internal::String, 264 )| 265 { 266 let host = &mut host_getter(caller.data_mut()); 267 let r = Host::c(host, arg0, arg1); 268 Ok((r,)) 269 }, 270 )?; 271 Ok(()) 272 } 273 } 274 } 275 } 276 pub mod exports { 277 pub mod foo { 278 pub mod foo { 279 #[allow(clippy::all)] 280 pub mod strings { 281 #[allow(unused_imports)] 282 use wasmtime::component::__internal::{anyhow, Box}; 283 pub struct Guest { 284 a: wasmtime::component::Func, 285 b: wasmtime::component::Func, 286 c: wasmtime::component::Func, 287 } 288 #[derive(Clone)] 289 pub struct GuestIndices { 290 a: wasmtime::component::ComponentExportIndex, 291 b: wasmtime::component::ComponentExportIndex, 292 c: wasmtime::component::ComponentExportIndex, 293 } 294 impl GuestIndices { 295 /// Constructor for [`GuestIndices`] which takes a 296 /// [`Component`](wasmtime::component::Component) as input and can be executed 297 /// before instantiation. 298 /// 299 /// This constructor can be used to front-load string lookups to find exports 300 /// within a component. 301 pub fn new<_T>( 302 _instance_pre: &wasmtime::component::InstancePre<_T>, 303 ) -> wasmtime::Result<GuestIndices> { 304 let instance = _instance_pre 305 .component() 306 .get_export_index(None, "foo:foo/strings") 307 .ok_or_else(|| { 308 anyhow::anyhow!( 309 "no exported instance named `foo:foo/strings`" 310 ) 311 })?; 312 let mut lookup = move |name| { 313 _instance_pre 314 .component() 315 .get_export_index(Some(&instance), name) 316 .ok_or_else(|| { 317 anyhow::anyhow!( 318 "instance export `foo:foo/strings` does \ 319 not have export `{name}`" 320 ) 321 }) 322 }; 323 let _ = &mut lookup; 324 let a = lookup("a")?; 325 let b = lookup("b")?; 326 let c = lookup("c")?; 327 Ok(GuestIndices { a, b, c }) 328 } 329 pub fn load( 330 &self, 331 mut store: impl wasmtime::AsContextMut, 332 instance: &wasmtime::component::Instance, 333 ) -> wasmtime::Result<Guest> { 334 let _instance = instance; 335 let _instance_pre = _instance.instance_pre(&store); 336 let _instance_type = _instance_pre.instance_type(); 337 let mut store = store.as_context_mut(); 338 let _ = &mut store; 339 let a = *_instance 340 .get_typed_func::<(&str,), ()>(&mut store, &self.a)? 341 .func(); 342 let b = *_instance 343 .get_typed_func::< 344 (), 345 (wasmtime::component::__internal::String,), 346 >(&mut store, &self.b)? 347 .func(); 348 let c = *_instance 349 .get_typed_func::< 350 (&str, &str), 351 (wasmtime::component::__internal::String,), 352 >(&mut store, &self.c)? 353 .func(); 354 Ok(Guest { a, b, c }) 355 } 356 } 357 impl Guest { 358 pub fn call_a<S: wasmtime::AsContextMut>( 359 &self, 360 mut store: S, 361 arg0: &str, 362 ) -> wasmtime::Result<()> { 363 let callee = unsafe { 364 wasmtime::component::TypedFunc::< 365 (&str,), 366 (), 367 >::new_unchecked(self.a) 368 }; 369 let () = callee.call(store.as_context_mut(), (arg0,))?; 370 callee.post_return(store.as_context_mut())?; 371 Ok(()) 372 } 373 pub fn call_b<S: wasmtime::AsContextMut>( 374 &self, 375 mut store: S, 376 ) -> wasmtime::Result<wasmtime::component::__internal::String> { 377 let callee = unsafe { 378 wasmtime::component::TypedFunc::< 379 (), 380 (wasmtime::component::__internal::String,), 381 >::new_unchecked(self.b) 382 }; 383 let (ret0,) = callee.call(store.as_context_mut(), ())?; 384 callee.post_return(store.as_context_mut())?; 385 Ok(ret0) 386 } 387 pub fn call_c<S: wasmtime::AsContextMut>( 388 &self, 389 mut store: S, 390 arg0: &str, 391 arg1: &str, 392 ) -> wasmtime::Result<wasmtime::component::__internal::String> { 393 let callee = unsafe { 394 wasmtime::component::TypedFunc::< 395 (&str, &str), 396 (wasmtime::component::__internal::String,), 397 >::new_unchecked(self.c) 398 }; 399 let (ret0,) = callee.call(store.as_context_mut(), (arg0, arg1))?; 400 callee.post_return(store.as_context_mut())?; 401 Ok(ret0) 402 } 403 } 404 } 405 } 406 } 407 } 408