71 lines
1.5 KiB
Rust
Raw Normal View History

2017-05-02 08:01:57 +03:00
use elements;
use super::invoke::{Invoke, Identity};
#[derive(Debug)]
2017-05-02 08:01:57 +03:00
pub struct TableDefinition {
pub min: u32,
pub max: Option<u32>,
2017-05-02 08:01:57 +03:00
pub elements: Vec<TableEntryDefinition>,
}
#[derive(Debug)]
2017-05-02 08:01:57 +03:00
pub struct TableEntryDefinition {
2017-05-02 19:10:23 +03:00
pub offset: elements::InitExpr,
pub values: Vec<u32>,
2017-05-02 08:01:57 +03:00
}
pub struct TableBuilder<F=Identity> {
callback: F,
table: TableDefinition,
}
impl TableBuilder {
pub fn new() -> Self {
TableBuilder::with_callback(Identity)
}
}
impl<F> TableBuilder<F> where F: Invoke<TableDefinition> {
pub fn with_callback(callback: F) -> Self {
TableBuilder {
callback: callback,
table: Default::default(),
}
}
pub fn with_min(mut self, min: u32) -> Self {
self.table.min = min;
self
}
pub fn with_max(mut self, max: Option<u32>) -> Self {
self.table.max = max;
self
}
2017-05-02 08:01:57 +03:00
pub fn with_element(mut self, index: u32, values: Vec<u32>) -> Self {
self.table.elements.push(TableEntryDefinition {
2017-12-05 16:31:07 +01:00
offset: elements::InitExpr::new(vec![
elements::Opcode::I32Const(index as i32),
elements::Opcode::End,
]),
2017-05-02 08:01:57 +03:00
values: values,
});
self
}
pub fn build(self) -> F::Result {
self.callback.invoke(self.table)
}
}
impl Default for TableDefinition {
fn default() -> Self {
TableDefinition {
min: 0,
max: None,
2017-05-02 08:01:57 +03:00
elements: Vec::new(),
}
}
}