Skip to content

Commit 4862162

Browse files
alexcrichtonPat Hickey
andauthored
Implement exporting interfaces from adapter modules (#421)
* start of support for adapter exports. doesnt actually work we're stuck on wit-bindgen-guest-rust performing allocations when it creates all the glue for the default export. we need a mode in the guest-rust crate for putting the type information about the default export into the object file, but not generating the actual functions, because in the case of this adapter, we want to create those functions manually so that they dont pull in std & therefore an allocator. * testwasi: export a "command" via an off-by-default feature * encode_exports: optional instance & realloc index in case the adapter isnt required * Remove command support from dummy preview1 This is only used for tests and none of the tests are exercising commands. * Disable default features for workspace deps Gets `wasi_snapshot_preview1` compiling correctly by actually turning off default features. * Finish up exported interfaces from adapters * Add missing default feature Co-authored-by: Pat Hickey <phickey@fastly.com>
1 parent e7fd381 commit 4862162

32 files changed

Lines changed: 597 additions & 201 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ wit-bindgen-gen-host-wasmtime-py = { path = 'crates/gen-host-wasmtime-py', versi
4141
wit-bindgen-gen-host-wasmtime-rust = { path = 'crates/gen-host-wasmtime-rust', version = '0.3.0' }
4242
wit-bindgen-gen-markdown = { path = 'crates/gen-markdown', version = '0.3.0' }
4343
wit-bindgen-gen-rust-lib = { path = 'crates/gen-rust-lib', version = '0.3.0' }
44-
wit-bindgen-guest-rust = { path = 'crates/guest-rust', version = '0.3.0' }
44+
wit-bindgen-guest-rust = { path = 'crates/guest-rust', version = '0.3.0', default-features = false }
4545
wit-bindgen-host-wasmtime-rust = { path = 'crates/host-wasmtime-rust', version = '0.3.0' }
4646
wit-parser = { path = 'crates/wit-parser', version = '0.3.0' }
4747
wit-component = { path = 'crates/wit-component', version = '0.3.0', default-features = false }

crates/guest-rust/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ wit-bindgen-guest-rust-macro = { path = "../guest-rust-macro", optional = true }
99
bitflags = { workspace = true }
1010

1111
[features]
12-
default = ["macros"]
12+
default = ["macros", "realloc"]
1313
macros = ["wit-bindgen-guest-rust-macro"]
14+
realloc = []

crates/guest-rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod rt {
1616
// Re-export things from liballoc for convenient use.
1717
pub use super::alloc::{alloc, string, vec};
1818

19+
#[cfg(feature = "realloc")]
1920
#[no_mangle]
2021
unsafe extern "C" fn cabi_realloc(
2122
old_ptr: *mut u8,

crates/wasi_snapshot_preview1/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition.workspace = true
55

66
[dependencies]
77
wasi = "0.11.0"
8-
wit-bindgen-guest-rust = { workspace = true }
8+
wit-bindgen-guest-rust = { workspace = true, default-features = false, features = ["macros"] }
99

1010
[lib]
1111
crate-type = ["cdylib"]

crates/wasi_snapshot_preview1/src/lib.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,9 @@ use wasi::*;
2020

2121
wit_bindgen_guest_rust::generate!({
2222
import: "testwasi.wit",
23-
name: "testwasi",
23+
name: "test_wasi",
2424
});
2525

26-
// Nothing in this wasm module should end up needing cabi_realloc. However, if
27-
// we don't define this trapping implementation of the export, we'll pull in
28-
// the one from wit_bindgen_guest_rust, which will pull in the libc allocator
29-
// and a bunch of panic related machinery from std, which will use vtables
30-
// and therefore create a Wasm ElementSection, which will make the resulting
31-
// wasm unusable as an adapter module.
32-
#[no_mangle]
33-
unsafe extern "C" fn cabi_realloc(
34-
old_ptr: *mut u8,
35-
old_len: usize,
36-
align: usize,
37-
new_len: usize,
38-
) -> *mut u8 {
39-
unreachable()
40-
}
41-
4226
#[no_mangle]
4327
pub extern "C" fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Errno {
4428
ERRNO_SUCCESS

crates/wit-bindgen-demo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ wit-bindgen-gen-host-js = { workspace = true }
2020
wit-bindgen-gen-guest-c = { workspace = true }
2121
wit-bindgen-gen-guest-teavm-java = { workspace = true }
2222
wit-bindgen-gen-markdown = { workspace = true }
23-
wit-bindgen-guest-rust = { workspace = true }
23+
wit-bindgen-guest-rust = { workspace = true, features = ['default'] }
2424
wasmprinter = { workspace = true }
2525
wit-component = { workspace = true }
2626
test-helpers = { path = '../test-helpers', default-features = false }

crates/wit-component/src/decoding.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ pub struct ComponentInterfaces {
9999
pub exports: IndexMap<String, Interface>,
100100
}
101101

102+
impl ComponentInterfaces {
103+
/// Returns an iterator which visits all the exported interfaces, both named
104+
/// and default. The second entry in each pair is whether the interface is
105+
/// the default interface.
106+
pub fn exports(&self) -> impl Iterator<Item = (&Interface, bool)> + '_ {
107+
self.exports
108+
.values()
109+
.map(|i| (i, false))
110+
.chain(self.default.iter().map(|i| (i, true)))
111+
}
112+
}
113+
102114
/// Decode the interfaces imported and exported by a component.
103115
///
104116
/// This function takes a binary component as input and will infer the

0 commit comments

Comments
 (0)