Skip to content

Commit eb2da2f

Browse files
Lucky1313David Hedin
andauthored
[cpp] Add move_if_necessary in C++ binding flush instruction (bytecodealliance#1338)
* Add variants containing data types test Rename * Add move_if_necessary in C++ binding flush instruction * Don't return const for function binding --------- Co-authored-by: David Hedin <[email protected]>
1 parent 73bcd58 commit eb2da2f

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

crates/cpp/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26392639
let case = format!("{elem_ns}::{}", case.name.to_pascal_case());
26402640
uwriteln!(
26412641
self.src,
2642-
"const {} &{} = std::get<{case}>({}.variants).value;",
2642+
"{} &{} = std::get<{case}>({}.variants).value;",
26432643
ty,
26442644
payload,
26452645
operands[0],
@@ -3063,7 +3063,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
30633063
for i in operands.iter().take(*amt) {
30643064
let tmp = self.tmp();
30653065
let result = format!("result{}", tmp);
3066-
uwriteln!(self.src, "auto {result} = {};", i);
3066+
uwriteln!(self.src, "auto {result} = {};", move_if_necessary(i));
30673067
results.push(result);
30683068
}
30693069
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <assert.h>
2+
#include <runner_cpp.h>
3+
4+
template <class T>
5+
static bool equal(T const& a, T const& b) {
6+
return a == b;
7+
}
8+
9+
template <class T>
10+
static bool equal(wit::vector<T> const& a, wit::vector<T> const& b) {
11+
if (a.size() != b.size()) return false;
12+
for (size_t i = 0; i < a.size(); i++) {
13+
if (!equal(a[i], b[i])) return false;
14+
}
15+
return true;
16+
}
17+
18+
static bool equal(wit::string const& a, wit::string const& b) {
19+
return a.get_view() == b.get_view();
20+
}
21+
22+
static bool equal(::test::variant_with_data::to_test::DataVariant const& a, ::test::variant_with_data::to_test::DataVariant const& b) {
23+
if (a.variants.index() != b.variants.index()) return false;
24+
switch (a.variants.index()) {
25+
case 0: return equal(std::get<::test::variant_with_data::to_test::DataVariant::Bytes>(a.variants).value, std::get<::test::variant_with_data::to_test::DataVariant::Bytes>(b.variants).value);
26+
case 1: return equal(std::get<::test::variant_with_data::to_test::DataVariant::Number>(a.variants).value, std::get<::test::variant_with_data::to_test::DataVariant::Number>(b.variants).value);
27+
case 2: return equal(std::get<::test::variant_with_data::to_test::DataVariant::Text>(a.variants).value, std::get<::test::variant_with_data::to_test::DataVariant::Text>(b.variants).value);
28+
}
29+
return false;
30+
}
31+
32+
int main() {
33+
using namespace ::test::variant_with_data::to_test;
34+
35+
// Test bytes variant
36+
auto bytes_variant = GetData(0);
37+
uint8_t expected_bytes[]{0x01, 0x02, 0x03, 0x04, 0x05};
38+
DataVariant expected_bytes_variant;
39+
expected_bytes_variant.variants = DataVariant::Bytes(wit::vector<uint8_t>::from_view(std::span<uint8_t>(expected_bytes)));
40+
assert(equal(bytes_variant, expected_bytes_variant));
41+
42+
// Test number variant
43+
auto number_variant = GetData(1);
44+
DataVariant expected_number_variant;
45+
expected_number_variant.variants = DataVariant::Number(42);
46+
assert(equal(number_variant, expected_number_variant));
47+
48+
// Test text variant
49+
auto text_variant = GetData(2);
50+
DataVariant expected_text_variant;
51+
expected_text_variant.variants = DataVariant::Text(wit::string::from_view("hello"));
52+
assert(equal(text_variant, expected_text_variant));
53+
54+
return 0;
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <assert.h>
2+
#include <vector>
3+
#include <test_cpp.h>
4+
5+
using ::test::variant_with_data::to_test::DataVariant;
6+
7+
DataVariant exports::test::variant_with_data::to_test::GetData(uint8_t num) {
8+
DataVariant variant;
9+
switch (num) {
10+
case 0: {
11+
uint8_t bytes[]{0x01, 0x02, 0x03, 0x04, 0x05};
12+
variant.variants = DataVariant::Bytes(wit::vector<uint8_t>::from_view(std::span<uint8_t>(bytes)));
13+
}
14+
case 1:
15+
variant.variants = DataVariant::Number(42);
16+
case 2:
17+
variant.variants = DataVariant::Text(wit::string::from_view("hello"));
18+
default:
19+
variant.variants = DataVariant::Number(0);
20+
}
21+
auto result = std::move(variant);
22+
return result;
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package test:variant-with-data;
2+
3+
interface to-test {
4+
variant data-variant {
5+
bytes(list<u8>),
6+
number(u32),
7+
text(string)
8+
}
9+
10+
get-data: func(num: u8) -> data-variant;
11+
}
12+
13+
world runner {
14+
import to-test;
15+
}
16+
17+
world test {
18+
export to-test;
19+
}

0 commit comments

Comments
 (0)