pub struct Linker { /* fields omitted */ }Structure used to link wasm modules/instances together.
This structure is used to assist in instantiating aModule. ALinkeris a way of performing name resolution to make instantiating a module easier(as opposed to callingInstance::new).Linker is a name-based resolverwhere names are dynamically defined and then used to instantiate aModule. The goal of aLinker is to have a one-argument method,Linker::instantiate, which takes aModule and produces anInstance. This method will automatically select all the right importsfor theModule to be instantiated, and will otherwise return an errorif an import isn't satisfied.
As mentioned previously,Linker is a form of name resolver. It will beusing the string-based names of imports on a module to attempt to select amatching item to hook up to it. This name resolution has two-levels ofnamespaces, a module level and a name level. Each item is defined within amodule and then has its own name. This basically follows the wasm standardfor modularization.
Names in aLinker can be defined twice, but only for different signaturesof items. This means that every item defined in aLinker has a uniquename/type pair. For example you can define two functions with the modulenamefoo and item namebar, so long as they have different functionsignatures. Currently duplicate memories and tables are not allowed, onlyone-per-name is allowed.
Note that allowing duplicates by shadowing the previous definition can becontrolled with theLinker::allow_shadowing method as well.
implLinker[src]pub fnnew(store: &Store) ->Linker[src]Creates a newLinker.
This function will create a newLinker which is ready to startlinking modules. All items defined in this linker and produced by thislinker will be connected withstore and must come from the samestore.
usewasmtime::{Linker,Store};letstore=Store::default();letmutlinker=Linker::new(&store);// ...
pub fnallow_shadowing(&mut self, allow:bool) -> &mutLinker[src]Configures whether thisLinker will shadow previous duplicatedefinitions of the same signature.
By default aLinker will disallow duplicate definitions of the samesignature. This method, however, can be used to instead allow duplicatesand have the latest definition take precedence when linking modules.
letmutlinker=Linker::new(&store);linker.func("","",|| {})?;// by default, duplicates are disallowedassert!(linker.func("","",|| {}).is_err());// but shadowing can be configured to be allowed as welllinker.allow_shadowing(true);linker.func("","",|| {})?;
pub fndefine(
&mut self,
module: &str,
name: &str,
item: implInto<Extern>
) ->Result<&mutSelf>[src]Defines a new item in thisLinker.
This method will add a new definition, by name, to this instance ofLinker. Themodule andname provided are what to name theitem.
Returns an error if themodule andname already identify an itemof the same type as theitem provided and if shadowing is disallowed.For more information see the documentation onLinker.
Also returns an error ifitem comes from a different store than thisLinker was created with.
letmutlinker=Linker::new(&store);letty=GlobalType::new(ValType::I32,Mutability::Const);letglobal=Global::new(&store,ty,Val::I32(0x1234))?;linker.define("host","offset",global)?;letwat=r#" (module (import "host" "offset" (global i32)) (memory 1) (data (global.get 0) "foo") )"#;letmodule=Module::new(store.engine(),wat)?;linker.instantiate(&module)?;
pub fnfunc<Params, Args>(
&mut self,
module: &str,
name: &str,
func: implIntoFunc<Params, Args>
) ->Result<&mutSelf>[src]Convenience wrapper to define a function import.
This method is a convenience wrapper aroundLinker::define whichinternally delegates toFunc::wrap.
Returns an error if themodule andname already identify an itemof the same type as theitem provided and if shadowing is disallowed.For more information see the documentation onLinker.
letmutlinker=Linker::new(&store);linker.func("host","double",|x:i32|x*2)?;linker.func("host","log_i32",|x:i32|println!("{}",x))?;linker.func("host","log_str",|caller:Caller,ptr:i32,len:i32| {// ...})?;letwat=r#" (module (import "host" "double" (func (param i32) (result i32))) (import "host" "log_i32" (func (param i32))) (import "host" "log_str" (func (param i32 i32))) )"#;letmodule=Module::new(store.engine(),wat)?;linker.instantiate(&module)?;
pub fninstance(
&mut self,
module_name: &str,
instance: &Instance
) ->Result<&mutSelf>[src]Convenience wrapper to define an entireInstance in this linker.
This function is a convenience wrapper aroundLinker::define whichwill define all exports oninstance into this linker. The module namefor each export ismodule_name, and the name for each export is thename in the instance itself.
Returns an error if the any item is redefined twice in this linker (forexample the samemodule_name was already defined) and shadowing isdisallowed, or ifinstance comes from a differentStore than thisLinker originally was created with.
letmutlinker=Linker::new(&store);// Instantiate a small instance...letwat=r#"(module (func (export "run") ))"#;letmodule=Module::new(store.engine(),wat)?;letinstance=linker.instantiate(&module)?;// ... and inform the linker that the name of this instance is// `instance1`. This defines the `instance1::run` name for our next// module to use.linker.instance("instance1",&instance)?;letwat=r#" (module (import "instance1" "run" (func $instance1_run)) (func (export "run") call $instance1_run ) )"#;letmodule=Module::new(store.engine(),wat)?;letinstance=linker.instantiate(&module)?;
pub fnmodule(
&mut self,
module_name: &str,
module: &Module
) ->Result<&mutSelf>[src]Define automatic instantiations of aModule in this linker.
This automatically handlesCommands and Reactors instantiation andinitialization.
Exported functions of a Command module may be called directly, howeverinstead of having a single instance which is reused for each call,each call creates a new instance, which lives for the duration of thecall. The imports of the Command are resolved once, and reused foreach instantiation, so all dependencies need to be present at the timewhenLinker::module is called.
For Reactors, a single instance is created, and an initializationfunction is called, and then its exports may be called.
Ordinary modules which don't declare themselves to be either Commandsor Reactors are treated as Reactors without any initialization calls.
Returns an error if the any item is redefined twice in this linker (forexample the samemodule_name was already defined) and shadowing isdisallowed, ifinstance comes from a differentStore than thisLinker originally was created with, or if a Reactor initializationfunction traps.
letmutlinker=Linker::new(&store);// Instantiate a small instance and inform the linker that the name of// this instance is `instance1`. This defines the `instance1::run` name// for our next module to use.letwat=r#"(module (func (export "run") ))"#;letmodule=Module::new(store.engine(),wat)?;linker.module("instance1",&module)?;letwat=r#" (module (import "instance1" "run" (func $instance1_run)) (func (export "run") call $instance1_run ) )"#;letmodule=Module::new(store.engine(),wat)?;letinstance=linker.instantiate(&module)?;
For a Command, a new instance is created for each call.
letmutlinker=Linker::new(&store);// Create a Command that attempts to count the number of times it is run, but is// foiled by each call getting a new instance.letwat=r#" (module (global $counter (mut i32) (i32.const 0)) (func (export "_start") (global.set $counter (i32.add (global.get $counter) (i32.const 1))) ) (func (export "read_counter") (result i32) (global.get $counter) ) )"#;letmodule=Module::new(store.engine(),wat)?;linker.module("commander",&module)?;letrun=linker.get_default("")?.get0::<()>()?;run()?;run()?;run()?;letwat=r#" (module (import "commander" "_start" (func $commander_start)) (import "commander" "read_counter" (func $commander_read_counter (result i32))) (func (export "run") (result i32) call $commander_start call $commander_start call $commander_start call $commander_read_counter ) )"#;letmodule=Module::new(store.engine(),wat)?;linker.module("",&module)?;letcount=linker.get_one_by_name("","run")?.into_func().unwrap().get0::<i32>()?()?;assert_eq!(count,0,"a Command should get a fresh instance on each invocation");
pub fnalias(&mut self, module: &str, as_module: &str) ->Result<()>[src]Aliases one module's name as another.
This method will alias all currently defined undermodule to also bedefined under the nameas_module too.
Returns an error if any shadowing violations happen while defining newitems.
pub fninstantiate(&self, module: &Module) ->Result<Instance>[src]Attempts to instantiate themodule provided.
This method will attempt to assemble a list of imports that correspondto the imports required by theModule provided. This listof imports is then passed toInstance::new to continue theinstantiation process.
Each import ofmodule will be looked up in thisLinker and musthave previously been defined. If it was previously defined with anincorrect signature or if it was not previously defined then an errorwill be returned because the import can not be satisfied.
Per the WebAssembly spec, instantiation includes running the module'sstart function, if it has one (not to be confused with the_startfunction, which is not run).
This method can fail because an import may not be found, or becauseinstantiation itself may fail. For information on instantiationfailures seeInstance::new.
letmutlinker=Linker::new(&store);linker.func("host","double",|x:i32|x*2)?;letwat=r#" (module (import "host" "double" (func (param i32) (result i32))) )"#;letmodule=Module::new(store.engine(),wat)?;linker.instantiate(&module)?;
pub fnstore(&self) -> &Store[src]Returns theStore that this linker is connected to.
pub fniter(&self) -> implIterator<Item =(&str, &str,Extern)>[src]Returns an iterator over all items defined in thisLinker, in arbitrary order.
The iterator returned will yield 3-tuples where the first two elementsare the module name and item name for the external item, and the thirditem is the item itself that is defined.
Note that multipleExtern items may be defined for the samemodule/name pair.
pub fnget(&self, import: &ImportType) ->Option<Extern>[src]Looks up a value in thisLinker which matches theimport typeprovided.
ReturnsNone if no match was found.
pub fnget_by_name<'a: 'p, 'p>(
&'a self,
module: &'pstr,
name: &'pstr
) -> implIterator<Item = &'aExtern> + 'p[src]Returns all items defined for themodule andname pair.
This may return an empty iterator, but it may also return multiple itemsif the module/name have been defined twice.
pub fnget_one_by_name(&self, module: &str, name: &str) ->Result<Extern>[src]Returns the single item defined for themodule andname pair.
Unlike the similarLinker::get_by_name method this function returnsa singleExtern item. If themodule andname pair isn't definedin this linker then an error is returned. If more than one value existsfor themodule andname pairs, then an error is returned as well.
pub fnget_default(&self, module: &str) ->Result<Func>[src]Returns the "default export" of a module.
An export with an empty string is considered to be a "default export"."_start" is also recognized for compatibility.
impl !RefUnwindSafe forLinkerimpl !Send forLinkerimpl !Sync forLinkerimplUnpin forLinkerimpl !UnwindSafe forLinkerimpl<T>Any for Twhere
T: 'static + ?Sized, [src]impl<T>Borrow<T> for Twhere
T: ?Sized, [src]impl<T>BorrowMut<T> for Twhere
T: ?Sized, [src]fnborrow_mut(&mut self) ->&mutT[src]impl<T>From<T> for T[src]impl<T, U>Into<U> for Twhere
U:From<T>, [src]impl<T> Same<T> for TtypeOutput = TShould always beSelf
impl<T, U>TryFrom<U> for Twhere
U:Into<T>, [src]typeError =InfallibleThe type returned in the event of a conversion error.
fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>[src]impl<T, U>TryInto<U> for Twhere
U:TryFrom<T>, [src]