Skip to content

Commit 9fea063

Browse files
committed
Add support for named imports to WASI implementations
This commit is at least an initial stab at making the `wasmtime-wasi` and `wasmtime-wasi-http` crates compatible with "named imports" or the `implements` field in the component model. This field enables importing an interface under a kebab-name while annotating that it's additionally to be considered an import of another interface's name. One example use case for this feature is [dependency isolation][diso] when composing two components together -- if they both import the filesystem the final component will import the filesystem twice under two different kebab names which means both components can have a different view of the filesystem. Wasmtime previously gained support for named imports and `implements` in `bindgen!` as part of bytecodealliance#13513 where the `named_imports` option can be specified at `bindgen!`-time which generates traits that take an extra id-style parameter. This runtime parameter indicates which kebab-name is being invoked through which the runtime can then dispatch on. The goal here is to actually wire all this up in a way that's usable for embedders. Specifically `named_imports` bindings generation is now available for all WASIp2 and WASIp3 interfaces. Additionally all implementations of these `id`-carrying traits are routed through the previous implementations after locating the correct context to operate over. All implementations of WASI functionality are already modeled more-or-less as methods on `Wasi*CtxView`-style types which internally have a borrow to the actual state and the resource table to operate on. This fits quite cleanly with named imports where conceptually what we want is the ability to configure the context-per-kebab-name. This in theory will keep the maintenance burden managable as there's still largely one source of truth for the implementation. This neatly works for all `Host`-style traits which are literally methods on `Wasi*CtxView` types, meaning the `id`-carrying versions actually do just acquire a `Wasi*CtxView` and then delegate the method. This requires more finesse for `*WithStore` traits which work with `Access` and `Accessor`, however. The `id` parameter cannot be threaded into the `fn(..)` within the `Accessor`, so refactoring is performed where appropriate to make the implementation of each interface a one-liner to reduce duplication. The end result of all of this is that this is a very large commit but it's written in such a way that the Rust compiler in theory should catch all mistakes. In other words we're heavily relying on the type system and type checking here and don't ever rely on duplication of methods that hopefully-won't-change. There's a lot of traits and a lot of interfaces, hence the size of the commit, but conceptually everything is intended to be pretty simple. Some design decisions as part of this commit, in no particular order: * IDs are represented by `wasmtime_wasi::NamedId` which is a newtype-wrapper around `usize`. The goal here is to enable an efficient implementation of dealing with ids. This notably forces the embedder to derive some sort of string-to-id (and perhaps back) map when adding items to a linker. * All of this is opt-in and nothing is changed by default. For example the `wasmtime` CLI does not support any of this yet -- in theory that would require the ability to configure `-S` flags per-named-import as opposed to all-at-once. * Mapping a `NamedId` to a context is abstracted behind a trait rather than dictating that a `Vec` or `HashMap` or similar is required. This increases the cognitive load when reading code (more generics), but avoids making this design decision within these crates and leaves exact representations up to embedders. * The `HasData` implementation can't reuse the preexisting `WasiCli`, and this uses a new `WasiCliNamed<T>` instead. This enables threading this trait-to-find-a-context to the right location for `*WithStore` trait impls. * Some miscellaneous `bindgen!` issues have been fixed during this commit to ensure that this compiles and works correctly. * An attempt has been made at documenting all the new primitives/structs/etc here. These are sort of difficult to align correctly unless you know what you're doing, so the documentation and examples are intended to serve as a way of spreading this knowledge. * One possible alternative I ended up deciding not to do was to put some sort of map-to-context storage within each preexisting context type. For example commit would be simpler for the `*WithStore` and infrastructure if it reused the exact same `Self` type as all other impls do. My thinking though is that this requires dictating the use of a `HashMap` or something else which I was hoping to avoid. Additionally the preexisting context structures are already minimal enough that they're basically what you already want as the source for each implementation, so I wanted to lean on them as much as possible. * The main wrinkle in the new implementation is that `Accessor` carries `fn(..)` to project out it's `D::Data<'_>` which means that it can't close over any information. This feature needs to in theory close over an `id: NamedId`, however, and there's no easy way to put this square peg into a round hole. To work around this internal implementations within `wasmtime-wasi{,-http}` now have a generic `F` parameter which is a closure which projects data, but this closure is typically only ever on the stack and doesn't make its way to the heap. This was one of the more awkward things to work around in this commit. * The design here is intentionally done to help ensure that this commit is correct with minimal testing. It's not really feasible to duplicate the entire test suite just for named imports but these are duplicate trait impls which otherwise shouldn't be wrong. By ensuring that there's either strict delegation or each-function-is-at-least-one-line that the light amount of testing here is sufficient for keeping this working over time. [diso]: spinframework/spin#3708
1 parent 84dbb9f commit 9fea063

68 files changed

