mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-22 19:21:59 +00:00
globals
This commit is contained in:
@ -17,13 +17,16 @@ fn main() {
|
||||
for section in module.sections() {
|
||||
match section {
|
||||
&Section::Import(ref import_section) => {
|
||||
println!("Imports {}", import_section.entries().len());
|
||||
println!(" Imports: {}", import_section.entries().len());
|
||||
},
|
||||
&Section::Export(ref exports_section) => {
|
||||
println!("Exports {}", exports_section.entries().len());
|
||||
println!(" Exports: {}", exports_section.entries().len());
|
||||
},
|
||||
&Section::Function(ref functions_section) => {
|
||||
println!("Functions {}", functions_section.entries().len());
|
||||
println!(" Functions: {}", functions_section.entries().len());
|
||||
},
|
||||
&Section::Global(ref globals_section) => {
|
||||
println!(" Globals: {}", globals_section.entries().len());
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
26
src/elements/global_entry.rs
Normal file
26
src/elements/global_entry.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use std::io;
|
||||
use super::{Deserialize, Error, GlobalType, InitExpr};
|
||||
|
||||
pub struct GlobalEntry {
|
||||
global_type: GlobalType,
|
||||
init_expr: InitExpr,
|
||||
}
|
||||
|
||||
impl GlobalEntry {
|
||||
pub fn global_type(&self) -> &GlobalType { &self.global_type }
|
||||
pub fn init_expr(&self) -> &InitExpr { &self.init_expr }
|
||||
}
|
||||
|
||||
impl Deserialize for GlobalEntry {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
||||
let global_type = GlobalType::deserialize(reader)?;
|
||||
let init_expr = InitExpr::deserialize(reader)?;
|
||||
|
||||
Ok(GlobalEntry {
|
||||
global_type: global_type,
|
||||
init_expr: init_expr,
|
||||
})
|
||||
}
|
||||
}
|
@ -6,15 +6,17 @@ mod primitives;
|
||||
mod types;
|
||||
mod import_entry;
|
||||
mod export_entry;
|
||||
mod global_entry;
|
||||
mod ops;
|
||||
|
||||
pub use self::module::Module;
|
||||
pub use self::section::Section;
|
||||
pub use self::import_entry::{ImportEntry, MemoryType, TableType, External};
|
||||
pub use self::import_entry::{ImportEntry, MemoryType, TableType, GlobalType, External};
|
||||
pub use self::export_entry::{ExportEntry, Internal};
|
||||
pub use self::global_entry::GlobalEntry;
|
||||
pub use self::primitives::{VarUint32, VarUint7, VarUint1, VarInt7, Uint32, Uint64, VarUint64, CountedList};
|
||||
pub use self::types::{ValueType, BlockType};
|
||||
pub use self::ops::{Opcode, Opcodes};
|
||||
pub use self::ops::{Opcode, Opcodes, InitExpr};
|
||||
|
||||
pub trait Deserialize : Sized {
|
||||
type Error;
|
||||
|
@ -10,6 +10,47 @@ impl Opcodes {
|
||||
pub fn elements(&self) -> &[Opcode] { &self.0 }
|
||||
}
|
||||
|
||||
impl Deserialize for Opcodes {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
||||
let mut opcodes = Vec::new();
|
||||
|
||||
loop {
|
||||
let opcode = Opcode::deserialize(reader)?;
|
||||
let is_terminal = opcode.is_terminal();
|
||||
opcodes.push(opcode);
|
||||
if is_terminal {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Opcodes(opcodes))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InitExpr(Vec<Opcode>);
|
||||
|
||||
// todo: check if kind of opcode sequence is valid as an expression
|
||||
impl Deserialize for InitExpr {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
||||
let mut opcodes = Vec::new();
|
||||
|
||||
loop {
|
||||
let opcode = Opcode::deserialize(reader)?;
|
||||
let is_terminal = opcode.is_terminal();
|
||||
opcodes.push(opcode);
|
||||
if is_terminal {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(InitExpr(opcodes))
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Opcode {
|
||||
Unreachable,
|
||||
Nop,
|
||||
@ -486,22 +527,3 @@ impl Deserialize for Opcode {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for Opcodes {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
||||
let mut opcodes = Vec::new();
|
||||
|
||||
loop {
|
||||
let opcode = Opcode::deserialize(reader)?;
|
||||
let is_terminal = opcode.is_terminal();
|
||||
opcodes.push(opcode);
|
||||
if is_terminal {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Opcodes(opcodes))
|
||||
}
|
||||
}
|
@ -37,7 +37,6 @@ impl Deserialize for VarUint32 {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct VarUint64(u64);
|
||||
|
||||
|
@ -12,6 +12,7 @@ use super::{
|
||||
ExportEntry,
|
||||
Opcodes,
|
||||
ValueType,
|
||||
GlobalEntry,
|
||||
};
|
||||
|
||||
use super::types::Type;
|
||||
@ -27,6 +28,7 @@ pub enum Section {
|
||||
Function(FunctionsSection),
|
||||
Table(TableSection),
|
||||
Memory(MemorySection),
|
||||
Global(GlobalSection),
|
||||
Export(ExportSection),
|
||||
Start(u32),
|
||||
Code(CodeSection),
|
||||
@ -62,6 +64,9 @@ impl Deserialize for Section {
|
||||
5 => {
|
||||
Section::Memory(MemorySection::deserialize(reader)?)
|
||||
},
|
||||
6 => {
|
||||
Section::Global(GlobalSection::deserialize(reader)?)
|
||||
},
|
||||
7 => {
|
||||
Section::Export(ExportSection::deserialize(reader)?)
|
||||
},
|
||||
@ -188,6 +193,25 @@ impl Deserialize for MemorySection {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GlobalSection(Vec<GlobalEntry>);
|
||||
|
||||
impl GlobalSection {
|
||||
pub fn entries(&self) -> &[GlobalEntry] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for GlobalSection {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
||||
// todo: maybe use reader.take(section_length)
|
||||
let _section_length = VarUint32::deserialize(reader)?;
|
||||
let entries: Vec<GlobalEntry> = CountedList::deserialize(reader)?.into_inner();
|
||||
Ok(GlobalSection(entries))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ExportSection(Vec<ExportEntry>);
|
||||
|
||||
impl ExportSection {
|
||||
|
Reference in New Issue
Block a user