mirror of
https://github.com/denoland/deno.git
synced 2025-01-07 22:58:24 -05:00
feat(ops): 'hybrid' ops - sync returning future (#14640)
This commit is contained in:
parent
99f525a66c
commit
753f32024f
1 changed files with 20 additions and 3 deletions
23
ops/lib.rs
23
ops/lib.rs
|
@ -86,9 +86,10 @@ pub fn op(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
let core = core_import();
|
let core = core_import();
|
||||||
|
|
||||||
let is_async = func.sig.asyncness.is_some();
|
let asyncness = func.sig.asyncness.is_some();
|
||||||
|
let is_async = asyncness || is_future(&func.sig.output);
|
||||||
let v8_body = if is_async {
|
let v8_body = if is_async {
|
||||||
codegen_v8_async(&core, &func, margs)
|
codegen_v8_async(&core, &func, margs, asyncness)
|
||||||
} else {
|
} else {
|
||||||
codegen_v8_sync(&core, &func, margs)
|
codegen_v8_sync(&core, &func, margs)
|
||||||
};
|
};
|
||||||
|
@ -145,6 +146,7 @@ fn codegen_v8_async(
|
||||||
core: &TokenStream2,
|
core: &TokenStream2,
|
||||||
f: &syn::ItemFn,
|
f: &syn::ItemFn,
|
||||||
_margs: MacroArgs,
|
_margs: MacroArgs,
|
||||||
|
asyncness: bool,
|
||||||
) -> TokenStream2 {
|
) -> TokenStream2 {
|
||||||
let arg0 = f.sig.inputs.first();
|
let arg0 = f.sig.inputs.first();
|
||||||
let uses_opstate = arg0.map(is_rc_refcell_opstate).unwrap_or_default();
|
let uses_opstate = arg0.map(is_rc_refcell_opstate).unwrap_or_default();
|
||||||
|
@ -157,6 +159,16 @@ fn codegen_v8_async(
|
||||||
let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
|
let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
|
||||||
let type_params = exclude_lifetime_params(&f.sig.generics.params);
|
let type_params = exclude_lifetime_params(&f.sig.generics.params);
|
||||||
|
|
||||||
|
let (pre_result, result_fut) = match asyncness {
|
||||||
|
true => (
|
||||||
|
quote! {},
|
||||||
|
quote! { Self::call::<#type_params>(#args_head #args_tail) },
|
||||||
|
),
|
||||||
|
false => (
|
||||||
|
quote! { let result_fut = Self::call::<#type_params>(#args_head #args_tail); },
|
||||||
|
quote! { result_fut },
|
||||||
|
),
|
||||||
|
};
|
||||||
let result_wrapper = match is_result(&f.sig.output) {
|
let result_wrapper = match is_result(&f.sig.output) {
|
||||||
true => quote! {},
|
true => quote! {},
|
||||||
false => quote! { let result = Ok(result); },
|
false => quote! { let result = Ok(result); },
|
||||||
|
@ -195,8 +207,9 @@ fn codegen_v8_async(
|
||||||
state.get_error_class_fn
|
state.get_error_class_fn
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#pre_result
|
||||||
#core::_ops::queue_async_op(scope, async move {
|
#core::_ops::queue_async_op(scope, async move {
|
||||||
let result = Self::call::<#type_params>(#args_head #args_tail).await;
|
let result = #result_fut.await;
|
||||||
#result_wrapper
|
#result_wrapper
|
||||||
(promise_id, op_id, #core::_ops::to_op_result(get_class, result))
|
(promise_id, op_id, #core::_ops::to_op_result(get_class, result))
|
||||||
});
|
});
|
||||||
|
@ -387,6 +400,10 @@ fn is_handle_scope(arg: &syn::FnArg) -> bool {
|
||||||
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope < 'a >")
|
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope < 'a >")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_future(ty: impl ToTokens) -> bool {
|
||||||
|
tokens(&ty).contains("impl Future < Output =")
|
||||||
|
}
|
||||||
|
|
||||||
fn tokens(x: impl ToTokens) -> String {
|
fn tokens(x: impl ToTokens) -> String {
|
||||||
x.to_token_stream().to_string()
|
x.to_token_stream().to_string()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue