mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-06-14 15:21:25 +00:00
exports and fix for no-std
This commit is contained in:
41
src/graph.rs
41
src/graph.rs
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
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::borrow::ToOwned;
|
||||||
|
use std::string::String;
|
||||||
|
|
||||||
enum ImportedOrDeclared<T=()> {
|
enum ImportedOrDeclared<T=()> {
|
||||||
Imported(String, String),
|
Imported(String, String),
|
||||||
@ -66,12 +69,13 @@ enum Export {
|
|||||||
struct Module {
|
struct Module {
|
||||||
types: RefList<elements::Type>,
|
types: RefList<elements::Type>,
|
||||||
funcs: RefList<Func>,
|
funcs: RefList<Func>,
|
||||||
tables: RefList<Table>,
|
|
||||||
memory: RefList<Memory>,
|
memory: RefList<Memory>,
|
||||||
|
tables: RefList<Table>,
|
||||||
globals: RefList<Global>,
|
globals: RefList<Global>,
|
||||||
|
start: Option<EntryRef<Func>>,
|
||||||
|
exports: Vec<Export>,
|
||||||
elements: Vec<ElementSegment>,
|
elements: Vec<ElementSegment>,
|
||||||
data: Vec<DataSegment>,
|
data: Vec<DataSegment>,
|
||||||
exports: Vec<Export>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
@ -141,6 +145,34 @@ impl Module {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
elements::Section::Global(global_section) => {
|
||||||
|
for g in global_section.entries() {
|
||||||
|
res.globals.push(Global {
|
||||||
|
content: g.global_type().content_type(),
|
||||||
|
is_mut: g.global_type().is_mutable(),
|
||||||
|
// TODO: init expr
|
||||||
|
origin: ImportedOrDeclared::Declared(Vec::new()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
elements::Section::Export(export_section) => {
|
||||||
|
for e in export_section.entries() {
|
||||||
|
match e.internal() {
|
||||||
|
&elements::Internal::Function(func_idx) => {
|
||||||
|
res.exports.push(Export::Func(res.funcs.clone_ref(func_idx as usize)));
|
||||||
|
},
|
||||||
|
&elements::Internal::Global(global_idx) => {
|
||||||
|
res.exports.push(Export::Global(res.globals.clone_ref(global_idx as usize)));
|
||||||
|
},
|
||||||
|
&elements::Internal::Memory(mem_idx) => {
|
||||||
|
res.exports.push(Export::Memory(res.memory.clone_ref(mem_idx as usize)));
|
||||||
|
},
|
||||||
|
&elements::Internal::Table(table_idx) => {
|
||||||
|
res.exports.push(Export::Table(res.tables.clone_ref(table_idx as usize)));
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,6 +199,7 @@ mod tests {
|
|||||||
(type (func))
|
(type (func))
|
||||||
(func (type 0))
|
(func (type 0))
|
||||||
(memory 0 1)
|
(memory 0 1)
|
||||||
|
(export "simple" (func 0))
|
||||||
)
|
)
|
||||||
"#).expect("Failed to read fixture");
|
"#).expect("Failed to read fixture");
|
||||||
|
|
||||||
@ -176,5 +209,9 @@ mod tests {
|
|||||||
assert_eq!(f.funcs.len(), 1);
|
assert_eq!(f.funcs.len(), 1);
|
||||||
assert_eq!(f.tables.len(), 0);
|
assert_eq!(f.tables.len(), 0);
|
||||||
assert_eq!(f.memory.len(), 1);
|
assert_eq!(f.memory.len(), 1);
|
||||||
|
assert_eq!(f.exports.len(), 1);
|
||||||
|
|
||||||
|
assert_eq!(f.types.get_ref(0).link_count(), 1);
|
||||||
|
assert_eq!(f.funcs.get_ref(0).link_count(), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -59,6 +59,10 @@ mod std {
|
|||||||
pub use core::*;
|
pub use core::*;
|
||||||
pub use alloc::{vec, string, boxed, borrow};
|
pub use alloc::{vec, string, boxed, borrow};
|
||||||
|
|
||||||
|
pub mod rc {
|
||||||
|
pub use alloc::rc::Rc;
|
||||||
|
}
|
||||||
|
|
||||||
pub mod collections {
|
pub mod collections {
|
||||||
pub use alloc::collections::{BTreeMap, BTreeSet};
|
pub use alloc::collections::{BTreeMap, BTreeSet};
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum EntryOrigin {
|
enum EntryOrigin {
|
||||||
@ -59,17 +60,21 @@ impl<T> From<Entry<T>> for EntryRef<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> EntryRef<T> {
|
impl<T> EntryRef<T> {
|
||||||
fn read(&self) -> ::std::cell::Ref<Entry<T>> {
|
pub fn read(&self) -> ::std::cell::Ref<Entry<T>> {
|
||||||
self.0.borrow()
|
self.0.borrow()
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn order(&self) -> Option<usize> {
|
pub fn order(&self) -> Option<usize> {
|
||||||
self.0.borrow().order()
|
self.0.borrow().order()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn link_count(&self) -> usize {
|
||||||
|
Rc::strong_count(&self.0) - 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RefList<T> {
|
pub struct RefList<T> {
|
||||||
@ -148,6 +153,14 @@ impl<T> RefList<T> {
|
|||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.items.len()
|
self.items.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clone_ref(&self, idx: usize) -> EntryRef<T> {
|
||||||
|
self.items[idx].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_ref(&self, idx: usize) -> &EntryRef<T> {
|
||||||
|
&self.items[idx]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
Reference in New Issue
Block a user