Skip to content

Commit

Permalink
merge from origin main
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Aug 1, 2024
2 parents d1f7e15 + 5d9964c commit 8d764ad
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 240 deletions.
332 changes: 140 additions & 192 deletions Cargo.lock

Large diffs are not rendered by default.

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
45 changes: 23 additions & 22 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,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 @@ -504,23 +504,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 @@ -754,26 +754,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 @@ -1484,9 +1485,9 @@ impl InterfaceGenerator<'_> {
{access} class {upper_camel}: IDisposable {{
internal int Handle {{ get; set; }}
internal readonly record struct THandle(int Handle);
{access} readonly record struct THandle(int Handle);
internal {upper_camel}(THandle handle) {{
{access} {upper_camel}(THandle handle) {{
Handle = handle.Handle;
}}
Expand All @@ -1497,7 +1498,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 @@ -1567,7 +1568,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 @@ -1847,15 +1848,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 @@ -2587,8 +2588,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 @@ -165,8 +165,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 @@ -178,6 +177,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 @@ -199,7 +235,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 @@ -209,7 +245,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 tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,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 8d764ad

Please sign in to comment.