2017-05-02 08:01:57 +03:00
|
|
|
use elements;
|
|
|
|
use super::invoke::{Invoke, Identity};
|
|
|
|
|
2017-09-04 12:00:01 +03:00
|
|
|
#[derive(Debug)]
|
2017-05-02 08:01:57 +03:00
|
|
|
pub struct TableDefinition {
|
|
|
|
pub min: u32,
|
2017-09-04 12:00:01 +03:00
|
|
|
pub max: Option<u32>,
|
2017-05-02 08:01:57 +03:00
|
|
|
pub elements: Vec<TableEntryDefinition>,
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:00:01 +03:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:00:01 +03:00
|
|
|
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 {
|
|
|
|
offset: elements::InitExpr::new(vec![elements::Opcode::I32Const(index as i32)]),
|
|
|
|
values: values,
|
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> F::Result {
|
|
|
|
self.callback.invoke(self.table)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TableDefinition {
|
|
|
|
fn default() -> Self {
|
|
|
|
TableDefinition {
|
|
|
|
min: 0,
|
2017-09-04 12:00:01 +03:00
|
|
|
max: None,
|
2017-05-02 08:01:57 +03:00
|
|
|
elements: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|