mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-29 15:42:16 +00:00
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes #1558 Closes #1695
85 lines
2.5 KiB
Rust
85 lines
2.5 KiB
Rust
//! See the README for `wasm-bindgen-test` for a bit more info about what's
|
|
//! going on here.
|
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro2::*;
|
|
use quote::quote;
|
|
use std::sync::atomic::*;
|
|
|
|
static CNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
#[proc_macro_attribute]
|
|
pub fn wasm_bindgen_test(
|
|
attr: proc_macro::TokenStream,
|
|
body: proc_macro::TokenStream,
|
|
) -> proc_macro::TokenStream {
|
|
let mut attr = attr.into_iter();
|
|
let mut r#async = false;
|
|
while let Some(token) = attr.next() {
|
|
match &token {
|
|
proc_macro::TokenTree::Ident(i) if i.to_string() == "async" => r#async = true,
|
|
_ => panic!("malformed `#[wasm_bindgen_test]` attribute"),
|
|
}
|
|
match &attr.next() {
|
|
Some(proc_macro::TokenTree::Punct(op)) if op.as_char() == ',' => {}
|
|
Some(_) => panic!("malformed `#[wasm_bindgen_test]` attribute"),
|
|
None => break,
|
|
}
|
|
}
|
|
|
|
let mut body = TokenStream::from(body).into_iter();
|
|
|
|
// Skip over other attributes to `fn #ident ...`, and extract `#ident`
|
|
let mut leading_tokens = Vec::new();
|
|
while let Some(token) = body.next() {
|
|
leading_tokens.push(token.clone());
|
|
if let TokenTree::Ident(token) = token {
|
|
if token == "async" {
|
|
r#async = true;
|
|
}
|
|
if token == "fn" {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
let ident = match body.next() {
|
|
Some(TokenTree::Ident(token)) => token,
|
|
_ => panic!("expected a function name"),
|
|
};
|
|
|
|
let mut tokens = Vec::<TokenTree>::new();
|
|
|
|
let test_body = if r#async {
|
|
quote! { cx.execute_async(test_name, #ident); }
|
|
} else {
|
|
quote! { cx.execute_sync(test_name, #ident); }
|
|
};
|
|
|
|
// We generate a `#[no_mangle]` with a known prefix so the test harness can
|
|
// later slurp up all of these functions and pass them as arguments to the
|
|
// main test harness. This is the entry point for all tests.
|
|
let name = format!(
|
|
"__wbg_test_{}_{}",
|
|
ident,
|
|
CNT.fetch_add(1, Ordering::SeqCst)
|
|
);
|
|
let name = Ident::new(&name, Span::call_site());
|
|
tokens.extend(
|
|
(quote! {
|
|
#[no_mangle]
|
|
pub extern "C" fn #name(cx: &::wasm_bindgen_test::__rt::Context) {
|
|
let test_name = concat!(module_path!(), "::", stringify!(#ident));
|
|
#test_body
|
|
}
|
|
})
|
|
.into_iter(),
|
|
);
|
|
|
|
tokens.extend(leading_tokens);
|
|
tokens.push(ident.into());
|
|
tokens.extend(body);
|
|
|
|
tokens.into_iter().collect::<TokenStream>().into()
|
|
}
|