diff --git a/lib/c-api/src/wasm_c_api/types/table.rs b/lib/c-api/src/wasm_c_api/types/table.rs index 3b76dca8e..0673c085a 100644 --- a/lib/c-api/src/wasm_c_api/types/table.rs +++ b/lib/c-api/src/wasm_c_api/types/table.rs @@ -12,6 +12,7 @@ const LIMITS_MAX_SENTINEL: u32 = u32::max_value(); pub(crate) struct WasmTableType { pub(crate) table_type: TableType, limits: Box, + content: Box, } impl WasmTableType { @@ -20,8 +21,13 @@ impl WasmTableType { min: table_type.minimum as _, max: table_type.maximum.unwrap_or(LIMITS_MAX_SENTINEL), }); + let content = Box::new(table_type.ty.into()); - Self { table_type, limits } + Self { + table_type, + limits, + content, + } } } @@ -77,15 +83,9 @@ pub unsafe extern "C" fn wasm_tabletype_limits(table_type: &wasm_tabletype_t) -> table_type.inner().limits.as_ref() } -// TODO: fix memory leak -// this function leaks memory because the returned limits pointer is not owned #[no_mangle] -pub unsafe extern "C" fn wasm_tabletype_element( - table_type: &wasm_tabletype_t, -) -> *const wasm_valtype_t { - let table_type = table_type.inner().table_type; - - Box::into_raw(Box::new(table_type.ty.into())) +pub unsafe extern "C" fn wasm_tabletype_element(table_type: &wasm_tabletype_t) -> &wasm_valtype_t { + table_type.inner().content.as_ref() } #[no_mangle]