From 166460f15857aec11e9d524318d1c667b1397acd Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sun, 1 Sep 2024 17:42:26 +0200 Subject: [PATCH] test both canonical and non-canonical lists --- .../tests/symmetric_lists/rust_a/src/main.rs | 3 + .../cpp/tests/symmetric_lists/rust_a/src/x.rs | 38 +++- .../tests/symmetric_lists/rust_b/src/lib.rs | 7 +- .../cpp/tests/symmetric_lists/rust_b/src/w.rs | 215 ++++++++++-------- .../cpp/tests/symmetric_lists/wit/lists.wit | 1 + 5 files changed, 162 insertions(+), 102 deletions(-) diff --git a/crates/cpp/tests/symmetric_lists/rust_a/src/main.rs b/crates/cpp/tests/symmetric_lists/rust_a/src/main.rs index 193f5d1cf..4f5f0a6b8 100644 --- a/crates/cpp/tests/symmetric_lists/rust_a/src/main.rs +++ b/crates/cpp/tests/symmetric_lists/rust_a/src/main.rs @@ -16,4 +16,7 @@ fn main() { let input = vec!["hello".into(), "world".into()]; let output = x::test::test::i::f(&input); println!("{output:?}"); + let input2 = vec![1,2,3]; + let output2 = x::test::test::i::g(&input2); + println!("{output2:?}"); } diff --git a/crates/cpp/tests/symmetric_lists/rust_a/src/x.rs b/crates/cpp/tests/symmetric_lists/rust_a/src/x.rs index 4a46bcfcb..bd8902872 100644 --- a/crates/cpp/tests/symmetric_lists/rust_a/src/x.rs +++ b/crates/cpp/tests/symmetric_lists/rust_a/src/x.rs @@ -85,6 +85,33 @@ pub mod test { result8 } } + #[allow(unused_unsafe, clippy::all)] + pub fn g(a: &[u8]) -> _rt::Vec { + unsafe { + #[cfg_attr(target_pointer_width = "64", repr(align(8)))] + #[cfg_attr(target_pointer_width = "32", repr(align(4)))] + struct RetArea( + [::core::mem::MaybeUninit; 2 * core::mem::size_of::<*const u8>()], + ); + let mut ret_area = RetArea( + [::core::mem::MaybeUninit::uninit(); 2 * core::mem::size_of::<*const u8>()], + ); + let vec0 = a; + let ptr0 = vec0.as_ptr().cast::(); + let len0 = vec0.len(); + let ptr1 = ret_area.0.as_mut_ptr().cast::(); + #[link(wasm_import_module = "test:test/i")] + extern "C" { + #[cfg_attr(target_arch = "wasm32", link_name = "g")] + fn testX3AtestX2FiX00g(_: *mut u8, _: usize, _: *mut u8); + } + testX3AtestX2FiX00g(ptr0.cast_mut(), len0, ptr1); + let l2 = *ptr1.add(0).cast::<*mut u8>(); + let l3 = *ptr1.add(core::mem::size_of::<*const u8>()).cast::(); + let len4 = l3; + _rt::Vec::from_raw_parts(l2.cast(), len4, len4) + } + } } } } @@ -112,11 +139,12 @@ mod _rt { #[cfg(target_arch = "wasm32")] #[link_section = "component-type:wit-bindgen:0.30.0:test:test:x:encoded world"] #[doc(hidden)] -pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 177] = *b"\ -\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07:\x01A\x02\x01A\x02\x01\ -B\x03\x01ps\x01@\x01\x01a\0\0\0\x04\0\x01f\x01\x01\x03\x01\x0btest:test/i\x05\0\x04\ -\x01\x0btest:test/x\x04\0\x0b\x07\x01\0\x01x\x03\0\0\0G\x09producers\x01\x0cproc\ -essed-by\x02\x0dwit-component\x070.216.0\x10wit-bindgen-rust\x060.30.0"; +pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 194] = *b"\ +\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07K\x01A\x02\x01A\x02\x01\ +B\x06\x01ps\x01@\x01\x01a\0\0\0\x04\0\x01f\x01\x01\x01p}\x01@\x01\x01a\x02\0\x02\ +\x04\0\x01g\x01\x03\x03\x01\x0btest:test/i\x05\0\x04\x01\x0btest:test/x\x04\0\x0b\ +\x07\x01\0\x01x\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\ +\x070.216.0\x10wit-bindgen-rust\x060.30.0"; #[inline(never)] #[doc(hidden)] diff --git a/crates/cpp/tests/symmetric_lists/rust_b/src/lib.rs b/crates/cpp/tests/symmetric_lists/rust_b/src/lib.rs index 1f863ea10..ebecc3949 100644 --- a/crates/cpp/tests/symmetric_lists/rust_b/src/lib.rs +++ b/crates/cpp/tests/symmetric_lists/rust_b/src/lib.rs @@ -1,10 +1,13 @@ - mod w; struct MyImpl; impl w::exports::test::test::i::Guest for MyImpl { - fn f(a: Vec::,) -> Vec:: { + fn f(a: Vec) -> Vec { + a + } + + fn g(a: Vec) -> Vec { a } } diff --git a/crates/cpp/tests/symmetric_lists/rust_b/src/w.rs b/crates/cpp/tests/symmetric_lists/rust_b/src/w.rs index 3ac41f90b..97804d38b 100644 --- a/crates/cpp/tests/symmetric_lists/rust_b/src/w.rs +++ b/crates/cpp/tests/symmetric_lists/rust_b/src/w.rs @@ -2,105 +2,130 @@ // Options used: #[allow(dead_code)] pub mod exports { - #[allow(dead_code)] - pub mod test { #[allow(dead_code)] pub mod test { - #[allow(dead_code, clippy::all)] - pub mod i { - #[used] - #[doc(hidden)] - static __FORCE_SECTION_REF: fn() = - super::super::super::super::__link_custom_section_describing_imports; - - use super::super::super::super::_rt; - #[doc(hidden)] - #[allow(non_snake_case)] - pub unsafe fn _export_f_cabi(arg0: *mut u8,arg1: usize,arg2: *mut u8,) {#[cfg(target_arch="wasm32")] - _rt::run_ctors_once();let base3 = arg0; - let len3 = arg1; - let mut result3 = _rt::Vec::with_capacity(len3); - for i in 0..len3 { - let base = base3.add(i * (2*core::mem::size_of::<*const u8>())); - let e3 = { - let l0 = *base.add(0).cast::<*mut u8>(); - let l1 = *base.add(core::mem::size_of::<*const u8>()).cast::(); - let len2 = l1; - let string2 = String::from(std::str::from_utf8(std::slice::from_raw_parts(l0, len2)).unwrap()); + #[allow(dead_code)] + pub mod test { + #[allow(dead_code, clippy::all)] + pub mod i { + #[used] + #[doc(hidden)] + static __FORCE_SECTION_REF: fn() = + super::super::super::super::__link_custom_section_describing_imports; - string2 - }; - result3.push(e3); - } - //_rt::cabi_dealloc(base3, len3 * (2*core::mem::size_of::<*const u8>()), core::mem::size_of::<*const u8>()); - let result4 = T::f(result3); - let vec6 = result4; - let len6 = vec6.len(); - let layout6 = _rt::alloc::Layout::from_size_align_unchecked(vec6.len() * (2*core::mem::size_of::<*const u8>()), core::mem::size_of::<*const u8>()); - let result6 = if layout6.size() != 0 { - let ptr = _rt::alloc::alloc(layout6).cast::(); - if ptr.is_null() - { - _rt::alloc::handle_alloc_error(layout6); - } - ptr - }else { - ::core::ptr::null_mut() - }; - for (i, e) in vec6.into_iter().enumerate() { - let base = result6.add(i * (2*core::mem::size_of::<*const u8>())); - { - let vec5 = (e.into_bytes()).into_boxed_slice(); - let ptr5 = vec5.as_ptr().cast::(); - let len5 = vec5.len(); - ::core::mem::forget(vec5); - *base.add(core::mem::size_of::<*const u8>()).cast::() = len5; - *base.add(0).cast::<*mut u8>() = ptr5.cast_mut(); - } - } - *arg2.add(core::mem::size_of::<*const u8>()).cast::() = len6; - *arg2.add(0).cast::<*mut u8>() = result6; - } - pub trait Guest { - fn f(a: _rt::Vec::<_rt::String>,) -> _rt::Vec::<_rt::String>; - } - #[doc(hidden)] + use super::super::super::super::_rt; + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_f_cabi(arg0: *mut u8, arg1: usize, arg2: *mut u8) { + #[cfg(target_arch = "wasm32")] + _rt::run_ctors_once(); + let base3 = arg0; + let len3 = arg1; + let mut result3 = _rt::Vec::with_capacity(len3); + for i in 0..len3 { + let base = base3.add(i * (2 * core::mem::size_of::<*const u8>())); + let e3 = { + let l0 = *base.add(0).cast::<*mut u8>(); + let l1 = *base.add(core::mem::size_of::<*const u8>()).cast::(); + let len2 = l1; + let string2 = String::from( + std::str::from_utf8(std::slice::from_raw_parts(l0, len2)).unwrap(), + ); - macro_rules! __export_test_test_i_cabi{ - ($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { + string2 + }; + result3.push(e3); + } + //_rt::cabi_dealloc(base3, len3 * (2*core::mem::size_of::<*const u8>()), core::mem::size_of::<*const u8>()); + let result4 = T::f(result3); + let vec6 = result4; + let len6 = vec6.len(); + let layout6 = _rt::alloc::Layout::from_size_align_unchecked( + vec6.len() * (2 * core::mem::size_of::<*const u8>()), + core::mem::size_of::<*const u8>(), + ); + let result6 = if layout6.size() != 0 { + let ptr = _rt::alloc::alloc(layout6).cast::(); + if ptr.is_null() { + _rt::alloc::handle_alloc_error(layout6); + } + ptr + } else { + ::core::ptr::null_mut() + }; + for (i, e) in vec6.into_iter().enumerate() { + let base = result6.add(i * (2 * core::mem::size_of::<*const u8>())); + { + let vec5 = (e.into_bytes()).into_boxed_slice(); + let ptr5 = vec5.as_ptr().cast::(); + let len5 = vec5.len(); + ::core::mem::forget(vec5); + *base.add(core::mem::size_of::<*const u8>()).cast::() = len5; + *base.add(0).cast::<*mut u8>() = ptr5.cast_mut(); + } + } + *arg2.add(core::mem::size_of::<*const u8>()).cast::() = len6; + *arg2.add(0).cast::<*mut u8>() = result6; + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_g_cabi(arg0: *mut u8, arg1: usize, arg2: *mut u8) { + #[cfg(target_arch = "wasm32")] + _rt::run_ctors_once(); + let len0 = arg1; + let result1 = + T::g(unsafe{std::slice::from_raw_parts(arg0.cast(), len0)}.to_vec()); + let vec2 = result1; + let ptr2 = vec2.as_ptr().cast::(); + let len2 = vec2.len(); + vec2.leak(); + *arg2.add(core::mem::size_of::<*const u8>()).cast::() = len2; + *arg2.add(0).cast::<*mut u8>() = ptr2.cast_mut(); + } + pub trait Guest { + fn f(a: _rt::Vec<_rt::String>) -> _rt::Vec<_rt::String>; + fn g(a: _rt::Vec) -> _rt::Vec; + } + #[doc(hidden)] - #[cfg_attr(target_arch = "wasm32", export_name = "f")] - #[cfg_attr(not(target_arch = "wasm32"), no_mangle)] - unsafe extern "C" fn testX3AtestX2FiX00f(arg0: *mut u8,arg1: usize,arg2: *mut u8,) { - $($path_to_types)*::_export_f_cabi::<$ty>(arg0, arg1, arg2) - } - };); - } - #[doc(hidden)] - pub(crate) use __export_test_test_i_cabi; + macro_rules! __export_test_test_i_cabi{ + ($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { + #[cfg_attr(target_arch = "wasm32", export_name = "f")] + #[cfg_attr(not(target_arch = "wasm32"), no_mangle)] + unsafe extern "C" fn testX3AtestX2FiX00f(arg0: *mut u8,arg1: usize,arg2: *mut u8,) { + $($path_to_types)*::_export_f_cabi::<$ty>(arg0, arg1, arg2) + } + #[cfg_attr(target_arch = "wasm32", export_name = "g")] + #[cfg_attr(not(target_arch = "wasm32"), no_mangle)] + unsafe extern "C" fn testX3AtestX2FiX00g(arg0: *mut u8,arg1: usize,arg2: *mut u8,) { + $($path_to_types)*::_export_g_cabi::<$ty>(arg0, arg1, arg2) + } + };); + } + #[doc(hidden)] + pub(crate) use __export_test_test_i_cabi; + } + } } - - } -} } mod _rt { - #[cfg(target_arch = "wasm32")] - pub fn run_ctors_once() { - wit_bindgen::rt::run_ctors_once(); - } - pub use alloc_crate::vec::Vec; - pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) { - if size == 0 { - return; + #[cfg(target_arch = "wasm32")] + pub fn run_ctors_once() { + wit_bindgen::rt::run_ctors_once(); } - let layout = alloc::Layout::from_size_align_unchecked(size, align); - alloc::dealloc(ptr, layout); - } - pub use alloc_crate::alloc; - pub use alloc_crate::string::String; - extern crate alloc as alloc_crate; + pub use alloc_crate::vec::Vec; + pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) { + if size == 0 { + return; + } + let layout = alloc::Layout::from_size_align_unchecked(size, align); + alloc::dealloc(ptr, layout); + } + pub use alloc_crate::alloc; + pub use alloc_crate::string::String; + extern crate alloc as alloc_crate; } /// Generates `#[no_mangle]` functions to export the specified type as the @@ -134,15 +159,15 @@ pub(crate) use __export_w_impl as export; #[cfg(target_arch = "wasm32")] #[link_section = "component-type:wit-bindgen:0.30.0:test:test:w:encoded world"] #[doc(hidden)] -pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 177] = *b"\ -\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07:\x01A\x02\x01A\x02\x01\ -B\x03\x01ps\x01@\x01\x01a\0\0\0\x04\0\x01f\x01\x01\x04\x01\x0btest:test/i\x05\0\x04\ -\x01\x0btest:test/w\x04\0\x0b\x07\x01\0\x01w\x03\0\0\0G\x09producers\x01\x0cproc\ -essed-by\x02\x0dwit-component\x070.216.0\x10wit-bindgen-rust\x060.30.0"; +pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 194] = *b"\ +\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07K\x01A\x02\x01A\x02\x01\ +B\x06\x01ps\x01@\x01\x01a\0\0\0\x04\0\x01f\x01\x01\x01p}\x01@\x01\x01a\x02\0\x02\ +\x04\0\x01g\x01\x03\x04\x01\x0btest:test/i\x05\0\x04\x01\x0btest:test/w\x04\0\x0b\ +\x07\x01\0\x01w\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\ +\x070.216.0\x10wit-bindgen-rust\x060.30.0"; #[inline(never)] #[doc(hidden)] pub fn __link_custom_section_describing_imports() { - wit_bindgen::rt::maybe_link_cabi_realloc(); + wit_bindgen::rt::maybe_link_cabi_realloc(); } - diff --git a/crates/cpp/tests/symmetric_lists/wit/lists.wit b/crates/cpp/tests/symmetric_lists/wit/lists.wit index 3bf276bc7..05185805c 100644 --- a/crates/cpp/tests/symmetric_lists/wit/lists.wit +++ b/crates/cpp/tests/symmetric_lists/wit/lists.wit @@ -2,6 +2,7 @@ package test:test; interface i { f: func(a: list) -> list; + g: func(a: list) -> list; } world w {