fn codegen_v8_async(
  core: &TokenStream2,
  f: &syn::ItemFn,
  _margs: MacroArgs,
  asyncness: bool,
) -> TokenStream2 {
  let arg0 = f.sig.inputs.first();
  let uses_opstate = arg0.map(is_rc_refcell_opstate).unwrap_or_default();
  let args_head = if uses_opstate {
    quote! { state, }
  } else {
    quote! {}
  };
  let rust_i0 = if uses_opstate { 1 } else { 0 };
  let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
  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) {
    true => quote! {},
    false => quote! { let result = Ok(result); },
  };

  quote! {
    use #core::futures::FutureExt;
    // SAFETY: #core guarantees args.data() is a v8 External pointing to an OpCtx for the isolates lifetime
    let ctx = unsafe {
      &*(#core::v8::Local::<#core::v8::External>::cast(args.data().unwrap_unchecked()).value()
      as *const #core::_ops::OpCtx)
    };
    let op_id = ctx.id;

    let promise_id = args.get(0);
    let promise_id = #core::v8::Local::<#core::v8::Integer>::try_from(promise_id)
      .map(|l| l.value() as #core::PromiseId)
      .map_err(#core::anyhow::Error::from);
    // Fail if promise id invalid (not an int)
    let promise_id: #core::PromiseId = match promise_id {
      Ok(promise_id) => promise_id,
      Err(err) => {
        #core::_ops::throw_type_error(scope, format!("invalid promise id: {}", err));
        return;
      }
    };

    #arg_decls

    let state = ctx.state.clone();

    // Track async call & get copy of get_error_class_fn
    let get_class = {
      let state = state.borrow();
      state.tracker.track_async(op_id);
      state.get_error_class_fn
    };

    #pre_result
    #core::_ops::queue_async_op(scope, async move {
      let result = #result_fut.await;
      #result_wrapper
      (promise_id, op_id, #core::_ops::to_op_result(get_class, result))
    });
  }
}
/// Generate the body of a v8 func for an async op
Generate the body of a v8 func for an async op
Generate the body of a v8 func for an async op
fn codegen_v8_async(
  core: &TokenStream2,
  f: &syn::ItemFn,
  _margs: MacroArgs,
  asyncness: bool,
) -> TokenStream2 {
  let arg0 = f.sig.inputs.first();
  let uses_opstate = arg0.map(is_rc_refcell_opstate).unwrap_or_default();
  let args_head = if uses_opstate {
    quote! { state, }
  } else {
    quote! {}
  };
  let rust_i0 = if uses_opstate { 1 } else { 0 };
  let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
  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) {
    true => quote! {},
    false => quote! { let result = Ok(result); },
  };
  quote! {
    use #core::futures::FutureExt;
    // SAFETY: #core guarantees args.data() is a v8 External pointing to an OpCtx for the isolates lifetime
    let ctx = unsafe {
      &*(#core::v8::Local::<#core::v8::External>::cast(args.data().unwrap_unchecked()).value()
      as *const #core::_ops::OpCtx)
    };
    let op_id = ctx.id;
    let promise_id = args.get(0);
    let promise_id = #core::v8::Local::<#core::v8::Integer>::try_from(promise_id)
      .map(|l| l.value() as #core::PromiseId)
      .map_err(#core::anyhow::Error::from);
    // Fail if promise id invalid (not an int)
    let promise_id: #core::PromiseId = match promise_id {
      Ok(promise_id) => promise_id,
      Err(err) => {
        #core::_ops::throw_type_error(scope, format!("invalid promise id: {}", err));
        return;
      }
    };
    #arg_decls
    let state = ctx.state.clone();
    // Track async call & get copy of get_error_class_fn
    let get_class = {
      let state = state.borrow();
      state.tracker.track_async(op_id);
      state.get_error_class_fn
    };
    #pre_result
    #core::_ops::queue_async_op(scope, async move {
      let result = #result_fut.await;
      #result_wrapper
      (promise_id, op_id, #core::_ops::to_op_result(get_class, result))
    });
  }
}
fn codegen_v8_async ( core : & TokenStream2 , f : & syn :: ItemFn , _margs : MacroArgs , asyncness : bool , ) -> TokenStream2 { let arg0 = f . sig . inputs . first ( ) ; let uses_opstate = arg0 . map ( is_rc_refcell_opstate ) . unwrap_or_default ( ) ; let args_head = if uses_opstate { quote ! { state } } else { quote ! { } } ; let rust_i0 = if uses_opstate { 1 } else { 0 } ; let ( arg_decls , args_tail ) = codegen_args ( core , f , rust_i0 , 1 ) ; 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 ) { true => quote ! { } , false => quote ! { let result Ok ( result ) } , } ; quote ! { use core futures FutureExt // SAFETY: #core guarantees args.data() is a v8 External pointing to an OpCtx for the isolates lifetime let ctx unsafe { ( core v8 Local core v8 External cast ( args data ( ) unwrap_unchecked ( ) ) value ( ) as const core ops OpCtx ) } let op_id ctx id let promise_id args get ( 0 ) let promise_id core v8 Local core v8 Integer try_from ( promise_id ) map ( l l value ( ) as core PromiseId ) map_err ( core anyhow Error from ) // Fail if promise id invalid (not an int) let promise_id core PromiseId match promise_id { Ok ( promise_id ) promise_id Err ( err ) { core ops throw_type_error ( scope format ( " " err ) ) return } } arg_decls let state ctx state clone ( ) // Track async call & get copy of get_error_class_fn let get_class { let state state borrow ( ) state tracker track_async ( op_id ) state get_error_class_fn } pre_result core ops queue_async_op ( scope async move { let result result_fut await result_wrapper ( promise_id op_id core ops to_op_result ( get_class result ) ) } ) } }
Generate the body of a v8 func for an async op
Generate the body of a v8 func for an async op
core
&TokenStream2
f
&syn::ItemFn
_margs
MacroArgs
asyncness
bool
core
&TokenStream2
f
&syn::ItemFn
_margs
MacroArgs
asyncness
bool