Skip to content

Commit

Permalink
generated code is now correct again
Browse files Browse the repository at this point in the history
cpetig committed Jul 18, 2024
1 parent fc24a0d commit b33aafe
Showing 2 changed files with 26 additions and 28 deletions.
30 changes: 14 additions & 16 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
@@ -975,33 +975,31 @@ impl<'a, B: Bindgen> Generator<'a, B> {
self.lower(ty);
}
} else {
match self.variant {
if self.variant == AbiVariant::GuestImport
|| self.lift_lower == LiftLower::Symmetric
{
// When a function is imported to a guest this means
// it's a host providing the implementation of the
// import. The result is stored in the pointer
// specified in the last argument, so we get the
// pointer here and then write the return value into
// it.
AbiVariant::GuestImport => {
self.emit(&Instruction::GetArg {
nth: sig.params.len() - 1,
});
let ptr = self.stack.pop().unwrap();
self.write_params_to_memory(func.results.iter_types(), ptr, 0);
}

self.emit(&Instruction::GetArg {
nth: sig.params.len() - 1,
});
let ptr = self.stack.pop().unwrap();
self.write_params_to_memory(func.results.iter_types(), ptr, 0);
} else {
// For a guest import this is a function defined in
// wasm, so we're returning a pointer where the
// value was stored at. Allocate some space here
// (statically) and then write the result into that
// memory, returning the pointer at the end.
AbiVariant::GuestExport => {
let (size, align) = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self.bindgen.return_pointer(size, align);
self.write_params_to_memory(func.results.iter_types(), ptr.clone(), 0);
if !matches!(self.lift_lower, LiftLower::Symmetric) {
self.stack.push(ptr);
}
let (size, align) = self.bindgen.sizes().params(func.results.iter_types());
let ptr = self.bindgen.return_pointer(size, align);
self.write_params_to_memory(func.results.iter_types(), ptr.clone(), 0);
if !matches!(self.lift_lower, LiftLower::Symmetric) {
self.stack.push(ptr);
}
}
}
24 changes: 12 additions & 12 deletions crates/cpp/tests/meshless_strings/component_a/the_world.cpp
Original file line number Diff line number Diff line change
@@ -87,15 +87,15 @@ a_fooX3AfooX2FstringsX00a(uint8_t *arg0, size_t arg1) {
comp_a::exports::foo::foo::strings::A(std::move(string0));
}
extern "C" __attribute__((__export_name__("foo:foo/strings#b"))) void
a_fooX3AfooX2FstringsX00b(uint8_t *ptr1) {
a_fooX3AfooX2FstringsX00b(uint8_t *arg0) {
auto result0 = comp_a::exports::foo::foo::strings::B();
auto const &vec2 = result0;
auto ptr2 = (uint8_t *)(vec2.data());
auto len2 = (size_t)(vec2.size());
auto const &vec1 = result0;
auto ptr1 = (uint8_t *)(vec1.data());
auto len1 = (size_t)(vec1.size());
result0.leak();

*((size_t *)(ptr1 + 8)) = len2;
*((uint8_t **)(ptr1 + 0)) = ptr2;
*((size_t *)(arg0 + 8)) = len1;
*((uint8_t **)(arg0 + 0)) = ptr1;
}
extern "C"
__attribute__((__weak__,
@@ -107,7 +107,7 @@ extern "C"
}
extern "C" __attribute__((__export_name__("foo:foo/strings#c"))) void
a_fooX3AfooX2FstringsX00c(uint8_t *arg0, size_t arg1, uint8_t *arg2, size_t arg3,
uint8_t *ptr3) {
uint8_t *arg4) {
auto len0 = arg1;

auto string0 =
@@ -120,13 +120,13 @@ a_fooX3AfooX2FstringsX00c(uint8_t *arg0, size_t arg1, uint8_t *arg2, size_t arg3

auto result2 = comp_a::exports::foo::foo::strings::C(std::move(string0),
std::move(string1));
auto const &vec4 = result2;
auto ptr4 = (uint8_t *)(vec4.data());
auto len4 = (size_t)(vec4.size());
auto const &vec3 = result2;
auto ptr3 = (uint8_t *)(vec3.data());
auto len3 = (size_t)(vec3.size());
result2.leak();

*((size_t *)(ptr3 + 8)) = len4;
*((uint8_t **)(ptr3 + 0)) = ptr4;
*((size_t *)(arg4 + 8)) = len3;
*((uint8_t **)(arg4 + 0)) = ptr3;
}
extern "C"
__attribute__((__weak__,

0 comments on commit b33aafe

Please sign in to comment.