mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-28 22:22:02 +00:00
import entries builder
This commit is contained in:
@ -1,11 +1,17 @@
|
|||||||
use super::invoke::{Invoke, Identity};
|
use super::invoke::{Invoke, Identity};
|
||||||
use elements;
|
use elements;
|
||||||
|
|
||||||
pub struct ImportBuilder<F: Identity> {
|
pub struct ImportBuilder<F=Identity> {
|
||||||
callback: F,
|
callback: F,
|
||||||
module: String,
|
module: String,
|
||||||
field: String,
|
field: String,
|
||||||
binding: ExternalBinding,
|
binding: elements::External,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImportBuilder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
ImportBuilder::with_callback(Identity)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> ImportBuilder<F> {
|
impl<F> ImportBuilder<F> {
|
||||||
@ -13,27 +19,97 @@ impl<F> ImportBuilder<F> {
|
|||||||
pub fn with_callback(callback: F) -> Self {
|
pub fn with_callback(callback: F) -> Self {
|
||||||
ImportBuilder {
|
ImportBuilder {
|
||||||
callback: callback,
|
callback: callback,
|
||||||
module
|
module: String::new(),
|
||||||
|
field: String::new(),
|
||||||
|
binding: elements::External::Function(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external(self) -> ImportExternalBuilder<Self> {
|
pub fn module(mut self, name: &str) -> Self {
|
||||||
|
self.module = name.to_owned();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn field(mut self, name: &str) -> Self {
|
||||||
|
self.field = name.to_owned();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(self, module: &str, field: &str) -> Self {
|
||||||
|
self.module(module).field(field)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_external(mut self, external: elements::External) -> Self {
|
||||||
|
self.binding = external;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn external(self) -> ImportExternalBuilder<Self> {
|
||||||
|
ImportExternalBuilder::with_callback(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> ImportBuilder<F> where F: Invoke<elements::ImportEntry> {
|
||||||
|
pub fn build(self) -> F::Result {
|
||||||
|
self.callback.invoke(elements::ImportEntry::new(self.module, self.field, self.binding))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> Invoke<elements::External> for ImportBuilder<F> {
|
||||||
|
type Result = Self;
|
||||||
|
fn invoke(self, val: elements::External) -> Self {
|
||||||
|
self.with_external(val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ImportExternalBuilder<F=Identity> {
|
pub struct ImportExternalBuilder<F=Identity> {
|
||||||
callback: F,
|
callback: F,
|
||||||
binding: ExternalBinding,
|
binding: elements::External,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> ImportExternalBuilder<F> where F: Invoke<ExternalBinding> {
|
impl<F> ImportExternalBuilder<F> where F: Invoke<elements::External> {
|
||||||
pub fn with_callback(callback: F) {
|
pub fn with_callback(callback: F) -> Self {
|
||||||
ImportExternalBuilder{
|
ImportExternalBuilder{
|
||||||
callback: callback,
|
callback: callback,
|
||||||
binding: ExternalBinding::ExistingFunc(0),
|
binding: elements::External::Function(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn
|
pub fn func(mut self, index: u32) -> F::Result {
|
||||||
|
self.binding = elements::External::Function(index);
|
||||||
|
self.callback.invoke(self.binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn memory(mut self, min: u32, max: Option<u32>) -> F::Result {
|
||||||
|
self.binding = elements::External::Memory(elements::MemoryType::new(min, max));
|
||||||
|
self.callback.invoke(self.binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn table(mut self, min: u32, max: Option<u32>) -> F::Result {
|
||||||
|
self.binding = elements::External::Table(elements::TableType::new(min, max));
|
||||||
|
self.callback.invoke(self.binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn global(mut self, value_type: elements::ValueType, is_mut: bool) -> F::Result {
|
||||||
|
self.binding = elements::External::Global(elements::GlobalType::new(value_type, is_mut));
|
||||||
|
self.callback.invoke(self.binding)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// New builder for import entry
|
||||||
|
pub fn import() -> ImportBuilder {
|
||||||
|
ImportBuilder::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::import;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example() {
|
||||||
|
let entry = import().module("env").field("memory").external().memory(256, Some(256)).build();
|
||||||
|
|
||||||
|
assert_eq!(entry.module(), "env");
|
||||||
|
assert_eq!(entry.field(), "memory");
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,6 +4,8 @@ mod invoke;
|
|||||||
mod module;
|
mod module;
|
||||||
mod code;
|
mod code;
|
||||||
mod misc;
|
mod misc;
|
||||||
|
mod import;
|
||||||
|
|
||||||
pub use self::module::{module, ModuleBuilder};
|
pub use self::module::{module, ModuleBuilder};
|
||||||
pub use self::code::signatures;
|
pub use self::code::signatures;
|
||||||
|
pub use self::import::import;
|
@ -11,8 +11,17 @@ pub struct GlobalType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GlobalType {
|
impl GlobalType {
|
||||||
|
/// New global type
|
||||||
|
pub fn new(content_type: ValueType, is_mutable: bool) -> Self {
|
||||||
|
GlobalType {
|
||||||
|
content_type: content_type,
|
||||||
|
is_mutable: is_mutable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Type of the global entry
|
/// Type of the global entry
|
||||||
pub fn content_type(&self) -> ValueType { self.content_type }
|
pub fn content_type(&self) -> ValueType { self.content_type }
|
||||||
|
|
||||||
/// Is global entry is declared as mutable
|
/// Is global entry is declared as mutable
|
||||||
pub fn is_mutable(&self) -> bool { self.is_mutable }
|
pub fn is_mutable(&self) -> bool { self.is_mutable }
|
||||||
}
|
}
|
||||||
@ -47,6 +56,13 @@ pub struct TableType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TableType {
|
impl TableType {
|
||||||
|
/// New table definition
|
||||||
|
pub fn new(min: u32, max: Option<u32>) -> Self {
|
||||||
|
TableType {
|
||||||
|
elem_type: 0,
|
||||||
|
limits: ResizableLimits::new(min, max),
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Table memory specification
|
/// Table memory specification
|
||||||
pub fn limits(&self) -> &ResizableLimits { &self.limits }
|
pub fn limits(&self) -> &ResizableLimits { &self.limits }
|
||||||
}
|
}
|
||||||
@ -81,6 +97,13 @@ pub struct ResizableLimits {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ResizableLimits {
|
impl ResizableLimits {
|
||||||
|
/// New memory limits definition
|
||||||
|
pub fn new(min: u32, max: Option<u32>) -> Self {
|
||||||
|
ResizableLimits {
|
||||||
|
initial: min,
|
||||||
|
maximum: max,
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Initial size
|
/// Initial size
|
||||||
pub fn initial(&self) -> u32 { self.initial }
|
pub fn initial(&self) -> u32 { self.initial }
|
||||||
/// Maximum size
|
/// Maximum size
|
||||||
@ -124,6 +147,10 @@ impl Serialize for ResizableLimits {
|
|||||||
pub struct MemoryType(ResizableLimits);
|
pub struct MemoryType(ResizableLimits);
|
||||||
|
|
||||||
impl MemoryType {
|
impl MemoryType {
|
||||||
|
/// New memory definition
|
||||||
|
pub fn new(min: u32, max: Option<u32>) -> Self {
|
||||||
|
MemoryType(ResizableLimits::new(min, max))
|
||||||
|
}
|
||||||
/// Limits of the memory entry.
|
/// Limits of the memory entry.
|
||||||
pub fn limits(&self) -> &ResizableLimits {
|
pub fn limits(&self) -> &ResizableLimits {
|
||||||
&self.0
|
&self.0
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//! Elemets of the WebAssembly binary format.
|
//! Elements of the WebAssembly binary format.
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user