Skip to content

Commit

Permalink
fix: bindgen trappable_errors using unversion/versioned packages
Browse files Browse the repository at this point in the history
Signed-off-by: Brian H <brian.hardock@fermyon.com>
  • Loading branch information
fibonacci1729 committed Apr 5, 2024
1 parent 86dcae4 commit 4f4ab17
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
27 changes: 27 additions & 0 deletions crates/component-macro/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ mod with_key_and_resources {
}
}

mod trappable_errors_with_versioned_and_unversioned_packages {
wasmtime::component::bindgen!({
inline: "
package foo:foo@0.1.0;
interface a {
variant error {
other(string),
}
f: func() -> result<_, error>;
}
world foo {
import a;
}
",
path: "tests/codegen/unversioned-foo.wit",
trappable_error_type: {
"foo:foo/a@0.1.0/error" => MyX,
},
});

#[allow(dead_code)]
type MyX = u64;
}

mod trappable_errors {
wasmtime::component::bindgen!({
inline: "
Expand Down
12 changes: 12 additions & 0 deletions crates/component-macro/tests/codegen/unversioned-foo.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package foo:foo;

interface a {
variant error {
other(string),
}
g: func() -> result<_, error>;
}

world nope {
import a;
}
25 changes: 17 additions & 8 deletions crates/wit-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,9 +968,13 @@ fn resolve_type_in_package(resolve: &Resolve, wit_path: &str) -> anyhow::Result<
.flat_map(|l| l)
.collect::<HashSet<_>>();

let mut found_interface = false;

// Look for an interface whose assigned prefix starts `wit_path`. Not
// exactly the most efficient thing ever but is sufficient for now.
for (id, interface) in resolve.interfaces.iter() {
found_interface = true;

let iface_name = match &interface.name {
Some(name) => name,
None => continue,
Expand All @@ -988,15 +992,20 @@ fn resolve_type_in_package(resolve: &Resolve, wit_path: &str) -> anyhow::Result<
Some(rest) => rest,
None => continue,
};
let wit_path = wit_path
.strip_prefix('/')
.ok_or_else(|| anyhow!("expected `/` after interface name"))?;

return interface
.types
.get(wit_path)
.copied()
.ok_or_else(|| anyhow!("no types found to match `{wit_path}` in interface"));
let wit_path = match wit_path.strip_prefix('/') {
Some(rest) => rest,
None => continue,
};

match interface.types.get(wit_path).copied() {
Some(type_id) => return Ok(type_id),
None => continue,
}
}

if found_interface {
bail!("no types found to match `{wit_path}` in interface");
}

bail!("no package/interface found to match `{wit_path}`")
Expand Down
2 changes: 1 addition & 1 deletion tests/spec_testsuite
Submodule spec_testsuite updated 78 files
+1 −1 README.md
+78 −158 binary-leb128.wast
+741 −22 binary.wast
+249 −0 br_table.wast
+ comments.wast
+8 −23 elem.wast
+0 −40 float_literals.wast
+0 −14 if.wast
+0 −6 imports.wast
+0 −82 obsolete-keywords.wast
+19 −21 proposals/annotations/annotations.wast
+741 −22 proposals/exception-handling/binary.wast
+0 −6 proposals/exception-handling/imports.wast
+0 −13 proposals/exception-handling/ref_null.wast
+96 −0 proposals/exception-handling/rethrow.wast
+8 −12 proposals/exception-handling/throw.wast
+0 −118 proposals/exception-handling/throw_ref.wast
+242 −0 proposals/exception-handling/try_catch.wast
+166 −0 proposals/exception-handling/try_delegate.wast
+0 −372 proposals/exception-handling/try_table.wast
+0 −80 proposals/extended-const/elem.wast
+742 −22 proposals/function-references/binary.wast
+249 −0 proposals/function-references/br_table.wast
+1 −1 proposals/function-references/call_ref.wast
+0 −489 proposals/function-references/data.wast
+0 −706 proposals/function-references/elem.wast
+9 −0 proposals/function-references/global.wast
+0 −14 proposals/function-references/if.wast
+1 −1 proposals/function-references/return_call_ref.wast
+0 −40 proposals/function-references/table.wast
+5 −5 proposals/function-references/type-equivalence.wast
+32 −41 proposals/gc/array.wast
+0 −139 proposals/gc/array_copy.wast
+0 −81 proposals/gc/array_fill.wast
+0 −110 proposals/gc/array_init_data.wast
+0 −108 proposals/gc/array_init_elem.wast
+0 −12 proposals/gc/binary-gc.wast
+747 −27 proposals/gc/binary.wast
+62 −139 proposals/gc/br_on_cast.wast
+85 −162 proposals/gc/br_on_cast_fail.wast
+249 −0 proposals/gc/br_table.wast
+1 −1 proposals/gc/call_ref.wast
+0 −57 proposals/gc/elem.wast
+8 −8 proposals/gc/extern.wast
+15 −81 proposals/gc/global.wast
+5 −5 proposals/gc/i31.wast
+0 −14 proposals/gc/if.wast
+40 −0 proposals/gc/local_get.wast
+0 −74 proposals/gc/local_init.wast
+63 −63 proposals/gc/ref_cast.wast
+9 −9 proposals/gc/ref_eq.wast
+133 −133 proposals/gc/ref_test.wast
+0 −202 proposals/gc/return_call.wast
+0 −536 proposals/gc/return_call_indirect.wast
+1 −1 proposals/gc/return_call_ref.wast
+0 −17 proposals/gc/select.wast
+18 −129 proposals/gc/struct.wast
+0 −40 proposals/gc/table.wast
+19 −19 proposals/gc/type-equivalence.wast
+4 −74 proposals/gc/type-rec.wast
+31 −620 proposals/gc/type-subtyping.wast
+71 −181 proposals/memory64/binary-leb128.wast
+80 −964 proposals/memory64/binary.wast
+0 −157 proposals/memory64/simd_address.wast
+741 −22 proposals/multi-memory/binary.wast
+0 −6 proposals/multi-memory/imports.wast
+1 −40 proposals/multi-memory/memory_copy1.wast
+0 −2 proposals/relaxed-simd/i16x8_relaxed_q15mulr_s.wast
+0 −1 proposals/relaxed-simd/i32x4_relaxed_trunc.wast
+0 −1 proposals/relaxed-simd/i8x16_relaxed_swizzle.wast
+0 −1 proposals/relaxed-simd/relaxed_dot_product.wast
+0 −11 proposals/relaxed-simd/relaxed_laneselect.wast
+0 −1 proposals/relaxed-simd/relaxed_madd_nmadd.wast
+0 −1 proposals/relaxed-simd/relaxed_min_max.wast
+0 −276 token.wast
+274 −0 tokens.wast
+2 −2 unreachable.wast
+16 −6 update-testsuite.sh

0 comments on commit 4f4ab17

Please sign in to comment.