Skip to content

Commit

Permalink
Add getter to table and other misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Mar 18, 2020
1 parent f864765 commit 71be2c6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
31 changes: 26 additions & 5 deletions lib/runtime-core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,24 @@ impl Module {

/// 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> + '_ {
self.inner
.info
Expand Down Expand Up @@ -254,11 +271,15 @@ impl Module {
out
}

/// Find the custom section(s?) matching the given name.
// TODO: JS API returns `Vec<&[u8]>` here
pub fn custom_section(&self, key: impl AsRef<str>) -> Option<&[u8]> {
/// Get the custom sections matching the given name.
pub fn custom_sections(&self, key: impl AsRef<str>) -> Option<Vec<&[u8]>> {
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()])
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/runtime-core/src/table/anyfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ impl AnyfuncTable {
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<(), ()> {
if let Some(slot) = self.backing.get_mut(index as usize) {
let anyfunc = match element.inner {
Expand Down
10 changes: 10 additions & 0 deletions lib/runtime-core/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ impl Table {
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.
pub fn set(&self, index: u32, element: Element) -> Result<(), ()> {
let mut storage = self.storage.lock().unwrap();
Expand Down
14 changes: 13 additions & 1 deletion lib/runtime-core/src/units.rs
Original file line number Diff line number Diff line change
@@ -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.
use crate::error::PageError;
use std::{
Expand Down Expand Up @@ -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.
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Bytes(pub usize);
Expand All @@ -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
where
T: Into<Pages>,
Expand Down
9 changes: 9 additions & 0 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,19 @@ impl LocalGlobal {
}

/// Identifier for a function signature.
///
/// A transparent `SigIndex`
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
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
#[derive(Debug, Clone, Copy)]
#[repr(C)]
Expand Down

0 comments on commit 71be2c6

Please sign in to comment.