1 /// Auto-generated bindings for a pre-instantiated version of a 2 /// component which implements the world `wasi`. 3 /// 4 /// This structure is created through [`WasiPre::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 [`Wasi`] as well. 9 pub struct WasiPre<T: 'static> { 10 instance_pre: wasmtime::component::InstancePre<T>, 11 indices: WasiIndices, 12 } 13 impl<T: 'static> Clone for WasiPre<T> { clone(&self) -> Self14 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> WasiPre<_T> { 22 /// Creates a new copy of `WasiPre` 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. new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>27 pub fn new( 28 instance_pre: wasmtime::component::InstancePre<_T>, 29 ) -> wasmtime::Result<Self> { 30 let indices = WasiIndices::new(&instance_pre)?; 31 Ok(Self { instance_pre, indices }) 32 } engine(&self) -> &wasmtime::Engine33 pub fn engine(&self) -> &wasmtime::Engine { 34 self.instance_pre.engine() 35 } instance_pre(&self) -> &wasmtime::component::InstancePre<_T>36 pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> { 37 &self.instance_pre 38 } 39 /// Instantiates a new instance of [`Wasi`] 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. instantiate( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Wasi>46 pub fn instantiate( 47 &self, 48 mut store: impl wasmtime::AsContextMut<Data = _T>, 49 ) -> wasmtime::Result<Wasi> { 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> WasiPre<_T> { 56 /// Same as [`Self::instantiate`], except with `async`. instantiate_async( &self, mut store: impl wasmtime::AsContextMut<Data = _T>, ) -> wasmtime::Result<Wasi>57 pub async fn instantiate_async( 58 &self, 59 mut store: impl wasmtime::AsContextMut<Data = _T>, 60 ) -> wasmtime::Result<Wasi> { 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 /// `wasi`. 68 /// 69 /// This is an implementation detail of [`WasiPre`] and can 70 /// be constructed if needed as well. 71 /// 72 /// For more information see [`Wasi`] as well. 73 #[derive(Clone)] 74 pub struct WasiIndices {} 75 /// Auto-generated bindings for an instance a component which 76 /// implements the world `wasi`. 77 /// 78 /// This structure can be created through a number of means 79 /// depending on your requirements and what you have on hand: 80 /// 81 /// * The most convenient way is to use 82 /// [`Wasi::instantiate`] which only needs a 83 /// [`Store`], [`Component`], and [`Linker`]. 84 /// 85 /// * Alternatively you can create a [`WasiPre`] ahead of 86 /// time with a [`Component`] to front-load string lookups 87 /// of exports once instead of per-instantiation. This 88 /// method then uses [`WasiPre::instantiate`] to 89 /// create a [`Wasi`]. 90 /// 91 /// * If you've instantiated the instance yourself already 92 /// then you can use [`Wasi::new`]. 93 /// 94 /// These methods are all equivalent to one another and move 95 /// around the tradeoff of what work is performed when. 96 /// 97 /// [`Store`]: wasmtime::Store 98 /// [`Component`]: wasmtime::component::Component 99 /// [`Linker`]: wasmtime::component::Linker 100 pub struct Wasi {} 101 const _: () = { 102 impl WasiIndices { 103 /// Creates a new copy of `WasiIndices` bindings which can then 104 /// be used to instantiate into a particular store. 105 /// 106 /// This method may fail if the component does not have the 107 /// required exports. new<_T>( _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result<Self>108 pub fn new<_T>( 109 _instance_pre: &wasmtime::component::InstancePre<_T>, 110 ) -> wasmtime::Result<Self> { 111 let _component = _instance_pre.component(); 112 let _instance_type = _instance_pre.instance_type(); 113 Ok(WasiIndices {}) 114 } 115 /// Uses the indices stored in `self` to load an instance 116 /// of [`Wasi`] from the instance provided. 117 /// 118 /// Note that at this time this method will additionally 119 /// perform type-checks of all exports. load( &self, mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Wasi>120 pub fn load( 121 &self, 122 mut store: impl wasmtime::AsContextMut, 123 instance: &wasmtime::component::Instance, 124 ) -> wasmtime::Result<Wasi> { 125 let _ = &mut store; 126 let _instance = instance; 127 Ok(Wasi {}) 128 } 129 } 130 impl Wasi { 131 /// Convenience wrapper around [`WasiPre::new`] and 132 /// [`WasiPre::instantiate`]. instantiate<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Wasi>133 pub fn instantiate<_T>( 134 store: impl wasmtime::AsContextMut<Data = _T>, 135 component: &wasmtime::component::Component, 136 linker: &wasmtime::component::Linker<_T>, 137 ) -> wasmtime::Result<Wasi> { 138 let pre = linker.instantiate_pre(component)?; 139 WasiPre::new(pre)?.instantiate(store) 140 } 141 /// Convenience wrapper around [`WasiIndices::new`] and 142 /// [`WasiIndices::load`]. new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result<Wasi>143 pub fn new( 144 mut store: impl wasmtime::AsContextMut, 145 instance: &wasmtime::component::Instance, 146 ) -> wasmtime::Result<Wasi> { 147 let indices = WasiIndices::new(&instance.instance_pre(&store))?; 148 indices.load(&mut store, instance) 149 } 150 /// Convenience wrapper around [`WasiPre::new`] and 151 /// [`WasiPre::instantiate_async`]. instantiate_async<_T>( store: impl wasmtime::AsContextMut<Data = _T>, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result<Wasi> where _T: Send,152 pub async fn instantiate_async<_T>( 153 store: impl wasmtime::AsContextMut<Data = _T>, 154 component: &wasmtime::component::Component, 155 linker: &wasmtime::component::Linker<_T>, 156 ) -> wasmtime::Result<Wasi> 157 where 158 _T: Send, 159 { 160 let pre = linker.instantiate_pre(component)?; 161 WasiPre::new(pre)?.instantiate_async(store).await 162 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: foo::foo::wasi_filesystem::HostWithStore + foo::foo::wall_clock::HostWithStore + Send, for<'a> D::Data< 'a, >: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host + Send, T: 'static + Send,163 pub fn add_to_linker<T, D>( 164 linker: &mut wasmtime::component::Linker<T>, 165 host_getter: fn(&mut T) -> D::Data<'_>, 166 ) -> wasmtime::Result<()> 167 where 168 D: foo::foo::wasi_filesystem::HostWithStore 169 + foo::foo::wall_clock::HostWithStore + Send, 170 for<'a> D::Data< 171 'a, 172 >: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host + Send, 173 T: 'static + Send, 174 { 175 foo::foo::wasi_filesystem::add_to_linker::<T, D>(linker, host_getter)?; 176 foo::foo::wall_clock::add_to_linker::<T, D>(linker, host_getter)?; 177 Ok(()) 178 } 179 } 180 }; 181 pub mod foo { 182 pub mod foo { 183 #[allow(clippy::all)] 184 pub mod wasi_filesystem { 185 #[allow(unused_imports)] 186 use wasmtime::component::__internal::Box; 187 #[derive(wasmtime::component::ComponentType)] 188 #[derive(wasmtime::component::Lift)] 189 #[derive(wasmtime::component::Lower)] 190 #[component(record)] 191 #[derive(Clone, Copy)] 192 pub struct DescriptorStat {} 193 impl core::fmt::Debug for DescriptorStat { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result194 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 195 f.debug_struct("DescriptorStat").finish() 196 } 197 } 198 const _: () = { 199 assert!( 200 0 == < DescriptorStat as wasmtime::component::ComponentType >::SIZE32 201 ); 202 assert!( 203 1 == < DescriptorStat as wasmtime::component::ComponentType 204 >::ALIGN32 205 ); 206 }; 207 #[derive(wasmtime::component::ComponentType)] 208 #[derive(wasmtime::component::Lift)] 209 #[derive(wasmtime::component::Lower)] 210 #[component(enum)] 211 #[derive(Clone, Copy, Eq, PartialEq)] 212 #[repr(u8)] 213 pub enum Errno { 214 #[component(name = "e")] 215 E, 216 } 217 impl Errno { name(&self) -> &'static str218 pub fn name(&self) -> &'static str { 219 match self { 220 Errno::E => "e", 221 } 222 } message(&self) -> &'static str223 pub fn message(&self) -> &'static str { 224 match self { 225 Errno::E => "", 226 } 227 } 228 } 229 impl core::fmt::Debug for Errno { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result230 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 231 f.debug_struct("Errno") 232 .field("code", &(*self as i32)) 233 .field("name", &self.name()) 234 .field("message", &self.message()) 235 .finish() 236 } 237 } 238 impl core::fmt::Display for Errno { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result239 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 240 write!(f, "{} (error {})", self.name(), * self as i32) 241 } 242 } 243 impl core::error::Error for Errno {} 244 const _: () = { 245 assert!(1 == < Errno as wasmtime::component::ComponentType >::SIZE32); 246 assert!(1 == < Errno as wasmtime::component::ComponentType >::ALIGN32); 247 }; 248 pub trait HostWithStore: wasmtime::component::HasData + Send {} 249 impl<_T: ?Sized> HostWithStore for _T 250 where 251 _T: wasmtime::component::HasData + Send, 252 {} 253 pub trait Host: Send { create_directory_at( &mut self, ) -> impl ::core::future::Future<Output = Result<(), Errno>> + Send254 fn create_directory_at( 255 &mut self, 256 ) -> impl ::core::future::Future<Output = Result<(), Errno>> + Send; stat( &mut self, ) -> impl ::core::future::Future< Output = Result<DescriptorStat, Errno>, > + Send257 fn stat( 258 &mut self, 259 ) -> impl ::core::future::Future< 260 Output = Result<DescriptorStat, Errno>, 261 > + Send; 262 } 263 impl<_T: Host + ?Sized + Send> Host for &mut _T { create_directory_at( &mut self, ) -> impl ::core::future::Future<Output = Result<(), Errno>> + Send264 fn create_directory_at( 265 &mut self, 266 ) -> impl ::core::future::Future<Output = Result<(), Errno>> + Send { 267 async move { Host::create_directory_at(*self).await } 268 } stat( &mut self, ) -> impl ::core::future::Future< Output = Result<DescriptorStat, Errno>, > + Send269 fn stat( 270 &mut self, 271 ) -> impl ::core::future::Future< 272 Output = Result<DescriptorStat, Errno>, 273 > + Send { 274 async move { Host::stat(*self).await } 275 } 276 } add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static + Send,277 pub fn add_to_linker<T, D>( 278 linker: &mut wasmtime::component::Linker<T>, 279 host_getter: fn(&mut T) -> D::Data<'_>, 280 ) -> wasmtime::Result<()> 281 where 282 D: HostWithStore, 283 for<'a> D::Data<'a>: Host, 284 T: 'static + Send, 285 { 286 let mut inst = linker.instance("foo:foo/wasi-filesystem")?; 287 inst.func_wrap_async( 288 "create-directory-at", 289 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 290 wasmtime::component::__internal::Box::new(async move { 291 let host = &mut host_getter(caller.data_mut()); 292 let r = Host::create_directory_at(host).await; 293 Ok((r,)) 294 }) 295 }, 296 )?; 297 inst.func_wrap_async( 298 "stat", 299 move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { 300 wasmtime::component::__internal::Box::new(async move { 301 let host = &mut host_getter(caller.data_mut()); 302 let r = Host::stat(host).await; 303 Ok((r,)) 304 }) 305 }, 306 )?; 307 Ok(()) 308 } 309 } 310 #[allow(clippy::all)] 311 pub mod wall_clock { 312 #[allow(unused_imports)] 313 use wasmtime::component::__internal::Box; 314 #[derive(wasmtime::component::ComponentType)] 315 #[derive(wasmtime::component::Lift)] 316 #[derive(wasmtime::component::Lower)] 317 #[component(record)] 318 #[derive(Clone, Copy)] 319 pub struct WallClock {} 320 impl core::fmt::Debug for WallClock { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result321 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 322 f.debug_struct("WallClock").finish() 323 } 324 } 325 const _: () = { 326 assert!( 327 0 == < WallClock as wasmtime::component::ComponentType >::SIZE32 328 ); 329 assert!( 330 1 == < WallClock as wasmtime::component::ComponentType >::ALIGN32 331 ); 332 }; 333 pub trait HostWithStore: wasmtime::component::HasData {} 334 impl<_T: ?Sized> HostWithStore for _T 335 where 336 _T: wasmtime::component::HasData, 337 {} 338 pub trait Host {} 339 impl<_T: Host + ?Sized> Host for &mut _T {} add_to_linker<T, D>( linker: &mut wasmtime::component::Linker<T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where D: HostWithStore, for<'a> D::Data<'a>: Host, T: 'static,340 pub fn add_to_linker<T, D>( 341 linker: &mut wasmtime::component::Linker<T>, 342 host_getter: fn(&mut T) -> D::Data<'_>, 343 ) -> wasmtime::Result<()> 344 where 345 D: HostWithStore, 346 for<'a> D::Data<'a>: Host, 347 T: 'static, 348 { 349 let mut inst = linker.instance("foo:foo/wall-clock")?; 350 Ok(()) 351 } 352 } 353 } 354 } 355