Merge branch 'master' into interpreter

This commit is contained in:
Svyatoslav Nikolsky
2017-05-10 13:09:37 +03:00
14 changed files with 282 additions and 15 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "parity-wasm" name = "parity-wasm"
version = "0.3.0" version = "0.4.2"
authors = ["NikVolf <nikvolf@gmail.com>"] authors = ["NikVolf <nikvolf@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
readme = "README.md" readme = "README.md"

View File

@ -202,7 +202,6 @@ impl<F> FuncBodyBuilder<F> {
} }
impl<F> FuncBodyBuilder<F> where F: Invoke<elements::FuncBody> { impl<F> FuncBodyBuilder<F> where F: Invoke<elements::FuncBody> {
pub fn with_func(mut self, func: elements::FuncBody) -> Self { pub fn with_func(mut self, func: elements::FuncBody) -> Self {
self.body = func; self.body = func;
self self

102
src/builder/export.rs Normal file
View File

@ -0,0 +1,102 @@
use super::invoke::{Invoke, Identity};
use elements;
pub struct ExportBuilder<F=Identity> {
callback: F,
field: String,
binding: elements::Internal,
}
impl ExportBuilder {
pub fn new() -> Self {
ExportBuilder::with_callback(Identity)
}
}
impl<F> ExportBuilder<F> {
pub fn with_callback(callback: F) -> Self {
ExportBuilder {
callback: callback,
field: String::new(),
binding: elements::Internal::Function(0),
}
}
pub fn field(mut self, field: &str) -> Self {
self.field = field.to_owned();
self
}
pub fn with_internal(mut self, external: elements::Internal) -> Self {
self.binding = external;
self
}
pub fn internal(self) -> ExportInternalBuilder<Self> {
ExportInternalBuilder::with_callback(self)
}
}
impl<F> ExportBuilder<F> where F: Invoke<elements::ExportEntry> {
pub fn build(self) -> F::Result {
self.callback.invoke(elements::ExportEntry::new(self.field, self.binding))
}
}
impl<F> Invoke<elements::Internal> for ExportBuilder<F> {
type Result = Self;
fn invoke(self, val: elements::Internal) -> Self {
self.with_internal(val)
}
}
pub struct ExportInternalBuilder<F=Identity> {
callback: F,
binding: elements::Internal,
}
impl<F> ExportInternalBuilder<F> where F: Invoke<elements::Internal> {
pub fn with_callback(callback: F) -> Self {
ExportInternalBuilder{
callback: callback,
binding: elements::Internal::Function(0),
}
}
pub fn func(mut self, index: u32) -> F::Result {
self.binding = elements::Internal::Function(index);
self.callback.invoke(self.binding)
}
pub fn memory(mut self, index: u32) -> F::Result {
self.binding = elements::Internal::Memory(index);
self.callback.invoke(self.binding)
}
pub fn table(mut self, index: u32) -> F::Result {
self.binding = elements::Internal::Table(index);
self.callback.invoke(self.binding)
}
pub fn global(mut self, index: u32) -> F::Result {
self.binding = elements::Internal::Global(index);
self.callback.invoke(self.binding)
}
}
/// New builder for export entry
pub fn export() -> ExportBuilder {
ExportBuilder::new()
}
#[cfg(test)]
mod tests {
use super::export;
#[test]
fn example() {
let entry = export().field("memory").internal().memory(0).build();
assert_eq!(entry.field(), "memory");
}
}

83
src/builder/global.rs Normal file
View File

@ -0,0 +1,83 @@
use super::invoke::{Invoke, Identity};
use super::misc::ValueTypeBuilder;
use elements;
pub struct GlobalBuilder<F=Identity> {
callback: F,
value_type: elements::ValueType,
is_mutable: bool,
init_expr: elements::InitExpr,
}
impl GlobalBuilder {
pub fn new() -> Self {
GlobalBuilder::with_callback(Identity)
}
}
impl<F> GlobalBuilder<F> {
pub fn with_callback(callback: F) -> Self {
GlobalBuilder {
callback: callback,
value_type: elements::ValueType::I32,
init_expr: elements::InitExpr::empty(),
is_mutable: false,
}
}
pub fn with_type(mut self, value_type: elements::ValueType) -> Self {
self.value_type = value_type;
self
}
pub fn mutable(mut self) -> Self {
self.is_mutable = true;
self
}
pub fn init_expr(mut self, opcode: elements::Opcode) -> Self {
self.init_expr = elements::InitExpr::new(vec![opcode, elements::Opcode::End]);
self
}
pub fn value_type(self) -> ValueTypeBuilder<Self> {
ValueTypeBuilder::with_callback(self)
}
}
impl<F> GlobalBuilder<F> where F: Invoke<elements::GlobalEntry> {
pub fn build(self) -> F::Result {
self.callback.invoke(
elements::GlobalEntry::new(
elements::GlobalType::new(self.value_type, self.is_mutable),
self.init_expr,
)
)
}
}
impl<F> Invoke<elements::ValueType> for GlobalBuilder<F> {
type Result = Self;
fn invoke(self, the_type: elements::ValueType) -> Self {
self.with_type(the_type)
}
}
/// New builder for export entry
pub fn global() -> GlobalBuilder {
GlobalBuilder::new()
}
#[cfg(test)]
mod tests {
use super::global;
use elements;
#[test]
fn example() {
let entry = global().value_type().i32().build();
assert_eq!(entry.global_type().content_type(), elements::ValueType::I32);
assert_eq!(entry.global_type().is_mutable(), false);
}
}

View File

@ -7,7 +7,11 @@ mod misc;
mod import; mod import;
mod memory; mod memory;
mod table; mod table;
mod export;
mod global;
pub use self::module::{module, from_module, ModuleBuilder}; pub use self::module::{module, from_module, ModuleBuilder};
pub use self::code::{signatures, signature, function}; pub use self::code::{signatures, signature, function};
pub use self::import::import; pub use self::import::import;
pub use self::export::export;
pub use self::global::global;

View File

@ -2,7 +2,7 @@ use super::invoke::{Invoke, Identity};
use super::code::{self, SignaturesBuilder, FunctionBuilder}; use super::code::{self, SignaturesBuilder, FunctionBuilder};
use super::memory::{self, MemoryBuilder}; use super::memory::{self, MemoryBuilder};
use super::table::{self, TableBuilder}; use super::table::{self, TableBuilder};
use super::import; use super::{import, export, global};
use elements; use elements;
/// Module builder /// Module builder
@ -305,6 +305,16 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
self self
} }
/// Export entry builder
pub fn export(self) -> export::ExportBuilder<Self> {
export::ExportBuilder::with_callback(self)
}
/// Glboal entry builder
pub fn global(self) -> global::GlobalBuilder<Self> {
global::GlobalBuilder::with_callback(self)
}
/// Build module (final step) /// Build module (final step)
pub fn build(self) -> F::Result { pub fn build(self) -> F::Result {
self.callback.invoke(self.module.into()) self.callback.invoke(self.module.into())
@ -378,6 +388,26 @@ impl<F> Invoke<elements::ImportEntry> for ModuleBuilder<F>
} }
} }
impl<F> Invoke<elements::ExportEntry> for ModuleBuilder<F>
where F: Invoke<elements::Module>
{
type Result = Self;
fn invoke(self, entry: elements::ExportEntry) -> Self::Result {
self.with_export(entry)
}
}
impl<F> Invoke<elements::GlobalEntry> for ModuleBuilder<F>
where F: Invoke<elements::Module>
{
type Result = Self;
fn invoke(self, entry: elements::GlobalEntry) -> Self::Result {
self.with_global(entry)
}
}
/// Start new module builder /// Start new module builder
pub fn module() -> ModuleBuilder { pub fn module() -> ModuleBuilder {
ModuleBuilder::new() ModuleBuilder::new()
@ -413,4 +443,21 @@ mod tests {
assert_eq!(module.code_section().expect("code section to exist").bodies().len(), 1); assert_eq!(module.code_section().expect("code section to exist").bodies().len(), 1);
} }
} #[test]
fn export() {
let module = module()
.export().field("call").internal().func(0).build()
.build();
assert_eq!(module.export_section().expect("export section to exist").entries().len(), 1);
}
#[test]
fn global() {
let module = module()
.global().value_type().i64().mutable().init_expr(::elements::Opcode::I64Const(5)).build()
.build();
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
}
}

