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 struct FunctionsBuilder<F=Identity> {
pub struct SignaturesBuilder<F=Identity> {
callback: F,
section: Vec<Signature>,
}
impl FunctionsBuilder {
impl SignaturesBuilder {
/// New empty functions section builder
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 {
FunctionsBuilder {
SignaturesBuilder {
callback: callback,
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> {
SignatureBuilder::with_callback(self)
}
}
impl<F> Invoke<elements::FunctionType> for FunctionsBuilder<F> {
impl<F> Invoke<elements::FunctionType> for SignaturesBuilder<F> {
type Result = 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;
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 {
let mut result = elements::FunctionsSection::new();
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>;
impl<F> FunctionsBuilder<F> where F: Invoke<SignatureBindings> {
impl<F> SignaturesBuilder<F> where F: Invoke<SignatureBindings> {
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()

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)
}
}

View File

@ -6,4 +6,4 @@ mod code;
mod misc;
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::code::{self, FunctionsBuilder};
use super::code::{self, SignaturesBuilder};
use elements;
/// Module builder
@ -86,12 +86,17 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
/// 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
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<u32> {
let module = &mut self.module;
let mut result = Vec::new();
let raw_functions: Vec<u32> = bindings.into_iter().map(|binding|
// 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));
@ -105,15 +110,15 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
for function in raw_functions {
module.functions.entries_mut().push(elements::Func::new(function));
}
result.push(module.functions.entries_mut().len() as u32 - 1);
}
self
result
}
/// Define functions section
pub fn functions(self) -> FunctionsBuilder<Self> {
FunctionsBuilder::with_callback(self)
pub fn functions(self) -> SignaturesBuilder<Self> {
SignaturesBuilder::with_callback(self)
}
/// Build module (final step)

View File

@ -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 {