177 lines
4.6 KiB
Rust
Raw Normal View History

2019-01-29 10:16:39 -08:00
use crate::{
2019-01-29 15:44:15 -08:00
error::CreationError,
2019-01-29 10:16:39 -08:00
export::Export,
import::IsExport,
2019-01-29 13:04:42 -08:00
types::{ElementType, TableDescriptor},
2019-01-29 10:16:39 -08:00
vm,
};
use std::{
fmt, ptr,
sync::{Arc, Mutex},
};
2019-01-29 10:16:39 -08:00
mod anyfunc;
pub use self::anyfunc::Anyfunc;
pub(crate) use self::anyfunc::AnyfuncTable;
use crate::error::GrowError;
2019-01-29 10:16:39 -08:00
pub enum Element<'a> {
Anyfunc(Anyfunc<'a>),
}
// #[derive(Debug)]
pub enum TableStorage {
/// This is intended to be a caller-checked Anyfunc.
Anyfunc(Box<AnyfuncTable>),
}
pub struct Table {
2019-01-29 13:04:42 -08:00
desc: TableDescriptor,
storage: Arc<Mutex<(TableStorage, vm::LocalTable)>>,
2019-01-29 10:16:39 -08:00
}
impl Table {
2019-01-29 14:15:59 -08:00
/// Create a new `Table` from a [`TableDescriptor`]
2019-01-29 15:44:15 -08:00
///
2019-01-29 14:15:59 -08:00
/// [`TableDescriptor`]: struct.TableDescriptor.html
2019-01-29 15:44:15 -08:00
///
2019-01-29 14:15:59 -08:00
/// Usage:
2019-01-29 15:44:15 -08:00
///
2019-01-29 14:15:59 -08:00
/// ```
/// # use wasmer_runtime_core::types::{TableDescriptor, ElementType};
/// # use wasmer_runtime_core::table::Table;
/// # use wasmer_runtime_core::error::Result;
/// # fn create_table() -> Result<()> {
/// let descriptor = TableDescriptor {
/// element: ElementType::Anyfunc,
/// minimum: 10,
/// maximum: None,
/// };
2019-01-29 15:44:15 -08:00
///
2019-01-29 14:15:59 -08:00
/// let table = Table::new(descriptor)?;
/// # Ok(())
/// # }
/// ```
2019-01-29 15:44:15 -08:00
pub fn new(desc: TableDescriptor) -> Result<Self, CreationError> {
if let Some(max) = desc.maximum {
if max < desc.minimum {
return Err(CreationError::InvalidDescriptor(
"Max table size is less than the minimum size".to_string(),
));
}
}
2019-01-29 10:16:39 -08:00
let mut local = vm::LocalTable {
base: ptr::null_mut(),
count: 0,
table: ptr::null_mut(),
};
2019-01-29 13:04:42 -08:00
let storage = match desc.element {
2019-01-29 10:16:39 -08:00
ElementType::Anyfunc => TableStorage::Anyfunc(AnyfuncTable::new(desc, &mut local)?),
};
Ok(Self {
desc,
storage: Arc::new(Mutex::new((storage, local))),
2019-01-29 10:16:39 -08:00
})
}
2019-01-29 15:44:15 -08:00
/// Get the `TableDescriptor` used to create this `Table`.
2019-01-29 13:04:42 -08:00
pub fn descriptor(&self) -> TableDescriptor {
2019-01-29 10:16:39 -08:00
self.desc
}
2019-01-29 15:44:15 -08:00
/// Set the element at index.
2019-01-29 10:16:39 -08:00
pub fn set(&self, index: u32, element: Element) -> Result<(), ()> {
let mut storage = self.storage.lock().unwrap();
match &mut *storage {
2019-01-29 10:16:39 -08:00
(TableStorage::Anyfunc(ref mut anyfunc_table), _) => {
match element {
Element::Anyfunc(anyfunc) => anyfunc_table.set(index, anyfunc),
// _ => panic!("wrong element type for anyfunc table"),
}
}
}
}
pub(crate) fn anyfunc_direct_access_mut<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut [vm::Anyfunc]) -> R,
{
let mut storage = self.storage.lock().unwrap();
match &mut *storage {
2019-01-29 10:16:39 -08:00
(TableStorage::Anyfunc(ref mut anyfunc_table), _) => f(anyfunc_table.internal_buffer()),
}
}
2019-01-29 15:44:15 -08:00
/// The current size of this table.
pub fn size(&self) -> u32 {
let storage = self.storage.lock().unwrap();
match &*storage {
2019-01-29 10:16:39 -08:00
(TableStorage::Anyfunc(ref anyfunc_table), _) => anyfunc_table.current_size(),
}
}
2019-01-29 15:44:15 -08:00
/// Grow this table by `delta`.
pub fn grow(&self, delta: u32) -> Result<u32, GrowError> {
2019-01-29 10:16:39 -08:00
if delta == 0 {
return Ok(self.size());
2019-01-29 10:16:39 -08:00
}
let mut storage = self.storage.lock().unwrap();
match &mut *storage {
(TableStorage::Anyfunc(ref mut anyfunc_table), ref mut local) => anyfunc_table
.grow(delta, local)
.ok_or(GrowError::TableGrowError),
2019-01-29 10:16:39 -08:00
}
}
pub fn vm_local_table(&mut self) -> *mut vm::LocalTable {
let mut storage = self.storage.lock().unwrap();
&mut storage.1
2019-01-29 10:16:39 -08:00
}
}
impl IsExport for Table {
2019-02-02 15:58:33 -08:00
fn to_export(&self) -> Export {
2019-01-29 10:16:39 -08:00
Export::Table(self.clone())
}
}
impl Clone for Table {
fn clone(&self) -> Self {
Self {
desc: self.desc,
storage: Arc::clone(&self.storage),
2019-01-29 10:16:39 -08:00
}
}
}
impl fmt::Debug for Table {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-01-29 15:44:15 -08:00
f.debug_struct("Table")
.field("desc", &self.desc)
.field("size", &self.size())
.finish()
2019-01-29 10:16:39 -08:00
}
}
#[cfg(test)]
mod table_tests {
use super::{ElementType, Table, TableDescriptor};
#[test]
fn test_initial_table_size() {
let table = Table::new(TableDescriptor {
element: ElementType::Anyfunc,
minimum: 10,
maximum: Some(20),
})
.unwrap();
assert_eq!(table.size(), 10);
}
}