exports and fix for no-std

This commit is contained in:
NikVolf 2019-01-22 14:31:21 +03:00
parent dd9169e30f
commit cf10b7d5d9
3 changed files with 59 additions and 5 deletions

View File

@ -2,6 +2,9 @@
use parity_wasm::elements;
use super::ref_list::{RefList, EntryRef};
use std::vec::Vec;
use std::borrow::ToOwned;
use std::string::String;
enum ImportedOrDeclared<T=()> {
Imported(String, String),
@ -66,12 +69,13 @@ enum Export {
struct Module {
types: RefList<elements::Type>,
funcs: RefList<Func>,
tables: RefList<Table>,
memory: RefList<Memory>,
tables: RefList<Table>,
globals: RefList<Global>,
start: Option<EntryRef<Func>>,
exports: Vec<Export>,
elements: Vec<ElementSegment>,
data: Vec<DataSegment>,
exports: Vec<Export>,
}
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,
}
}
@ -167,6 +199,7 @@ mod tests {
(type (func))
(func (type 0))
(memory 0 1)
(export "simple" (func 0))
)
"#).expect("Failed to read fixture");
@ -176,5 +209,9 @@ mod tests {
assert_eq!(f.funcs.len(), 1);
assert_eq!(f.tables.len(), 0);
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);
}
}

View File

@ -59,6 +59,10 @@ mod std {
pub use core::*;
pub use alloc::{vec, string, boxed, borrow};
pub mod rc {
pub use alloc::rc::Rc;
}
pub mod collections {
pub use alloc::collections::{BTreeMap, BTreeSet};
}

View File

@ -1,6 +1,7 @@
use std::rc::Rc;
use std::cell::RefCell;
use std::vec::Vec;
#[derive(Debug)]
enum EntryOrigin {
@ -59,17 +60,21 @@ impl<T> From<Entry<T>> for 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()
}
fn write(&self) -> ::std::cell::RefMut<Entry<T>> {
pub fn write(&self) -> ::std::cell::RefMut<Entry<T>> {
self.0.borrow_mut()
}
fn order(&self) -> Option<usize> {
pub fn order(&self) -> Option<usize> {
self.0.borrow().order()
}
pub fn link_count(&self) -> usize {
Rc::strong_count(&self.0) - 1
}
}
pub struct RefList<T> {
@ -148,6 +153,14 @@ impl<T> RefList<T> {
pub fn len(&self) -> usize {
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]