Skip to content

Commit

Permalink
optionally allow output world name when importizing (#1806)
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <[email protected]>
  • Loading branch information
karthik2804 authored Sep 19, 2024
1 parent e5132af commit 6cea1f3
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 8 deletions.
11 changes: 8 additions & 3 deletions crates/wit-parser/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,16 +1021,21 @@ package {name} is defined in two different locations:\n\
/// bindings in a context that is importing the original world. This
/// is intended to be used as part of language tooling when depending on
/// other components.
pub fn importize(&mut self, world_id: WorldId) -> Result<()> {
pub fn importize(&mut self, world_id: WorldId, out_world_name: Option<String>) -> Result<()> {
// Rename the world to avoid having it get confused with the original
// name of the world. Add `-importized` to it for now. Precisely how
// this new world is created may want to be updated over time if this
// becomes problematic.
let world = &mut self.worlds[world_id];
let pkg = &mut self.packages[world.package.unwrap()];
pkg.worlds.shift_remove(&world.name);
world.name.push_str("-importized");
pkg.worlds.insert(world.name.clone(), world_id);
if let Some(name) = out_world_name {
world.name = name.clone();
pkg.worlds.insert(name, world_id);
} else {
world.name.push_str("-importized");
pkg.worlds.insert(world.name.clone(), world_id);
}

// Trim all non-type definitions from imports. Types can be used by
// exported functions, for example, so they're preserved.
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/roundtrip_wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
// valid.
log::debug!("... importizing this world");
let mut resolve2 = resolve.clone();
let _ = resolve2.importize(id);
let _ = resolve2.importize(id, None);
}

if decoded_bindgens.len() < 2 {
Expand Down
21 changes: 17 additions & 4 deletions src/bin/wasm-tools/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ pub struct WitOpts {
#[clap(long, conflicts_with = "importize_world")]
importize: bool,

/// The name of the world to generate when using `--importize` or `importize-world`.
#[clap(long = "importize-out-world-name")]
importize_out_world_name: Option<String>,

/// Generates a WIT world to import a component which corresponds to the
/// selected world.
///
Expand Down Expand Up @@ -549,9 +553,13 @@ impl WitOpts {
let mut decoded = self.decode_input()?;

if self.importize {
self.importize(&mut decoded, None)?;
self.importize(&mut decoded, None, self.importize_out_world_name.as_ref())?;
} else if self.importize_world.is_some() {
self.importize(&mut decoded, self.importize_world.as_deref())?;
self.importize(
&mut decoded,
self.importize_world.as_deref(),
self.importize_out_world_name.as_ref(),
)?;
}

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

fn importize(&self, decoded: &mut DecodedWasm, world: Option<&str>) -> Result<()> {
fn importize(
&self,
decoded: &mut DecodedWasm,
world: Option<&str>,
out_world_name: Option<&String>,
) -> Result<()> {
let (resolve, world_id) = match (&mut *decoded, world) {
(DecodedWasm::Component(resolve, world), None) => (resolve, *world),
(DecodedWasm::Component(..), Some(_)) => {
Expand All @@ -653,7 +666,7 @@ impl WitOpts {
// let pkg = decoded.package();
// let world_id = decoded.resolve().select_world(main, None)?;
resolve
.importize(world_id)
.importize(world_id, out_world_name.cloned())
.context("failed to move world exports to imports")?;
let resolve = mem::take(resolve);
*decoded = DecodedWasm::Component(resolve, world_id);
Expand Down
5 changes: 5 additions & 0 deletions tests/cli/importize.wit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN[simple]: component wit --importize-world simple %
// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
// RUN[simple-component]: component embed --dummy --world simple % | \
// component wit --importize
// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -34,6 +35,10 @@ world simple {
export t;
}

world simple-rename {
export t;
}

world with-deps {
export qux;
}
Expand Down
97 changes: 97 additions & 0 deletions tests/cli/importize.wit.simple-rename.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
/// RUN[simple-toplevel]: component wit --importize-world simple-toplevel %
/// RUN[toplevel-deps]: component wit --importize-world toplevel-deps %
/// FAIL[fail1]: component wit --importize-world fail1 %
/// RUN[trim-imports]: component wit --importize-world trim-imports %
/// RUN[tricky-import]: component wit --importize-world tricky-import %
package importize:importize;

interface t {
resource r;
}

interface bar {
use t.{r};

record foo {
x: string,
}

importize: func(name: r);
}

interface qux {
use bar.{foo};

blah: func(boo: foo);
}

interface something-else-dep {
type t = u32;
}

interface a {
}

interface b {
}

interface with-dep {
type t = u32;
}

world simple {
export t;
}
world with-deps {
import t;
import bar;

export qux;
}
world simple-toplevel {
export foo: func();
export something: interface {
foo: func();
}
}
world toplevel-deps {
import something-else-dep;

type s = u32;

export bar: func() -> s;
export something-else: interface {
use something-else-dep.{t};

bar: func() -> t;
}
}
world fail1 {
type foo = u32;

export foo: func() -> foo;
}
world trim-imports {
import a;
import foo: func();
import bar: interface {
}

type t = u32;

export b;
}
world tricky-import {
import with-dep;
use with-dep.{t};

export f: func() -> t;
}
world test-rename {
import t;
}
4 changes: 4 additions & 0 deletions tests/cli/importize.wit.simple-toplevel.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -46,6 +47,9 @@ interface with-dep {
world simple {
export t;
}
world simple-rename {
export t;
}
world with-deps {
import t;
import bar;
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/importize.wit.simple.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -43,6 +44,9 @@ interface with-dep {
type t = u32;
}

world simple-rename {
export t;
}
world with-deps {
import t;
import bar;
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/importize.wit.toplevel-deps.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -46,6 +47,9 @@ interface with-dep {
world simple {
export t;
}
world simple-rename {
export t;
}
world with-deps {
import t;
import bar;
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/importize.wit.tricky-import.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -46,6 +47,9 @@ interface with-dep {
world simple {
export t;
}
world simple-rename {
export t;
}
world with-deps {
import t;
import bar;
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/importize.wit.trim-imports.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -46,6 +47,9 @@ interface with-dep {
world simple {
export t;
}
world simple-rename {
export t;
}
world with-deps {
import t;
import bar;
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/importize.wit.with-deps.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// RUN[simple]: component wit --importize-world simple %
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
/// RUN[simple-component]: component embed --dummy --world simple % | /
/// component wit --importize
/// RUN[with-deps]: component wit --importize-world with-deps %
Expand Down Expand Up @@ -46,6 +47,9 @@ interface with-dep {
world simple {
export t;
}
world simple-rename {
export t;
}
world simple-toplevel {
export foo: func();
export something: interface {
Expand Down

0 comments on commit 6cea1f3

Please sign in to comment.