diff --git a/src/graph.rs b/src/graph.rs index 5a3db0d..71a3670 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,5 +1,7 @@ //! Wasm binary graph format +#![warn(missing_docs)] + use parity_wasm::elements; use super::ref_list::{RefList, EntryRef}; use std::vec::Vec; @@ -168,15 +170,25 @@ pub struct Export { /// Module #[derive(Debug, Default)] pub struct Module { + /// Refence-tracking list of types. pub types: RefList, + /// Refence-tracking list of funcs. pub funcs: RefList, + /// Refence-tracking list of memory instances. pub memory: RefList, + /// Refence-tracking list of table instances. pub tables: RefList, + /// Refence-tracking list of globals. pub globals: RefList, + /// Reference to start function. pub start: Option>, + /// References to exported objects. pub exports: Vec, + /// List of element segments. pub elements: Vec, + /// List of data segments. pub data: Vec, + /// Other module functions that are not decoded or processed. pub other: BTreeMap, } @@ -210,6 +222,7 @@ impl Module { }).collect() } + /// Initialize module from parity-wasm `Module`. pub fn from_elements(module: &elements::Module) -> Self { let mut idx = 0; @@ -721,10 +734,12 @@ fn custom_round( } } +/// New module from parity-wasm `Module` pub fn parse(wasm: &[u8]) -> Module { Module::from_elements(&::parity_wasm::deserialize_buffer(wasm).expect("failed to parse wasm")) } +/// Generate parity-wasm `Module` pub fn generate(f: &Module) -> Vec { let pm = f.generate(); ::parity_wasm::serialize(pm).expect("failed to generate wasm") diff --git a/src/ref_list.rs b/src/ref_list.rs index 6c5026e..739ab43 100644 --- a/src/ref_list.rs +++ b/src/ref_list.rs @@ -1,3 +1,4 @@ +#![warn(missing_docs)] use std::rc::Rc; use std::cell::RefCell; @@ -16,6 +17,7 @@ impl From for EntryOrigin { } } +/// Reference counting, link-handling object. #[derive(Debug)] pub struct Entry { val: T, @@ -23,13 +25,15 @@ pub struct Entry { } impl Entry { - fn new(val: T, index: usize) -> Entry { + /// New entity. + pub fn new(val: T, index: usize) -> Entry { Entry { val: val, index: EntryOrigin::Index(index), } } + /// Index of the element within the reference list. pub fn order(&self) -> Option { match self.index { EntryOrigin::Detached => None, @@ -52,6 +56,8 @@ impl ::std::ops::DerefMut for Entry { } } +/// Reference to the entry in the rerence list. +#[derive(Debug)] pub struct EntryRef(Rc>>); impl Clone for EntryRef { @@ -67,18 +73,24 @@ impl From> for EntryRef { } impl EntryRef { + /// Read the reference data. pub fn read(&self) -> ::std::cell::Ref> { self.0.borrow() } + /// Try to modify internal content of the referenced object. + /// + /// May panic if it is already borrowed. pub fn write(&self) -> ::std::cell::RefMut> { self.0.borrow_mut() } + /// Index of the element within the reference list. pub fn order(&self) -> Option { self.0.borrow().order() } + /// Number of active links to this entity. pub fn link_count(&self) -> usize { Rc::strong_count(&self.0) - 1 }