1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

use core::panic;
use once_cell::sync::Lazy;
use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_crate::crate_name;
use proc_macro_crate::FoundCrate;
use quote::format_ident;
use quote::quote;
use quote::ToTokens;
use regex::Regex;
use std::collections::HashMap;
use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::FnArg;
use syn::GenericParam;
use syn::Ident;

#[cfg(test)]
mod tests;

// Identifier to the `deno_core` crate.
//
// If macro called in deno_core, `crate` is used.
// If macro called outside deno_core, `deno_core` OR the renamed
// version from Cargo.toml is used.
fn core_import() -> TokenStream2 {
  let found_crate =
    crate_name("deno_core").expect("deno_core not present in `Cargo.toml`");

  match found_crate {
    FoundCrate::Itself => {
      // TODO(@littledivy): This won't work for `deno_core` examples
      // since `crate` does not refer to `deno_core`.
      // examples must re-export deno_core to make this work
      // until Span inspection APIs are stabalized.
      //
      // https://github.com/rust-lang/rust/issues/54725
      quote!(crate)
    }
    FoundCrate::Name(name) => {
      let ident = Ident::new(&name, Span::call_site());
      quote!(#ident)
    }
  }
}

#[derive(Copy, Clone, Debug, Default)]
struct MacroArgs {
  is_unstable: bool,
  is_v8: bool,
  must_be_fast: bool,
  deferred: bool,
}

impl syn::parse::Parse for MacroArgs {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    let vars =
      syn::punctuated::Punctuated::<Ident, syn::Token![,]>::parse_terminated(
        input,
      )?;
    let vars: Vec<_> = vars.iter().map(Ident::to_string).collect();
    let vars: Vec<_> = vars.iter().map(String::as_str).collect();
    for var in vars.iter() {
      if !["unstable", "v8", "fast", "deferred"].contains(var) {
        return Err(syn::Error::new(
          input.span(),
          "Ops expect #[op] or #[op(unstable)]",
        ));
      }
    }
    Ok(Self {
      is_unstable: vars.contains(&"unstable"),
      is_v8: vars.contains(&"v8"),
      must_be_fast: vars.contains(&"fast"),
      deferred: vars.contains(&"deferred"),
    })
  }
}

#[proc_macro_attribute]
pub fn op(attr: TokenStream, item: TokenStream) -> TokenStream {
  let margs = syn::parse_macro_input!(attr as MacroArgs);
  let MacroArgs {
    is_unstable,
    is_v8,
    must_be_fast,
    deferred,
  } = margs;
  let func = syn::parse::<syn::ItemFn>(item).expect("expected a function");
  let name = &func.sig.ident;
  let mut generics = func.sig.generics.clone();
  let scope_lifetime =
    syn::LifetimeDef::new(syn::Lifetime::new("'scope", Span::call_site()));
  if !generics.lifetimes().any(|def| *def == scope_lifetime) {
    generics
      .params
      .push(syn::GenericParam::Lifetime(scope_lifetime));
  }
  let type_params = exclude_lifetime_params(&func.sig.generics.params);
  let where_clause = &func.sig.generics.where_clause;

  // Preserve the original func as op_foo::call()
  let original_func = {
    let mut func = func.clone();
    func.sig.ident = quote::format_ident!("call");
    func
  };

  let core = core_import();

  let asyncness = func.sig.asyncness.is_some();
  let is_async = asyncness || is_future(&func.sig.output);

  // First generate fast call bindings to opt-in to error handling in slow call
  let (has_fallible_fast_call, fast_impl, fast_field) =
    codegen_fast_impl(&core, &func, name, is_async, must_be_fast);

  let v8_body = if is_async {
    codegen_v8_async(&core, &func, margs, asyncness, deferred)
  } else {
    codegen_v8_sync(&core, &func, margs, has_fallible_fast_call)
  };

  let docline = format!("Use `{name}::decl()` to get an op-declaration");
  // Generate wrapper
  quote! {
    #[allow(non_camel_case_types)]
    #[doc="Auto-generated by `deno_ops`, i.e: `#[op]`"]
    #[doc=""]
    #[doc=#docline]
    #[doc="you can include in a `deno_core::Extension`."]
    pub struct #name;

    #[doc(hidden)]
    impl #name {
      pub fn name() -> &'static str {
        stringify!(#name)
      }

      pub fn v8_fn_ptr #generics () -> #core::v8::FunctionCallback #where_clause {
        use #core::v8::MapFnTo;
        Self::v8_func::<#type_params>.map_fn_to()
      }

      pub fn decl #generics () -> #core::OpDecl #where_clause {
        #core::OpDecl {
          name: Self::name(),
          v8_fn_ptr: Self::v8_fn_ptr::<#type_params>(),
          enabled: true,
          fast_fn: #fast_field,
          is_async: #is_async,
          is_unstable: #is_unstable,
          is_v8: #is_v8,
        }
      }

      #[inline]
      #[allow(clippy::too_many_arguments)]
      #original_func

      pub fn v8_func #generics (
        scope: &mut #core::v8::HandleScope<'scope>,
        args: #core::v8::FunctionCallbackArguments,
        mut rv: #core::v8::ReturnValue,
      ) #where_clause {
        #v8_body
      }
    }

    #fast_impl
  }.into()
}

