Path: blob/main/crates/component-macro/tests/expanded/many-arguments.rs
3090 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 _: () = {105impl TheWorldIndices {106/// Creates a new copy of `TheWorldIndices` bindings which can then107/// be used to instantiate into a particular store.108///109/// This method may fail if the component does not have the110/// required exports.111pub fn new<_T>(112_instance_pre: &wasmtime::component::InstancePre<_T>,113) -> wasmtime::Result<Self> {114let _component = _instance_pre.component();115let _instance_type = _instance_pre.instance_type();116let interface0 = exports::foo::foo::manyarg::GuestIndices::new(117_instance_pre,118)?;119Ok(TheWorldIndices { interface0 })120}121/// Uses the indices stored in `self` to load an instance122/// of [`TheWorld`] from the instance provided.123///124/// Note that at this time this method will additionally125/// perform type-checks of all exports.126pub fn load(127&self,128mut store: impl wasmtime::AsContextMut,129instance: &wasmtime::component::Instance,130) -> wasmtime::Result<TheWorld> {131let _ = &mut store;132let _instance = instance;133let interface0 = self.interface0.load(&mut store, &_instance)?;134Ok(TheWorld { interface0 })135}136}137impl TheWorld {138/// Convenience wrapper around [`TheWorldPre::new`] and139/// [`TheWorldPre::instantiate`].140pub fn instantiate<_T>(141store: impl wasmtime::AsContextMut<Data = _T>,142component: &wasmtime::component::Component,143linker: &wasmtime::component::Linker<_T>,144) -> wasmtime::Result<TheWorld> {145let pre = linker.instantiate_pre(component)?;146TheWorldPre::new(pre)?.instantiate(store)147}148/// Convenience wrapper around [`TheWorldIndices::new`] and149/// [`TheWorldIndices::load`].150pub fn new(151mut store: impl wasmtime::AsContextMut,152instance: &wasmtime::component::Instance,153) -> wasmtime::Result<TheWorld> {154let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;155indices.load(&mut store, instance)156}157/// Convenience wrapper around [`TheWorldPre::new`] and158/// [`TheWorldPre::instantiate_async`].159pub async fn instantiate_async<_T>(160store: impl wasmtime::AsContextMut<Data = _T>,161component: &wasmtime::component::Component,162linker: &wasmtime::component::Linker<_T>,163) -> wasmtime::Result<TheWorld>164where165_T: Send,166{167let pre = linker.instantiate_pre(component)?;168TheWorldPre::new(pre)?.instantiate_async(store).await169}170pub fn add_to_linker<T, D>(171linker: &mut wasmtime::component::Linker<T>,172host_getter: fn(&mut T) -> D::Data<'_>,173) -> wasmtime::Result<()>174where175D: foo::foo::manyarg::HostWithStore,176for<'a> D::Data<'a>: foo::foo::manyarg::Host,177T: 'static,178{179foo::foo::manyarg::add_to_linker::<T, D>(linker, host_getter)?;180Ok(())181}182pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest {183&self.interface0184}185}186};187pub mod foo {188pub mod foo {189#[allow(clippy::all)]190pub mod manyarg {191#[allow(unused_imports)]192use wasmtime::component::__internal::Box;193#[derive(wasmtime::component::ComponentType)]194#[derive(wasmtime::component::Lift)]195#[derive(wasmtime::component::Lower)]196#[component(record)]197#[derive(Clone)]198pub struct BigStruct {199#[component(name = "a1")]200pub a1: wasmtime::component::__internal::String,201#[component(name = "a2")]202pub a2: wasmtime::component::__internal::String,203#[component(name = "a3")]204pub a3: wasmtime::component::__internal::String,205#[component(name = "a4")]206pub a4: wasmtime::component::__internal::String,207#[component(name = "a5")]208pub a5: wasmtime::component::__internal::String,209#[component(name = "a6")]210pub a6: wasmtime::component::__internal::String,211#[component(name = "a7")]212pub a7: wasmtime::component::__internal::String,213#[component(name = "a8")]214pub a8: wasmtime::component::__internal::String,215#[component(name = "a9")]216pub a9: wasmtime::component::__internal::String,217#[component(name = "a10")]218pub a10: wasmtime::component::__internal::String,219#[component(name = "a11")]220pub a11: wasmtime::component::__internal::String,221#[component(name = "a12")]222pub a12: wasmtime::component::__internal::String,223#[component(name = "a13")]224pub a13: wasmtime::component::__internal::String,225#[component(name = "a14")]226pub a14: wasmtime::component::__internal::String,227#[component(name = "a15")]228pub a15: wasmtime::component::__internal::String,229#[component(name = "a16")]230pub a16: wasmtime::component::__internal::String,231#[component(name = "a17")]232pub a17: wasmtime::component::__internal::String,233#[component(name = "a18")]234pub a18: wasmtime::component::__internal::String,235#[component(name = "a19")]236pub a19: wasmtime::component::__internal::String,237#[component(name = "a20")]238pub a20: wasmtime::component::__internal::String,239}240impl core::fmt::Debug for BigStruct {241fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {242f.debug_struct("BigStruct")243.field("a1", &self.a1)244.field("a2", &self.a2)245.field("a3", &self.a3)246.field("a4", &self.a4)247.field("a5", &self.a5)248.field("a6", &self.a6)249.field("a7", &self.a7)250.field("a8", &self.a8)251.field("a9", &self.a9)252.field("a10", &self.a10)253.field("a11", &self.a11)254.field("a12", &self.a12)255.field("a13", &self.a13)256.field("a14", &self.a14)257.field("a15", &self.a15)258.field("a16", &self.a16)259.field("a17", &self.a17)260.field("a18", &self.a18)261.field("a19", &self.a19)262.field("a20", &self.a20)263.finish()264}265}266const _: () = {267assert!(268160 == < BigStruct as wasmtime::component::ComponentType >::SIZE32269);270assert!(2714 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32272);273};274pub trait HostWithStore: wasmtime::component::HasData {}275impl<_T: ?Sized> HostWithStore for _T276where277_T: wasmtime::component::HasData,278{}279pub trait Host {280fn many_args(281&mut self,282a1: u64,283a2: u64,284a3: u64,285a4: u64,286a5: u64,287a6: u64,288a7: u64,289a8: u64,290a9: u64,291a10: u64,292a11: u64,293a12: u64,294a13: u64,295a14: u64,296a15: u64,297a16: u64,298) -> ();299fn big_argument(&mut self, x: BigStruct) -> ();300}301impl<_T: Host + ?Sized> Host for &mut _T {302fn many_args(303&mut self,304a1: u64,305a2: u64,306a3: u64,307a4: u64,308a5: u64,309a6: u64,310a7: u64,311a8: u64,312a9: u64,313a10: u64,314a11: u64,315a12: u64,316a13: u64,317a14: u64,318a15: u64,319a16: u64,320) -> () {321Host::many_args(322*self,323a1,324a2,325a3,326a4,327a5,328a6,329a7,330a8,331a9,332a10,333a11,334a12,335a13,336a14,337a15,338a16,339)340}341fn big_argument(&mut self, x: BigStruct) -> () {342Host::big_argument(*self, x)343}344}345pub fn add_to_linker<T, D>(346linker: &mut wasmtime::component::Linker<T>,347host_getter: fn(&mut T) -> D::Data<'_>,348) -> wasmtime::Result<()>349where350D: HostWithStore,351for<'a> D::Data<'a>: Host,352T: 'static,353{354let mut inst = linker.instance("foo:foo/manyarg")?;355inst.func_wrap(356"many-args",357move |358mut caller: wasmtime::StoreContextMut<'_, T>,359(360arg0,361arg1,362arg2,363arg3,364arg4,365arg5,366arg6,367arg7,368arg8,369arg9,370arg10,371arg11,372arg12,373arg13,374arg14,375arg15,376): (377u64,378u64,379u64,380u64,381u64,382u64,383u64,384u64,385u64,386u64,387u64,388u64,389u64,390u64,391u64,392u64,393)|394{395let host = &mut host_getter(caller.data_mut());396let r = Host::many_args(397host,398arg0,399arg1,400arg2,401arg3,402arg4,403arg5,404arg6,405arg7,406arg8,407arg9,408arg10,409arg11,410arg12,411arg13,412arg14,413arg15,414);415Ok(r)416},417)?;418inst.func_wrap(419"big-argument",420move |421mut caller: wasmtime::StoreContextMut<'_, T>,422(arg0,): (BigStruct,)|423{424let host = &mut host_getter(caller.data_mut());425let r = Host::big_argument(host, arg0);426Ok(r)427},428)?;429Ok(())430}431}432}433}434pub mod exports {435pub mod foo {436pub mod foo {437#[allow(clippy::all)]438pub mod manyarg {439#[allow(unused_imports)]440use wasmtime::component::__internal::Box;441#[derive(wasmtime::component::ComponentType)]442#[derive(wasmtime::component::Lift)]443#[derive(wasmtime::component::Lower)]444#[component(record)]445#[derive(Clone)]446pub struct BigStruct {447#[component(name = "a1")]448pub a1: wasmtime::component::__internal::String,449#[component(name = "a2")]450pub a2: wasmtime::component::__internal::String,451#[component(name = "a3")]452pub a3: wasmtime::component::__internal::String,453#[component(name = "a4")]454pub a4: wasmtime::component::__internal::String,455#[component(name = "a5")]456pub a5: wasmtime::component::__internal::String,457#[component(name = "a6")]458pub a6: wasmtime::component::__internal::String,459#[component(name = "a7")]460pub a7: wasmtime::component::__internal::String,461#[component(name = "a8")]462pub a8: wasmtime::component::__internal::String,463#[component(name = "a9")]464pub a9: wasmtime::component::__internal::String,465#[component(name = "a10")]466pub a10: wasmtime::component::__internal::String,467#[component(name = "a11")]468pub a11: wasmtime::component::__internal::String,469#[component(name = "a12")]470pub a12: wasmtime::component::__internal::String,471#[component(name = "a13")]472pub a13: wasmtime::component::__internal::String,473#[component(name = "a14")]474pub a14: wasmtime::component::__internal::String,475#[component(name = "a15")]476pub a15: wasmtime::component::__internal::String,477#[component(name = "a16")]478pub a16: wasmtime::component::__internal::String,479#[component(name = "a17")]480pub a17: wasmtime::component::__internal::String,481#[component(name = "a18")]482pub a18: wasmtime::component::__internal::String,483#[component(name = "a19")]484pub a19: wasmtime::component::__internal::String,485#[component(name = "a20")]486pub a20: wasmtime::component::__internal::String,487}488impl core::fmt::Debug for BigStruct {489fn fmt(490&self,491f: &mut core::fmt::Formatter<'_>,492) -> core::fmt::Result {493f.debug_struct("BigStruct")494.field("a1", &self.a1)495.field("a2", &self.a2)496.field("a3", &self.a3)497.field("a4", &self.a4)498.field("a5", &self.a5)499.field("a6", &self.a6)500.field("a7", &self.a7)501.field("a8", &self.a8)502.field("a9", &self.a9)503.field("a10", &self.a10)504.field("a11", &self.a11)505.field("a12", &self.a12)506.field("a13", &self.a13)507.field("a14", &self.a14)508.field("a15", &self.a15)509.field("a16", &self.a16)510.field("a17", &self.a17)511.field("a18", &self.a18)512.field("a19", &self.a19)513.field("a20", &self.a20)514.finish()515}516}517const _: () = {518assert!(519160 == < BigStruct as wasmtime::component::ComponentType520>::SIZE32521);522assert!(5234 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32524);525};526#[derive(Clone)]527pub struct Guest {528many_args: wasmtime::component::Func,529big_argument: wasmtime::component::Func,530}531#[derive(Clone)]532pub struct GuestIndices {533many_args: wasmtime::component::ComponentExportIndex,534big_argument: wasmtime::component::ComponentExportIndex,535}536impl GuestIndices {537/// Constructor for [`GuestIndices`] which takes a538/// [`Component`](wasmtime::component::Component) as input and can be executed539/// before instantiation.540///541/// This constructor can be used to front-load string lookups to find exports542/// within a component.543pub fn new<_T>(544_instance_pre: &wasmtime::component::InstancePre<_T>,545) -> wasmtime::Result<GuestIndices> {546let instance = _instance_pre547.component()548.get_export_index(None, "foo:foo/manyarg")549.ok_or_else(|| {550wasmtime::format_err!(551"no exported instance named `foo:foo/manyarg`"552)553})?;554let mut lookup = move |name| {555_instance_pre556.component()557.get_export_index(Some(&instance), name)558.ok_or_else(|| {559wasmtime::format_err!(560"instance export `foo:foo/manyarg` does \561not have export `{name}`"562)563})564};565let _ = &mut lookup;566let many_args = lookup("many-args")?;567let big_argument = lookup("big-argument")?;568Ok(GuestIndices {569many_args,570big_argument,571})572}573pub fn load(574&self,575mut store: impl wasmtime::AsContextMut,576instance: &wasmtime::component::Instance,577) -> wasmtime::Result<Guest> {578let _instance = instance;579let _instance_pre = _instance.instance_pre(&store);580let _instance_type = _instance_pre.instance_type();581let mut store = store.as_context_mut();582let _ = &mut store;583let many_args = *_instance584.get_typed_func::<585(586u64,587u64,588u64,589u64,590u64,591u64,592u64,593u64,594u64,595u64,596u64,597u64,598u64,599u64,600u64,601u64,602),603(),604>(&mut store, &self.many_args)?605.func();606let big_argument = *_instance607.get_typed_func::<608(&BigStruct,),609(),610>(&mut store, &self.big_argument)?611.func();612Ok(Guest { many_args, big_argument })613}614}615impl Guest {616pub fn call_many_args<S: wasmtime::AsContextMut>(617&self,618mut store: S,619arg0: u64,620arg1: u64,621arg2: u64,622arg3: u64,623arg4: u64,624arg5: u64,625arg6: u64,626arg7: u64,627arg8: u64,628arg9: u64,629arg10: u64,630arg11: u64,631arg12: u64,632arg13: u64,633arg14: u64,634arg15: u64,635) -> wasmtime::Result<()> {636let callee = unsafe {637wasmtime::component::TypedFunc::<638(639u64,640u64,641u64,642u64,643u64,644u64,645u64,646u64,647u64,648u64,649u64,650u64,651u64,652u64,653u64,654u64,655),656(),657>::new_unchecked(self.many_args)658};659let () = callee660.call(661store.as_context_mut(),662(663arg0,664arg1,665arg2,666arg3,667arg4,668arg5,669arg6,670arg7,671arg8,672arg9,673arg10,674arg11,675arg12,676arg13,677arg14,678arg15,679),680)?;681Ok(())682}683pub fn call_big_argument<S: wasmtime::AsContextMut>(684&self,685mut store: S,686arg0: &BigStruct,687) -> wasmtime::Result<()> {688let callee = unsafe {689wasmtime::component::TypedFunc::<690(&BigStruct,),691(),692>::new_unchecked(self.big_argument)693};694let () = callee.call(store.as_context_mut(), (arg0,))?;695Ok(())696}697}698}699}700}701}702703704