diff --git a/src/builder/code.rs b/src/builder/code.rs index e33cf8f..e86d53e 100644 --- a/src/builder/code.rs +++ b/src/builder/code.rs @@ -15,6 +15,7 @@ pub struct SignatureBuilder { } impl SignatureBuilder { + /// New signature builder pub fn new() -> Self { SignatureBuilder::with_callback(Identity) } @@ -142,6 +143,7 @@ impl SignaturesBuilder { } impl SignaturesBuilder { + /// New builder chained with specified callback pub fn with_callback(callback: F) -> Self { SignaturesBuilder { callback: callback, @@ -149,17 +151,20 @@ impl SignaturesBuilder { } } + /// Push new signature into the builder output pub fn with_signature(mut self, signature: Signature) -> Self { self.section.push(signature); self } + /// Start building new signature with `TypeRefBuilder` pub fn type_ref(self) -> TypeRefBuilder { TypeRefBuilder::with_callback(self) } } impl SignaturesBuilder where F: Invoke { + /// Start building new signature with dedicated builder pub fn signature(self) -> SignatureBuilder { SignatureBuilder::with_callback(self) } @@ -182,6 +187,8 @@ impl Invoke for SignaturesBuilder { } impl SignaturesBuilder where F: Invoke { + + /// Finalize builder spawning element pub fn build(self) -> F::Result { let mut result = elements::FunctionSection::default(); for f in self.section.into_iter() { @@ -195,20 +202,24 @@ impl SignaturesBuilder where F: Invoke { } } +/// Signature bindings pub type SignatureBindings = Vec; impl SignaturesBuilder where F: Invoke { + /// Bind signature list pub fn bind(self) -> F::Result { self.callback.invoke(self.section) } } +/// Function body (code) builder pub struct FuncBodyBuilder { callback: F, body: elements::FuncBody, } impl FuncBodyBuilder { + /// New body (code) builder given the chain callback pub fn with_callback(callback: F) -> Self { FuncBodyBuilder { callback: callback, @@ -218,26 +229,31 @@ impl FuncBodyBuilder { } impl FuncBodyBuilder where F: Invoke { + /// Set/override entirely with FuncBody struct pub fn with_func(mut self, func: elements::FuncBody) -> Self { self.body = func; self } + /// Extend function local list with new entries pub fn with_locals(mut self, locals: Vec) -> Self { self.body.locals_mut().extend(locals); self } + /// Set code of the function pub fn with_opcodes(mut self, opcodes: elements::Opcodes) -> Self { *self.body.code_mut() = opcodes; self } + /// Finish current builder spawning resulting struct pub fn build(self) -> F::Result { self.callback.invoke(self.body) } } +/// Function definition (extended structure to specify function entirely, incl. signature, mainness and code) pub struct FunctionDefinition { pub is_main: bool, pub signature: Signature, @@ -254,18 +270,21 @@ impl Default for FunctionDefinition { } } +/// Function definition builder pub struct FunctionBuilder { callback: F, func: FunctionDefinition, } impl FunctionBuilder { + /// New function builder pub fn new() -> Self { FunctionBuilder::with_callback(Identity) } } impl FunctionBuilder where F: Invoke { + /// New function builder with chained callback pub fn with_callback(callback: F) -> Self { FunctionBuilder { callback: callback, @@ -273,29 +292,35 @@ impl FunctionBuilder where F: Invoke { } } + /// Set that this function is main entry point pub fn main(mut self) -> Self { self.func.is_main = true; self } + /// Start signature builder of the function pub fn signature(self) -> SignatureBuilder { SignatureBuilder::with_callback(self) } + /// Override current signature entirely with new one from known struct pub fn with_signature(mut self, signature: Signature) -> Self { self.func.signature = signature; self } + /// Start code (body) builder pub fn body(self) -> FuncBodyBuilder { FuncBodyBuilder::with_callback(self) } + /// Set body (code) for this function pub fn with_body(mut self, body: elements::FuncBody) -> Self { self.func.code = body; self } + /// Finalize current builder spawning resulting struct in the callback pub fn build(self) -> F::Result { self.callback.invoke(self.func) } diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 771174c..10ec945 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -13,7 +13,7 @@ mod data; pub use self::code::{ signatures, signature, function, SignatureBuilder, SignaturesBuilder, - FunctionBuilder, TypeRefBuilder + FunctionBuilder, TypeRefBuilder, FuncBodyBuilder, FunctionDefinition, }; pub use self::invoke::Identity; pub use self::module::{module, from_module, ModuleBuilder};