global builder

This commit is contained in:
NikVolf
2017-05-09 17:34:36 +03:00
parent 1fae5952e1
commit 81f79e84e7
4 changed files with 93 additions and 2 deletions

View File

@ -202,7 +202,6 @@ impl<F> FuncBodyBuilder<F> {
}
impl<F> FuncBodyBuilder<F> where F: Invoke<elements::FuncBody> {
pub fn with_func(mut self, func: elements::FuncBody) -> Self {
self.body = func;
self

83
src/builder/global.rs Normal file
View File

@ -0,0 +1,83 @@
use super::invoke::{Invoke, Identity};
use super::misc::ValueTypeBuilder;
use elements;
pub struct GlobalBuilder<F=Identity> {
callback: F,
value_type: elements::ValueType,
is_mutable: bool,
init_expr: elements::InitExpr,
}
impl GlobalBuilder {
pub fn new() -> Self {
GlobalBuilder::with_callback(Identity)
}
}
impl<F> GlobalBuilder<F> {
pub fn with_callback(callback: F) -> Self {
GlobalBuilder {
callback: callback,
value_type: elements::ValueType::I32,
init_expr: elements::InitExpr::empty(),
is_mutable: false,
}
}
pub fn with_type(mut self, value_type: elements::ValueType) -> Self {
self.value_type = value_type;
self
}
pub fn mutable(mut self) -> Self {
self.is_mutable = true;
self
}
pub fn init_expr(mut self, opcode: elements::Opcode) -> Self {
self.init_expr = elements::InitExpr::new(vec![opcode, elements::Opcode::End]);
self
}
pub fn value_type(self) -> ValueTypeBuilder<Self> {
ValueTypeBuilder::with_callback(self)
}
}
impl<F> GlobalBuilder<F> where F: Invoke<elements::GlobalEntry> {
pub fn build(self) -> F::Result {
self.callback.invoke(
elements::GlobalEntry::new(
elements::GlobalType::new(self.value_type, self.is_mutable),
self.init_expr,
)
)
}
}
impl<F> Invoke<elements::ValueType> for GlobalBuilder<F> {
type Result = Self;
fn invoke(self, the_type: elements::ValueType) -> Self {
self.with_type(the_type)
}
}
/// New builder for export entry
pub fn global() -> GlobalBuilder {
GlobalBuilder::new()
}
#[cfg(test)]
mod tests {
use super::global;
use elements;
#[test]
fn example() {
let entry = global().value_type().i32().build();
assert_eq!(entry.global_type().content_type(), elements::ValueType::I32);
assert_eq!(entry.global_type().is_mutable(), false);
}
}

View File

@ -6,8 +6,10 @@ mod code;
mod misc;
mod import;
mod export;
mod global;
pub use self::module::{module, from_module, ModuleBuilder};
pub use self::code::{signatures, signature, function};
pub use self::import::import;
pub use self::export::export;
pub use self::export::export;
pub use self::global::global;

View File

@ -8,6 +8,13 @@ pub struct GlobalEntry {
}
impl GlobalEntry {
/// New global entry
pub fn new(global_type: GlobalType, init_expr: InitExpr) -> Self {
GlobalEntry {
global_type: global_type,
init_expr: init_expr,
}
}
/// Global type.
pub fn global_type(&self) -> &GlobalType { &self.global_type }
/// Initialization expression (opcodes) for global.