Repository: bytecodealliance/wasmtime
Pull request #7672 (2023-12-11 15:21:20)

Update `*_badfd` methods in the adapter to return an errno
Makes them a bit more consistent with the rest of the WASI functions as opposed to returning a boolean.

<!--
Please make sure you include the following information:

- If this work has been discussed elsewhere, please include a link to that
  conversation. If it was discussed in an issue, just mention "issue #...".

- Explain why this change is needed. If the details are in an issue already,
  this can be brief.

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->



Patch:
diff --git a/crates/wasi-preview1-component-adapter/src/lib.rs b/crates/wasi-preview1-component-adapter/src/lib.rs
index 59561f3ef..8ef496d56 100644
--- a/crates/wasi-preview1-component-adapter/src/lib.rs
+++ b/crates/wasi-preview1-component-adapter/src/lib.rs
@@ -152,17 +152,17 @@ impl<T, E> TrappingUnwrap<T> for Result<T, E> {
 /// from WASI Preview 1 to Preview 2.  It will use this function to reserve
 /// descriptors for its own use, valid only for use with libc functions.
 #[no_mangle]
-pub unsafe extern "C" fn adapter_open_badfd(fd: *mut u32) -> bool {
+pub unsafe extern "C" fn adapter_open_badfd(fd: *mut u32) -> Errno {
     State::with(|state| {
         *fd = state.descriptors_mut().open(Descriptor::Bad)?;
         Ok(())
-    }) == wasi::ERRNO_SUCCESS
+    })
 }
 
 /// Close a descriptor previously opened using `adapter_open_badfd`.
 #[no_mangle]
-pub unsafe extern "C" fn adapter_close_badfd(fd: u32) -> bool {
-    State::with(|state| state.descriptors_mut().close(fd)) == wasi::ERRNO_SUCCESS
+pub unsafe extern "C" fn adapter_close_badfd(fd: u32) -> Errno {
+    State::with(|state| state.descriptors_mut().close(fd))
 }
 
 #[no_mangle]
diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs
index eca7fd391..2c1422f59 100644
--- a/crates/wit-bindgen/src/lib.rs
+++ b/crates/wit-bindgen/src/lib.rs
@@ -474,7 +474,7 @@ impl Wasmtime {
                         format!(
                             "{}_{}_{snake}",
                             pkgname.namespace.to_snake_case(),
-                            pkgname.name.to_snake_case()
+                            self.name_package_module(resolve, iface.package.unwrap())
                         ),
                     ),
                     None => (format!("exports::{snake}::{camel}"), snake.clone()),


Test patch:
diff --git a/crates/component-macro/tests/codegen/multiversion/root.wit b/crates/component-macro/tests/codegen/multiversion/root.wit
index 52225f02b..b3bb3c80d 100644
--- a/crates/component-macro/tests/codegen/multiversion/root.wit
+++ b/crates/component-macro/tests/codegen/multiversion/root.wit
@@ -3,5 +3,6 @@ package foo:bar;
 world foo {
   import my:dep/a@0.1.0;
   import my:dep/a@0.2.0;
+  export my:dep/a@0.1.0;
   export my:dep/a@0.2.0;
 }
diff --git a/crates/test-programs/src/bin/preview2_adapter_badfd.rs b/crates/test-programs/src/bin/preview2_adapter_badfd.rs
index 7baa33717..e437dd668 100644
--- a/crates/test-programs/src/bin/preview2_adapter_badfd.rs
+++ b/crates/test-programs/src/bin/preview2_adapter_badfd.rs
@@ -2,15 +2,15 @@ fn main() {
     #[link(wasm_import_module = "wasi_snapshot_preview1")]
     extern "C" {
         #[cfg_attr(target_arch = "wasm32", link_name = "adapter_open_badfd")]
-        fn adapter_open_badfd(fd: *mut u32) -> bool;
+        fn adapter_open_badfd(fd: *mut u32) -> wasi::Errno;
 
         #[cfg_attr(target_arch = "wasm32", link_name = "adapter_close_badfd")]
-        fn adapter_close_badfd(fd: u32) -> bool;
+        fn adapter_close_badfd(fd: u32) -> wasi::Errno;
     }
 
     unsafe {
         let mut fd = 0;
-        assert!(adapter_open_badfd(&mut fd));
+        assert_eq!(adapter_open_badfd(&mut fd), wasi::ERRNO_SUCCESS);
 
         assert_eq!(wasi::fd_close(fd), Err(wasi::ERRNO_BADF));
 
@@ -41,6 +41,6 @@ fn main() {
             Err(wasi::ERRNO_BADF)
         );
 
-        assert!(adapter_close_badfd(fd));
+        assert_eq!(adapter_close_badfd(fd), wasi::ERRNO_SUCCESS);
     }
 }