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