2019-01-21 17:04:31 +03:00
|
|
|
//! Wasm binary graph format
|
|
|
|
|
|
|
|
use parity_wasm::elements;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
enum ImportedOrDeclared<T=()> {
|
|
|
|
Imported(String, String),
|
|
|
|
Declared(T),
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:56:30 +03:00
|
|
|
impl<T> ImportedOrDeclared<T> {
|
|
|
|
fn imported(module: String, name: String) -> Self {
|
|
|
|
ImportedOrDeclared::Imported(module, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<&elements::ImportEntry> for ImportedOrDeclared<T> {
|
|
|
|
fn from(v: &elements::ImportEntry) -> Self {
|
|
|
|
ImportedOrDeclared::Imported(v.module().to_owned(), v.field().to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:04:31 +03:00
|
|
|
type FuncOrigin = ImportedOrDeclared<Vec<Instruction>>;
|
|
|
|
type GlobalOrigin = ImportedOrDeclared<Vec<Instruction>>;
|
|
|
|
type MemoryOrigin = ImportedOrDeclared;
|
|
|
|
type TableOrigin = ImportedOrDeclared;
|
|
|
|
|
|
|
|
type TypeRef = Rc<RefCell<elements::Type>>;
|
|
|
|
type FuncRef = Rc<RefCell<Func>>;
|
|
|
|
type GlobalRef = Rc<RefCell<Global>>;
|
2019-01-21 17:56:30 +03:00
|
|
|
type MemoryRef = Rc<RefCell<Memory>>;
|
|
|
|
type TableRef = Rc<RefCell<Table>>;
|
2019-01-21 17:04:31 +03:00
|
|
|
|
|
|
|
struct Func {
|
|
|
|
type_ref: TypeRef,
|
|
|
|
origin: FuncOrigin,
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:56:30 +03:00
|
|
|
impl Func {
|
|
|
|
fn into_ref(self) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::from(RefCell::from(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:04:31 +03:00
|
|
|
struct Global {
|
|
|
|
content: elements::ValueType,
|
|
|
|
is_mut: bool,
|
|
|
|
origin: GlobalOrigin,
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:56:30 +03:00
|
|
|
impl Global {
|
|
|
|
fn into_ref(self) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::from(RefCell::from(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:04:31 +03:00
|
|
|
enum Instruction {
|
|
|
|
Plain(elements::Instruction),
|
|
|
|
Call(FuncRef),
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Memory {
|
|
|
|
limits: elements::ResizableLimits,
|
|
|
|
origin: MemoryOrigin,
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:56:30 +03:00
|
|
|
impl Memory {
|
|
|
|
fn into_ref(self) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::from(RefCell::from(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:04:31 +03:00
|
|
|
struct Table {
|
|
|
|
origin: TableOrigin,
|
2019-01-21 17:56:30 +03:00
|
|
|
limits: elements::ResizableLimits,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Table {
|
|
|
|
fn into_ref(self) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::from(RefCell::from(self))
|
|
|
|
}
|
2019-01-21 17:04:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DataSegment {
|
|
|
|
offset_expr: Vec<Instruction>,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ElementSegment {
|
|
|
|
offset_expr: Vec<Instruction>,
|
|
|
|
data: Vec<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Export {
|
|
|
|
Func(FuncRef),
|
|
|
|
Global(GlobalRef),
|
2019-01-21 17:56:30 +03:00
|
|
|
Table(TableRef),
|
|
|
|
Memory(MemoryRef),
|
2019-01-21 17:04:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Module {
|
|
|
|
types: Vec<TypeRef>,
|
|
|
|
funcs: Vec<FuncRef>,
|
2019-01-21 17:56:30 +03:00
|
|
|
tables: Vec<TableRef>,
|
|
|
|
memory: Vec<MemoryRef>,
|
2019-01-21 17:04:31 +03:00
|
|
|
globals: Vec<GlobalRef>,
|
|
|
|
elements: Vec<ElementSegment>,
|
|
|
|
data: Vec<DataSegment>,
|
|
|
|
exports: Vec<Export>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
|
|
|
|
|
|
|
fn from_elements(module: &elements::Module) -> Self {
|
|
|
|
|
|
|
|
let mut res = Module::default();
|
|
|
|
|
|
|
|
for section in module.sections() {
|
|
|
|
match section {
|
|
|
|
elements::Section::Type(type_section) => {
|
2019-01-21 17:56:30 +03:00
|
|
|
res.types = type_section
|
|
|
|
.types()
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.map(RefCell::<_>::from)
|
|
|
|
.map(Rc::<_>::from)
|
|
|
|
.collect();
|
|
|
|
},
|
|
|
|
elements::Section::Import(import_section) => {
|
|
|
|
for entry in import_section.entries() {
|
|
|
|
match *entry.external() {
|
|
|
|
elements::External::Function(f) => {
|
|
|
|
res.funcs.push(Func {
|
|
|
|
type_ref: res.types[f as usize].clone(),
|
|
|
|
origin: entry.into(),
|
|
|
|
}.into_ref())
|
|
|
|
},
|
|
|
|
elements::External::Memory(m) => {
|
|
|
|
res.memory.push(Memory {
|
|
|
|
limits: m.limits().clone(),
|
|
|
|
origin: entry.into(),
|
|
|
|
}.into_ref())
|
|
|
|
},
|
|
|
|
elements::External::Global(g) => {
|
|
|
|
res.globals.push(Global {
|
|
|
|
content: g.content_type(),
|
|
|
|
is_mut: g.is_mutable(),
|
|
|
|
origin: entry.into(),
|
|
|
|
}.into_ref())
|
|
|
|
},
|
|
|
|
elements::External::Table(t) => {
|
|
|
|
res.tables.push(Table {
|
|
|
|
limits: t.limits().clone(),
|
|
|
|
origin: entry.into(),
|
|
|
|
}.into_ref())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 17:04:31 +03:00
|
|
|
},
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
}
|