/// Generate the body of a v8 func for an async op
fn codegen_v8_async(
  core: &TokenStream2,
  f: &syn::ItemFn,
  margs: MacroArgs,
  asyncness: bool,
  deferred: bool,
) -> TokenStream2 {
  let MacroArgs { is_v8, .. } = margs;
  let special_args = f
    .sig
    .inputs
    .iter()
    .map_while(|a| {
      (if is_v8 { scope_arg(a) } else { None }).or_else(|| opstate_arg(a))
    })
    .collect::<Vec<_>>();
  let rust_i0 = special_args.len();
  let args_head = special_args.into_iter().collect::<TokenStream2>();

  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, mut result_fut) = match asyncness {
    true => (
      quote! {},
      quote! { Self::call::<#type_params>(#args_head #args_tail).await; },
    ),
    false => (
      quote! { let result_fut = Self::call::<#type_params>(#args_head #args_tail); },
      quote! { result_fut.await; },
    ),
  };
  let result_wrapper = match is_result(&f.sig.output) {
    true => {
      // Support `Result<impl Future<Output = Result<T, AnyError>> + 'static, AnyError>`
      if !asyncness {
        result_fut = quote! { result_fut; };
        quote! {
          let result = match result {
            Ok(fut) => fut.await,
            Err(e) => return (context, promise_id, op_id, #core::_ops::to_op_result::<()>(get_class, Err(e))),
          };
        }
      } else {
        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
    };

    let context = {
      let local = scope.get_current_context();
      #core::v8::Global::new(scope, local)
    };

    #pre_result
    #core::_ops::queue_async_op(state, scope, #deferred, async move {
      let result = #result_fut
      #result_wrapper
      (context, promise_id, op_id, #core::_ops::to_op_result(get_class, result))
    });
  }
}

fn scope_arg(arg: &FnArg) -> Option<TokenStream2> {
  if is_handle_scope(arg) {
    Some(quote! { scope, })
  } else {
    None
  }
}

fn opstate_arg(arg: &FnArg) -> Option<TokenStream2> {
  match arg {
    arg if is_rc_refcell_opstate(arg) => Some(quote! { ctx.state.clone(), }),
    arg if is_mut_ref_opstate(arg) => {
      Some(quote! { &mut std::cell::RefCell::borrow_mut(&ctx.state), })
    }
    _ => None,
  }
}

fn codegen_fast_impl(
  core: &TokenStream2,
  f: &syn::ItemFn,
  name: &syn::Ident,
  is_async: bool,
  must_be_fast: bool,
) -> (bool, TokenStream2, TokenStream2) {
  if is_async {
    if must_be_fast {
      panic!("async op cannot be a fast api. enforced by #[op(fast)]")
    }
    return (false, quote! {}, quote! { None });
  }
  let fast_info = can_be_fast_api(core, f);
  if must_be_fast && fast_info.is_none() {
    panic!("op cannot be a fast api. enforced by #[op(fast)]")
  }
  if !is_async {
    if let Some(FastApiSyn {
      args,
      ret,
      use_op_state,
      use_fast_cb_opts,
      v8_values,
      returns_result,
      slices,
    }) = fast_info
    {
      let offset = if use_op_state { 1 } else { 0 };
      let mut inputs = f
        .sig
        .inputs
        .iter()
        .skip(offset)
        .enumerate()
        .map(|(idx, arg)| {
          let ident = match arg {
            FnArg::Receiver(_) => unreachable!(),
            FnArg::Typed(t) => match &*t.pat {
              syn::Pat::Ident(i) => format_ident!("{}", i.ident),
              _ => unreachable!(),
            },
          };
          if let Some(ty) = slices.get(&(idx + offset)) {
            return quote! { #ident: *const #core::v8::fast_api::FastApiTypedArray< #ty > };
          }
          if use_fast_cb_opts && idx + offset == f.sig.inputs.len() - 1 {
            return quote! { fast_api_callback_options: *mut #core::v8::fast_api::FastApiCallbackOptions };
          }
          if v8_values.contains(&idx) {
            return quote! { #ident: #core::v8::Local < #core::v8::Value > };
          }
          quote!(#arg)
        })
        .collect::<Vec<_>>();
      if (!slices.is_empty() || use_op_state || returns_result)
        && !use_fast_cb_opts
      {
        inputs.push(quote! { fast_api_callback_options: *mut #core::v8::fast_api::FastApiCallbackOptions });
      }
      let input_idents = f
        .sig
        .inputs
        .iter()
        .enumerate()
        .map(|(idx, a)| {
          let ident = match a {
            FnArg::Receiver(_) => unreachable!(),
            FnArg::Typed(t) => match &*t.pat {
              syn::Pat::Ident(i) => format_ident!("{}", i.ident),
              _ => unreachable!(),
            },
          };
          if slices.get(&idx).is_some() {
            return quote! {
              match unsafe { &* #ident }.get_storage_if_aligned() {
                Some(s) => s,
                None => {
                  unsafe { &mut * fast_api_callback_options }.fallback = true;
                  return Default::default();
                },
              }
            };
          }
          if use_fast_cb_opts && idx == f.sig.inputs.len() - 1 {
            return quote! { Some(unsafe { &mut * fast_api_callback_options }) };
          }
          if v8_values.contains(&idx) {
            return quote! {
              #core::serde_v8::Value {
                v8_value: #ident,
              }
            };
          }
          quote! { #ident }
        })
        .collect::<Vec<_>>();
      let generics = &f.sig.generics;
      let (impl_generics, ty_generics, where_clause) =
        generics.split_for_impl();
      let type_params = exclude_lifetime_params(&f.sig.generics.params);
      let (trampoline, raw_block) = if is_async {
        // TODO(@littledivy): Fast async calls.
        (
          quote! {
            fn func(recv: #core::v8::Local<#core::v8::Object>, __promise_id: u32, #(#inputs),*) {
              // SAFETY: V8 calling convention guarantees that the callback options pointer is non-null.
              let opts: &#core::v8::fast_api::FastApiCallbackOptions = unsafe { &*fast_api_callback_options };
              // SAFETY: data union is always created as the `v8::Local<v8::Value>` version
              let data = unsafe { opts.data.data };
              // SAFETY: #core guarantees data is a v8 External pointing to an OpCtx for the isolates lifetime
              let ctx = unsafe {
                &*(#core::v8::Local::<#core::v8::External>::cast(data).value()
                as *const #core::_ops::OpCtx)
              };
              let op_id = ctx.op_id;
              #core::_ops::queue_async_op(scope, async move {
                let result = Self::call(#args);
                (__promise_id, __op_id, #core::_ops::OpResult::Ok(result))
              });
            }
            func as *const _
          },
          quote! {},
        )
      } else {
        let output = if returns_result {
          get_fast_result_return_type(&f.sig.output)
        } else {
          let output = &f.sig.output;
          quote! { #output }
        };
        let func_name = format_ident!("func_{}", name);
        let op_state_name = if use_op_state {
          input_idents.first().unwrap().clone()
        } else {
          quote! { op_state }
        };
        let recv_decl = if use_op_state || returns_result {
          quote! {
            // SAFETY: V8 calling convention guarantees that the callback options pointer is non-null.
            let opts: &mut #core::v8::fast_api::FastApiCallbackOptions = unsafe { &mut *fast_api_callback_options };
            // SAFETY: data union is always created as the `v8::Local<v8::Value>` version.
            let data = unsafe { opts.data.data };
            // SAFETY: #core guarantees data is a v8 External pointing to an OpCtx for the isolates lifetime
            let ctx = unsafe {
              &*(#core::v8::Local::<#core::v8::External>::cast(data).value()
              as *const #core::_ops::OpCtx)
            };
            let #op_state_name = &mut std::cell::RefCell::borrow_mut(&ctx.state);
          }
        } else {
          quote! {}
        };

        let result_handling = if returns_result {
          quote! {
            match result {
              Ok(result) => {
                result
              },
              Err(err) => {
                #op_state_name.last_fast_op_error.replace(err);
                opts.fallback = true;
                Default::default()
              },
            }
          }
        } else {
          quote! { result }
        };

        (
          quote! {
            fn #func_name #generics (_recv: #core::v8::Local<#core::v8::Object>, #(#inputs),*) #output #where_clause {
              #recv_decl
              let result = #name::call::<#type_params>(#(#input_idents),*);
              #result_handling
            }
          },
          quote! {
            #func_name::<#type_params> as *const _
          },
        )
      };

      let fast_struct = format_ident!("fast_{}", name);
      let (type_params, ty_generics, struct_generics) =
        if type_params.is_empty() {
          (quote! { () }, quote! {}, quote! {})
        } else {
          (
            quote! { #type_params },
            quote! { #ty_generics },
            quote! { ::<#type_params> },
          )
        };
      return (
        returns_result,
        quote! {
          #[allow(non_camel_case_types)]
          #[doc(hidden)]
          struct #fast_struct #ty_generics {
            _phantom: ::std::marker::PhantomData<#type_params>,
          }
          #trampoline
          impl #impl_generics #core::v8::fast_api::FastFunction for #fast_struct #ty_generics #where_clause {
            fn function(&self) -> *const ::std::ffi::c_void  {
              #raw_block
            }
            fn args(&self) -> &'static [#core::v8::fast_api::Type] {
              &[ #args ]
            }
            fn return_type(&self) -> #core::v8::fast_api::CType {
              #ret
            }
          }
        },
        quote! { Some(Box::new(#fast_struct #struct_generics { _phantom: ::std::marker::PhantomData })) },
      );
    }
  }

  // Default impl to satisfy generic bounds for non-fast ops
  (false, quote! {}, quote! { None })
}

/// Generate the body of a v8 func for a sync op
fn codegen_v8_sync(
  core: &TokenStream2,
  f: &syn::ItemFn,
  margs: MacroArgs,
  has_fallible_fast_call: bool,
) -> TokenStream2 {
  let MacroArgs { is_v8, .. } = margs;
  let special_args = f
    .sig
    .inputs
    .iter()
    .map_while(|a| {
      (if is_v8 { scope_arg(a) } else { None }).or_else(|| opstate_arg(a))
    })
    .collect::<Vec<_>>();
  let rust_i0 = special_args.len();
  let args_head = special_args.into_iter().collect::<TokenStream2>();
  let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 0);
  let ret = codegen_sync_ret(core, &f.sig.output);
  let type_params = exclude_lifetime_params(&f.sig.generics.params);

  let fast_error_handler = if has_fallible_fast_call {
    quote! {
      {
        let op_state = &mut std::cell::RefCell::borrow_mut(&ctx.state);
        if let Some(err) = op_state.last_fast_op_error.take() {
          let exception = #core::error::to_v8_error(scope, op_state.get_error_class_fn, &err);
          scope.throw_exception(exception);
          return;
        }
      }
    }
  } else {
    quote! {}
  };

  quote! {
    // 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)
    };

    #fast_error_handler
    #arg_decls

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

    // use RefCell::borrow instead of state.borrow to avoid clash with std::borrow::Borrow
    let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
    op_state.tracker.track_sync(ctx.id);

    #ret
  }
}

struct FastApiSyn {
  args: TokenStream2,
  ret: TokenStream2,
  use_op_state: bool,
  use_fast_cb_opts: bool,
  v8_values: Vec<usize>,
  returns_result: bool,
  slices: HashMap<usize, TokenStream2>,
}

fn can_be_fast_api(core: &TokenStream2, f: &syn::ItemFn) -> Option<FastApiSyn> {
  let inputs = &f.sig.inputs;
  let mut returns_result = false;
  let ret = match &f.sig.output {
    syn::ReturnType::Default => quote!(#core::v8::fast_api::CType::Void),
    syn::ReturnType::Type(_, ty) => match is_fast_return_type(core, ty) {
      Some((ret, is_result)) => {
        returns_result = is_result;
        ret
      }
      None => return None,
    },
  };

  let mut use_op_state = false;
  let mut use_fast_cb_opts = false;
  let mut v8_values = Vec::new();
  let mut slices = HashMap::new();
  let mut args = vec![quote! { #core::v8::fast_api::Type::V8Value }];
  for (pos, input) in inputs.iter().enumerate() {
    if pos == inputs.len() - 1 && is_optional_fast_callback_option(input) {
      use_fast_cb_opts = true;
      continue;
    }

    if pos == 0 && is_mut_ref_opstate(input) {
      use_op_state = true;
      continue;
    }

    let ty = match input {
      syn::FnArg::Typed(pat) => &pat.ty,
      _ => unreachable!(),
    };

    if let Some(arg) = is_fast_v8_value(core, ty) {
      args.push(arg);
      v8_values.push(pos);
    } else {
      match is_fast_scalar(core, ty, false) {
        None => match is_fast_arg_sequence(core, ty) {
          Some(arg) => {
            args.push(arg);
          }
          None => match is_ref_slice(&ty) {
            Some(SliceType::U32Mut) => {
              args.push(quote! { #core::v8::fast_api::Type::TypedArray(#core::v8::fast_api::CType::Uint32) });
              slices.insert(pos, quote!(u32));
            }
            Some(_) => {
              args.push(quote! { #core::v8::fast_api::Type::TypedArray(#core::v8::fast_api::CType::Uint8) });
              slices.insert(pos, quote!(u8));
            }
            // early return, this function cannot be a fast call.
            None => return None,
          },
        },
        Some(arg) => {
          args.push(arg);
        }
      }
    }
  }

  if use_fast_cb_opts || use_op_state {
    // Push CallbackOptions into args; it must be the last argument.
    args.push(quote! { #core::v8::fast_api::Type::CallbackOptions });
  }

  let args = args
    .iter()
    .map(|arg| format!("{}", arg))
    .collect::<Vec<_>>()
    .join(", ");
  Some(FastApiSyn {
    args: args.parse().unwrap(),
    ret,
    use_op_state,
    slices,
    v8_values,
    use_fast_cb_opts,
    returns_result,
  })
}

// A v8::Local<v8::Array> or FastApiTypedArray<T>
fn is_fast_arg_sequence(
  core: &TokenStream2,
  ty: impl ToTokens,
) -> Option<TokenStream2> {
  // TODO(@littledivy): Make `v8::` parts optional.
  if is_fast_typed_array(&ty) {
    return Some(
      quote! { #core::v8::fast_api::Type::TypedArray(#core::v8::fast_api::CType::Uint32) },
    );
  }
  if is_local_array(&ty) {
    return Some(
      quote! { #core::v8::fast_api::Type::Sequence(#core::v8::fast_api::CType::Void) },
    );
  }
  None
}

fn is_fast_v8_value(
  core: &TokenStream2,
  arg: impl ToTokens,
) -> Option<TokenStream2> {
  if tokens(&arg).contains("serde_v8 :: Value") {
    return Some(quote! { #core::v8::fast_api::Type::V8Value });
  }
  None
}

fn is_local_array(arg: impl ToTokens) -> bool {
  static RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^v8::Local<v8::Array>$").unwrap());
  RE.is_match(&tokens(arg))
}

fn is_fast_typed_array(arg: impl ToTokens) -> bool {
  static RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#": (?:deno_core :: )?FastApiTypedArray$"#).unwrap()
  });
  RE.is_match(&tokens(arg))
}

fn is_fast_return_type(
  core: &TokenStream2,
  ty: impl ToTokens,
) -> Option<(TokenStream2, bool)> {
  if is_result(&ty) {
    if tokens(&ty).contains("Result < u32") || is_resource_id(&ty) {
      Some((quote! { #core::v8::fast_api::CType::Uint32 }, true))
    } else if tokens(&ty).contains("Result < i32") {
      Some((quote! { #core::v8::fast_api::CType::Int32 }, true))
    } else if tokens(&ty).contains("Result < f32") {
      Some((quote! { #core::v8::fast_api::CType::Float32 }, true))
    } else if tokens(&ty).contains("Result < f64") {
      Some((quote! { #core::v8::fast_api::CType::Float64 }, true))
    } else if tokens(&ty).contains("Result < bool") {
      Some((quote! { #core::v8::fast_api::CType::Bool }, true))
    } else if tokens(&ty).contains("Result < ()") {
      Some((quote! { #core::v8::fast_api::CType::Void }, true))
    } else {
      None
    }
  } else {
    is_fast_scalar(core, ty, true).map(|s| (s, false))
  }
}

fn get_fast_result_return_type(ty: impl ToTokens) -> TokenStream2 {
  if tokens(&ty).contains("Result < u32") || is_resource_id(&ty) {
    quote! { -> u32 }
  } else if tokens(&ty).contains("Result < i32") {
    quote! { -> i32 }
  } else if tokens(&ty).contains("Result < f32") {
    quote! { -> f32 }
  } else if tokens(&ty).contains("Result < f64") {
    quote! { -> f64 }
  } else if tokens(&ty).contains("Result < bool") {
    quote! { -> bool }
  } else if tokens(&ty).contains("Result < ()") {
    quote! {}
  } else {
    unreachable!()
  }
}

fn is_fast_scalar(
  core: &TokenStream2,
  ty: impl ToTokens,
  is_ret: bool,
) -> Option<TokenStream2> {
  let cty = if is_ret {
    quote! { CType }
  } else {
    quote! { Type }
  };
  if is_resource_id(&ty) {
    return Some(quote! { #core::v8::fast_api::#cty::Uint32 });
  }
  if is_void(&ty) {
    return Some(quote! { #core::v8::fast_api::#cty::Void });
  }
  // TODO(@littledivy): Support u8, i8, u16, i16 by casting.
  match tokens(&ty).as_str() {
    "u32" => Some(quote! { #core::v8::fast_api::#cty::Uint32 }),
    "i32" => Some(quote! { #core::v8::fast_api::#cty::Int32 }),
    "f32" => Some(quote! { #core::v8::fast_api::#cty::Float32 }),
    "f64" => Some(quote! { #core::v8::fast_api::#cty::Float64 }),
    "bool" => Some(quote! { #core::v8::fast_api::#cty::Bool }),
    _ => None,
  }
}

fn codegen_args(
  core: &TokenStream2,
  f: &syn::ItemFn,
  rust_i0: usize, // Index of first generic arg in rust
  v8_i0: usize,   // Index of first generic arg in v8/js
) -> (TokenStream2, TokenStream2) {
  let inputs = &f.sig.inputs.iter().skip(rust_i0).enumerate();
  let ident_seq: TokenStream2 = inputs
    .clone()
    .map(|(i, _)| format!("arg_{i}"))
    .collect::<Vec<_>>()
    .join(", ")
    .parse()
    .unwrap();
  let decls: TokenStream2 = inputs
    .clone()
    .map(|(i, arg)| {
      codegen_arg(core, arg, format!("arg_{i}").as_ref(), v8_i0 + i)
    })
    .collect();
  (decls, ident_seq)
}

fn codegen_arg(
  core: &TokenStream2,
  arg: &syn::FnArg,
  name: &str,
  idx: usize,
) -> TokenStream2 {
  let ident = quote::format_ident!("{name}");
  let (pat, ty) = match arg {
    syn::FnArg::Typed(pat) => {
      if is_optional_fast_callback_option(&pat.ty) {
        return quote! { let #ident = None; };
      }
      (&pat.pat, &pat.ty)
    }
    _ => unreachable!(),
  };
  // Fast path if arg should be skipped
  if matches!(**pat, syn::Pat::Wild(_)) {
    return quote! { let #ident = (); };
  }
  // Fast path for `String`
  if is_string(&**ty) {
    return quote! {
      let #ident = match #core::v8::Local::<#core::v8::String>::try_from(args.get(#idx as i32)) {
        Ok(v8_string) => #core::serde_v8::to_utf8(v8_string, scope),
        Err(_) => {
          return #core::_ops::throw_type_error(scope, format!("Expected string at position {}", #idx));
        }
      };
    };
  }
  // Fast path for `Option<String>`
  if is_option_string(&**ty) {
    return quote! {
      let #ident = match #core::v8::Local::<#core::v8::String>::try_from(args.get(#idx as i32)) {
        Ok(v8_string) => Some(#core::serde_v8::to_utf8(v8_string, scope)),
        Err(_) => None
      };
    };
  }
  // Fast path for &/&mut [u8] and &/&mut [u32]
  match is_ref_slice(&**ty) {
    None => {}
    Some(SliceType::U32Mut) => {
      let blck = codegen_u32_mut_slice(core, idx);
      return quote! {
        let #ident = #blck;
      };
    }
    Some(_) => {
      let blck = codegen_u8_slice(core, idx);
      return quote! {
        let #ident = #blck;
      };
    }
  }
  // Otherwise deserialize it via serde_v8
  quote! {
    let #ident = args.get(#idx as i32);
    let #ident = match #core::serde_v8::from_v8(scope, #ident) {
      Ok(v) => v,
      Err(err) => {
        let msg = format!("Error parsing args at position {}: {}", #idx, #core::anyhow::Error::from(err));
        return #core::_ops::throw_type_error(scope, msg);
      }
    };
  }
}

fn codegen_u8_slice(core: &TokenStream2, idx: usize) -> TokenStream2 {
  quote! {{
    let value = args.get(#idx as i32);
    match #core::v8::Local::<#core::v8::ArrayBuffer>::try_from(value) {
      Ok(b) => {
        let store = b.data() as *mut u8;
        // SAFETY: rust guarantees that lifetime of slice is no longer than the call.
        unsafe { ::std::slice::from_raw_parts_mut(store, b.byte_length()) }
      },
      Err(_) => {
        if let Ok(view) = #core::v8::Local::<#core::v8::ArrayBufferView>::try_from(value) {
          let (offset, len) = (view.byte_offset(), view.byte_length());
          let buffer = match view.buffer(scope) {
              Some(v) => v,
              None => {
                return #core::_ops::throw_type_error(scope, format!("Expected ArrayBufferView at position {}", #idx));
              }
          };
          let store = buffer.data() as *mut u8;
          // SAFETY: rust guarantees that lifetime of slice is no longer than the call.
          unsafe { ::std::slice::from_raw_parts_mut(store.add(offset), len) }
        } else {
          return #core::_ops::throw_type_error(scope, format!("Expected ArrayBufferView at position {}", #idx));
        }
      }
    }}
  }
}

fn codegen_u32_mut_slice(core: &TokenStream2, idx: usize) -> TokenStream2 {
  quote! {
    if let Ok(view) = #core::v8::Local::<#core::v8::Uint32Array>::try_from(args.get(#idx as i32)) {
      let (offset, len) = (view.byte_offset(), view.byte_length());
      let buffer = match view.buffer(scope) {
          Some(v) => v,
          None => {
            return #core::_ops::throw_type_error(scope, format!("Expected Uint32Array at position {}", #idx));
          }
      };
      let store = buffer.data() as *mut u8;
      // SAFETY: buffer from Uint32Array. Rust guarantees that lifetime of slice is no longer than the call.
      unsafe { ::std::slice::from_raw_parts_mut(store.add(offset) as *mut u32, len / 4) }
    } else {
      return #core::_ops::throw_type_error(scope, format!("Expected Uint32Array at position {}", #idx));
    }
  }
}

fn codegen_sync_ret(
  core: &TokenStream2,
  output: &syn::ReturnType,
) -> TokenStream2 {
  if is_void(output) {
    return quote! {};
  }

  if is_u32_rv(output) {
    return quote! {
      rv.set_uint32(result as u32);
    };
  }

  // Optimize Result<(), Err> to skip serde_v8 when Ok(...)
  let ok_block = if is_unit_result(output) {
    quote! {}
  } else if is_u32_rv_result(output) {
    quote! {
      rv.set_uint32(result as u32);
    }
  } else {
    quote! {
      match #core::serde_v8::to_v8(scope, result) {
        Ok(ret) => rv.set(ret),
        Err(err) => #core::_ops::throw_type_error(
          scope,
          format!("Error serializing return: {}", #core::anyhow::Error::from(err)),
        ),
      };
    }
  };

  if !is_result(output) {
    return ok_block;
  }

  quote! {
    match result {
      Ok(result) => {
        #ok_block
      },
      Err(err) => {
        let exception = #core::error::to_v8_error(scope, op_state.get_error_class_fn, &err);
        scope.throw_exception(exception);
      },
    };
  }
}

fn is_void(ty: impl ToTokens) -> bool {
  tokens(ty).is_empty()
}

fn is_result(ty: impl ToTokens) -> bool {
  let tokens = tokens(ty);
  if tokens.trim_start_matches("-> ").starts_with("Result <") {
    return true;
  }
  // Detect `io::Result<...>`, `anyhow::Result<...>`, etc...
  // i.e: Result aliases/shorthands which are unfortunately "opaque" at macro-time
  match tokens.find(":: Result <") {
    Some(idx) => !tokens.split_at(idx).0.contains('<'),
    None => false,
  }
}

fn is_string(ty: impl ToTokens) -> bool {
  tokens(ty) == "String"
}

fn is_option_string(ty: impl ToTokens) -> bool {
  tokens(ty) == "Option < String >"
}

enum SliceType {
  U8,
  U8Mut,
  U32Mut,
}

fn is_ref_slice(ty: impl ToTokens) -> Option<SliceType> {
  if is_u8_slice(&ty) {
    return Some(SliceType::U8);
  }
  if is_u8_slice_mut(&ty) {
    return Some(SliceType::U8Mut);
  }
  if is_u32_slice_mut(&ty) {
    return Some(SliceType::U32Mut);
  }
  None
}

fn is_u8_slice(ty: impl ToTokens) -> bool {
  tokens(ty) == "& [u8]"
}

fn is_u8_slice_mut(ty: impl ToTokens) -> bool {
  tokens(ty) == "& mut [u8]"
}

fn is_u32_slice_mut(ty: impl ToTokens) -> bool {
  tokens(ty) == "& mut [u32]"
}

fn is_optional_fast_callback_option(ty: impl ToTokens) -> bool {
  tokens(&ty).contains("Option < & mut FastApiCallbackOptions")
}

/// Detects if the type can be set using `rv.set_uint32` fast path
fn is_u32_rv(ty: impl ToTokens) -> bool {
  ["u32", "u8", "u16"].iter().any(|&s| tokens(&ty) == s) || is_resource_id(&ty)
}

/// Detects if the type is of the format Result<u32/u8/u16, Err>
fn is_u32_rv_result(ty: impl ToTokens) -> bool {
  is_result(&ty)
    && (tokens(&ty).contains("Result < u32")
      || tokens(&ty).contains("Result < u8")
      || tokens(&ty).contains("Result < u16")
      || is_resource_id(&ty))
}

/// Detects if a type is of the form Result<(), Err>
fn is_unit_result(ty: impl ToTokens) -> bool {
  is_result(&ty) && tokens(&ty).contains("Result < ()")
}

fn is_resource_id(arg: impl ToTokens) -> bool {
  static RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r#": (?:deno_core :: )?ResourceId$"#).unwrap());
  RE.is_match(&tokens(arg))
}

fn is_mut_ref_opstate(arg: impl ToTokens) -> bool {
  static RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r#": & mut (?:deno_core :: )?OpState$"#).unwrap());
  RE.is_match(&tokens(arg))
}

fn is_rc_refcell_opstate(arg: &syn::FnArg) -> bool {
  static RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#": Rc < RefCell < (?:deno_core :: )?OpState > >$"#).unwrap()
  });
  RE.is_match(&tokens(arg))
}

fn is_handle_scope(arg: &syn::FnArg) -> bool {
  static RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r#": & mut (?:deno_core :: )?v8 :: HandleScope(?: < '\w+ >)?$"#)
      .unwrap()
  });
  RE.is_match(&tokens(arg))
}

fn is_future(ty: impl ToTokens) -> bool {
  tokens(&ty).contains("impl Future < Output =")
}

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

fn exclude_lifetime_params(
  generic_params: &Punctuated<GenericParam, Comma>,
) -> Punctuated<GenericParam, Comma> {
  generic_params
    .iter()
    .filter(|t| !tokens(t).starts_with('\''))
    .cloned()
    .collect::<Punctuated<GenericParam, Comma>>()
}