View File

@ -55,19 +55,23 @@ pub struct ExportEntry {
} }
impl ExportEntry { impl ExportEntry {
/// Create new export entry /// New export entry
pub fn new(field: String, internal: Internal) -> Self { pub fn new(field: String, internal: Internal) -> Self {
Self { ExportEntry {
field_str: field, field_str: field,
internal: internal, internal: internal
} }
} }
/// Public name /// Public name
pub fn field(&self) -> &str { &self.field_str } pub fn field(&self) -> &str { &self.field_str }
/// Public name (mutable) /// Public name (mutable)
pub fn field_mut(&mut self) -> &mut str { &mut self.field_str } pub fn field_mut(&mut self) -> &mut str { &mut self.field_str }
/// Internal reference of the export entry. /// Internal reference of the export entry.
pub fn internal(&self) -> &Internal { &self.internal } pub fn internal(&self) -> &Internal { &self.internal }
/// Internal reference of the export entry (mutable). /// Internal reference of the export entry (mutable).
pub fn internal_mut(&mut self) -> &mut Internal { &mut self.internal } pub fn internal_mut(&mut self) -> &mut Internal { &mut self.internal }
} }

View File

@ -15,6 +15,11 @@ impl Func {
pub fn type_ref(&self) -> u32 { pub fn type_ref(&self) -> u32 {
self.0 self.0
} }
/// Function signature type reference (mutable).
pub fn type_ref_mut(&mut self) -> &mut u32 {
&mut self.0
}
} }
/// Local definition inside the function body. /// Local definition inside the function body.

