usecrate::component::*; usecrate::{ExportKind, Module, RawSection, ValType}; use std::mem;
/// Convenience type to build a component incrementally and automatically keep /// track of index spaces. /// /// This type is intended to be a wrapper around the [`Component`] encoding type /// which is useful for building it up incrementally over time. This type will /// automatically collect definitions into sections and reports the index of all /// items added by keeping track of indices internally. #[derive(Debug, Default)] pubstruct ComponentBuilder { /// The binary component that's being built.
component: Component,
/// The last section which was appended to during encoding. This type is /// generated by the `section_accessors` macro below. /// /// When something is encoded this is used if it matches the kind of item /// being encoded, otherwise it's "flushed" to the output component and a /// new section is started.
last_section: LastSection,
impl ComponentBuilder { /// Returns the current number of core modules. pubfn core_module_count(&self) -> u32 { self.core_modules
}
/// Returns the current number of core funcs. pubfn core_func_count(&self) -> u32 { self.core_funcs
}
/// Returns the current number of core types. pubfn core_type_count(&self) -> u32 { self.core_types
}
/// Returns the current number of core memories. pubfn core_memory_count(&self) -> u32 { self.core_memories
}
/// Returns the current number of core tables. pubfn core_table_count(&self) -> u32 { self.core_tables
}
/// Returns the current number of core instances. pubfn core_instance_count(&self) -> u32 { self.core_instances
}
/// Returns the current number of core tags. pubfn core_tag_count(&self) -> u32 { self.core_tags
}
/// Returns the current number of core globals. pubfn core_global_count(&self) -> u32 { self.core_globals
}
/// Returns the current number of component funcs. pubfn func_count(&self) -> u32 { self.funcs
}
/// Returns the current number of component instances. pubfn instance_count(&self) -> u32 { self.instances
}
/// Returns the current number of component values. pubfn value_count(&self) -> u32 { self.values
}
/// Returns the current number of components. pubfn component_count(&self) -> u32 { self.components
}
/// Returns the current number of component types. pubfn type_count(&self) -> u32 { self.types
}
/// Completes this component and returns the binary encoding of the entire /// component. pubfn finish(mutself) -> Vec<u8> { self.flush(); self.component.finish()
}
/// Encodes a core wasm `Module` into this component, returning its index. pubfn core_module(&mutself, module: &Module) -> u32 { self.flush(); self.component.section(&ModuleSection(module));
inc(&mutself.core_modules)
}
/// Encodes a core wasm `module` into this component, returning its index. pubfn core_module_raw(&mutself, module: &[u8]) -> u32 { self.flush(); self.component.section(&RawSection {
id: ComponentSectionId::CoreModule.into(),
data: module,
});
inc(&mutself.core_modules)
}
/// Instantiates a core wasm module at `module_index` with the `args` /// provided. /// /// Returns the index of the core wasm instance crated. pubfn core_instantiate<'a, A>(&mut self, module_index: u32, args: A) -> u32 where
A: IntoIterator<Item = (&'a str, ModuleArg)>,
A::IntoIter: ExactSizeIterator,
{ self.instances().instantiate(module_index, args);
inc(&mutself.core_instances)
}
/// Creates a new core wasm instance from the `exports` provided. /// /// Returns the index of the core wasm instance crated. pubfn core_instantiate_exports<'a, E>(&mut self, exports: E) -> u32 where
E: IntoIterator<Item = (&'a str, ExportKind, u32)>,
E::IntoIter: ExactSizeIterator,
{ self.instances().export_items(exports);
inc(&mutself.core_instances)
}
/// Creates a new aliased item where the core `instance` specified has its /// export `name` aliased out with the `kind` specified. /// /// Returns the index of the item crated. pubfn core_alias_export(&mutself, instance: u32, name: &str, kind: ExportKind) -> u32 { self.alias(Alias::CoreInstanceExport {
instance,
kind,
name,
})
}
/// Adds a new alias to this component pubfn alias(&mutself, alias: Alias<'_>) -> u32 { self.aliases().alias(alias); match alias {
Alias::InstanceExport { kind, .. } => self.inc_kind(kind),
Alias::CoreInstanceExport { kind, .. } => self.inc_core_kind(kind),
Alias::Outer {
kind: ComponentOuterAliasKind::Type,
..
} => inc(&mutself.types),
Alias::Outer {
kind: ComponentOuterAliasKind::CoreModule,
..
} => inc(&mutself.core_modules),
Alias::Outer {
kind: ComponentOuterAliasKind::Component,
..
} => inc(&mutself.components),
Alias::Outer {
kind: ComponentOuterAliasKind::CoreType,
..
} => inc(&mutself.core_types),
}
}
/// Creates an alias to a previous component instance's exported item. /// /// The `instance` provided is the instance to access and the `name` is the /// item to access. /// /// Returns the index of the new item defined. pubfn alias_export(&mutself, instance: u32, name: &str, kind: ComponentExportKind) -> u32 { self.alias(Alias::InstanceExport {
instance,
kind,
name,
})
}
/// Lowers the `func_index` component function into a core wasm function /// using the `options` provided. /// /// Returns the index of the core wasm function created. pubfn lower_func<O>(&mutself, func_index: u32, options: O) -> u32 where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{ self.canonical_functions().lower(func_index, options);
inc(&mutself.core_funcs)
}
/// Lifts the core wasm `core_func_index` function with the component /// function type `type_index` and `options`. /// /// Returns the index of the component function created. pubfn lift_func<O>(&mutself, core_func_index: u32, type_index: u32, options: O) -> u32 where
O: IntoIterator<Item = CanonicalOption>,
O::IntoIter: ExactSizeIterator,
{ self.canonical_functions()
.lift(core_func_index, type_index, options);
inc(&mutself.funcs)
}
/// Imports a new item into this component with the `name` and `ty` specified. pubfn import(&mutself, name: &str, ty: ComponentTypeRef) -> u32 { let ret = match &ty {
ComponentTypeRef::Instance(_) => inc(&mutself.instances),
ComponentTypeRef::Func(_) => inc(&mutself.funcs),
ComponentTypeRef::Type(..) => inc(&mutself.types),
ComponentTypeRef::Component(_) => inc(&mutself.components),
ComponentTypeRef::Module(_) => inc(&mutself.core_modules),
ComponentTypeRef::Value(_) => inc(&mutself.values),
}; self.imports().import(name, ty);
ret
}
/// Exports a new item from this component with the `name` and `kind` /// specified. /// /// The `idx` is the item to export and the `ty` is an optional type to /// ascribe to the export. pubfn export(
&mutself,
name: &str,
kind: ComponentExportKind,
idx: u32,
ty: Option<ComponentTypeRef>,
) -> u32 { self.exports().export(name, kind, idx, ty); self.inc_kind(kind)
}
/// Creates a new encoder for the next core type in this component. pubfn core_type(&mutself) -> (u32, ComponentCoreTypeEncoder<'_>) {
(inc(&mutself.core_types), self.core_types().ty())
}
/// Creates a new encoder for the next type in this component. pubfn ty(&mutself) -> (u32, ComponentTypeEncoder<'_>) {
(inc(&mutself.types), self.types().ty())
}
/// Creates a new instance type within this component. pubfn type_instance(&mutself, ty: &InstanceType) -> u32 { self.types().instance(ty);
inc(&mutself.types)
}
/// Creates a new component type within this component. pubfn type_component(&mutself, ty: &ComponentType) -> u32 { self.types().component(ty);
inc(&mutself.types)
}
/// Creates a new defined component type within this component. pubfn type_defined(&mutself) -> (u32, ComponentDefinedTypeEncoder<'_>) {
(inc(&mutself.types), self.types().defined_type())
}
/// Creates a new component function type within this component. pubfn type_function(&mutself) -> (u32, ComponentFuncTypeEncoder<'_>) {
(inc(&mutself.types), self.types().function())
}
/// Defines a new subcomponent of this component. pubfn component(&mutself, mut builder: ComponentBuilder) -> u32 {
builder.flush(); self.flush(); self.component
.section(&NestedComponentSection(&builder.component));
inc(&mutself.components)
}
/// Defines a new subcomponent of this component. pubfn component_raw(&mutself, data: &[u8]) -> u32 { let raw_section = RawSection {
id: ComponentSectionId::Component.into(),
data,
}; self.flush(); self.component.section(&raw_section);
inc(&mutself.components)
}
/// Instantiates the `component_index` specified with the `args` specified. pubfn instantiate<A, S>(&mutself, component_index: u32, args: A) -> u32 where
A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
A::IntoIter: ExactSizeIterator,
S: AsRef<str>,
{ self.component_instances()
.instantiate(component_index, args);
inc(&mutself.instances)
}
/// Declares a new `resource.drop` intrinsic. pubfn resource_drop(&mutself, ty: u32) -> u32 { self.canonical_functions().resource_drop(ty);
inc(&mutself.core_funcs)
}
/// Declares a new `resource.new` intrinsic. pubfn resource_new(&mutself, ty: u32) -> u32 { self.canonical_functions().resource_new(ty);
inc(&mutself.core_funcs)
}
/// Declares a new `resource.rep` intrinsic. pubfn resource_rep(&mutself, ty: u32) -> u32 { self.canonical_functions().resource_rep(ty);
inc(&mutself.core_funcs)
}
/// Declares a new `thread.spawn` intrinsic. pubfn thread_spawn(&mutself, ty: u32) -> u32 { self.canonical_functions().thread_spawn(ty);
inc(&mutself.core_funcs)
}
/// Declares a new `thread.hw_concurrency` intrinsic. pubfn thread_hw_concurrency(&mutself) -> u32 { self.canonical_functions().thread_hw_concurrency();
inc(&mutself.core_funcs)
}
/// Adds a new custom section to this component. pubfn custom_section(&mutself, section: &CustomSection<'_>) { self.flush(); self.component.section(section);
}
/// Adds a new custom section to this component. pubfn raw_custom_section(&mutself, section: &[u8]) { self.flush(); self.component.section(&RawCustomSection(section));
}
}
// Helper macro to generate methods on `ComponentBuilder` to get specific // section encoders that automatically flush and write out prior sections as // necessary.
macro_rules! section_accessors {
($($method:ident => $section:ident)*) => ( #[derive(Debug, Default)] enum LastSection { #[default]
None,
$($section($section),)*
}
impl ComponentBuilder {
$( fn $method(&mutself) -> &mut $section { match &self.last_section { // The last encoded section matches the section that's // being requested, so no change is necessary.
LastSection::$section(_) => {}
// Otherwise the last section didn't match this section, // so flush any prior section if needed and start // encoding the desired section of this method.
_ => { self.flush(); self.last_section = LastSection::$section($section::new());
}
} match &mutself.last_section {
LastSection::$section(ret) => ret,
_ => unreachable!()
}
}
)*
/// Writes out the last section into the final component binary if /// there is a section specified, otherwise does nothing. fn flush(&mutself) { match mem::take(&mutself.last_section) {
LastSection::None => {}
$(
LastSection::$section(section) => { self.component.section(§ion);
}
)*
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.