Path: blob/main/crates/component-macro/tests/expanded/many-arguments.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::manyarg::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::manyarg::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::manyarg::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::manyarg::HostWithStore,178for<'a> D::Data<'a>: foo::foo::manyarg::Host,179T: 'static,180{181foo::foo::manyarg::add_to_linker::<T, D>(linker, host_getter)?;182Ok(())183}184pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest {185&self.interface0186}187}188};189pub mod foo {190pub mod foo {191#[allow(clippy::all)]192pub mod manyarg {193#[allow(unused_imports)]194use wasmtime::component::__internal::{anyhow, Box};195#[derive(wasmtime::component::ComponentType)]196#[derive(wasmtime::component::Lift)]197#[derive(wasmtime::component::Lower)]198#[component(record)]199#[derive(Clone)]200pub struct BigStruct {201#[component(name = "a1")]202pub a1: wasmtime::component::__internal::String,203#[component(name = "a2")]204pub a2: wasmtime::component::__internal::String,205#[component(name = "a3")]206pub a3: wasmtime::component::__internal::String,207#[component(name = "a4")]208pub a4: wasmtime::component::__internal::String,209#[component(name = "a5")]210pub a5: wasmtime::component::__internal::String,211#[component(name = "a6")]212pub a6: wasmtime::component::__internal::String,213#[component(name = "a7")]214pub a7: wasmtime::component::__internal::String,215#[component(name = "a8")]216pub a8: wasmtime::component::__internal::String,217#[component(name = "a9")]218pub a9: wasmtime::component::__internal::String,219#[component(name = "a10")]220pub a10: wasmtime::component::__internal::String,221#[component(name = "a11")]222pub a11: wasmtime::component::__internal::String,223#[component(name = "a12")]224pub a12: wasmtime::component::__internal::String,225#[component(name = "a13")]226pub a13: wasmtime::component::__internal::String,227#[component(name = "a14")]228pub a14: wasmtime::component::__internal::String,229#[component(name = "a15")]230pub a15: wasmtime::component::__internal::String,231#[component(name = "a16")]232pub a16: wasmtime::component::__internal::String,233#[component(name = "a17")]234pub a17: wasmtime::component::__internal::String,235#[component(name = "a18")]236pub a18: wasmtime::component::__internal::String,237#[component(name = "a19")]238pub a19: wasmtime::component::__internal::String,239#[component(name = "a20")]240pub a20: wasmtime::component::__internal::String,241}242impl core::fmt::Debug for BigStruct {243fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {244f.debug_struct("BigStruct")245.field("a1", &self.a1)246.field("a2", &self.a2)247.field("a3", &self.a3)248.field("a4", &self.a4)249.field("a5", &self.a5)250.field("a6", &self.a6)251.field("a7", &self.a7)252.field("a8", &self.a8)253.field("a9", &self.a9)254.field("a10", &self.a10)255.field("a11", &self.a11)256.field("a12", &self.a12)257.field("a13", &self.a13)258.field("a14", &self.a14)259.field("a15", &self.a15)260.field("a16", &self.a16)261.field("a17", &self.a17)262.field("a18", &self.a18)263.field("a19", &self.a19)264.field("a20", &self.a20)265.finish()266}267}268const _: () = {269assert!(270160 == < BigStruct as wasmtime::component::ComponentType >::SIZE32271);272assert!(2734 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32274);275};276pub trait HostWithStore: wasmtime::component::HasData {}277impl<_T: ?Sized> HostWithStore for _T278where279_T: wasmtime::component::HasData,280{}281pub trait Host {282fn many_args(283&mut self,284a1: u64,285a2: u64,286a3: u64,287a4: u64,288a5: u64,289a6: u64,290a7: u64,291a8: u64,292a9: u64,293a10: u64,294a11: u64,295a12: u64,296a13: u64,297a14: u64,298a15: u64,299a16: u64,300) -> ();301fn big_argument(&mut self, x: BigStruct) -> ();302}303impl<_T: Host + ?Sized> Host for &mut _T {304fn many_args(305&mut self,306a1: u64,307a2: u64,308a3: u64,309a4: u64,310a5: u64,311a6: u64,312a7: u64,313a8: u64,314a9: u64,315a10: u64,316a11: u64,317a12: u64,318a13: u64,319a14: u64,320a15: u64,321a16: u64,322) -> () {323Host::many_args(324*self,325a1,326a2,327a3,328a4,329a5,330a6,331a7,332a8,333a9,334a10,335a11,336a12,337a13,338a14,339a15,340a16,341)342}343fn big_argument(&mut self, x: BigStruct) -> () {344Host::big_argument(*self, x)345}346}347pub fn add_to_linker<T, D>(348linker: &mut wasmtime::component::Linker<T>,349host_getter: fn(&mut T) -> D::Data<'_>,350) -> wasmtime::Result<()>351where352D: HostWithStore,353for<'a> D::Data<'a>: Host,354T: 'static,355{356let mut inst = linker.instance("foo:foo/manyarg")?;357inst.func_wrap(358"many-args",359move |360mut caller: wasmtime::StoreContextMut<'_, T>,361(362arg0,363arg1,364arg2,365arg3,366arg4,367arg5,368arg6,369arg7,370arg8,371arg9,372arg10,373arg11,374arg12,375arg13,376arg14,377arg15,378): (379u64,380u64,381u64,382u64,383u64,384u64,385u64,386u64,387u64,388u64,389u64,390u64,391u64,392u64,393u64,394u64,395)|396{397let host = &mut host_getter(caller.data_mut());398let r = Host::many_args(399host,400arg0,401arg1,402arg2,403arg3,404arg4,405arg5,406arg6,407arg7,408arg8,409arg9,410arg10,411arg11,412arg12,413arg13,414arg14,415arg15,416);417Ok(r)418},419)?;420inst.func_wrap(421"big-argument",422move |423mut caller: wasmtime::StoreContextMut<'_, T>,424(arg0,): (BigStruct,)|425{426let host = &mut host_getter(caller.data_mut());427let r = Host::big_argument(host, arg0);428Ok(r)429},430)?;431Ok(())432}433}434}435}436pub mod exports {437pub mod foo {438pub mod foo {439#[allow(clippy::all)]440pub mod manyarg {441#[allow(unused_imports)]442use wasmtime::component::__internal::{anyhow, Box};443#[derive(wasmtime::component::ComponentType)]444#[derive(wasmtime::component::Lift)]445#[derive(wasmtime::component::Lower)]446#[component(record)]447#[derive(Clone)]448pub struct BigStruct {449#[component(name = "a1")]450pub a1: wasmtime::component::__internal::String,451#[component(name = "a2")]452pub a2: wasmtime::component::__internal::String,453#[component(name = "a3")]454pub a3: wasmtime::component::__internal::String,455#[component(name = "a4")]456pub a4: wasmtime::component::__internal::String,457#[component(name = "a5")]458pub a5: wasmtime::component::__internal::String,459#[component(name = "a6")]460pub a6: wasmtime::component::__internal::String,461#[component(name = "a7")]462pub a7: wasmtime::component::__internal::String,463#[component(name = "a8")]464pub a8: wasmtime::component::__internal::String,465#[component(name = "a9")]466pub a9: wasmtime::component::__internal::String,467#[component(name = "a10")]468pub a10: wasmtime::component::__internal::String,469#[component(name = "a11")]470pub a11: wasmtime::component::__internal::String,471#[component(name = "a12")]472pub a12: wasmtime::component::__internal::String,473#[component(name = "a13")]474pub a13: wasmtime::component::__internal::String,475#[component(name = "a14")]476pub a14: wasmtime::component::__internal::String,477#[component(name = "a15")]478pub a15: wasmtime::component::__internal::String,479#[component(name = "a16")]480pub a16: wasmtime::component::__internal::String,481#[component(name = "a17")]482pub a17: wasmtime::component::__internal::String,483#[component(name = "a18")]484pub a18: wasmtime::component::__internal::String,485#[component(name = "a19")]486pub a19: wasmtime::component::__internal::String,487#[component(name = "a20")]488pub a20: wasmtime::component::__internal::String,489}490impl core::fmt::Debug for BigStruct {491fn fmt(492&self,493f: &mut core::fmt::Formatter<'_>,494) -> core::fmt::Result {495f.debug_struct("BigStruct")496.field("a1", &self.a1)497.field("a2", &self.a2)498.field("a3", &self.a3)499.field("a4", &self.a4)500.field("a5", &self.a5)501.field("a6", &self.a6)502.field("a7", &self.a7)503.field("a8", &self.a8)504.field("a9", &self.a9)505.field("a10", &self.a10)506.field("a11", &self.a11)507.field("a12", &self.a12)508.field("a13", &self.a13)509.field("a14", &self.a14)510.field("a15", &self.a15)511.field("a16", &self.a16)512.field("a17", &self.a17)513.field("a18", &self.a18)514.field("a19", &self.a19)515.field("a20", &self.a20)516.finish()517}518}519const _: () = {520assert!(521160 == < BigStruct as wasmtime::component::ComponentType522>::SIZE32523);524assert!(5254 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32526);527};528pub struct Guest {529many_args: wasmtime::component::Func,530big_argument: wasmtime::component::Func,531}532#[derive(Clone)]533pub struct GuestIndices {534many_args: wasmtime::component::ComponentExportIndex,535big_argument: wasmtime::component::ComponentExportIndex,536}537impl GuestIndices {538/// Constructor for [`GuestIndices`] which takes a539/// [`Component`](wasmtime::component::Component) as input and can be executed540/// before instantiation.541///542/// This constructor can be used to front-load string lookups to find exports543/// within a component.544pub fn new<_T>(545_instance_pre: &wasmtime::component::InstancePre<_T>,546) -> wasmtime::Result<GuestIndices> {547let instance = _instance_pre548.component()549.get_export_index(None, "foo:foo/manyarg")550.ok_or_else(|| {551anyhow::anyhow!(552"no exported instance named `foo:foo/manyarg`"553)554})?;555let mut lookup = move |name| {556_instance_pre557.component()558.get_export_index(Some(&instance), name)559.ok_or_else(|| {560anyhow::anyhow!(561"instance export `foo:foo/manyarg` does \562not have export `{name}`"563)564})565};566let _ = &mut lookup;567let many_args = lookup("many-args")?;568let big_argument = lookup("big-argument")?;569Ok(GuestIndices {570many_args,571big_argument,572})573}574pub fn load(575&self,576mut store: impl wasmtime::AsContextMut,577instance: &wasmtime::component::Instance,578) -> wasmtime::Result<Guest> {579let _instance = instance;580let _instance_pre = _instance.instance_pre(&store);581let _instance_type = _instance_pre.instance_type();582let mut store = store.as_context_mut();583let _ = &mut store;584let many_args = *_instance585.get_typed_func::<586(587u64,588u64,589u64,590u64,591u64,592u64,593u64,594u64,595u64,596u64,597u64,598u64,599u64,600u64,601u64,602u64,603),604(),605>(&mut store, &self.many_args)?606.func();607let big_argument = *_instance608.get_typed_func::<609(&BigStruct,),610(),611>(&mut store, &self.big_argument)?612.func();613Ok(Guest { many_args, big_argument })614}615}616impl Guest {617pub fn call_many_args<S: wasmtime::AsContextMut>(618&self,619mut store: S,620arg0: u64,621arg1: u64,622arg2: u64,623arg3: u64,624arg4: u64,625arg5: u64,626arg6: u64,627arg7: u64,628arg8: u64,629arg9: u64,630arg10: u64,631arg11: u64,632arg12: u64,633arg13: u64,634arg14: u64,635arg15: u64,636) -> wasmtime::Result<()> {637let callee = unsafe {638wasmtime::component::TypedFunc::<639(640u64,641u64,642u64,643u64,644u64,645u64,646u64,647u64,648u64,649u64,650u64,651u64,652u64,653u64,654u64,655u64,656),657(),658>::new_unchecked(self.many_args)659};660let () = callee661.call(662store.as_context_mut(),663(664arg0,665arg1,666arg2,667arg3,668arg4,669arg5,670arg6,671arg7,672arg8,673arg9,674arg10,675arg11,676arg12,677arg13,678arg14,679arg15,680),681)?;682callee.post_return(store.as_context_mut())?;683Ok(())684}685pub fn call_big_argument<S: wasmtime::AsContextMut>(686&self,687mut store: S,688arg0: &BigStruct,689) -> wasmtime::Result<()> {690let callee = unsafe {691wasmtime::component::TypedFunc::<692(&BigStruct,),693(),694>::new_unchecked(self.big_argument)695};696let () = callee.call(store.as_context_mut(), (arg0,))?;697callee.post_return(store.as_context_mut())?;698Ok(())699}700}701}702}703}704}705706707