Skip to content

Commit e5bd8e3

Browse files
authored
Add world package name into custom section (#1029)
This adds in the package name in addition to the world name in the custom section for Rust code that has the component type information within it. This helps prevent clashes between worlds of the same name. Closes #975
1 parent 7b4d9e5 commit e5bd8e3

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

crates/rust/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ macro_rules! __export_{world_name}_impl {{
744744
fn emit_custom_section(
745745
&mut self,
746746
resolve: &Resolve,
747-
world: WorldId,
747+
world_id: WorldId,
748748
section_suffix: &str,
749749
func_name: Option<&str>,
750750
) {
@@ -755,11 +755,13 @@ macro_rules! __export_{world_name}_impl {{
755755
// concatenated to other custom sections by LLD by accident since LLD will
756756
// concatenate custom sections of the same name.
757757
let opts_suffix = self.opts.type_section_suffix.as_deref().unwrap_or("");
758-
let world_name = &resolve.worlds[world].name;
758+
let world = &resolve.worlds[world_id];
759+
let world_name = &world.name;
760+
let pkg = &resolve.packages[world.package.unwrap()].name;
759761
let version = env!("CARGO_PKG_VERSION");
760762
self.src.push_str(&format!(
761763
"#[link_section = \"component-type:wit-bindgen:{version}:\
762-
{world_name}:{section_suffix}{opts_suffix}\"]\n"
764+
{pkg}:{world_name}:{section_suffix}{opts_suffix}\"]\n"
763765
));
764766

765767
let mut producers = wasm_metadata::Producers::empty();
@@ -771,7 +773,7 @@ macro_rules! __export_{world_name}_impl {{
771773

772774
let component_type = wit_component::metadata::encode(
773775
resolve,
774-
world,
776+
world_id,
775777
wit_component::StringEncoding::UTF8,
776778
Some(&producers),
777779
)

tests/runtime/type_section_suffix.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ use self::test::suffix::imports::Host;
77
#[derive(Default)]
88
pub struct MyFoo;
99

10+
impl RequiredExportsImports for MyFoo {
11+
fn foo(&mut self) {}
12+
fn bar(&mut self) {}
13+
}
14+
1015
impl Host for MyFoo {
1116
fn foo(&mut self) {}
1217
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:a ;
2+
3+
world imports {
4+
import foo: func();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:b;
2+
3+
world imports {
4+
import bar: func();
5+
}

tests/runtime/type_section_suffix/wasm.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,26 @@ mod b {
2222
});
2323
}
2424

25+
mod c {
26+
wit_bindgen::generate!({
27+
world: "test:a/imports",
28+
path: "../../tests/runtime/type_section_suffix",
29+
});
30+
}
31+
mod d {
32+
wit_bindgen::generate!({
33+
world: "test:b/imports",
34+
path: "../../tests/runtime/type_section_suffix",
35+
});
36+
}
37+
2538
struct Exports;
2639

2740
impl Guest for Exports {
2841
fn run() {
2942
a::test::suffix::imports::foo();
3043
b::test::suffix::imports::foo();
44+
c::foo();
45+
d::bar();
3146
}
3247
}

tests/runtime/type_section_suffix/world.wit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ interface imports {
66

77
world available-imports {
88
import imports;
9+
include test:a/imports;
10+
include test:b/imports;
911
}
1012

1113
world required-exports {

0 commit comments

Comments
 (0)