Make `global_by_index` pub, add `globals` getter

This commit is contained in:
Leonardo Yvens Schwarzstein 2019-01-15 15:45:40 -02:00
parent 073e4e7f1f
commit 704b4d50b8
1 changed files with 15 additions and 1 deletions

View File

@ -183,7 +183,10 @@ impl ModuleInstance {
self.tables.borrow_mut().get(idx as usize).cloned()
}
pub(crate) fn global_by_index(&self, idx: u32) -> Option<GlobalRef> {
/// Get global by index. Index can be obtained by calling [globals].
///
/// [globals]: [#method.globals]
pub fn global_by_index(&self, idx: u32) -> Option<GlobalRef> {
self.globals.borrow_mut().get(idx as usize).cloned()
}
@ -215,6 +218,17 @@ impl ModuleInstance {
self.globals.borrow_mut().push(global)
}
/// Enumerates over globals, index can be used as an argument to [global_by_index].
///
/// [global_by_index]: #method.global_by_index
pub fn globals(&self) -> impl Iterator<Item = (u32, GlobalRef)> {
let globals = self.globals.borrow().clone();
globals
.into_iter()
.enumerate()
.map(|(idx, global)| (idx as u32, global))
}
fn insert_export<N: Into<String>>(&self, name: N, extern_val: ExternVal) {
self.exports.borrow_mut().insert(name.into(), extern_val);
}