diff --git a/src/builder/code.rs b/src/builder/code.rs index def72bb..a4d237a 100644 --- a/src/builder/code.rs +++ b/src/builder/code.rs @@ -103,21 +103,21 @@ impl TypeRefBuilder where F: Invoke { pub fn build(self) -> F::Result { self.callback.invoke(self.type_ref) } } -pub struct FunctionsBuilder { +pub struct SignaturesBuilder { callback: F, section: Vec, } -impl FunctionsBuilder { +impl SignaturesBuilder { /// New empty functions section builder pub fn new() -> Self { - FunctionsBuilder::with_callback(Identity) + SignaturesBuilder::with_callback(Identity) } } -impl FunctionsBuilder { +impl SignaturesBuilder { pub fn with_callback(callback: F) -> Self { - FunctionsBuilder { + SignaturesBuilder { callback: callback, section: Vec::new(), } @@ -133,13 +133,13 @@ impl FunctionsBuilder { } } -impl FunctionsBuilder where F: Invoke { +impl SignaturesBuilder where F: Invoke { pub fn signature(self) -> SignatureBuilder { SignatureBuilder::with_callback(self) } } -impl Invoke for FunctionsBuilder { +impl Invoke for SignaturesBuilder { type Result = Self; fn invoke(self, signature: elements::FunctionType) -> Self { @@ -147,7 +147,7 @@ impl Invoke for FunctionsBuilder { } } -impl Invoke for FunctionsBuilder { +impl Invoke for SignaturesBuilder { type Result = Self; fn invoke(self, type_ref: u32) -> Self { @@ -155,7 +155,7 @@ impl Invoke for FunctionsBuilder { } } -impl FunctionsBuilder where F: Invoke { +impl SignaturesBuilder where F: Invoke { pub fn build(self) -> F::Result { let mut result = elements::FunctionsSection::new(); for f in self.section.into_iter() { @@ -171,31 +171,31 @@ impl FunctionsBuilder where F: Invoke { pub type SignatureBindings = Vec; -impl FunctionsBuilder where F: Invoke { +impl SignaturesBuilder where F: Invoke { pub fn bind(self) -> F::Result { self.callback.invoke(self.section) } } /// New function builder. -pub fn function() -> FunctionsBuilder { - FunctionsBuilder::new() +pub fn signatures() -> SignaturesBuilder { + SignaturesBuilder::new() } #[cfg(test)] mod tests { - use super::function; + use super::signatures; #[test] fn example() { - let result = function() + let result = signatures() .type_ref().val(1).build() .build(); assert_eq!(result.entries().len(), 1); - let result = function() + let result = signatures() .signature() .param().i32() .param().i32() diff --git a/src/builder/import.rs b/src/builder/import.rs new file mode 100644 index 0000000..1137aa5 --- /dev/null +++ b/src/builder/import.rs @@ -0,0 +1,39 @@ +use super::invoke::{Invoke, Identity}; +use elements; + +pub struct ImportBuilder { + callback: F, + module: String, + field: String, + binding: ExternalBinding, +} + +impl ImportBuilder { + + pub fn with_callback(callback: F) -> Self { + ImportBuilder { + callback: callback, + module + } + } + + pub fn external(self) -> ImportExternalBuilder { + + } +} + +pub struct ImportExternalBuilder { + callback: F, + binding: ExternalBinding, +} + +impl ImportExternalBuilder where F: Invoke { + pub fn with_callback(callback: F) { + ImportExternalBuilder{ + callback: callback, + binding: ExternalBinding::ExistingFunc(0), + } + } + + pub fn +} \ No newline at end of file diff --git a/src/builder/misc.rs b/src/builder/misc.rs index 9c6bc6e..6a18fd2 100644 --- a/src/builder/misc.rs +++ b/src/builder/misc.rs @@ -89,5 +89,4 @@ impl ValueTypesBuilder where F: Invoke> { pub fn build(self) -> F::Result { self.callback.invoke(self.value_types) } -} - +} \ No newline at end of file diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 599e40b..2972ffb 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -6,4 +6,4 @@ mod code; mod misc; pub use self::module::{module, ModuleBuilder}; -pub use self::code::function; \ No newline at end of file +pub use self::code::signatures; \ No newline at end of file diff --git a/src/builder/module.rs b/src/builder/module.rs index 062fbca..dd272f5 100644 --- a/src/builder/module.rs +++ b/src/builder/module.rs @@ -1,5 +1,5 @@ use super::invoke::{Invoke, Identity}; -use super::code::{self, FunctionsBuilder}; +use super::code::{self, SignaturesBuilder}; use elements; /// Module builder @@ -86,34 +86,39 @@ impl ModuleBuilder where F: Invoke { /// Binds to the type section, creates additional types when required pub fn with_signatures(mut self, bindings: code::SignatureBindings) -> Self { - // todo bind to type section - - { - let module = &mut self.module; - - let raw_functions: Vec = bindings.into_iter().map(|binding| - match binding { - code::Signature::Inline(func_type) => { - module.types.types_mut().push(elements::Type::Function(func_type)); - module.types.types().len() as u32 - 1 - } - code::Signature::TypeReference(type_ref) => { - type_ref - } - } - ).collect(); - - for function in raw_functions { - module.functions.entries_mut().push(elements::Func::new(function)); - } - } - + self.push_signatures(bindings); self } + /// Push signatures in the module, returning corresponding indices of pushed signatures + pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec { + let module = &mut self.module; + let mut result = Vec::new(); + + // todo: maybe reuse existing types with the equal signatures + let raw_functions: Vec = signatures.into_iter().map(|binding| + match binding { + code::Signature::Inline(func_type) => { + module.types.types_mut().push(elements::Type::Function(func_type)); + module.types.types().len() as u32 - 1 + } + code::Signature::TypeReference(type_ref) => { + type_ref + } + } + ).collect(); + + for function in raw_functions { + module.functions.entries_mut().push(elements::Func::new(function)); + result.push(module.functions.entries_mut().len() as u32 - 1); + } + + result + } + /// Define functions section - pub fn functions(self) -> FunctionsBuilder { - FunctionsBuilder::with_callback(self) + pub fn functions(self) -> SignaturesBuilder { + SignaturesBuilder::with_callback(self) } /// Build module (final step) diff --git a/src/elements/import_entry.rs b/src/elements/import_entry.rs index 42bea48..fd08ae1 100644 --- a/src/elements/import_entry.rs +++ b/src/elements/import_entry.rs @@ -210,12 +210,36 @@ pub struct ImportEntry { } impl ImportEntry { + /// New import entry. + pub fn new(module_str: String, field_str: String, external: External) -> Self { + ImportEntry { + module_str: module_str, + field_str: field_str, + external: external, + } + } + /// Module reference of the import entry. pub fn module(&self) -> &str { &self.module_str } + + /// Module reference of the import entry (mutable). + pub fn module_mut(&mut self) -> &mut str { + &mut self.module_str + } + /// Field reference of the import entry. pub fn field(&self) -> &str { &self.field_str } + + /// Field reference of the import entry (mutable) + pub fn field_mut(&mut self) -> &mut str { + &mut self.field_str + } + /// Local binidng of the import entry. pub fn external(&self) -> &External { &self.external } + + /// Local binidng of the import entry (mutable) + pub fn external_mut(&mut self) -> &External { &mut self.external } } impl Deserialize for ImportEntry {