Skip to content

Commit

Permalink
test both canonical and non-canonical lists
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Sep 1, 2024
1 parent a2f5cd7 commit 166460f
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 102 deletions.
3 changes: 3 additions & 0 deletions crates/cpp/tests/symmetric_lists/rust_a/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
}
38 changes: 33 additions & 5 deletions crates/cpp/tests/symmetric_lists/rust_a/src/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ pub mod test {
result8
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn g(a: &[u8]) -> _rt::Vec<u8> {
unsafe {
#[cfg_attr(target_pointer_width = "64", repr(align(8)))]
#[cfg_attr(target_pointer_width = "32", repr(align(4)))]
struct RetArea(
[::core::mem::MaybeUninit<u8>; 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::<u8>();
let len0 = vec0.len();
let ptr1 = ret_area.0.as_mut_ptr().cast::<u8>();
#[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::<usize>();
let len4 = l3;
_rt::Vec::from_raw_parts(l2.cast(), len4, len4)
}
}
}
}
}
Expand Down Expand Up @@ -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)]
Expand Down
7 changes: 5 additions & 2 deletions crates/cpp/tests/symmetric_lists/rust_b/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

mod w;

struct MyImpl;

impl w::exports::test::test::i::Guest for MyImpl {
fn f(a: Vec::<String>,) -> Vec::<String> {
fn f(a: Vec<String>) -> Vec<String> {
a
}

fn g(a: Vec<u8>) -> Vec<u8> {
a
}
}
Expand Down
215 changes: 120 additions & 95 deletions crates/cpp/tests/symmetric_lists/rust_b/src/w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Guest>(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::<usize>();
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::<u8>();
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::<u8>();
let len5 = vec5.len();
::core::mem::forget(vec5);
*base.add(core::mem::size_of::<*const u8>()).cast::<usize>() = len5;
*base.add(0).cast::<*mut u8>() = ptr5.cast_mut();
}
}
*arg2.add(core::mem::size_of::<*const u8>()).cast::<usize>() = 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<T: Guest>(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::<usize>();
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::<u8>();
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::<u8>();
let len5 = vec5.len();
::core::mem::forget(vec5);
*base.add(core::mem::size_of::<*const u8>()).cast::<usize>() = len5;
*base.add(0).cast::<*mut u8>() = ptr5.cast_mut();
}
}
*arg2.add(core::mem::size_of::<*const u8>()).cast::<usize>() = len6;
*arg2.add(0).cast::<*mut u8>() = result6;
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_g_cabi<T: Guest>(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::<u8>();
let len2 = vec2.len();
vec2.leak();
*arg2.add(core::mem::size_of::<*const u8>()).cast::<usize>() = 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<u8>) -> _rt::Vec<u8>;
}
#[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
Expand Down Expand Up @@ -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();
}

1 change: 1 addition & 0 deletions crates/cpp/tests/symmetric_lists/wit/lists.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test:test;

interface i {
f: func(a: list<string>) -> list<string>;
g: func(a: list<u8>) -> list<u8>;
}

world w {
Expand Down

0 comments on commit 166460f

Please sign in to comment.