diff --git a/src/builder/code.rs b/src/builder/code.rs index a4d237a..8f11683 100644 --- a/src/builder/code.rs +++ b/src/builder/code.rs @@ -177,6 +177,40 @@ impl SignaturesBuilder where F: Invoke { } } +pub struct FunctionDefinition { + pub signature: Signature, + pub code: elements::FuncBody, +} + +impl Default for FunctionDefinition { + fn default() -> Self { + FunctionDefinition { + signature: Signature::TypeReference(0), + code: elements::FuncBody::empty(), + } + } +} + +pub struct FunctionBuilder { + callback: F, + code: FunctionDefinition, +} + +impl FunctionBuilder { + pub fn new() -> Self { + FunctionBuilder::with_callback(Identity) + } +} + +impl FunctionBuilder where F: Invoke { + pub fn with_callback(callback: F) -> Self { + FunctionBuilder { + callback: callback, + code: Default::default(), + } + } +} + /// New function builder. pub fn signatures() -> SignaturesBuilder { SignaturesBuilder::new() diff --git a/src/elements/func.rs b/src/elements/func.rs index 3ae29de..f022e72 100644 --- a/src/elements/func.rs +++ b/src/elements/func.rs @@ -68,6 +68,11 @@ impl FuncBody { FuncBody { locals: locals, opcodes: opcodes } } + /// List of individual opcodes + pub fn empty() -> Self { + FuncBody { locals: Vec::new(), opcodes: Opcodes::empty() } + } + /// Locals declared in function body. pub fn locals(&self) -> &[Local] { &self.locals } diff --git a/src/elements/ops.rs b/src/elements/ops.rs index 23fcdec..68710d1 100644 --- a/src/elements/ops.rs +++ b/src/elements/ops.rs @@ -15,6 +15,11 @@ impl Opcodes { Opcodes(elements) } + /// Empty expression with only `Opcode::End` opcode + pub fn empty() -> Self { + Opcodes(vec![Opcode::End]) + } + /// List of individual opcodes pub fn elements(&self) -> &[Opcode] { &self.0 } }