View File

@ -8,7 +8,7 @@ pub struct GlobalEntry {
} }
impl GlobalEntry { impl GlobalEntry {
/// Create new global. /// New global entry
pub fn new(global_type: GlobalType, init_expr: InitExpr) -> Self { pub fn new(global_type: GlobalType, init_expr: InitExpr) -> Self {
GlobalEntry { GlobalEntry {
global_type: global_type, global_type: global_type,
@ -19,6 +19,10 @@ impl GlobalEntry {
pub fn global_type(&self) -> &GlobalType { &self.global_type } pub fn global_type(&self) -> &GlobalType { &self.global_type }
/// Initialization expression (opcodes) for global. /// Initialization expression (opcodes) for global.
pub fn init_expr(&self) -> &InitExpr { &self.init_expr } pub fn init_expr(&self) -> &InitExpr { &self.init_expr }
/// Global type (mutable)
pub fn global_type_mut(&mut self) -> &mut GlobalType { &mut self.global_type }
/// Initialization expression (opcodes) for global (mutable)
pub fn init_expr_mut(&mut self) -> &mut InitExpr { &mut self.init_expr }
} }
impl Deserialize for GlobalEntry { impl Deserialize for GlobalEntry {

View File

@ -271,7 +271,7 @@ impl ImportEntry {
pub fn external(&self) -> &External { &self.external } pub fn external(&self) -> &External { &self.external }
/// Local binidng of the import entry (mutable) /// Local binidng of the import entry (mutable)
pub fn external_mut(&mut self) -> &External { &mut self.external } pub fn external_mut(&mut self) -> &mut External { &mut self.external }
} }
impl Deserialize for ImportEntry { impl Deserialize for ImportEntry {

View File

@ -2,7 +2,7 @@ use std::io;
use super::{Deserialize, Serialize, Error, Uint32}; use super::{Deserialize, Serialize, Error, Uint32};
use super::section::{ use super::section::{
Section, CodeSection, TypeSection, ImportSection, ExportSection, FunctionsSection, Section, CodeSection, TypeSection, ImportSection, ExportSection, FunctionsSection,
GlobalSection, TableSection, ElementSection, DataSection, MemorySection, GlobalSection, TableSection, ElementSection, DataSection, MemorySection
}; };
/// WebAssembly module /// WebAssembly module

View File

@ -67,6 +67,11 @@ impl InitExpr {
pub fn code(&self) -> &[Opcode] { pub fn code(&self) -> &[Opcode] {
&self.0 &self.0
} }
/// List of opcodes used in the expression.
pub fn code_mut(&mut self) -> &mut Vec<Opcode> {
&mut self.0
}
} }
// todo: check if kind of opcode sequence is valid as an expression // todo: check if kind of opcode sequence is valid as an expression

View File

@ -410,7 +410,7 @@ impl GlobalSection {
&self.0 &self.0
} }
/// Mutable list of all global entries in the section /// List of all global entries in the section (mutable)
pub fn entries_mut(&mut self) -> &mut Vec<GlobalEntry> { pub fn entries_mut(&mut self) -> &mut Vec<GlobalEntry> {
&mut self.0 &mut self.0
} }
@ -549,10 +549,10 @@ impl ElementSection {
&self.0 &self.0
} }
/// Mutable elements entries in the section /// List of all data entries in the section (mutable)
pub fn entries_mut(&mut self) -> &mut Vec<ElementSegment> { pub fn entries_mut(&mut self) -> &mut Vec<ElementSegment> {
&mut self.0 &mut self.0
} }
} }
impl Deserialize for ElementSection { impl Deserialize for ElementSection {
@ -597,7 +597,7 @@ impl DataSection {
&self.0 &self.0
} }
/// Mutable list of all data entries in the section /// List of all data entries in the section (mutable)
pub fn entries_mut(&mut self) -> &mut Vec<DataSegment> { pub fn entries_mut(&mut self) -> &mut Vec<DataSegment> {
&mut self.0 &mut self.0
} }

View File

@ -18,11 +18,17 @@ impl ElementSegment {
/// Sequence of function indices. /// Sequence of function indices.
pub fn members(&self) -> &[u32] { &self.members } pub fn members(&self) -> &[u32] { &self.members }
/// Sequence of function indices (mutable)
pub fn members_mut(&mut self) -> &mut Vec<u32> { &mut self.members }
/// Table index (currently valid only value of `0`) /// Table index (currently valid only value of `0`)
pub fn index(&self) -> u32 { self.index } pub fn index(&self) -> u32 { self.index }
/// An i32 initializer expression that computes the offset at which to place the elements. /// An i32 initializer expression that computes the offset at which to place the elements.
pub fn offset(&self) -> &InitExpr { &self.offset } pub fn offset(&self) -> &InitExpr { &self.offset }
/// An i32 initializer expression that computes the offset at which to place the elements (mutable)
pub fn offset_mut(&mut self) -> &mut InitExpr { &mut self.offset }
} }
impl Deserialize for ElementSegment { impl Deserialize for ElementSegment {
@ -80,10 +86,18 @@ impl DataSegment {
/// Linear memory index (currently the only valid value is `0`). /// Linear memory index (currently the only valid value is `0`).
pub fn index(&self) -> u32 { self.index } pub fn index(&self) -> u32 { self.index }
/// An i32 initializer expression that computes the offset at which to place the data. /// An i32 initializer expression that computes the offset at which to place the data.
pub fn offset(&self) -> &InitExpr { &self.offset } pub fn offset(&self) -> &InitExpr { &self.offset }
/// An i32 initializer expression that computes the offset at which to place the data (mutable)
pub fn offset_mut(&mut self) -> &mut InitExpr { &mut self.offset }
/// Initial value of the data segment. /// Initial value of the data segment.
pub fn value(&self) -> &[u8] { &self.value } pub fn value(&self) -> &[u8] { &self.value }
/// Initial value of the data segment (mutable).
pub fn value_mut(&mut self) -> &mut Vec<u8> { &mut self.value }
} }
impl Deserialize for DataSegment { impl Deserialize for DataSegment {