From 81f79e84e78345fa8911382860921cf7865dce72 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 9 May 2017 17:34:36 +0300 Subject: [PATCH] global builder --- src/builder/code.rs | 1 - src/builder/global.rs | 83 ++++++++++++++++++++++++++++++++++++ src/builder/mod.rs | 4 +- src/elements/global_entry.rs | 7 +++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/builder/global.rs diff --git a/src/builder/code.rs b/src/builder/code.rs index 710a377..a863938 100644 --- a/src/builder/code.rs +++ b/src/builder/code.rs @@ -202,7 +202,6 @@ impl FuncBodyBuilder { } impl FuncBodyBuilder where F: Invoke { - pub fn with_func(mut self, func: elements::FuncBody) -> Self { self.body = func; self diff --git a/src/builder/global.rs b/src/builder/global.rs new file mode 100644 index 0000000..2cae4ba --- /dev/null +++ b/src/builder/global.rs @@ -0,0 +1,83 @@ +use super::invoke::{Invoke, Identity}; +use super::misc::ValueTypeBuilder; +use elements; + +pub struct GlobalBuilder { + callback: F, + value_type: elements::ValueType, + is_mutable: bool, + init_expr: elements::InitExpr, +} + +impl GlobalBuilder { + pub fn new() -> Self { + GlobalBuilder::with_callback(Identity) + } +} + +impl GlobalBuilder { + 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 { + ValueTypeBuilder::with_callback(self) + } +} + +impl GlobalBuilder where F: Invoke { + 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 Invoke for GlobalBuilder { + 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); + } +} \ No newline at end of file diff --git a/src/builder/mod.rs b/src/builder/mod.rs index f77ed5c..8ecea0e 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -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; \ No newline at end of file +pub use self::export::export; +pub use self::global::global; \ No newline at end of file diff --git a/src/elements/global_entry.rs b/src/elements/global_entry.rs index 9a79f4d..1f27b21 100644 --- a/src/elements/global_entry.rs +++ b/src/elements/global_entry.rs @@ -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.