From 22fb3ecbbf287a78cb8e449e550b871ae6fef126 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 29 Jun 2022 08:38:36 -0600 Subject: [PATCH] add ComponentType/Lift/Lower derive macro for record types (#4337) This is the first stage of implementing https://github.com/bytecodealliance/wasmtime/issues/4308, i.e. derive macros for `ComponentType`, `Lift`, and `Lower` for composite types in the component model. This stage only covers records; I expect the other composite types will follow a similar pattern. It borrows heavily from the work Jamey Sharp did in https://github.com/bytecodealliance/wasmtime/pull/4217. Thanks for that, and thanks to both Jamey and Alex Crichton for their excellent review feedback. Thanks also to Brian for pairing up on the initial draft. Signed-off-by: Joel Dice --- Cargo.lock | 10 + crates/component-macro/Cargo.toml | 22 + crates/component-macro/src/lib.rs | 382 ++++++++++++++++++ crates/wasmtime/Cargo.toml | 2 + crates/wasmtime/src/component/func.rs | 7 +- crates/wasmtime/src/component/func/host.rs | 4 +- crates/wasmtime/src/component/func/options.rs | 2 +- crates/wasmtime/src/component/func/typed.rs | 59 ++- crates/wasmtime/src/component/mod.rs | 17 +- crates/wasmtime/src/store.rs | 1 + scripts/publish.rs | 1 + tests/all/component_model.rs | 21 +- tests/all/component_model/func.rs | 20 +- tests/all/component_model/macros.rs | 158 ++++++++ 14 files changed, 660 insertions(+), 46 deletions(-) create mode 100644 crates/component-macro/Cargo.toml create mode 100644 crates/component-macro/src/lib.rs create mode 100644 tests/all/component_model/macros.rs diff --git a/Cargo.lock b/Cargo.lock index 4796d80f2a..f6d8f01c96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3332,6 +3332,7 @@ dependencies = [ "wasi-cap-std-sync", "wasmparser", "wasmtime-cache", + "wasmtime-component-macro", "wasmtime-cranelift", "wasmtime-environ", "wasmtime-fiber", @@ -3457,6 +3458,15 @@ dependencies = [ "wasmtime", ] +[[package]] +name = "wasmtime-component-macro" +version = "0.39.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "wasmtime-cranelift" version = "0.39.0" diff --git a/crates/component-macro/Cargo.toml b/crates/component-macro/Cargo.toml new file mode 100644 index 0000000000..4e924ec976 --- /dev/null +++ b/crates/component-macro/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "wasmtime-component-macro" +version = "0.39.0" +authors = ["The Wasmtime Project Developers"] +description = "Macros for deriving component interface types from Rust types" +license = "Apache-2.0 WITH LLVM-exception" +repository = "https://github.com/bytecodealliance/wasmtime" +documentation = "https://docs.rs/wasmtime-component-macro/" +categories = ["wasm"] +keywords = ["webassembly", "wasm"] +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0" +quote = "1.0" +syn = { version = "1.0", features = ["extra-traits"] } + +[badges] +maintenance = { status = "actively-developed" } diff --git a/crates/component-macro/src/lib.rs b/crates/component-macro/src/lib.rs new file mode 100644 index 0000000000..d319e6df75 --- /dev/null +++ b/crates/component-macro/src/lib.rs @@ -0,0 +1,382 @@ +use proc_macro2::{Literal, TokenStream, TokenTree}; +use quote::{format_ident, quote}; +use std::collections::HashSet; +use syn::{parse_macro_input, parse_quote, Data, DeriveInput, Error, Result}; + +#[derive(Debug)] +enum Style { + Record, + Variant, + Enum, + Union, +} + +fn find_style(input: &DeriveInput) -> Result