From 4a9eb81ae93713439e54ecf0e0e0852c301f8c8d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 12 Apr 2024 17:44:03 -0700 Subject: [PATCH 1/2] Fix calculation of gc refs in functions In addition to excluding i31 also exclude funcrefs. --- crates/types/src/lib.rs | 8 ++++++-- tests/all/func.rs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index fbddd878c776..34423e01d06f 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -374,14 +374,18 @@ impl WasmFuncType { let non_i31_gc_ref_params_count = params .iter() .filter(|p| match **p { - WasmValType::Ref(rt) => rt.heap_type != WasmHeapType::I31, + WasmValType::Ref(rt) => { + rt.heap_type != WasmHeapType::I31 && rt.heap_type != WasmHeapType::Func + } _ => false, }) .count(); let non_i31_gc_ref_returns_count = returns .iter() .filter(|r| match **r { - WasmValType::Ref(rt) => rt.heap_type != WasmHeapType::I31, + WasmValType::Ref(rt) => { + rt.heap_type != WasmHeapType::I31 && rt.heap_type != WasmHeapType::Func + } _ => false, }) .count(); diff --git a/tests/all/func.rs b/tests/all/func.rs index 3f84cbdc339a..30cebd6d487a 100644 --- a/tests/all/func.rs +++ b/tests/all/func.rs @@ -2112,3 +2112,20 @@ fn wrap_and_typed_i31ref() -> Result<()> { assert_eq!(HITS.load(Ordering::SeqCst), 2); Ok(()) } + +#[test] +fn call_func_with_funcref_both_typed_and_untyped() -> Result<()> { + let mut config = Config::new(); + config.wasm_function_references(true); + config.wasm_gc(true); + + let engine = Engine::new(&config)?; + let mut store = Store::new(&engine, ()); + + let f1 = Func::wrap(&mut store, |_: Option| {}); + let f2 = Func::wrap(&mut store, || {}); + + f1.typed::(&mut store)?.call(&mut store, f2)?; + f1.call(&mut store, &[Val::FuncRef(Some(f2))], &mut [])?; + Ok(()) +} From bf07ea4efdbfd198e68ea132c7e4ce7bd9ae489d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 13 Apr 2024 11:37:09 -0700 Subject: [PATCH 2/2] Review comments --- crates/types/src/lib.rs | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 34423e01d06f..81d1c5e700b9 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -371,24 +371,8 @@ impl TypeTrace for WasmFuncType { impl WasmFuncType { #[inline] pub fn new(params: Box<[WasmValType]>, returns: Box<[WasmValType]>) -> Self { - let non_i31_gc_ref_params_count = params - .iter() - .filter(|p| match **p { - WasmValType::Ref(rt) => { - rt.heap_type != WasmHeapType::I31 && rt.heap_type != WasmHeapType::Func - } - _ => false, - }) - .count(); - let non_i31_gc_ref_returns_count = returns - .iter() - .filter(|r| match **r { - WasmValType::Ref(rt) => { - rt.heap_type != WasmHeapType::I31 && rt.heap_type != WasmHeapType::Func - } - _ => false, - }) - .count(); + let non_i31_gc_ref_params_count = params.iter().filter(|p| p.is_gc_heap_type()).count(); + let non_i31_gc_ref_returns_count = returns.iter().filter(|r| r.is_gc_heap_type()).count(); WasmFuncType { params, non_i31_gc_ref_params_count,