Skip to content

Commit 6cea1f3

Browse files
authored
optionally allow output world name when importizing (#1806)
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
1 parent e5132af commit 6cea1f3

11 files changed

Lines changed: 152 additions & 8 deletions

crates/wit-parser/src/resolve.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,16 +1021,21 @@ package {name} is defined in two different locations:\n\
10211021
/// bindings in a context that is importing the original world. This
10221022
/// is intended to be used as part of language tooling when depending on
10231023
/// other components.
1024-
pub fn importize(&mut self, world_id: WorldId) -> Result<()> {
1024+
pub fn importize(&mut self, world_id: WorldId, out_world_name: Option<String>) -> Result<()> {
10251025
// Rename the world to avoid having it get confused with the original
10261026
// name of the world. Add `-importized` to it for now. Precisely how
10271027
// this new world is created may want to be updated over time if this
10281028
// becomes problematic.
10291029
let world = &mut self.worlds[world_id];
10301030
let pkg = &mut self.packages[world.package.unwrap()];
10311031
pkg.worlds.shift_remove(&world.name);
1032-
world.name.push_str("-importized");
1033-
pkg.worlds.insert(world.name.clone(), world_id);
1032+
if let Some(name) = out_world_name {
1033+
world.name = name.clone();
1034+
pkg.worlds.insert(name, world_id);
1035+
} else {
1036+
world.name.push_str("-importized");
1037+
pkg.worlds.insert(world.name.clone(), world_id);
1038+
}
10341039

10351040
// Trim all non-type definitions from imports. Types can be used by
10361041
// exported functions, for example, so they're preserved.

fuzz/src/roundtrip_wit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
6262
// valid.
6363
log::debug!("... importizing this world");
6464
let mut resolve2 = resolve.clone();
65-
let _ = resolve2.importize(id);
65+
let _ = resolve2.importize(id, None);
6666
}
6767

6868
if decoded_bindgens.len() < 2 {

src/bin/wasm-tools/component.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ pub struct WitOpts {
511511
#[clap(long, conflicts_with = "importize_world")]
512512
importize: bool,
513513

514+
/// The name of the world to generate when using `--importize` or `importize-world`.
515+
#[clap(long = "importize-out-world-name")]
516+
importize_out_world_name: Option<String>,
517+
514518
/// Generates a WIT world to import a component which corresponds to the
515519
/// selected world.
516520
///
@@ -549,9 +553,13 @@ impl WitOpts {
549553
let mut decoded = self.decode_input()?;
550554

551555
if self.importize {
552-
self.importize(&mut decoded, None)?;
556+
self.importize(&mut decoded, None, self.importize_out_world_name.as_ref())?;
553557
} else if self.importize_world.is_some() {
554-
self.importize(&mut decoded, self.importize_world.as_deref())?;
558+
self.importize(
559+
&mut decoded,
560+
self.importize_world.as_deref(),
561+
self.importize_out_world_name.as_ref(),
562+
)?;
555563
}
556564

557565
// Now that the WIT document has been decoded, it's time to emit it.
@@ -636,7 +644,12 @@ impl WitOpts {
636644
}
637645
}
638646

639-
fn importize(&self, decoded: &mut DecodedWasm, world: Option<&str>) -> Result<()> {
647+
fn importize(
648+
&self,
649+
decoded: &mut DecodedWasm,
650+
world: Option<&str>,
651+
out_world_name: Option<&String>,
652+
) -> Result<()> {
640653
let (resolve, world_id) = match (&mut *decoded, world) {
641654
(DecodedWasm::Component(resolve, world), None) => (resolve, *world),
642655
(DecodedWasm::Component(..), Some(_)) => {
@@ -653,7 +666,7 @@ impl WitOpts {
653666
// let pkg = decoded.package();
654667
// let world_id = decoded.resolve().select_world(main, None)?;
655668
resolve
656-
.importize(world_id)
669+
.importize(world_id, out_world_name.cloned())
657670
.context("failed to move world exports to imports")?;
658671
let resolve = mem::take(resolve);
659672
*decoded = DecodedWasm::Component(resolve, world_id);

tests/cli/importize.wit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN[simple]: component wit --importize-world simple %
2+
// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
23
// RUN[simple-component]: component embed --dummy --world simple % | \
34
// component wit --importize
45
// RUN[with-deps]: component wit --importize-world with-deps %
@@ -34,6 +35,10 @@ world simple {
3435
export t;
3536
}
3637

38+
world simple-rename {
39+
export t;
40+
}
41+
3742
world with-deps {
3843
export qux;
3944
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/// RUN[simple]: component wit --importize-world simple %
2+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
3+
/// RUN[simple-component]: component embed --dummy --world simple % | /
4+
/// component wit --importize
5+
/// RUN[with-deps]: component wit --importize-world with-deps %
6+
/// RUN[simple-toplevel]: component wit --importize-world simple-toplevel %
7+
/// RUN[toplevel-deps]: component wit --importize-world toplevel-deps %
8+
/// FAIL[fail1]: component wit --importize-world fail1 %
9+
/// RUN[trim-imports]: component wit --importize-world trim-imports %
10+
/// RUN[tricky-import]: component wit --importize-world tricky-import %
11+
package importize:importize;
12+
13+
interface t {
14+
resource r;
15+
}
16+
17+
interface bar {
18+
use t.{r};
19+
20+
record foo {
21+
x: string,
22+
}
23+
24+
importize: func(name: r);
25+
}
26+
27+
interface qux {
28+
use bar.{foo};
29+
30+
blah: func(boo: foo);
31+
}
32+
33+
interface something-else-dep {
34+
type t = u32;
35+
}
36+
37+
interface a {
38+
}
39+
40+
interface b {
41+
}
42+
43+
interface with-dep {
44+
type t = u32;
45+
}
46+
47+
world simple {
48+
export t;
49+
}
50+
world with-deps {
51+
import t;
52+
import bar;
53+
54+
export qux;
55+
}
56+
world simple-toplevel {
57+
export foo: func();
58+
export something: interface {
59+
foo: func();
60+
}
61+
}
62+
world toplevel-deps {
63+
import something-else-dep;
64+
65+
type s = u32;
66+
67+
export bar: func() -> s;
68+
export something-else: interface {
69+
use something-else-dep.{t};
70+
71+
bar: func() -> t;
72+
}
73+
}
74+
world fail1 {
75+
type foo = u32;
76+
77+
export foo: func() -> foo;
78+
}
79+
world trim-imports {
80+
import a;
81+
import foo: func();
82+
import bar: interface {
83+
}
84+
85+
type t = u32;
86+
87+
export b;
88+
}
89+
world tricky-import {
90+
import with-dep;
91+
use with-dep.{t};
92+
93+
export f: func() -> t;
94+
}
95+
world test-rename {
96+
import t;
97+
}

tests/cli/importize.wit.simple-toplevel.stdout

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// RUN[simple]: component wit --importize-world simple %
2+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
23
/// RUN[simple-component]: component embed --dummy --world simple % | /
34
/// component wit --importize
45
/// RUN[with-deps]: component wit --importize-world with-deps %
@@ -46,6 +47,9 @@ interface with-dep {
4647
world simple {
4748
export t;
4849
}
50+
world simple-rename {
51+
export t;
52+
}
4953
world with-deps {
5054
import t;
5155
import bar;

tests/cli/importize.wit.simple.stdout

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// RUN[simple]: component wit --importize-world simple %
2+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
23
/// RUN[simple-component]: component embed --dummy --world simple % | /
34
/// component wit --importize
45
/// RUN[with-deps]: component wit --importize-world with-deps %
@@ -43,6 +44,9 @@ interface with-dep {
4344
type t = u32;
4445
}
4546

47+
world simple-rename {
48+
export t;
49+
}
4650
world with-deps {
4751
import t;
4852
import bar;

tests/cli/importize.wit.toplevel-deps.stdout

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// RUN[simple]: component wit --importize-world simple %
2+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
23
/// RUN[simple-component]: component embed --dummy --world simple % | /
34
/// component wit --importize
45
/// RUN[with-deps]: component wit --importize-world with-deps %
@@ -46,6 +47,9 @@ interface with-dep {
4647
world simple {
4748
export t;
4849
}
50+
world simple-rename {
51+
export t;
52+
}
4953
world with-deps {
5054
import t;
5155
import bar;

tests/cli/importize.wit.tricky-import.stdout

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// RUN[simple]: component wit --importize-world simple %
2+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
23
/// RUN[simple-component]: component embed --dummy --world simple % | /
34
/// component wit --importize
45
/// RUN[with-deps]: component wit --importize-world with-deps %
@@ -46,6 +47,9 @@ interface with-dep {
4647
world simple {
4748
export t;
4849
}
50+
world simple-rename {
51+
export t;
52+
}
4953
world with-deps {
5054
import t;
5155
import bar;

tests/cli/importize.wit.trim-imports.stdout

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// RUN[simple]: component wit --importize-world simple %
2+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
23
/// RUN[simple-component]: component embed --dummy --world simple % | /
34
/// component wit --importize
45
/// RUN[with-deps]: component wit --importize-world with-deps %
@@ -46,6 +47,9 @@ interface with-dep {
4647
world simple {
4748
export t;
4849
}
50+
world simple-rename {
51+
export t;
52+
}
4953
world with-deps {
5054
import t;
5155
import bar;

0 commit comments

Comments
 (0)