Skip to content

Commit

Permalink
feat(ops): allow passing scope handle to ops (denoland#14574)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed May 12, 2022
1 parent e6142fa commit 3166506
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions ops/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,31 @@ fn codegen_v8_async(core: &TokenStream2, f: &syn::ItemFn) -> TokenStream2 {

/// Generate the body of a v8 func for a sync op
fn codegen_v8_sync(core: &TokenStream2, f: &syn::ItemFn) -> TokenStream2 {
let arg0 = f.sig.inputs.first();
let args = f.sig.inputs.iter().collect::<Vec<_>>();
let mut arg0 = args.first();
let has_scope = match arg0 {
Some(arg0) => is_handle_scope(arg0),
None => false,
};
let rust_i0;
let scope_arg = if has_scope {
arg0 = args.get(1); // Next arg might be OpState.
rust_i0 = 1;
quote! { scope, }
} else {
rust_i0 = 0;
quote! {}
};
let (rust_i0, args_head) = match arg0 {
Some(arg0) if is_rc_refcell_opstate(arg0) => {
(1, quote! { ctx.state.clone(), })
(rust_i0 + 1, quote! { ctx.state.clone(), })
}
Some(arg0) if is_mut_ref_opstate(arg0) => {
(1, quote! { &mut ctx.state.borrow_mut(), })
(rust_i0 + 1, quote! { &mut ctx.state.borrow_mut(), })
}
_ => (0, quote! {}),
_ => (rust_i0, quote! {}),
};

let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 0);
let ret = codegen_sync_ret(core, &f.sig.output);
let type_params = &f.sig.generics.params;
Expand All @@ -207,7 +222,7 @@ fn codegen_v8_sync(core: &TokenStream2, f: &syn::ItemFn) -> TokenStream2 {

#arg_decls

let result = Self::call::<#type_params>(#args_head #args_tail);
let result = Self::call::<#type_params>(#scope_arg #args_head #args_tail);

let op_state = &mut ctx.state.borrow();
op_state.tracker.track_sync(ctx.id);
Expand Down Expand Up @@ -340,6 +355,11 @@ fn is_rc_refcell_opstate(arg: &syn::FnArg) -> bool {
|| tokens(arg).ends_with(": Rc < RefCell < deno_core :: OpState > >")
}

fn is_handle_scope(arg: &syn::FnArg) -> bool {
tokens(arg).ends_with(": & mut v8 :: HandleScope")
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope")
}

fn tokens(x: impl ToTokens) -> String {
x.to_token_stream().to_string()
}

0 comments on commit 3166506

Please sign in to comment.