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