add few mutable methods

This commit is contained in:
NikVolf
2017-04-07 14:04:27 +03:00
parent 5aaab83aed
commit 30ee09837f
6 changed files with 110 additions and 43 deletions

View File

@ -103,21 +103,21 @@ impl<F> TypeRefBuilder<F> where F: Invoke<u32> {
pub fn build(self) -> F::Result { self.callback.invoke(self.type_ref) } pub fn build(self) -> F::Result { self.callback.invoke(self.type_ref) }
} }
pub struct FunctionsBuilder<F=Identity> { pub struct SignaturesBuilder<F=Identity> {
callback: F, callback: F,
section: Vec<Signature>, section: Vec<Signature>,
} }
impl FunctionsBuilder { impl SignaturesBuilder {
/// New empty functions section builder /// New empty functions section builder
pub fn new() -> Self { pub fn new() -> Self {
FunctionsBuilder::with_callback(Identity) SignaturesBuilder::with_callback(Identity)
} }
} }
impl<F> FunctionsBuilder<F> { impl<F> SignaturesBuilder<F> {
pub fn with_callback(callback: F) -> Self { pub fn with_callback(callback: F) -> Self {
FunctionsBuilder { SignaturesBuilder {
callback: callback, callback: callback,
section: Vec::new(), section: Vec::new(),
} }
@ -133,13 +133,13 @@ impl<F> FunctionsBuilder<F> {
} }
} }
impl<F> FunctionsBuilder<F> where F: Invoke<SignatureBindings> { impl<F> SignaturesBuilder<F> where F: Invoke<SignatureBindings> {
pub fn signature(self) -> SignatureBuilder<Self> { pub fn signature(self) -> SignatureBuilder<Self> {
SignatureBuilder::with_callback(self) SignatureBuilder::with_callback(self)
} }
} }
impl<F> Invoke<elements::FunctionType> for FunctionsBuilder<F> { impl<F> Invoke<elements::FunctionType> for SignaturesBuilder<F> {
type Result = Self; type Result = Self;
fn invoke(self, signature: elements::FunctionType) -> Self { fn invoke(self, signature: elements::FunctionType) -> Self {
@ -147,7 +147,7 @@ impl<F> Invoke<elements::FunctionType> for FunctionsBuilder<F> {
} }
} }
impl<F> Invoke<u32> for FunctionsBuilder<F> { impl<F> Invoke<u32> for SignaturesBuilder<F> {
type Result = Self; type Result = Self;
fn invoke(self, type_ref: u32) -> Self { fn invoke(self, type_ref: u32) -> Self {
@ -155,7 +155,7 @@ impl<F> Invoke<u32> for FunctionsBuilder<F> {
} }
} }
impl<F> FunctionsBuilder<F> where F: Invoke<elements::FunctionsSection> { impl<F> SignaturesBuilder<F> where F: Invoke<elements::FunctionsSection> {
pub fn build(self) -> F::Result { pub fn build(self) -> F::Result {
let mut result = elements::FunctionsSection::new(); let mut result = elements::FunctionsSection::new();
for f in self.section.into_iter() { for f in self.section.into_iter() {
@ -171,31 +171,31 @@ impl<F> FunctionsBuilder<F> where F: Invoke<elements::FunctionsSection> {
pub type SignatureBindings = Vec<Signature>; pub type SignatureBindings = Vec<Signature>;
impl<F> FunctionsBuilder<F> where F: Invoke<SignatureBindings> { impl<F> SignaturesBuilder<F> where F: Invoke<SignatureBindings> {
pub fn bind(self) -> F::Result { pub fn bind(self) -> F::Result {
self.callback.invoke(self.section) self.callback.invoke(self.section)
} }
} }
/// New function builder. /// New function builder.
pub fn function() -> FunctionsBuilder { pub fn signatures() -> SignaturesBuilder {
FunctionsBuilder::new() SignaturesBuilder::new()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::function; use super::signatures;
#[test] #[test]
fn example() { fn example() {
let result = function() let result = signatures()
.type_ref().val(1).build() .type_ref().val(1).build()
.build(); .build();
assert_eq!(result.entries().len(), 1); assert_eq!(result.entries().len(), 1);
let result = function() let result = signatures()
.signature() .signature()
.param().i32() .param().i32()
.param().i32() .param().i32()

39
src/builder/import.rs Normal file
View File

@ -0,0 +1,39 @@
use super::invoke::{Invoke, Identity};
use elements;
pub struct ImportBuilder<F: Identity> {
callback: F,
module: String,
field: String,
binding: ExternalBinding,
}
impl<F> ImportBuilder<F> {
pub fn with_callback(callback: F) -> Self {
ImportBuilder {
callback: callback,
module
}
}
pub fn external(self) -> ImportExternalBuilder<Self> {
}
}
pub struct ImportExternalBuilder<F=Identity> {
callback: F,
binding: ExternalBinding,
}
impl<F> ImportExternalBuilder<F> where F: Invoke<ExternalBinding> {
pub fn with_callback(callback: F) {
ImportExternalBuilder{
callback: callback,
binding: ExternalBinding::ExistingFunc(0),
}
}
pub fn
}

View File

@ -90,4 +90,3 @@ impl<F> ValueTypesBuilder<F> where F: Invoke<Vec<elements::ValueType>> {
self.callback.invoke(self.value_types) self.callback.invoke(self.value_types)
} }
} }

View File

@ -6,4 +6,4 @@ mod code;
mod misc; mod misc;
pub use self::module::{module, ModuleBuilder}; pub use self::module::{module, ModuleBuilder};
pub use self::code::function; pub use self::code::signatures;

View File

@ -1,5 +1,5 @@
use super::invoke::{Invoke, Identity}; use super::invoke::{Invoke, Identity};
use super::code::{self, FunctionsBuilder}; use super::code::{self, SignaturesBuilder};
use elements; use elements;
/// Module builder /// Module builder
@ -86,34 +86,39 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
/// Binds to the type section, creates additional types when required /// Binds to the type section, creates additional types when required
pub fn with_signatures(mut self, bindings: code::SignatureBindings) -> Self { pub fn with_signatures(mut self, bindings: code::SignatureBindings) -> Self {
// todo bind to type section self.push_signatures(bindings);
{
let module = &mut self.module;
let raw_functions: Vec<u32> = 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 self
} }
/// Push signatures in the module, returning corresponding indices of pushed signatures
pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> {
let module = &mut self.module;
let mut result = Vec::new();
// todo: maybe reuse existing types with the equal signatures
let raw_functions: Vec<u32> = 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 /// Define functions section
pub fn functions(self) -> FunctionsBuilder<Self> { pub fn functions(self) -> SignaturesBuilder<Self> {
FunctionsBuilder::with_callback(self) SignaturesBuilder::with_callback(self)
} }
/// Build module (final step) /// Build module (final step)

View File

@ -210,12 +210,36 @@ pub struct ImportEntry {
} }
impl 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. /// Module reference of the import entry.
pub fn module(&self) -> &str { &self.module_str } 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. /// Field reference of the import entry.
pub fn field(&self) -> &str { &self.field_str } 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. /// Local binidng of the import entry.
pub fn external(&self) -> &External { &self.external } 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 { impl Deserialize for ImportEntry {