Add getter to table and other misc changes

This commit is contained in:
Mark McCaskey
2020-03-18 15:29:29 -07:00
parent f864765298
commit 71be2c6763
5 changed files with 70 additions and 6 deletions

View File

@ -168,7 +168,24 @@ impl Module {
/// Iterate over the exports that this module provides. /// Iterate over the exports that this module provides.
/// ///
/// TODO: show example here /// ```
/// # use wasmer_runtime_core::module::*;
/// # fn example(module: &Module) {
/// // We can filter by `ExportKind` to get only certain types of exports.
/// // For example, here we get all the names of the functions exported by this module.
/// let function_names =
/// module.exports()
/// .filter(|ed| ed.kind == ExportKind::Function)
/// .map(|ed| ed.name)
/// .collect::<Vec<String>>();
///
/// // And here we count the number of global variables exported by this module.
/// let num_globals =
/// module.exports()
/// .filter(|ed| ed.kind == ExportKind::Global)
/// .count();
/// # }
/// ```
pub fn exports(&self) -> impl Iterator<Item = ExportDescriptor> + '_ { pub fn exports(&self) -> impl Iterator<Item = ExportDescriptor> + '_ {
self.inner self.inner
.info .info
@ -254,11 +271,15 @@ impl Module {
out out
} }
/// Find the custom section(s?) matching the given name. /// Get the custom sections matching the given name.
// TODO: JS API returns `Vec<&[u8]>` here pub fn custom_sections(&self, key: impl AsRef<str>) -> Option<Vec<&[u8]>> {
pub fn custom_section(&self, key: impl AsRef<str>) -> Option<&[u8]> {
let key = key.as_ref(); let key = key.as_ref();
self.inner.info.custom_sections.get(key).map(|v| v.as_ref()) // TODO: handle multiple better when our system does
self.inner
.info
.custom_sections
.get(key)
.map(|v| vec![v.as_ref()])
} }
} }

View File

@ -98,6 +98,18 @@ impl AnyfuncTable {
Some(starting_len) Some(starting_len)
} }
/// Get The vm::AnyFunc at the given index.
pub fn get<'outer_table>(&self, index: u32) -> Option<Anyfunc<'outer_table>> {
let vm_any_func = self.backing.get(index as usize)?;
let signature = SigRegistry.lookup_signature(vm_any_func.sig_id.into());
Some(Anyfunc {
inner: AnyfuncInner::Host {
ptr: vm_any_func.func,
signature,
},
})
}
pub fn set(&mut self, index: u32, element: Anyfunc) -> Result<(), ()> { pub fn set(&mut self, index: u32, element: Anyfunc) -> Result<(), ()> {
if let Some(slot) = self.backing.get_mut(index as usize) { if let Some(slot) = self.backing.get_mut(index as usize) {
let anyfunc = match element.inner { let anyfunc = match element.inner {

View File

@ -89,6 +89,16 @@ impl Table {
self.desc self.desc
} }
/// Get the `Element` at the given index in the table
pub fn get(&self, index: u32) -> Option<Element> {
let storage = self.storage.lock().unwrap();
match &*storage {
(TableStorage::Anyfunc(ref anyfunc_table), _) => {
anyfunc_table.get(index).map(Element::Anyfunc)
}
}
}
/// Set the element at index. /// Set the element at index.
pub fn set(&self, index: u32, element: Element) -> Result<(), ()> { pub fn set(&self, index: u32, element: Element) -> Result<(), ()> {
let mut storage = self.storage.lock().unwrap(); let mut storage = self.storage.lock().unwrap();

View File

@ -1,4 +1,4 @@
//! The units module provides common WebAssembly units like [`Pages`] and conversion functions into //! This module provides common WebAssembly units like [`Pages`] and conversion functions into
//! other units. //! other units.
use crate::error::PageError; use crate::error::PageError;
use std::{ use std::{
@ -45,6 +45,12 @@ impl fmt::Debug for Pages {
} }
} }
impl From<u32> for Pages {
fn from(other: u32) -> Self {
Pages(other)
}
}
/// Units of WebAssembly memory in terms of 8-bit bytes. /// Units of WebAssembly memory in terms of 8-bit bytes.
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Bytes(pub usize); pub struct Bytes(pub usize);
@ -61,6 +67,12 @@ impl From<Pages> for Bytes {
} }
} }
impl From<usize> for Bytes {
fn from(other: usize) -> Self {
Bytes(other)
}
}
impl<T> Sub<T> for Pages impl<T> Sub<T> for Pages
where where
T: Into<Pages>, T: Into<Pages>,

View File

@ -716,10 +716,19 @@ impl LocalGlobal {
} }
/// Identifier for a function signature. /// Identifier for a function signature.
///
/// A transparent `SigIndex`
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(transparent)] #[repr(transparent)]
pub struct SigId(pub u32); pub struct SigId(pub u32);
use crate::types::SigIndex;
impl From<SigId> for SigIndex {
fn from(other: SigId) -> SigIndex {
SigIndex::new(other.0 as _)
}
}
/// Caller-checked anyfunc /// Caller-checked anyfunc
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(C)] #[repr(C)]