mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
cleanup(ops): shorter codegen'd infallible sync ops return (#14587)
Co-authored-by: Aaron O'Mullan <aaron.omullan@gmail.com>
This commit is contained in:
parent
f18d0539b1
commit
6fff813029
1 changed files with 13 additions and 15 deletions
28
ops/lib.rs
28
ops/lib.rs
|
@ -309,19 +309,16 @@ fn codegen_sync_ret(
|
||||||
core: &TokenStream2,
|
core: &TokenStream2,
|
||||||
output: &syn::ReturnType,
|
output: &syn::ReturnType,
|
||||||
) -> TokenStream2 {
|
) -> TokenStream2 {
|
||||||
let ret_type = match output {
|
if is_void(output) {
|
||||||
// Func with no return no-ops
|
return quote! {};
|
||||||
syn::ReturnType::Default => return quote! { let ret = (); },
|
}
|
||||||
// Func with a return Result<T, E>
|
|
||||||
syn::ReturnType::Type(_, ty) => ty,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Optimize Result<(), Err> to skip serde_v8 when Ok(...)
|
// Optimize Result<(), Err> to skip serde_v8 when Ok(...)
|
||||||
let ok_block = if is_unit_result(&**ret_type) {
|
let ok_block = if is_unit_result(output) {
|
||||||
quote! {}
|
quote! {}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
match #core::serde_v8::to_v8(scope, v) {
|
match #core::serde_v8::to_v8(scope, result) {
|
||||||
Ok(ret) => rv.set(ret),
|
Ok(ret) => rv.set(ret),
|
||||||
Err(err) => #core::_ops::throw_type_error(
|
Err(err) => #core::_ops::throw_type_error(
|
||||||
scope,
|
scope,
|
||||||
|
@ -331,16 +328,13 @@ fn codegen_sync_ret(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let result_wrapper = match is_result(&**ret_type) {
|
if !is_result(output) {
|
||||||
true => quote! {},
|
return ok_block;
|
||||||
false => quote! { let result = Ok(result); },
|
}
|
||||||
};
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#result_wrapper
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(v) => {
|
Ok(result) => {
|
||||||
#ok_block
|
#ok_block
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -351,6 +345,10 @@ fn codegen_sync_ret(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_void(ty: impl ToTokens) -> bool {
|
||||||
|
tokens(ty).is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
fn is_result(ty: impl ToTokens) -> bool {
|
fn is_result(ty: impl ToTokens) -> bool {
|
||||||
let tokens = tokens(ty);
|
let tokens = tokens(ty);
|
||||||
if tokens.trim_start_matches("-> ").starts_with("Result <") {
|
if tokens.trim_start_matches("-> ").starts_with("Result <") {
|
||||||
|
|
Loading…
Reference in a new issue