Skip to content

Commit ad6dbc5

Browse files
authored
[C] Prefex function export names with exports_. (bytecodealliance#1107)
In the C bindings, prefix exported function names with `exports_` so that a world that imports and exports the same function can do so without collisions. For example, with this WIT: ```wit package green:blue; world my-world { import purple: func(); export purple: func(); } ```wit This fixes a wit-bindgen panic with: ``` duplicate symbols: "name `my_world_purple` already defined" ```
1 parent f675254 commit ad6dbc5

File tree

12 files changed

+25
-14
lines changed

12 files changed

+25
-14
lines changed

crates/c/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,12 @@ pub fn c_func_name(
893893
!in_import,
894894
renamed_interfaces,
895895
)),
896-
None => name.push_str(&world.to_snake_case()),
896+
None => {
897+
if !in_import {
898+
name.push_str("exports_");
899+
}
900+
name.push_str(&world.to_snake_case());
901+
}
897902
}
898903
name.push_str("_");
899904
name.push_str(&func.name.to_snake_case().replace('.', "_"));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package green:blue;
2+
3+
world my-world {
4+
import purple: func();
5+
export purple: func();
6+
}

tests/runtime/flavorful/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <stdlib.h>
44
#include <string.h>
55

6-
void flavorful_test_imports() {
6+
void exports_flavorful_test_imports() {
77
{
88
test_flavorful_test_list_in_record1_t a;
99
flavorful_string_set(&a.a, "list_in_record1");

tests/runtime/lists/wasm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include <stdlib.h>
88
#include <string.h>
99

10-
uint32_t lists_allocated_bytes(void) {
10+
uint32_t exports_lists_allocated_bytes(void) {
1111
return 0;
1212
}
1313

14-
void lists_test_imports() {
14+
void exports_lists_test_imports() {
1515
{
1616
uint8_t list[] = {};
1717
lists_list_u8_t a;

tests/runtime/many_arguments/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <limits.h>
44
#include <math.h>
55

6-
void many_arguments_many_arguments(
6+
void exports_many_arguments_many_arguments(
77
uint64_t a1,
88
uint64_t a2,
99
uint64_t a3,

tests/runtime/numbers/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ uint32_t exports_test_numbers_test_get_scalar(void) {
5858
}
5959

6060

61-
void numbers_test_imports() {
61+
void exports_numbers_test_imports() {
6262
assert(test_numbers_test_roundtrip_u8(1) == 1);
6363
assert(test_numbers_test_roundtrip_u8(0) == 0);
6464
assert(test_numbers_test_roundtrip_u8(UCHAR_MAX) == UCHAR_MAX);

tests/runtime/records/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <assert.h>
22
#include <records.h>
33

4-
void records_test_imports() {
4+
void exports_records_test_imports() {
55
{
66
records_tuple2_u8_u16_t ret;
77
test_records_test_multiple_results(&ret);

tests/runtime/resource_borrow_simple/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <assert.h>
22
#include <resource_borrow_simple.h>
33

4-
void resource_borrow_simple_test_imports(void) {
4+
void exports_resource_borrow_simple_test_imports(void) {
55
resource_borrow_simple_own_r_t r = resource_borrow_simple_constructor_r();
66
resource_borrow_simple_borrow_r_t b = resource_borrow_simple_borrow_r(r);
77
resource_borrow_simple_test(b);

tests/runtime/resource_import_and_export/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct exports_test_resource_import_and_export_test_thing_t {
1212
};
1313

1414
resource_import_and_export_own_thing_t
15-
resource_import_and_export_toplevel_export(resource_import_and_export_own_thing_t a) {
15+
exports_resource_import_and_export_toplevel_export(resource_import_and_export_own_thing_t a) {
1616
return resource_import_and_export_toplevel_import(a);
1717
}
1818

tests/runtime/smoke/wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <smoke.h>
22
#include <stdio.h>
33

4-
void smoke_thunk() {
4+
void exports_smoke_thunk() {
55
test_smoke_imports_thunk();
66

77
printf("howdy\n");

0 commit comments

Comments
 (0)