Skip to content

Commit 829d556

Browse files
author
Guy Bedford
authored
gen-guest-c: weak attribute on post-return abi functions (#412)
1 parent 2612c44 commit 829d556

5 files changed

Lines changed: 45 additions & 3 deletions

File tree

crates/gen-guest-c/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ impl InterfaceGenerator<'_> {
761761
if self.iface.guest_export_needs_post_return(func) {
762762
uwriteln!(
763763
self.src.c_fns,
764-
"__attribute__((export_name(\"cabi_post_{export_name}\")))"
764+
"__attribute__((weak, export_name(\"cabi_post_{export_name}\")))"
765765
);
766766
uwrite!(self.src.c_fns, "void {import_name}_post_return(");
767767

crates/test-helpers/macros/build.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,13 @@ fn read_interfaces(dir: &Path) -> ComponentInterfaces {
294294
println!("cargo:rerun-if-changed={}", imports.display());
295295
println!("cargo:rerun-if-changed={}", exports.display());
296296

297-
let import = Interface::parse_file(&imports).unwrap();
297+
let imports = match Interface::parse_file(&imports) {
298+
Ok(import) => [(import.name.clone(), import)].into_iter().collect(),
299+
Err(_) => [].into(),
300+
};
298301
let export = Interface::parse_file(&exports).unwrap();
299302
ComponentInterfaces {
300-
imports: [(import.name.clone(), import)].into_iter().collect(),
303+
imports,
301304
exports: Default::default(),
302305
default: Some(export),
303306
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
thunk: func() -> string

tests/runtime/exports_only/host.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Flags: --base64 --nodejs-compat
2+
// @ts-ignore
3+
import { ok, strictEqual } from 'assert';
4+
// @ts-ignore
5+
import { readFile } from 'fs/promises';
6+
// @ts-ignore
7+
import { fileURLToPath } from 'url';
8+
import { thunk } from './exports_only.js';
9+
10+
const result = thunk();
11+
strictEqual(result, 'test');
12+
13+
// Verify the inlined file size does not regress
14+
const url = new URL('./exports_only.js', import.meta.url);
15+
const jsSource = await readFile(url);
16+
const max_size = 6375;
17+
ok(jsSource.byteLength < max_size, `JS inlined bytelength ${jsSource.byteLength} is greater than ${max_size} bytes, at ${fileURLToPath(url)}`);

tests/runtime/exports_only/wasm.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <exports_only.h>
2+
#include <stdio.h>
3+
4+
__attribute__((weak, export_name("cabi_realloc")))
5+
void *cabi_realloc(void *ptr, size_t orig_size, size_t org_align, size_t new_size) {
6+
return ptr;
7+
}
8+
9+
__attribute__((weak, export_name("cabi_post_thunk")))
10+
void __wasm_export_exports_thunk_post_return(int32_t arg0) {
11+
}
12+
13+
static char msg[] = "test";
14+
15+
void exports_thunk(exports_only_string_t* ret) {
16+
exports_only_string_t result = {
17+
.ptr = &msg[0],
18+
.len = sizeof(msg) - 1,
19+
};
20+
*ret = result;
21+
}

0 commit comments

Comments
 (0)