Lines changed: 7413 additions & 653 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/component-macro/tests/codegen.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,64 @@ mod named_imports {
10281028
imports: { default: async | store },
10291029
});
10301030
}
1031+
1032+
mod trappable_errors {
1033+
use wasmtime::component::Resource;
1034+
1035+
wasmtime::component::bindgen!({
1036+
inline: "
1037+
package foo:foo;
1038+
1039+
interface store {
1040+
variant error {
1041+
not-found,
1042+
}
1043+
1044+
resource cache {
1045+
get: func(key: u32) -> result<u32, error>;
1046+
}
1047+
}
1048+
1049+
world the-world {
1050+
import store;
1051+
}
1052+
",
1053+
imports: { default: trappable },
1054+
named_imports: {
1055+
"foo:foo/store": String,
1056+
},
1057+
trappable_error_type: {
1058+
"foo:foo/store.error" => MyError,
1059+
},
1060+
});
1061+
1062+
pub struct MyError;
1063+
1064+
struct MyHost;
1065+
1066+
impl named_imports::foo::foo::store::HostCache for MyHost {
1067+
fn get(
1068+
&mut self,
1069+
_id: String,
1070+
_self_: Resource<foo::foo::store::Cache>,
1071+
key: u32,
1072+
) -> Result<u32, MyError> {
1073+
Ok(key)
1074+
}
1075+
fn drop(
1076+
&mut self,
1077+
_id: String,
1078+
_rep: Resource<foo::foo::store::Cache>,
1079+
) -> wasmtime::Result<()> {
1080+
Ok(())
1081+
}
1082+
}
1083+
impl named_imports::foo::foo::store::Host for MyHost {
1084+
fn convert_error(&mut self, _err: MyError) -> wasmtime::Result<foo::foo::store::Error> {
1085+
Ok(foo::foo::store::Error::NotFound)
1086+
}
1087+
}
1088+
}
10311089
}
10321090

10331091
mod include_component_type {

crates/component-macro/tests/expanded/unstable-features.rs

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/component-macro/tests/expanded/unstable-features_async.rs

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/component-macro/tests/expanded/unstable-features_concurrent.rs

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/component-macro/tests/expanded/unstable-features_tracing_async.rs

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
mod a {
2+
wit_bindgen::generate!({
3+
inline: r#"
4+
package a:b;
5+
6+
world foo {
7+
import a: wasi:cli/environment@0.2.12;
8+
}
9+
"#,
10+
path: "../wasi/src/p2/wit",
11+
generate_all,
12+
});
13+
}
14+
15+
mod b {
16+
wit_bindgen::generate!({
17+
inline: r#"
18+
package a:b;
19+
20+
world bar {
21+
import b: wasi:cli/environment@0.2.12;
22+
}
23+
"#,
24+
path: "../wasi/src/p2/wit",
25+
generate_all,
26+
});
27+
}
28+
29+
fn main() {
30+
let a = a::a::get_environment();
31+
let b = b::b::get_environment();
32+
let c = wasip2::cli::environment::get_environment();
33+
assert_ne!(a, b);
34+
assert_ne!(a, c);
35+
assert_ne!(b, c);
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
mod a {
2+
wit_bindgen::generate!({
3+
inline: r#"
4+
package a:b;
5+
6+
world foo {
7+
import a: wasi:http/types@0.2.12;
8+
}
9+
"#,
10+
path: "../wasi-http/wit",
11+
generate_all,
12+
});
13+
}
14+
15+
mod b {
16+
wit_bindgen::generate!({
17+
inline: r#"
18+
package a:b;
19+
20+
world bar {
21+
import b: wasi:http/types@0.2.12;
22+
}
23+
"#,
24+
path: "../wasi-http/wit",
25+
generate_all,
26+
});
27+
}
28+
29+
fn main() {
30+
let a = a::a::Fields::new();
31+
assert!(a.append("a", b"0").is_err());
32+
assert!(a.append("b", b"0").is_ok());
33+
34+
let b = b::b::Fields::new();
35+
assert!(b.append("a", b"0").is_ok());
36+
assert!(b.append("b", b"0").is_err());
37+
38+
let c = wasip2::http::types::Fields::new();
39+
assert!(c.append("a", b"0").is_ok());
40+
assert!(c.append("b", b"0").is_ok());
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
mod a {
2+
wit_bindgen::generate!({
3+
inline: r#"
4+
package a:b;
5+
6+
world foo {
7+
import a: wasi:cli/environment@0.3.0;
8+
}
9+
"#,
10+
path: "../wasi/src/p3/wit",
11+
generate_all,
12+
});
13+
}
14+
15+
mod b {
16+
wit_bindgen::generate!({
17+
inline: r#"
18+
package a:b;
19+
20+
world bar {
21+
import b: wasi:cli/environment@0.3.0;
22+
}
23+
"#,
24+
path: "../wasi/src/p3/wit",
25+
generate_all,
26+
});
27+
}
28+
29+
struct Component;
30+
31+
test_programs::p3::export!(Component);
32+
33+
impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
34+
async fn run() -> Result<(), ()> {
35+
let a = a::a::get_environment();
36+
let b = b::b::get_environment();
37+
let c = test_programs::p3::wasi::cli::environment::get_environment();
38+
assert_ne!(a, b);
39+
assert_ne!(a, c);
40+
assert_ne!(b, c);
41+
Ok(())
42+
}
43+
}
44+
45+
fn main() {}

0 commit comments

Comments
 (0)