Skip to content

Commit

Permalink
Require the use of remappings provided via --with (bytecodealliance#842)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian H <[email protected]>
  • Loading branch information
fibonacci1729 authored Feb 16, 2024
1 parent 1f4ab0f commit 785edc9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
4 changes: 3 additions & 1 deletion crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl WorldGenerator for C {
gen.gen.src.append(&gen.src);
}

fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) -> Result<()> {
let linking_symbol = component_type_object::linking_symbol(&self.world);
self.include("<stdlib.h>");
let snake = self.world.to_snake_case();
Expand Down Expand Up @@ -463,6 +463,8 @@ impl WorldGenerator for C {
.as_slice(),
);
}

Ok(())
}

fn pre_export_interface(&mut self, resolve: &Resolve, _files: &mut Files) -> Result<()> {
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ pub trait WorldGenerator {
for (name, id) in interfaces {
self.export_interface(resolve, name, *id, files)?;
}
self.finish(resolve, id, files);
Ok(())
self.finish(resolve, id, files)
}

fn finish_imports(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) {
Expand Down Expand Up @@ -348,7 +347,7 @@ pub trait WorldGenerator {
types: &[(&str, TypeId)],
files: &mut Files,
);
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files);
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) -> Result<()>;
}

/// This is a possible replacement for the `Generator` trait above, currently
Expand Down
4 changes: 3 additions & 1 deletion crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl WorldGenerator for CSharp {
gen.add_world_fragment();
}

fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) -> Result<()> {
let world = &resolve.worlds[id];
let world_namespace = self.qualifier();
let world_namespace = world_namespace.strip_suffix(".").unwrap();
Expand Down Expand Up @@ -654,6 +654,8 @@ impl WorldGenerator for CSharp {
generate_stub(name.to_string(), files, Stubs::Interface(fragments));
}
}

Ok(())
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl WorldGenerator for TinyGo {
self.src.push_str(&src);
}

fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) -> Result<()> {
// make sure all types are defined on top of the file
let src = mem::take(&mut self.src);
self.src.push_str(&src);
Expand Down Expand Up @@ -351,7 +351,9 @@ impl WorldGenerator for TinyGo {
opts.no_object_file = true;
opts.build()
.generate(resolve, id, files)
.expect("C generator should be infallible")
.expect("C generator should be infallible");

Ok(())
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl WorldGenerator for Markdown {
}
}

fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) {
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) -> Result<()> {
let world = &resolve.worlds[world];
let parser = Parser::new(&self.src);
let mut events = Vec::new();
Expand All @@ -227,6 +227,8 @@ impl WorldGenerator for Markdown {
files.push(&format!("{}.md", world.name), self.src.as_bytes());
files.push(&format!("{}.html", world.name), html_output.as_bytes());
}

Ok(())
}
}

Expand Down
20 changes: 19 additions & 1 deletion crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ struct RustWasm {
resources: HashMap<TypeId, ResourceInfo>,
import_funcs_called: bool,
with_name_counter: usize,
// Track the with options that were used. Remapped interfaces provided via `with`
// are required to be used.
used_with_opts: HashSet<String>,
}

#[cfg(feature = "clap")]
Expand Down Expand Up @@ -288,6 +291,7 @@ impl RustWasm {
let with_name = resolve.name_world_key(name);
let entry = if let Some(remapped_path) = self.opts.with.get(&with_name) {
let name = format!("__with_name{}", self.with_name_counter);
self.used_with_opts.insert(with_name);
self.with_name_counter += 1;
uwriteln!(self.src, "use {remapped_path} as {name};");
InterfaceName {
Expand Down Expand Up @@ -459,7 +463,7 @@ impl WorldGenerator for RustWasm {
}
}

fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) {
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) -> Result<()> {
let name = &resolve.worlds[world].name;

let imports = mem::take(&mut self.import_modules);
Expand Down Expand Up @@ -582,6 +586,20 @@ impl WorldGenerator for RustWasm {

let module_name = name.to_snake_case();
files.push(&format!("{module_name}.rs"), src.as_bytes());

let remapping_keys = self.opts.with.keys().cloned().collect::<HashSet<String>>();

let mut unused_keys = remapping_keys
.difference(&self.used_with_opts)
.collect::<Vec<&String>>();

unused_keys.sort();

if !unused_keys.is_empty() {
bail!("unused remappings provided via `with`: {unused_keys:?}");
}

Ok(())
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl WorldGenerator for TeaVmJava {
gen.add_world_fragment();
}

fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) -> Result<()> {
let name = world_name(resolve, id);
let (package, name) = split_qualified_name(&name);

Expand Down Expand Up @@ -431,6 +431,8 @@ impl WorldGenerator for TeaVmJava {
generate_stub(&package, format!("{name}Impl"), fragments, files);
}
}

Ok(())
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/bin/wit-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,15 @@ fn gen_world(
let (pkg, _files) = resolve.push_path(&opts.wit)?;
let world = resolve.select_world(pkg, opts.world.as_deref())?;
if let Err(e) = generator.generate(&resolve, world, files) {
eprintln!(
"{e:?}\n\n\
help: Specify export implementations using the `--exports` option.\n \
For example: `--exports world=MyWorld,ns:pkg/iface=MyIface`\n \
Alternatively, specify `--stubs` to generate stub implementations."
);
if e.to_string().starts_with("no `exports` map provided") {
bail!(
"{e:?}\n\n\
help: Specify export implementations using the `--exports` option.\n \
For example: `--exports world=MyWorld,ns:pkg/iface=MyIface`\n \
Alternatively, specify `--stubs` to generate stub implementations."
);
}
bail!("{e:?}");
}

Ok(())
Expand Down

0 comments on commit 785edc9

Please sign in to comment.