pub struct Component { /* private fields */ }
component-model
only.Expand description
A compiled WebAssembly Component.
Implementations§
source§impl Component
impl Component
sourcepub fn new(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Component>
Available on crate features cranelift
or winch
only.
pub fn new(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Component>
cranelift
or winch
only.Compiles a new WebAssembly component from the in-memory wasm image provided.
sourcepub fn from_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Component>
Available on crate features cranelift
or winch
only.
pub fn from_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Component>
cranelift
or winch
only.Compiles a new WebAssembly component from a wasm file on disk pointed to
by file
.
sourcepub fn from_binary(engine: &Engine, binary: &[u8]) -> Result<Component>
Available on crate features cranelift
or winch
only.
pub fn from_binary(engine: &Engine, binary: &[u8]) -> Result<Component>
cranelift
or winch
only.Compiles a new WebAssembly component from the in-memory wasm image provided.
sourcepub unsafe fn deserialize(
engine: &Engine,
bytes: impl AsRef<[u8]>
) -> Result<Component>
pub unsafe fn deserialize( engine: &Engine, bytes: impl AsRef<[u8]> ) -> Result<Component>
Same as Module::deserialize
, but for components.
Note that the file referenced here must contain contents previously
produced by Engine::precompile_component
or
Component::serialize
.
For more information see the Module::deserialize
method.
sourcepub unsafe fn deserialize_file(
engine: &Engine,
path: impl AsRef<Path>
) -> Result<Component>
pub unsafe fn deserialize_file( engine: &Engine, path: impl AsRef<Path> ) -> Result<Component>
Same as Module::deserialize_file
, but for components.
For more information see the Component::deserialize
and
Module::deserialize_file
methods.
sourcepub fn serialize(&self) -> Result<Vec<u8>>
pub fn serialize(&self) -> Result<Vec<u8>>
Same as Module::serialize
, except for a component.
Note that the artifact produced here must be passed to
Component::deserialize
and is not compatible for use with
Module
.
sourcepub fn resources_required(&self) -> Option<ResourcesRequired>
pub fn resources_required(&self) -> Option<ResourcesRequired>
Returns a summary of the resources required to instantiate this
Component
.
Note that when a component imports and instantiates another component or
core module, we cannot determine ahead of time how many resources
instantiating this component will require, and therefore this method
will return None
in these scenarios.
Potential uses of the returned information:
-
Determining whether your pooling allocator configuration supports instantiating this component.
-
Deciding how many of which
Component
you want to instantiate within a fixed amount of resources, e.g. determining whether to create 5 instances of component X or 10 instances of component Y.
Example
use wasmtime::{Config, Engine, component::Component};
let mut config = Config::new();
config.wasm_multi_memory(true);
config.wasm_component_model(true);
let engine = Engine::new(&config)?;
let component = Component::new(&engine, &r#"
(component
;; Define a core module that uses two memories.
(core module $m
(memory 1)
(memory 6)
)
;; Instantiate that core module three times.
(core instance $i1 (instantiate (module $m)))
(core instance $i2 (instantiate (module $m)))
(core instance $i3 (instantiate (module $m)))
)
"#)?;
let resources = component.resources_required()
.expect("this component does not import any core modules or instances");
// Instantiating the component will require allocating two memories per
// core instance, and there are three instances, so six total memories.
assert_eq!(resources.num_memories, 6);
assert_eq!(resources.max_initial_memory_size, Some(6));
// The component doesn't need any tables.
assert_eq!(resources.num_tables, 0);
assert_eq!(resources.max_initial_table_size, None);