feat (macro-rs): handle async fn

This commit is contained in:
sup39 2024-04-12 04:30:48 +09:00 committed by naskya
parent c231bf43cb
commit b6b07d562c
No known key found for this signature in database
GPG Key ID: 712D413B3A9FED5C
1 changed files with 25 additions and 0 deletions

View File

@ -146,6 +146,13 @@ fn napi_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
// append "_napi" to function name
item_fn_sig.ident = syn::parse_str(&format!("{}_napi", &ident)).unwrap();
// append `.await` to function call in async function
if item_fn_sig.asyncness.is_some() {
function_call_modifiers.push(quote! {
.await
});
}
// convert return type `...::Result<T, ...>` to `napi::Result<T>`
if let syn::ReturnType::Type(_, ref mut return_type) = item_fn_sig.output {
if let Some(result_generic_type) = (|| {
@ -349,4 +356,22 @@ mod tests {
}
);
}
#[test]
fn async_function() {
test_macro!(
quote! {
pub async fn async_add_one(x: i32) -> i32 {
x + 1
}
},
quote! {
#[napi_derive::napi(js_name = "asyncAddOne")]
pub async fn async_add_one_napi(x: i32) -> i32 {
async_add_one(x)
.await
}
}
)
}
}