From 252ba39c27c8fe3e88488391b7c8189f53e4cc0f Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 21 Oct 2021 14:15:53 -0700 Subject: [PATCH] implement table _async methods, test passes now --- crates/wasmtime/src/externals.rs | 50 ++++++++++++++++++++++++++++++++ tests/all/limits.rs | 14 +++++---- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/crates/wasmtime/src/externals.rs b/crates/wasmtime/src/externals.rs index d267bf14a7..686b154472 100644 --- a/crates/wasmtime/src/externals.rs +++ b/crates/wasmtime/src/externals.rs @@ -436,6 +436,31 @@ impl Table { Table::_new(store.as_context_mut().0, ty, init) } + /// Async variant of [`Table::new`]. You must use this variant with [`Store`]s which have a + /// [`ResourceLimiterAsync`]. + /// + /// # Panics + /// + /// This function will panic when used with a non-async [`Store`]. + #[cfg(feature = "async")] + pub async fn new_async( + mut store: impl AsContextMut, + ty: TableType, + init: Val, + ) -> Result + where + T: Send, + { + let mut store = store.as_context_mut(); + assert!( + store.0.async_support(), + "cannot use `new_async` without enabling async support on the config" + ); + store + .on_fiber(|store| Table::_new(store.0, ty, init)) + .await? + } + fn _new(store: &mut StoreOpaque, ty: TableType, init: Val) -> Result
{ let wasmtime_export = generate_table_export(store, &ty)?; let init = init.into_table_element(store, ty.element())?; @@ -562,6 +587,31 @@ impl Table { } } + /// Async variant of [`Table::grow`]. Required when using a [`ResourceLimiterAsync`]. + /// + /// # Panics + /// + /// This function will panic when used with a non-async [`Store`]. + #[cfg(feature = "async")] + pub async fn grow_async( + &self, + mut store: impl AsContextMut, + delta: u32, + init: Val, + ) -> Result + where + T: Send, + { + let mut store = store.as_context_mut(); + assert!( + store.0.async_support(), + "cannot use `grow_async` without enabling async support on the config" + ); + store + .on_fiber(|store| self.grow(store, delta, init)) + .await? + } + /// Copy `len` elements from `src_table[src_index..]` into /// `dst_table[dst_index..]`. /// diff --git a/tests/all/limits.rs b/tests/all/limits.rs index 3a5388cf20..c19ab4b047 100644 --- a/tests/all/limits.rs +++ b/tests/all/limits.rs @@ -134,19 +134,21 @@ async fn test_limits_async() -> Result<()> { // Test instance exports and host objects hitting the limit for table in std::array::IntoIter::new([ instance.get_table(&mut store, "t").unwrap(), - Table::new( + Table::new_async( &mut store, TableType::new(ValType::FuncRef, 0, None), Val::FuncRef(None), - )?, + ) + .await?, ]) { - table.grow(&mut store, 2, Val::FuncRef(None))?; - table.grow(&mut store, 1, Val::FuncRef(None))?; - table.grow(&mut store, 2, Val::FuncRef(None))?; + table.grow_async(&mut store, 2, Val::FuncRef(None)).await?; + table.grow_async(&mut store, 1, Val::FuncRef(None)).await?; + table.grow_async(&mut store, 2, Val::FuncRef(None)).await?; assert_eq!( table - .grow(&mut store, 1, Val::FuncRef(None)) + .grow_async(&mut store, 1, Val::FuncRef(None)) + .await .map_err(|e| e.to_string()) .unwrap_err(), "failed to grow table by `1`"