From 55edc61ad83bf16c8d974ee136086fbe6d88c9d9 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sat, 28 Jun 2025 17:49:45 +0200 Subject: [PATCH 1/3] sharing types between import and export --- tests/runtime/common-types/compose.wac | 15 ++++++++++++++ tests/runtime/common-types/leaf.cpp | 11 ++++++++++ tests/runtime/common-types/middle.cpp | 7 +++++++ tests/runtime/common-types/runner.cpp | 16 +++++++++++++++ tests/runtime/common-types/test.wit | 28 ++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 tests/runtime/common-types/compose.wac create mode 100644 tests/runtime/common-types/leaf.cpp create mode 100644 tests/runtime/common-types/middle.cpp create mode 100644 tests/runtime/common-types/runner.cpp create mode 100644 tests/runtime/common-types/test.wit diff --git a/tests/runtime/common-types/compose.wac b/tests/runtime/common-types/compose.wac new file mode 100644 index 000000000..a54149470 --- /dev/null +++ b/tests/runtime/common-types/compose.wac @@ -0,0 +1,15 @@ +package example:composition; + +let leaf = new test:leaf { + ... +}; +let middle = new test:middle { + to-test: leaf.to-test, + ... +}; +let runner = new test:runner { + to-test: middle.to-test, + ... +}; + +export runner...; diff --git a/tests/runtime/common-types/leaf.cpp b/tests/runtime/common-types/leaf.cpp new file mode 100644 index 000000000..9bc768c7b --- /dev/null +++ b/tests/runtime/common-types/leaf.cpp @@ -0,0 +1,11 @@ +#include + +using namespace test::common::test_types; + +R1 exports::test::common::to_test::Wrap(F1 flag) { + if (flag == F1::kA) { + return R1{ 1, flag }; + } else { + return R1{ 2, flag }; + } +} diff --git a/tests/runtime/common-types/middle.cpp b/tests/runtime/common-types/middle.cpp new file mode 100644 index 000000000..7d5998163 --- /dev/null +++ b/tests/runtime/common-types/middle.cpp @@ -0,0 +1,7 @@ +#include + +using namespace test::common::test_types; + +R1 exports::test::common::to_test::Wrap(F1 flag) { + return ::test::common::to_test::Wrap(flag); +} diff --git a/tests/runtime/common-types/runner.cpp b/tests/runtime/common-types/runner.cpp new file mode 100644 index 000000000..dbcd6a19f --- /dev/null +++ b/tests/runtime/common-types/runner.cpp @@ -0,0 +1,16 @@ +#include +#include + +int main() { + using namespace ::test::common::test_types; + + R1 res = test::common::to_test::Wrap(F1::kA); + assert(res.b == F1::kA); + assert(res.a == 1); + + R1 res2 = test::common::to_test::Wrap(F1::kB); + assert(res2.b == F1::kB); + assert(res2.a == 2); + + return 0; +} diff --git a/tests/runtime/common-types/test.wit b/tests/runtime/common-types/test.wit new file mode 100644 index 000000000..cc800363f --- /dev/null +++ b/tests/runtime/common-types/test.wit @@ -0,0 +1,28 @@ +//@ dependencies = ['middle', 'leaf'] +//@ wac = 'compose.wac' + +package test:common; + +interface test-types { + flags f1 { a, b } + record r1 { a: u8, b: f1 } +} + +interface to-test { + use test-types.{f1, r1}; + + wrap: func(flag: f1) -> r1; +} + +world leaf { + export to-test; +} + +world middle { + import to-test; + export to-test; +} + +world runner { + import to-test; +} From 0c9e5687829a8e322c8d0ee79f09afc36a762cc7 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sat, 28 Jun 2025 23:46:21 +0200 Subject: [PATCH 2/3] also test variant compatibility --- tests/runtime/common-types/leaf.cpp | 4 ++++ tests/runtime/common-types/middle.cpp | 4 ++++ tests/runtime/common-types/runner.cpp | 4 ++++ tests/runtime/common-types/test.wit | 4 +++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/runtime/common-types/leaf.cpp b/tests/runtime/common-types/leaf.cpp index 9bc768c7b..56acae02e 100644 --- a/tests/runtime/common-types/leaf.cpp +++ b/tests/runtime/common-types/leaf.cpp @@ -9,3 +9,7 @@ R1 exports::test::common::to_test::Wrap(F1 flag) { return R1{ 2, flag }; } } + +V1 exports::test::common::to_test::VarF(void) { + return V1(V1::B(42)); +} diff --git a/tests/runtime/common-types/middle.cpp b/tests/runtime/common-types/middle.cpp index 7d5998163..1f0aec36a 100644 --- a/tests/runtime/common-types/middle.cpp +++ b/tests/runtime/common-types/middle.cpp @@ -5,3 +5,7 @@ using namespace test::common::test_types; R1 exports::test::common::to_test::Wrap(F1 flag) { return ::test::common::to_test::Wrap(flag); } + +V1 exports::test::common::to_test::VarF(void) { + return ::test::common::to_test::VarF(); +} \ No newline at end of file diff --git a/tests/runtime/common-types/runner.cpp b/tests/runtime/common-types/runner.cpp index dbcd6a19f..fe4dd89af 100644 --- a/tests/runtime/common-types/runner.cpp +++ b/tests/runtime/common-types/runner.cpp @@ -12,5 +12,9 @@ int main() { assert(res2.b == F1::kB); assert(res2.a == 2); + V1 res3 = test::common::to_test::VarF(); + assert(res3.variants.index() == 1); + assert(std::get<1>(res3.variants).value == 42); + return 0; } diff --git a/tests/runtime/common-types/test.wit b/tests/runtime/common-types/test.wit index cc800363f..aefb6bac2 100644 --- a/tests/runtime/common-types/test.wit +++ b/tests/runtime/common-types/test.wit @@ -6,12 +6,14 @@ package test:common; interface test-types { flags f1 { a, b } record r1 { a: u8, b: f1 } + variant v1 { a, b(u8) } } interface to-test { - use test-types.{f1, r1}; + use test-types.{f1, r1, v1}; wrap: func(flag: f1) -> r1; + var-f: func() -> v1; } world leaf { From 869005154998c48467e8f720a1347a174d805071 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sat, 28 Jun 2025 23:58:59 +0200 Subject: [PATCH 3/3] Rust test --- tests/runtime/common-types/middle.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/runtime/common-types/middle.rs diff --git a/tests/runtime/common-types/middle.rs b/tests/runtime/common-types/middle.rs new file mode 100644 index 000000000..0a3aa3aa9 --- /dev/null +++ b/tests/runtime/common-types/middle.rs @@ -0,0 +1,19 @@ +include!(env!("BINDINGS")); + +use crate::test::common::to_test::{F1, R1, V1}; + +use exports::test::common::to_test; + +pub struct Test {} + +export!(Test); + +impl to_test::Guest for Test { + fn wrap(flag: F1) -> R1 { + crate::test::common::to_test::wrap(flag) + } + + fn var_f() -> V1 { + crate::test::common::to_test::var_f() + } +}