pub struct Element { /* private fields */ }Expand description
The initial contents of a table is uninitialized. Element segments can be used to initialize a subrange of a table from a static vector of elements. Each element segment defines a reference type and a corresponding list of constant element expressions. Element segments have a mode that identifies them as either passive, active, or declarative. A passive element segment’s elements can be copied to a table using the 𝗍𝖺𝖻𝗅𝖾.𝗂𝗇𝗂𝗍 instruction. An active element segment copies its elements into a table during instantiation, as specified by a table index and a constant expression defining an offset into that table. A declarative element segment is not available at runtime but merely serves to forward-declare references that are formed in code with instructions like 𝗋𝖾𝖿.𝖿𝗎𝗇𝖼. The 𝗈𝖿𝖿𝗌𝖾𝗍 is given by a constant expression. Element segments are referenced through element indices.
See https://webassembly.github.io/spec/core/syntax/modules.html#element-segments
§Examples
§Active
use wasm_ast::{Element, ElementInitializer, ElementMode, TableIndex, FunctionIndex, Expression, ReferenceType};
let offset: Expression = vec![0i32.into()].into();
let initializers = vec![0].to_initializers();
let element = Element::active(0, offset.clone(), ReferenceType::Function, initializers.clone());
assert_eq!(element, Element::new(
ReferenceType::Function,
ElementMode::Active(0, offset.clone()),
initializers.clone()
));
assert_eq!(element.kind(), ReferenceType::Function);
assert_eq!(element.mode(), &ElementMode::Active(0, offset.clone()));
assert_eq!(element.initializers(), initializers.as_slice());§Passive
use wasm_ast::{Element, ElementInitializer, ElementMode, TableIndex, Expression, ReferenceType, NumericInstruction};
let initializers = vec![Expression::from(vec![2i32.into()])].to_initializers();
let element = Element::passive(ReferenceType::External, initializers.clone());
assert_eq!(element, Element::new(
ReferenceType::External,
ElementMode::Passive,
initializers.clone()
));
assert_eq!(element.kind(), ReferenceType::External);
assert_eq!(element.mode(), &ElementMode::Passive);
assert_eq!(element.initializers(), initializers.as_slice());§Declarative
use wasm_ast::{Element, ElementInitializer, ElementMode, TableIndex, Expression, ReferenceType, NumericInstruction};
let initializer: Vec<Expression> = vec![Expression::from(vec![2i32.into()])].into();
let element = Element::declarative(ReferenceType::External, initializer.clone());
assert_eq!(element, Element::new(
ReferenceType::External,
ElementMode::Declarative,
initializer.clone()
));
assert_eq!(element.kind(), ReferenceType::External);
assert_eq!(element.mode(), &ElementMode::Declarative);
assert_eq!(element.initializers(), &initializer);Implementations§
Source§impl Element
impl Element
Sourcepub fn new(
kind: ReferenceType,
mode: ElementMode,
initializers: Vec<Expression>,
) -> Self
pub fn new( kind: ReferenceType, mode: ElementMode, initializers: Vec<Expression>, ) -> Self
Creates a new instance of an element segment.
Sourcepub fn passive(kind: ReferenceType, initializers: Vec<Expression>) -> Self
pub fn passive(kind: ReferenceType, initializers: Vec<Expression>) -> Self
Creates a passive element segment.
Sourcepub fn active(
table: TableIndex,
offset: Expression,
kind: ReferenceType,
initializers: Vec<Expression>,
) -> Self
pub fn active( table: TableIndex, offset: Expression, kind: ReferenceType, initializers: Vec<Expression>, ) -> Self
Creates an active element segment.
Sourcepub fn declarative(kind: ReferenceType, initializers: Vec<Expression>) -> Self
pub fn declarative(kind: ReferenceType, initializers: Vec<Expression>) -> Self
Creates a declarative element segment.
Sourcepub fn kind(&self) -> ReferenceType
pub fn kind(&self) -> ReferenceType
The reference type of the element segment.
Sourcepub fn initializers(&self) -> &[Expression]
pub fn initializers(&self) -> &[Expression]
The initializer for the element segment.
Sourcepub fn mode(&self) -> &ElementMode
pub fn mode(&self) -> &ElementMode
The mode of the element segment.