Path: blob/main/crates/wasi-common/src/table.rs
1691 views
use crate::{Error, ErrorExt};1use std::any::Any;2use std::collections::HashMap;3use std::sync::{Arc, RwLock};45/// The `Table` type is designed to map u32 handles to resources. The table is now part of the6/// public interface to a `WasiCtx` - it is reference counted so that it can be shared beyond a7/// `WasiCtx` with other WASI proposals (e.g. `wasi-crypto` and `wasi-nn`) to manage their8/// resources. Elements in the `Table` are `Any` typed.9///10/// The `Table` type is intended to model how the Interface Types concept of Resources is shaping11/// up. Right now it is just an approximation.12pub struct Table(RwLock<Inner>);1314struct Inner {15map: HashMap<u32, Arc<dyn Any + Send + Sync>>,16next_key: u32,17}1819impl Table {20/// Create an empty table. New insertions will begin at 3, above stdio.21pub fn new() -> Self {22Table(RwLock::new(Inner {23map: HashMap::new(),24next_key: 3, // 0, 1 and 2 are reserved for stdio25}))26}2728/// Insert a resource at a certain index.29pub fn insert_at<T: Any + Send + Sync>(&self, key: u32, a: Arc<T>) {30self.0.write().unwrap().map.insert(key, a);31}3233/// Insert a resource at the next available index.34pub fn push<T: Any + Send + Sync>(&self, a: Arc<T>) -> Result<u32, Error> {35let mut inner = self.0.write().unwrap();36// NOTE: The performance of this new key calculation could be very bad once keys wrap37// around.38if inner.map.len() == u32::MAX as usize {39return Err(Error::trap(anyhow::Error::msg("table has no free keys")));40}41loop {42let key = inner.next_key;43inner.next_key += 1;44if inner.map.contains_key(&key) {45continue;46}47inner.map.insert(key, a);48return Ok(key);49}50}5152/// Check if the table has a resource at the given index.53pub fn contains_key(&self, key: u32) -> bool {54self.0.read().unwrap().map.contains_key(&key)55}5657/// Check if the resource at a given index can be downcast to a given type.58/// Note: this will always fail if the resource is already borrowed.59pub fn is<T: Any + Sized>(&self, key: u32) -> bool {60if let Some(r) = self.0.read().unwrap().map.get(&key) {61r.is::<T>()62} else {63false64}65}6667/// Get an Arc reference to a resource of a given type at a given index. Multiple68/// immutable references can be borrowed at any given time.69pub fn get<T: Any + Send + Sync + Sized>(&self, key: u32) -> Result<Arc<T>, Error> {70if let Some(r) = self.0.read().unwrap().map.get(&key).cloned() {71r.downcast::<T>()72.map_err(|_| Error::badf().context("element is a different type"))73} else {74Err(Error::badf().context("key not in table"))75}76}7778/// Get a mutable reference to a resource of a given type at a given index.79/// Only one such reference can be borrowed at any given time.80pub fn get_mut<T: Any>(&mut self, key: u32) -> Result<&mut T, Error> {81let entry = match self.0.get_mut().unwrap().map.get_mut(&key) {82Some(entry) => entry,83None => return Err(Error::badf().context("key not in table")),84};85let entry = match Arc::get_mut(entry) {86Some(entry) => entry,87None => return Err(Error::badf().context("cannot mutably borrow shared file")),88};89entry90.downcast_mut::<T>()91.ok_or_else(|| Error::badf().context("element is a different type"))92}9394/// Remove a resource at a given index from the table. Returns the resource95/// if it was present.96pub fn delete<T: Any + Send + Sync>(&self, key: u32) -> Option<Arc<T>> {97self.098.write()99.unwrap()100.map101.remove(&key)102.map(|r| r.downcast::<T>().unwrap())103}104105/// Remove a resource at a given index from the table. Returns the resource106/// if it was present.107pub fn renumber(&self, from: u32, to: u32) -> Result<(), Error> {108let map = &mut self.0.write().unwrap().map;109let from_entry = map.remove(&from).ok_or(Error::badf())?;110map.insert(to, from_entry);111Ok(())112}113}114115116