diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index b746a65ed..fbae06915 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -325,6 +325,18 @@ impl<'a> Exportable<'a> for Function { } } +impl<'a> Exportable<'a> for &'a Function { + fn to_export(&self) -> Export { + self.exported.clone().into() + } + fn get_self_from_extern(_extern: &'a Extern) -> Result { + match _extern { + Extern::Function(func) => Ok(func), + _ => Err(ExportError::IncompatibleType), + } + } +} + impl std::fmt::Debug for Function { fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) diff --git a/lib/api/src/externals/global.rs b/lib/api/src/externals/global.rs index 6a093b6cc..16912b57e 100644 --- a/lib/api/src/externals/global.rs +++ b/lib/api/src/externals/global.rs @@ -137,3 +137,16 @@ impl<'a> Exportable<'a> for Global { } } } + +impl<'a> Exportable<'a> for &'a Global { + fn to_export(&self) -> Export { + self.exported.clone().into() + } + + fn get_self_from_extern(_extern: &'a Extern) -> Result { + match _extern { + Extern::Global(global) => Ok(global), + _ => Err(ExportError::IncompatibleType), + } + } +} diff --git a/lib/api/src/externals/memory.rs b/lib/api/src/externals/memory.rs index 7cd387fbc..5e045c744 100644 --- a/lib/api/src/externals/memory.rs +++ b/lib/api/src/externals/memory.rs @@ -145,6 +145,18 @@ impl<'a> Exportable<'a> for Memory { } } +impl<'a> Exportable<'a> for &'a Memory { + fn to_export(&self) -> Export { + self.exported.clone().into() + } + fn get_self_from_extern(_extern: &'a Extern) -> Result { + match _extern { + Extern::Memory(memory) => Ok(memory), + _ => Err(ExportError::IncompatibleType), + } + } +} + impl Drop for Memory { fn drop(&mut self) { if self.owned_by_store { diff --git a/lib/api/src/externals/mod.rs b/lib/api/src/externals/mod.rs index 0ab06cc1c..35ee19837 100644 --- a/lib/api/src/externals/mod.rs +++ b/lib/api/src/externals/mod.rs @@ -52,11 +52,26 @@ impl<'a> Exportable<'a> for Extern { } fn get_self_from_extern(_extern: &'a Extern) -> Result { - // Since this is already an extern, we can just return it. Ok(_extern.clone()) } } +impl<'a> Exportable<'a> for &'a Extern { + fn to_export(&self) -> Export { + match self { + Extern::Function(f) => f.to_export(), + Extern::Global(g) => g.to_export(), + Extern::Memory(m) => m.to_export(), + Extern::Table(t) => t.to_export(), + } + } + + fn get_self_from_extern(_extern: &'a Extern) -> Result { + // Since this is already an extern, we can just return it. + Ok(_extern) + } +} + impl StoreObject for Extern { fn comes_from_same_store(&self, store: &Store) -> bool { let my_store = match self { diff --git a/lib/api/src/externals/table.rs b/lib/api/src/externals/table.rs index 6aa4dce93..f6bd68fea 100644 --- a/lib/api/src/externals/table.rs +++ b/lib/api/src/externals/table.rs @@ -165,3 +165,15 @@ impl<'a> Exportable<'a> for Table { } } } + +impl<'a> Exportable<'a> for &'a Table { + fn to_export(&self) -> Export { + self.exported.clone().into() + } + fn get_self_from_extern(_extern: &'a Extern) -> Result { + match _extern { + Extern::Table(table) => Ok(table), + _ => Err(ExportError::IncompatibleType), + } + } +} diff --git a/lib/api/tests/externals.rs b/lib/api/tests/externals.rs index 37ff860fc..bfae5994d 100644 --- a/lib/api/tests/externals.rs +++ b/lib/api/tests/externals.rs @@ -106,8 +106,6 @@ fn table_set() -> Result<()> { Ok(()) } -// TODO: review, was this working before? -#[ignore] #[test] fn table_grow() -> Result<()> { let store = Store::default(); @@ -119,13 +117,12 @@ fn table_grow() -> Result<()> { let f = Function::new(&store, |num: i32| num + 1); let table = Table::new(&store, table_type, Value::FuncRef(f.clone()))?; // Growing to a bigger maximum should return None - let new_len = table.grow(12, Value::FuncRef(f.clone())); - assert!(new_len.is_err()); + let old_len = table.grow(12, Value::FuncRef(f.clone())); + assert!(old_len.is_err()); // Growing to a bigger maximum should return None - let new_len = table.grow(5, Value::FuncRef(f.clone()))?; - // TODO: new len should be instead previous length, similarly to memory - assert_eq!(new_len, 5); + let old_len = table.grow(5, Value::FuncRef(f.clone()))?; + assert_eq!(old_len, 0); Ok(()) } @@ -333,6 +330,7 @@ fn function_new_dynamic_env() -> Result<()> { Ok(()) } +// TODO: unignore this when calling host functions has been implemented #[ignore] #[test] fn native_function_works() -> Result<()> {