Skip to content

Commit

Permalink
Update to wasm-tools 215 (#1011)
Browse files Browse the repository at this point in the history
* Update to wasm-tools 215

Brings in a breaking change which is hoped to not affect anyone around
how multi-package WITs are handled.

* Fix C test

* Fix main test build

* Remove patch  entries
  • Loading branch information
alexcrichton authored Jul 31, 2024
1 parent 50ef795 commit 9fa2861
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 253 deletions.
348 changes: 148 additions & 200 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ indexmap = "2.0.0"
prettyplease = "0.2.20"
syn = { version = "2.0", features = ["printing"] }

wasmparser = "0.214.0"
wasm-encoder = "0.214.0"
wasm-metadata = "0.214.0"
wit-parser = "0.214.0"
wit-component = "0.214.0"
wasmparser = "0.215.0"
wasm-encoder = "0.215.0"
wasm-metadata = "0.215.0"
wit-parser = "0.215.0"
wit-component = "0.215.0"

wit-bindgen-core = { path = 'crates/core', version = '0.28.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.28.0' }
Expand Down
4 changes: 2 additions & 2 deletions crates/c/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn rename_option() -> Result<()> {
opts.rename.push(("c".to_string(), "rename3".to_string()));

let mut resolve = Resolve::default();
let pkgs = resolve.push_group(UnresolvedPackageGroup::parse(
let pkg = resolve.push_group(UnresolvedPackageGroup::parse(
"input.wit",
r#"
package foo:bar;
Expand All @@ -118,7 +118,7 @@ fn rename_option() -> Result<()> {
}
"#,
)?)?;
let world = resolve.select_world(&pkgs, None)?;
let world = resolve.select_world(pkg, None)?;
let mut files = Default::default();
opts.build().generate(&resolve, world, &mut files)?;
for (file, contents) in files.iter() {
Expand Down
41 changes: 21 additions & 20 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ impl WorldGenerator for CSharp {
{{
get
{{
if (Tag == OK)
if (Tag == OK)
return (Ok)value;
else
else
throw new ArgumentException("expected OK, got " + Tag);
}}
}}
Expand Down Expand Up @@ -503,23 +503,23 @@ impl WorldGenerator for CSharp {
{access} class Option<T> {{
private static Option<T> none = new ();
private Option()
{{
HasValue = false;
}}
{access} Option(T v)
{{
HasValue = true;
Value = v;
}}
{access} static Option<T> None => none;
[MemberNotNullWhen(true, nameof(Value))]
{access} bool HasValue {{ get; }}
{access} T? Value {{ get; }}
}}
"#,
Expand Down Expand Up @@ -753,26 +753,27 @@ impl WorldGenerator for CSharp {
// intended to be used non-interactively at link time, the
// linker will have no additional information to resolve such
// ambiguity.
let (resolve, _) =
let (resolve, world) =
wit_parser::decoding::decode_world(&wit_component::metadata::encode(
&resolve,
id,
self.opts.string_encoding,
None,
)?)?;
let pkg = resolve.worlds[world].package.unwrap();

files.push(
&format!("{world_namespace}_component_type.wit"),
WitPrinter::default()
.emit_docs(false)
.print(
&resolve,
pkg,
&resolve
.packages
.iter()
.map(|(id, _)| id)
.filter_map(|(id, _)| if id == pkg { None } else { Some(id) })
.collect::<Vec<_>>(),
false,
)?
.as_bytes(),
);
Expand Down Expand Up @@ -1494,7 +1495,7 @@ impl InterfaceGenerator<'_> {
[DllImport("{module_name}", EntryPoint = "[resource-drop]{name}"), WasmImportLinkage]
private static extern void wasmImportResourceDrop(int p0);
protected virtual void Dispose(bool disposing) {{
if (Handle != 0) {{
wasmImportResourceDrop(Handle);
Expand Down Expand Up @@ -1564,7 +1565,7 @@ impl InterfaceGenerator<'_> {
[DllImport("{module_name}", EntryPoint = "[resource-new]{name}"), WasmImportLinkage]
internal static extern int wasmImportResourceNew(int p0);
[DllImport("{module_name}", EntryPoint = "[resource-rep]{name}"), WasmImportLinkage]
internal static extern int wasmImportResourceRep(int p0);
}}
Expand Down Expand Up @@ -1844,15 +1845,15 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
let tag = case.name.to_shouty_snake_case();
let ty = self.type_name(ty);
format!(
r#"{access} {ty} As{case_name}
{{
get
r#"{access} {ty} As{case_name}
{{
get
{{
if (Tag == {tag})
if (Tag == {tag})
return ({ty})value!;
else
else
throw new ArgumentException("expected {tag}, got " + Tag);
}}
}}
}}
"#
)
Expand Down Expand Up @@ -2584,8 +2585,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwrite!(
self.src,
"
var {array} = new {ty}[{length}];
new Span<{ty}>((void*)({address}), {length}).CopyTo(new Span<{ty}>({array}));
var {array} = new {ty}[{length}];
new Span<{ty}>((void*)({address}), {length}).CopyTo(new Span<{ty}>({array}));
"
);

Expand Down
44 changes: 40 additions & 4 deletions crates/guest-rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ impl Parse for Config {
}
let (resolve, pkgs, files) =
parse_source(&source, &features).map_err(|err| anyhow_to_syn(call_site, err))?;
let world = resolve
.select_world(&pkgs, world.as_deref())
let world = select_world(&resolve, &pkgs, world.as_deref())
.map_err(|e| anyhow_to_syn(call_site, e))?;
Ok(Config {
opts,
Expand All @@ -161,6 +160,43 @@ impl Parse for Config {
}
}

fn select_world(
resolve: &Resolve,
pkgs: &[PackageId],
world: Option<&str>,
) -> anyhow::Result<WorldId> {
if pkgs.len() == 1 {
resolve.select_world(pkgs[0], world)
} else {
assert!(!pkgs.is_empty());
match world {
Some(name) => {
if !name.contains(":") {
anyhow::bail!(
"with multiple packages a fully qualified \
world name must be specified"
)
}

// This will ignore the package argument due to the fully
// qualified name being used.
resolve.select_world(pkgs[0], world)
}
None => {
let worlds = pkgs
.iter()
.filter_map(|p| resolve.select_world(*p, None).ok())
.collect::<Vec<_>>();
match &worlds[..] {
[] => anyhow::bail!("no packages have a world"),
[world] => Ok(*world),
_ => anyhow::bail!("multiple packages have a world, must specify which to use"),
}
}
}
}
}

/// Parse the source
fn parse_source(
source: &Option<Source>,
Expand All @@ -182,7 +218,7 @@ fn parse_source(
Err(_) => p.to_path_buf(),
};
let (pkg, sources) = resolve.push_path(normalized_path)?;
pkgs.extend(pkg);
pkgs.push(pkg);
files.extend(sources);
}
Ok(())
Expand All @@ -192,7 +228,7 @@ fn parse_source(
if let Some(p) = path {
parse(p)?;
}
pkgs = resolve.push_group(UnresolvedPackageGroup::parse("macro-input", s)?)?;
pkgs.push(resolve.push_group(UnresolvedPackageGroup::parse("macro-input", s)?)?);
}
Some(Source::Paths(p)) => parse(p)?,
None => parse(&vec![root.join("wit")])?,
Expand Down
18 changes: 9 additions & 9 deletions crates/rust/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ mod generate_unused_types {
mod gated_features {
wit_bindgen::generate!({
inline: r#"
package foo:bar;
package foo:bar@1.2.3;
world bindings {
@unstable(feature = x)
Expand All @@ -519,10 +519,10 @@ mod simple_with_option {
mod a {
wit_bindgen::generate!({
inline: r#"
package foo:bar {
interface a {
x: func();
}
package foo:bar;
interface a {
x: func();
}
package foo:baz {
Expand All @@ -539,10 +539,10 @@ mod simple_with_option {
mod b {
wit_bindgen::generate!({
inline: r#"
package foo:bar {
interface a {
x: func();
}
package foo:bar;
interface a {
x: func();
}
package foo:baz {
Expand Down
4 changes: 0 additions & 4 deletions crates/rust/tests/codegen_no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
#![allow(unused_macros)]
#![allow(dead_code, unused_variables)]

// This test expects `"std"` to be absent.
#[cfg(feature = "std")]
fn std_enabled() -> CompileError;

extern crate alloc;

mod codegen_tests {
Expand Down
8 changes: 3 additions & 5 deletions crates/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub use codegen_macro::*;
#[cfg(feature = "runtime-macro")]
pub use runtime_macro::*;

use std::fs;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -143,10 +141,10 @@ pub fn run_component_codegen_test(

fn parse_wit(path: &Path) -> (Resolve, WorldId) {
let mut resolve = Resolve::default();
let (pkgs, _files) = resolve.push_path(path).unwrap();
let world = resolve.select_world(&pkgs, None).unwrap_or_else(|_| {
let (pkg, _files) = resolve.push_path(path).unwrap();
let world = resolve.select_world(pkg, None).unwrap_or_else(|_| {
// note: if there are multiples worlds in the wit package, we assume the "imports" world
resolve.select_world(&pkgs, Some("imports")).unwrap()
resolve.select_world(pkg, Some("imports")).unwrap()
});
(resolve, world)
}
4 changes: 2 additions & 2 deletions src/bin/wit-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ fn gen_world(
resolve.features.insert(feature.to_string());
}
}
let (pkgs, _files) = resolve.push_path(&opts.wit)?;
let world = resolve.select_world(&pkgs, opts.world.as_deref())?;
let (pkg, _files) = resolve.push_path(&opts.wit)?;
let world = resolve.select_world(pkg, opts.world.as_deref())?;
generator.generate(&resolve, world, files)?;

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {
#[allow(dead_code)] // not used by all generators
fn resolve_wit_dir(dir: &PathBuf) -> (Resolve, WorldId) {
let mut resolve = Resolve::new();
let (pkgs, _files) = resolve.push_path(dir).unwrap();
let world = resolve.select_world(&pkgs, None).unwrap();
let (pkg, _files) = resolve.push_path(dir).unwrap();
let world = resolve.select_world(pkg, None).unwrap();
(resolve, world)
}

0 comments on commit 9fa2861

Please sign in to comment.