mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-06-22 11:01:37 +00:00
more docs and warnings
This commit is contained in:
15
src/graph.rs
15
src/graph.rs
@ -1,5 +1,7 @@
|
|||||||
//! Wasm binary graph format
|
//! Wasm binary graph format
|
||||||
|
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
use parity_wasm::elements;
|
use parity_wasm::elements;
|
||||||
use super::ref_list::{RefList, EntryRef};
|
use super::ref_list::{RefList, EntryRef};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
@ -168,15 +170,25 @@ pub struct Export {
|
|||||||
/// Module
|
/// Module
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
|
/// Refence-tracking list of types.
|
||||||
pub types: RefList<elements::Type>,
|
pub types: RefList<elements::Type>,
|
||||||
|
/// Refence-tracking list of funcs.
|
||||||
pub funcs: RefList<Func>,
|
pub funcs: RefList<Func>,
|
||||||
|
/// Refence-tracking list of memory instances.
|
||||||
pub memory: RefList<Memory>,
|
pub memory: RefList<Memory>,
|
||||||
|
/// Refence-tracking list of table instances.
|
||||||
pub tables: RefList<Table>,
|
pub tables: RefList<Table>,
|
||||||
|
/// Refence-tracking list of globals.
|
||||||
pub globals: RefList<Global>,
|
pub globals: RefList<Global>,
|
||||||
|
/// Reference to start function.
|
||||||
pub start: Option<EntryRef<Func>>,
|
pub start: Option<EntryRef<Func>>,
|
||||||
|
/// References to exported objects.
|
||||||
pub exports: Vec<Export>,
|
pub exports: Vec<Export>,
|
||||||
|
/// List of element segments.
|
||||||
pub elements: Vec<ElementSegment>,
|
pub elements: Vec<ElementSegment>,
|
||||||
|
/// List of data segments.
|
||||||
pub data: Vec<DataSegment>,
|
pub data: Vec<DataSegment>,
|
||||||
|
/// Other module functions that are not decoded or processed.
|
||||||
pub other: BTreeMap<usize, elements::Section>,
|
pub other: BTreeMap<usize, elements::Section>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,6 +222,7 @@ impl Module {
|
|||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initialize module from parity-wasm `Module`.
|
||||||
pub fn from_elements(module: &elements::Module) -> Self {
|
pub fn from_elements(module: &elements::Module) -> Self {
|
||||||
|
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
@ -721,10 +734,12 @@ fn custom_round(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// New module from parity-wasm `Module`
|
||||||
pub fn parse(wasm: &[u8]) -> Module {
|
pub fn parse(wasm: &[u8]) -> Module {
|
||||||
Module::from_elements(&::parity_wasm::deserialize_buffer(wasm).expect("failed to parse wasm"))
|
Module::from_elements(&::parity_wasm::deserialize_buffer(wasm).expect("failed to parse wasm"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate parity-wasm `Module`
|
||||||
pub fn generate(f: &Module) -> Vec<u8> {
|
pub fn generate(f: &Module) -> Vec<u8> {
|
||||||
let pm = f.generate();
|
let pm = f.generate();
|
||||||
::parity_wasm::serialize(pm).expect("failed to generate wasm")
|
::parity_wasm::serialize(pm).expect("failed to generate wasm")
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -16,6 +17,7 @@ impl From<usize> for EntryOrigin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reference counting, link-handling object.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Entry<T> {
|
pub struct Entry<T> {
|
||||||
val: T,
|
val: T,
|
||||||
@ -23,13 +25,15 @@ pub struct Entry<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Entry<T> {
|
impl<T> Entry<T> {
|
||||||
fn new(val: T, index: usize) -> Entry<T> {
|
/// New entity.
|
||||||
|
pub fn new(val: T, index: usize) -> Entry<T> {
|
||||||
Entry {
|
Entry {
|
||||||
val: val,
|
val: val,
|
||||||
index: EntryOrigin::Index(index),
|
index: EntryOrigin::Index(index),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Index of the element within the reference list.
|
||||||
pub fn order(&self) -> Option<usize> {
|
pub fn order(&self) -> Option<usize> {
|
||||||
match self.index {
|
match self.index {
|
||||||
EntryOrigin::Detached => None,
|
EntryOrigin::Detached => None,
|
||||||
@ -52,6 +56,8 @@ impl<T> ::std::ops::DerefMut for Entry<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reference to the entry in the rerence list.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct EntryRef<T>(Rc<RefCell<Entry<T>>>);
|
pub struct EntryRef<T>(Rc<RefCell<Entry<T>>>);
|
||||||
|
|
||||||
impl<T> Clone for EntryRef<T> {
|
impl<T> Clone for EntryRef<T> {
|
||||||
@ -67,18 +73,24 @@ impl<T> From<Entry<T>> for EntryRef<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> EntryRef<T> {
|
impl<T> EntryRef<T> {
|
||||||
|
/// Read the reference data.
|
||||||
pub fn read(&self) -> ::std::cell::Ref<Entry<T>> {
|
pub fn read(&self) -> ::std::cell::Ref<Entry<T>> {
|
||||||
self.0.borrow()
|
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<Entry<T>> {
|
pub fn write(&self) -> ::std::cell::RefMut<Entry<T>> {
|
||||||
self.0.borrow_mut()
|
self.0.borrow_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Index of the element within the reference list.
|
||||||
pub fn order(&self) -> Option<usize> {
|
pub fn order(&self) -> Option<usize> {
|
||||||
self.0.borrow().order()
|
self.0.borrow().order()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Number of active links to this entity.
|
||||||
pub fn link_count(&self) -> usize {
|
pub fn link_count(&self) -> usize {
|
||||||
Rc::strong_count(&self.0) - 1
|
Rc::strong_count(&self.0) - 1
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user