mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-27 21:52:03 +00:00
code builder doc effort
This commit is contained in:
@ -2,11 +2,13 @@ use elements;
|
|||||||
use super::invoke::{Invoke, Identity};
|
use super::invoke::{Invoke, Identity};
|
||||||
use super::misc::{ValueTypeBuilder, ValueTypesBuilder, OptionalValueTypeBuilder};
|
use super::misc::{ValueTypeBuilder, ValueTypesBuilder, OptionalValueTypeBuilder};
|
||||||
|
|
||||||
|
/// Signature template description
|
||||||
pub enum Signature {
|
pub enum Signature {
|
||||||
TypeReference(u32),
|
TypeReference(u32),
|
||||||
Inline(elements::FunctionType),
|
Inline(elements::FunctionType),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Signature builder
|
||||||
pub struct SignatureBuilder<F=Identity> {
|
pub struct SignatureBuilder<F=Identity> {
|
||||||
callback: F,
|
callback: F,
|
||||||
signature: elements::FunctionType,
|
signature: elements::FunctionType,
|
||||||
@ -19,44 +21,53 @@ impl SignatureBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<F> SignatureBuilder<F> where F: Invoke<elements::FunctionType> {
|
impl<F> SignatureBuilder<F> where F: Invoke<elements::FunctionType> {
|
||||||
|
/// New builder with callback function specified
|
||||||
pub fn with_callback(callback: F) -> Self {
|
pub fn with_callback(callback: F) -> Self {
|
||||||
SignatureBuilder {
|
SignatureBuilder {
|
||||||
callback: callback,
|
callback: callback,
|
||||||
signature: elements::FunctionType::default(),
|
signature: elements::FunctionType::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add argument to signature builder
|
||||||
pub fn with_param(mut self, value_type: elements::ValueType) -> Self {
|
pub fn with_param(mut self, value_type: elements::ValueType) -> Self {
|
||||||
self.signature.params_mut().push(value_type);
|
self.signature.params_mut().push(value_type);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add multiple arguments to signature builder
|
||||||
pub fn with_params(mut self, value_types: Vec<elements::ValueType>) -> Self {
|
pub fn with_params(mut self, value_types: Vec<elements::ValueType>) -> Self {
|
||||||
self.signature.params_mut().extend(value_types);
|
self.signature.params_mut().extend(value_types);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Override signature return type
|
||||||
pub fn with_return_type(mut self, return_type: Option<elements::ValueType>) -> Self {
|
pub fn with_return_type(mut self, return_type: Option<elements::ValueType>) -> Self {
|
||||||
*self.signature.return_type_mut() = return_type;
|
*self.signature.return_type_mut() = return_type;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start build new argument
|
||||||
pub fn param(self) -> ValueTypeBuilder<Self> {
|
pub fn param(self) -> ValueTypeBuilder<Self> {
|
||||||
ValueTypeBuilder::with_callback(self)
|
ValueTypeBuilder::with_callback(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start build multiple arguments
|
||||||
pub fn params(self) -> ValueTypesBuilder<Self> {
|
pub fn params(self) -> ValueTypesBuilder<Self> {
|
||||||
ValueTypesBuilder::with_callback(self)
|
ValueTypesBuilder::with_callback(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start building return type
|
||||||
pub fn return_type(self) -> OptionalValueTypeBuilder<Self> {
|
pub fn return_type(self) -> OptionalValueTypeBuilder<Self> {
|
||||||
OptionalValueTypeBuilder::with_callback(self)
|
OptionalValueTypeBuilder::with_callback(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finish current builder
|
||||||
pub fn build(self) -> F::Result {
|
pub fn build(self) -> F::Result {
|
||||||
self.callback.invoke(self.signature)
|
self.callback.invoke(self.signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finish current builder returning intermediate `Signature` struct
|
||||||
pub fn build_sig(self) -> Signature {
|
pub fn build_sig(self) -> Signature {
|
||||||
Signature::Inline(self.signature)
|
Signature::Inline(self.signature)
|
||||||
}
|
}
|
||||||
@ -82,8 +93,8 @@ impl<F> Invoke<Option<elements::ValueType>> for SignatureBuilder<F>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> Invoke<elements::ValueType> for SignatureBuilder<F>
|
impl<F> Invoke<elements::ValueType> for SignatureBuilder<F>
|
||||||
where F: Invoke<elements::FunctionType>
|
where F: Invoke<elements::FunctionType>
|
||||||
{
|
{
|
||||||
type Result = Self;
|
type Result = Self;
|
||||||
|
|
||||||
@ -92,27 +103,32 @@ impl<F> Invoke<elements::ValueType> for SignatureBuilder<F>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Type (signature) reference builder (for function/import/indirect call)
|
||||||
pub struct TypeRefBuilder<F=Identity> {
|
pub struct TypeRefBuilder<F=Identity> {
|
||||||
callback: F,
|
callback: F,
|
||||||
type_ref: u32,
|
type_ref: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> TypeRefBuilder<F> where F: Invoke<u32> {
|
impl<F> TypeRefBuilder<F> where F: Invoke<u32> {
|
||||||
|
/// New builder chained with specified callback
|
||||||
pub fn with_callback(callback: F) -> Self {
|
pub fn with_callback(callback: F) -> Self {
|
||||||
TypeRefBuilder {
|
TypeRefBuilder {
|
||||||
callback: callback,
|
callback: callback,
|
||||||
type_ref: 0
|
type_ref: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set/override of type reference
|
||||||
pub fn val(mut self, val: u32) -> Self {
|
pub fn val(mut self, val: u32) -> Self {
|
||||||
self.type_ref = val;
|
self.type_ref = val;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finish current builder
|
||||||
pub fn build(self) -> F::Result { self.callback.invoke(self.type_ref) }
|
pub fn build(self) -> F::Result { self.callback.invoke(self.type_ref) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Multiple signatures builder
|
||||||
pub struct SignaturesBuilder<F=Identity> {
|
pub struct SignaturesBuilder<F=Identity> {
|
||||||
callback: F,
|
callback: F,
|
||||||
section: Vec<Signature>,
|
section: Vec<Signature>,
|
||||||
@ -140,7 +156,7 @@ impl<F> SignaturesBuilder<F> {
|
|||||||
|
|
||||||
pub fn type_ref(self) -> TypeRefBuilder<Self> {
|
pub fn type_ref(self) -> TypeRefBuilder<Self> {
|
||||||
TypeRefBuilder::with_callback(self)
|
TypeRefBuilder::with_callback(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> SignaturesBuilder<F> where F: Invoke<SignatureBindings> {
|
impl<F> SignaturesBuilder<F> where F: Invoke<SignatureBindings> {
|
||||||
@ -154,7 +170,7 @@ impl<F> Invoke<elements::FunctionType> for SignaturesBuilder<F> {
|
|||||||
|
|
||||||
fn invoke(self, signature: elements::FunctionType) -> Self {
|
fn invoke(self, signature: elements::FunctionType) -> Self {
|
||||||
self.with_signature(Signature::Inline(signature))
|
self.with_signature(Signature::Inline(signature))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> Invoke<u32> for SignaturesBuilder<F> {
|
impl<F> Invoke<u32> for SignaturesBuilder<F> {
|
||||||
@ -162,7 +178,7 @@ impl<F> Invoke<u32> for SignaturesBuilder<F> {
|
|||||||
|
|
||||||
fn invoke(self, type_ref: u32) -> Self {
|
fn invoke(self, type_ref: u32) -> Self {
|
||||||
self.with_signature(Signature::TypeReference(type_ref))
|
self.with_signature(Signature::TypeReference(type_ref))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> SignaturesBuilder<F> where F: Invoke<elements::FunctionSection> {
|
impl<F> SignaturesBuilder<F> where F: Invoke<elements::FunctionSection> {
|
||||||
@ -290,7 +306,7 @@ impl<F> Invoke<elements::FunctionType> for FunctionBuilder<F> where F: Invoke<Fu
|
|||||||
|
|
||||||
fn invoke(self, signature: elements::FunctionType) -> Self {
|
fn invoke(self, signature: elements::FunctionType) -> Self {
|
||||||
self.with_signature(Signature::Inline(signature))
|
self.with_signature(Signature::Inline(signature))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> Invoke<u32> for FunctionBuilder<F> where F: Invoke<FunctionDefinition> {
|
impl<F> Invoke<u32> for FunctionBuilder<F> where F: Invoke<FunctionDefinition> {
|
||||||
@ -298,7 +314,7 @@ impl<F> Invoke<u32> for FunctionBuilder<F> where F: Invoke<FunctionDefinition> {
|
|||||||
|
|
||||||
fn invoke(self, type_ref: u32) -> Self {
|
fn invoke(self, type_ref: u32) -> Self {
|
||||||
self.with_signature(Signature::TypeReference(type_ref))
|
self.with_signature(Signature::TypeReference(type_ref))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> Invoke<elements::FuncBody> for FunctionBuilder<F> where F: Invoke<FunctionDefinition> {
|
impl<F> Invoke<elements::FuncBody> for FunctionBuilder<F> where F: Invoke<FunctionDefinition> {
|
||||||
@ -344,7 +360,7 @@ mod tests {
|
|||||||
.param().i32()
|
.param().i32()
|
||||||
.return_type().i64()
|
.return_type().i64()
|
||||||
.build()
|
.build()
|
||||||
.bind();
|
.bind();
|
||||||
|
|
||||||
assert_eq!(result.len(), 1);
|
assert_eq!(result.len(), 1);
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,12 @@ mod export;
|
|||||||
mod global;
|
mod global;
|
||||||
mod data;
|
mod data;
|
||||||
|
|
||||||
|
pub use self::code::{
|
||||||
|
signatures, signature, function, SignatureBuilder, SignaturesBuilder,
|
||||||
|
FunctionBuilder, TypeRefBuilder
|
||||||
|
};
|
||||||
pub use self::invoke::Identity;
|
pub use self::invoke::Identity;
|
||||||
pub use self::module::{module, from_module, ModuleBuilder};
|
pub use self::module::{module, from_module, ModuleBuilder};
|
||||||
pub use self::code::{signatures, signature, function, SignatureBuilder, FunctionBuilder};
|
|
||||||
pub use self::memory::MemoryBuilder;
|
pub use self::memory::MemoryBuilder;
|
||||||
pub use self::import::{import, ImportBuilder};
|
pub use self::import::{import, ImportBuilder};
|
||||||
pub use self::export::{export, ExportBuilder};
|
pub use self::export::{export, ExportBuilder};
|
||||||
|
Reference in New Issue
Block a user