Skip to content

Commit db99d22

Browse files
authored
Loosely integrate implements into bindgen! (#13497)
* Loosely integrate `implements` into `bindgen!` This commit is the next step in fleshing out #12698 and following in the footsteps of #13361 to integrate the `(implements ...)` directive into `bindgen!`. Notably this world: world foo { import a: thing; import b: thing; } will only generate a single trait for `thing::Host` as opposed to the previous 2 traits that were generated. This enables hosts to use the same trait implementation for both, or customize as appropriate. Additionally the `add_to_linker`-generated methods for interfaces are now refactored to additionally have an `add_to_linker_instance` entrypoint. This new entrypoint takes a `LinkerInstance` instead of a `Linker` and fills in the provided instance directly. This is in-turn used to fill out a linker for the `foo` world above. Notably, however, embedders can call `add_to_linker_instance` with distinct closures accessing different parts of `T`, the store's state, meaning that it's possible to have two distinct implementations for `a` and `b` above. Many golden tests were updated with this new `add_to_linker_instance` method, and codegen/runtime tests were added for a simple world as well showcasing the bindings modes that are possible. The (rarely used) top-level world `add_to_linker` only uses a single trait implementation, but invoking `add_to_linker_instance` enables passing in two separate implementations. * Hide generated files in diffs by default * Add some runtime tests
1 parent 82a7bff commit db99d22

119 files changed

Lines changed: 3637 additions & 527 deletions

File tree

Some content is hidden

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

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
# ISLE should use lisp syntax highlighting.
1010
*.isle linguist-language=lisp
11+
12+
# Generated files don't need diffs rendered by default
13+
crates/component-macro/tests/expanded/** linguist-generated=true

crates/component-macro/src/bindgen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub struct Config {
2020
include_generated_code_from_file: bool,
2121
}
2222

23-
pub fn expand(input: &Config) -> Result<TokenStream> {
24-
let mut src = match input.opts.generate(&input.resolve, input.world) {
23+
pub fn expand(input: &mut Config) -> Result<TokenStream> {
24+
let mut src = match input.opts.generate(&mut input.resolve, input.world) {
2525
Ok(s) => s,
2626
Err(e) => return Err(Error::new(Span::call_site(), e.to_string())),
2727
};

crates/component-macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn flags(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
4949

5050
#[proc_macro]
5151
pub fn bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
52-
bindgen::expand(&parse_macro_input!(input as bindgen::Config))
52+
bindgen::expand(&mut parse_macro_input!(input as bindgen::Config))
5353
.unwrap_or_else(Error::into_compile_error)
5454
.into()
5555
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package foo:foo;
2+
3+
interface store {
4+
get: func(key: string) -> option<string>;
5+
set: func(key: string, value: string);
6+
}
7+
8+
world cache {
9+
import hot-cache: store;
10+
import durable: store;
11+
}

crates/component-macro/tests/expanded.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ fn process_expanded(path: &str, suffix: &str, src: &str) -> Result<()> {
4343
Path::new("tests/expanded").join(stem).with_extension("rs")
4444
};
4545
if std::env::var("BINDGEN_TEST_BLESS").is_ok_and(|val| !val.is_empty()) {
46+
if let Ok(prev) = std::fs::read(&expanded_path)
47+
&& prev == formatted_src.as_bytes()
48+
{
49+
return Ok(());
50+
}
4651
std::fs::write(expanded_path, formatted_src)?;
4752
} else {
4853
match std::fs::read_to_string(&expanded_path) {

crates/component-macro/tests/expanded/char.rs

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

crates/component-macro/tests/expanded/char_async.rs

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

crates/component-macro/tests/expanded/char_concurrent.rs

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

crates/component-macro/tests/expanded/char_tracing_async.rs

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

crates/component-macro/tests/expanded/conventions.rs

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

0 commit comments

Comments
 (0)