-
Notifications
You must be signed in to change notification settings - Fork 19
Add wstd::main and wstd::test macros for WASIp3
#149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamrk
wants to merge
6
commits into
bytecodealliance:main
Choose a base branch
from
adamrk:abk/main-macro-for-wasip3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42d7884
add p3 feature
adamrk 4a6e65c
add p3 test CI job
adamrk ea32edf
use cfg alias
adamrk 0487755
Require `&mut` for Streams
adamrk 443ef72
remove &self stream methods
adamrk cabda1f
Add `wstd::main` and `wstd::test` macros for WASIp3
adamrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| use cfg_aliases::cfg_aliases; | ||
|
|
||
| fn main() { | ||
| cfg_aliases! { | ||
| // TODO https://github.com/bytecodealliance/wstd/issues/147: Swap these | ||
| // to use `target_env` instead. | ||
| p2: { feature = "p2" }, | ||
| p3: { feature = "p3" }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #![cfg(p2)] | ||
| //! Support for the [`axum`] web server framework in wasi-http components, via | ||
| //! [`wstd`]. | ||
| //! | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| use cfg_aliases::cfg_aliases; | ||
|
|
||
| fn main() { | ||
| cfg_aliases! { | ||
| // TODO https://github.com/bytecodealliance/wstd/issues/147: Swap these | ||
| // to use `target_env` instead. | ||
| p2: { feature = "p2" }, | ||
| p3: { feature = "p3" }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,15 @@ use proc_macro::TokenStream; | |
| use quote::{quote, quote_spanned}; | ||
| use syn::{ItemFn, parse_macro_input, spanned::Spanned}; | ||
|
|
||
| fn returns_unit(output: &syn::ReturnType) -> bool { | ||
| match output { | ||
| syn::ReturnType::Default => true, | ||
| syn::ReturnType::Type(_, ty) => { | ||
| matches!(&**ty, syn::Type::Tuple(tuple) if tuple.elems.is_empty()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[proc_macro_attribute] | ||
| pub fn attr_macro_main(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(item as ItemFn); | ||
|
|
@@ -84,6 +93,149 @@ pub fn attr_macro_test(_attr: TokenStream, item: TokenStream) -> TokenStream { | |
| .into() | ||
| } | ||
|
|
||
| #[proc_macro_attribute] | ||
| pub fn attr_macro_main_p3(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(item as ItemFn); | ||
|
|
||
| if input.sig.asyncness.is_none() { | ||
| return quote_spanned! { input.sig.fn_token.span()=> | ||
| compile_error!("fn must be `async fn`"); | ||
| } | ||
| .into(); | ||
| } | ||
|
|
||
| if input.sig.ident != "main" { | ||
| return quote_spanned! { input.sig.ident.span()=> | ||
| compile_error!("only `async fn main` can be used for #[wstd::main]"); | ||
| } | ||
| .into(); | ||
| } | ||
|
|
||
| if !input.sig.inputs.is_empty() { | ||
| return quote_spanned! { input.sig.inputs.span()=> | ||
| compile_error!("arguments to main are not supported"); | ||
| } | ||
| .into(); | ||
| } | ||
| let attrs = input.attrs; | ||
| let output = input.sig.output; | ||
| let block = input.block; | ||
| let run_result = if returns_unit(&output) { | ||
| quote! { | ||
| __run().await; | ||
| ::core::result::Result::Ok(()) | ||
| } | ||
| } else { | ||
| quote! { | ||
| ::core::result::Result::map_err(__run().await, |_| ()) | ||
| } | ||
| }; | ||
| quote! { | ||
| struct __WstdCliRunner; | ||
|
|
||
| impl ::wstd::__internal::wasip3::exports::cli::run::Guest for __WstdCliRunner { | ||
| async fn run() -> ::core::result::Result<(), ()> { | ||
| #(#attrs)* | ||
| async fn __run() #output { | ||
| #block | ||
| } | ||
|
|
||
| #run_result | ||
| } | ||
| } | ||
|
|
||
| ::wstd::__internal::wasip3::cli::command::export!( | ||
| __WstdCliRunner with_types_in ::wstd::__internal::wasip3 | ||
| ); | ||
|
|
||
| // Provide a `main` so users don't have to write `#![no_main]`. | ||
| fn main() { | ||
| ::core::unreachable!( | ||
| "wstd p3 components run via the wasi:cli/run@0.3.0 export, not `main`" | ||
| ) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this breaks in wasm32-wasip3 target |
||
| } | ||
| .into() | ||
| } | ||
|
|
||
| #[proc_macro_attribute] | ||
| pub fn attr_macro_test_p3(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(item as ItemFn); | ||
|
|
||
| if input.sig.asyncness.is_none() { | ||
| return quote_spanned! { input.sig.fn_token.span()=> | ||
| compile_error!("fn must be `async fn`"); | ||
| } | ||
| .into(); | ||
| } | ||
|
|
||
| if !input.sig.inputs.is_empty() { | ||
| return quote_spanned! { input.sig.inputs.span()=> | ||
| compile_error!("arguments to a test are not supported"); | ||
| } | ||
| .into(); | ||
| } | ||
| let attrs = input.attrs; | ||
| let output = input.sig.output; | ||
| let block = input.block; | ||
| let name = input.sig.ident; | ||
| let message = format!("test {name} ... "); | ||
| let run_result = if returns_unit(&output) { | ||
| quote! { | ||
| ::std::println!(""); | ||
| ::std::print!(#message); | ||
| __run().await; | ||
| ::std::println!("ok"); | ||
| ::std::println!(""); | ||
| ::core::result::Result::Ok(()) | ||
| } | ||
| } else { | ||
| quote! { | ||
| ::std::println!(""); | ||
| ::std::print!(#message); | ||
| match __run().await { | ||
| ::core::result::Result::Ok(_) => { | ||
| ::std::println!("ok"); | ||
| ::std::println!(""); | ||
| ::core::result::Result::Ok(()) | ||
| } | ||
| ::core::result::Result::Err(err) => { | ||
| ::std::println!("failed"); | ||
| ::std::println!("Error {:?}", err); | ||
| ::std::println!(""); | ||
| ::core::result::Result::Err(()) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| quote! { | ||
| struct __WstdCliRunner; | ||
|
|
||
| impl ::wstd::__internal::wasip3::exports::cli::run::Guest for __WstdCliRunner { | ||
| async fn run() -> ::core::result::Result<(), ()> { | ||
| #(#attrs)* | ||
| async fn __run() #output { | ||
| #block | ||
| } | ||
|
|
||
| #run_result | ||
| } | ||
| } | ||
|
|
||
| ::wstd::__internal::wasip3::cli::command::export!( | ||
| __WstdCliRunner with_types_in ::wstd::__internal::wasip3 | ||
| ); | ||
|
|
||
| // Provide a `main` so users don't have to write `#![no_main]`. | ||
| fn main() { | ||
| ::core::unreachable!( | ||
| "wstd p3 components run via the wasi:cli/run@0.3.0 export, not `main`" | ||
| ) | ||
| } | ||
| } | ||
| .into() | ||
| } | ||
|
|
||
| /// Enables a HTTP server main function, for creating [HTTP servers]. | ||
| /// | ||
| /// [HTTP servers]: https://docs.rs/wstd/latest/wstd/http/server/index.html | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Transition to using wasm32-wasip3 target should mean we have a separate section with this changed runner, and can exclude p3 stuff on the p2 target