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
extern crate proc_macro;
use proc_macro::TokenStream;

use quote::{quote, quote_spanned, ToTokens};
use syn::{parse_macro_input, spanned::Spanned};

use janetrs_version::JanetVersion;

mod utils;
use utils::{janet_path_checker, Arg, Args, ArityArgs, JanetVersionArgs};

use crate::utils::ModArgs;

/// Macro that tranforms a high-level Janet function (`fn(&mut [Janet]) -> Janet`)
/// to the thing the Janet C API is expecting (`fn(i32, *mut janetrs::lowlevel::Janet) ->
/// janetrs::lowlevel::Janet`)
///
/// The optional argument `catch` adds code to catch Rust panics and transform them to
/// Janet panics. This argument is ignored if the `std` feature is deactivated.
///
/// The optional argument `arity` adds a arity check for the function. It must receive the
/// kind of arity check. These kinds are `fix`, for fixed arity, and `range`, for ranged
/// or variadic arity. The `fix` kind receives a integer of the number of the parameters
/// the Janet function must have and the `range` kind can receive two arguments, the first
/// one if mandatory while the second one is optional, the first represents the minimal of
/// arguments the Janet function have to receive and the second represents the maximum of
/// arguments the Janet function can receive. If the maximum is not set for the range
/// arity, the maximum check is disabled, allowing variadic arguments.
///
///
/// The optional arg `check_mut_ref` adds a check to see if the function received more
/// than one reference to the same `*mut` pointer. This check is not the default because
/// Janet Types act like types with interior mutability and the check is expensive, but if
/// you want to make sure that your function never receives the same pointer more than
/// once you can use this.
///
///
/// **Usages**:
/// - `#[janet_fn]`
/// - `#[janet_fn(arity(fix(<N>)))]` where `N` is an integer literal
/// - `#[janet_fn(arity(range(<MIN> [, MAX])))]` where `MIN` and `MAX` are integer
///   literals
/// - `#[janet_fn(catch)]`
/// - `#[janet_fn(check_mut_ref)]`
/// - `#[janet_fn(arity(<...>), check_mut_ref)]` Combining both
#[proc_macro_attribute]
pub fn janet_fn(args: TokenStream, input: TokenStream) -> TokenStream {
    let func = parse_macro_input!(input as syn::Item);

    let args = parse_macro_input!(args as Args);

    let mut is_catch = false;

    let extra: Vec<_> = args
        .0
        .iter()
        .map(|arg| match arg {
            Arg::Catch(_) => {
                is_catch = true;
                quote! {}
            },
            Arg::CheckMutRef(_) => quote! {
                if (1..args.len()).any(|i| {
                    let a = args[i - 1];
                    matches!(
                        a.kind(),
                        ::janetrs::JanetType::Abstract
                            | ::janetrs::JanetType::Array
                            | ::janetrs::JanetType::Buffer
                            | ::janetrs::JanetType::CFunction
                            | ::janetrs::JanetType::Fiber
                            | ::janetrs::JanetType::Function
                            | ::janetrs::JanetType::Keyword
                            | ::janetrs::JanetType::Pointer
                            | ::janetrs::JanetType::Symbol
                            | ::janetrs::JanetType::Table
                    ) && args[i..].contains(&a)
                }) {
                    ::janetrs::jpanic!("Received two mutable references as arguments");
                }
            },
            Arg::Arity(ArityArgs::Fix(n), _) => quote! {
                ::janetrs::util::check_fix_arity(args, #n);
            },
            Arg::Arity(ArityArgs::Range(min, None), _) => quote! {
                ::janetrs::util::check_range_arity(args, #min, None);
            },
            Arg::Arity(ArityArgs::Range(min, Some(max)), _) => quote! {
                ::janetrs::util::check_range_arity(args, #min, Some(#max));
            },
        })
        .collect();

    let ts = if let syn::Item::Fn(f) = func {
        let f_clone = f.clone();
        let fun_clone = if cfg!(feature = "std") && is_catch {
            let clone = f.clone();
            let attrs = clone.attrs;
            let vis = clone.vis;
            let sig = clone.sig;
            let block = clone.block;
            quote! {
                #(#attrs)* #vis #sig {
                    ::janetrs::jtry!(#block)
                }
            }
        } else {
            let clone = f.clone();
            quote! { #clone }
        };
        // let f_clone = f.clone();
        let attrs = f.attrs;
        let doc_str = utils::get_doc(attrs.as_ref());
        let vis = f.vis;
        let name = f.sig.ident;
        let name_docstring_ = {
            let mut docstring = name.to_string();
            docstring.push_str("_docstring_");
            syn::Ident::new(&docstring, name.span())
        };
        let name_file_ = {
            let mut file = name.to_string();
            file.push_str("_file_");
            syn::Ident::new(&file, name.span())
        };
        let name_line_ = {
            let mut line = name.to_string();
            line.push_str("_line_");
            syn::Ident::new(&line, name.span())
        };

        // check for inputs
        if matches!(f.sig.inputs.len(), 1) {
            if let Some(syn::FnArg::Typed(syn::PatType { ty, .. })) = f.sig.inputs.first() {
                if let syn::Type::Reference(syn::TypeReference {
                    mutability,
                    ref elem,
                    ..
                }) = **ty
                {
                    if mutability.is_none() {
                        return quote_spanned! {ty.span() => compile_error!("expected argument to be a mutable reference and found a immutable reference");}.into();
                    } else if let syn::Type::Slice(syn::TypeSlice {
                        elem: ref slice, ..
                    }) = **elem
                    {
                        if let syn::Type::Path(syn::TypePath { ref path, .. }) = **slice {
                            if !janet_path_checker(path) {
                                return quote_spanned! {path.span() => compile_error!("expected to be a `janetrs::Janet`");}.into();
                            }
                        } else {
                            return quote_spanned! {slice.span() => compile_error!("expected to be a `janetrs::Janet`");}.into();
                        }
                    } else {
                        return quote_spanned! {elem.span() => compile_error!("expected to be a slice of `janetrs::Janet`");}.into();
                    }
                } else {
                    return quote_spanned! {ty.span() => compile_error!("expected argument to be a mutable reference and found something that is not a reference at all");}.into();
                }
            }

            // check output type
            let output_span = match f.sig.output.clone() {
                syn::ReturnType::Default => f_clone.sig.span(),
                syn::ReturnType::Type(_, ty) => ty.span(),
            };
            if let syn::ReturnType::Type(_, ty) = f.sig.output {
                if let syn::Type::Path(syn::TypePath { ref path, .. }) = *ty {
                    if !janet_path_checker(path) {
                        return quote_spanned! {output_span => compile_error!("expected return type to be `janetrs::Janet`");}.into();
                    }
                } else {
                    return quote_spanned! {output_span => compile_error!("expected return type to be `janetrs::Janet`");}.into();
                }
            } else {
                return quote_spanned! {output_span => compile_error!("expected return type to be `janetrs::Janet`");}.into();
            }

            quote! {
                #[allow(non_upper_case_globals)]
                const #name_docstring_: &str = #doc_str;
                #[allow(non_upper_case_globals)]
                const #name_file_: &str = ::core::concat!(::core::file!(), "\0");
                #[allow(non_upper_case_globals)]
                const #name_line_: u32 = ::core::line!() + 1;
                #(#attrs)* #[no_mangle] #vis unsafe extern "C-unwind" fn #name(argc: i32, argv: *mut ::janetrs::lowlevel::Janet) -> ::janetrs::lowlevel::Janet {
                    #[inline]
                    #fun_clone

                    let args = unsafe { core::slice::from_raw_parts_mut(argv, argc as usize) };
                    let mut args = unsafe { &mut *(args as *mut [::janetrs::lowlevel::Janet] as *mut [::janetrs::Janet])};

                    #(#extra)*

                    #name(args).into()
                }
            }
        } else {
            quote_spanned! {f_clone.sig.inputs.span() => compile_error!("expected exactly one argument of type `&mut [janetrs::Janet]`");}
        }
    } else {
        quote_spanned! {func.span() => compile_error!("expected fn item");}
    };

    ts.into()
}


const CURRENT_JANET: JanetVersion = JanetVersion::current();

/// Conditional Janet Version Gate
///
/// **Usage:** `#[janet_version(<MIN_VERSION>, [MAX_VERSION])]` where `MIN_VERSION` and
/// `MAX_VERSION` are string literals.
///
/// A macro da conditionally includes the `input` if the version of Janet is bigger or
/// equal to the passed minimal version and smaller than the passed maximum version.
///
/// That means that the range is open in the maximum version: [MIN_VERSION, MAX_VERSION).
#[proc_macro_attribute]
pub fn janet_version(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as JanetVersionArgs);

    match parse_args(&args.min_version.value()) {
        Ok(req_min_ver) => {
            if let Some(max_lit) = args.max_version {
                match parse_args(&max_lit.value()) {
                    Ok(req_max_ver) => {
                        if req_min_ver <= CURRENT_JANET && req_max_ver > CURRENT_JANET {
                            input
                        } else {
                            TokenStream::new()
                        }
                    },
                    Err(err) => {
                        let err = format!("invalid string literal: {err}");
                        (quote_spanned! {max_lit.span() => compile_error!(#err);}).into()
                    },
                }
            } else if req_min_ver <= CURRENT_JANET {
                input
            } else {
                TokenStream::new()
            }
        },
        Err(err) => {
            let err = format!("invalid string literal: {err}");
            (quote_spanned! {args.min_version.span() => compile_error!(#err);}).into()
        },
    }
}

fn parse_args(arg: &str) -> Result<JanetVersion, String> {
    let vec_values = arg
        .split('.')
        .map(|e| e.parse::<u32>())
        .collect::<Result<Vec<_>, _>>()
        .map_err(|err| format!("{err}"))?;

    Ok(match &vec_values[..] {
        [major] => JanetVersion::custom(*major, 0, 0),
        [major, minor] => JanetVersion::custom(*major, *minor, 0),
        [major, minor, patch] => JanetVersion::custom(*major, *minor, *patch),
        _ => return Err("Invalid version string".to_string()),
    })
}

/// Conditional Janet Version Gate
///
/// **Usage:** `#[cjvg(<MIN_VERSION>, [MAX_VERSION])]` where `MIN_VERSION` and
/// `MAX_VERSION` are string literals.
///
/// A macro that conditionally includes the `input` if the version of Janet is bigger or
/// equal to the passed minimal version and smaller than the passed maximum version.
///
/// That means that the range is open in the maximum version: [MIN_VERSION, MAX_VERSION).
#[proc_macro_attribute]
pub fn cjvg(args: TokenStream, input: TokenStream) -> TokenStream {
    janet_version(args, input)
}

/// Declare a Janet module.
///
/// This macro can detect and get the documentation from the function, so you just need to
/// pass the function name for Janet and the identifier of the native function.
///
/// # Examples
/// ```ignore
/// use janetrs::{janet_mod, Janet, janet_fn};
///
/// /// (rust/hello)
/// ///
/// /// Rust says hello to you! 🦀
/// #[janet_fn(arity(fix(0)))]
/// fn rust_hello(args: &mut [Janet]) -> Janet {
///     println!("Hello from Rust!");
///     Janet::nil()
/// }
///
/// /// (rust/hi)
/// ///
/// /// I introducing myself to you! 🙆
/// #[janet_fn(arity(fix(0)))]
/// fn hi(args: &mut [Janet]) -> Janet {
///     Janet::from("Hi! My name is GrayJack!")
/// }
///
/// #[janet_fn(arity(fix(0)))]
/// fn no_doc_fn(args: &mut [Janet]) -> Janet {
///     Janet::nil()
/// }
///
/// declare_janet_mod!("rust";
///     {"hello", rust_hello},
///     {"hi", hi},
///     {"no_doc_fn", no_doc_fn, "Using custom docs as string literal"},
/// );
/// ```
#[proc_macro]
pub fn declare_janet_mod(input: TokenStream) -> TokenStream {
    let ModArgs {
        mod_name,
        fn_names,
        fn_ptr_idents,
        fn_doc_idents,
        fn_line_idents,
        fn_file_idents,
        fn_doc_lits,
    } = parse_macro_input!(input as ModArgs);

    let regs_ext = fn_doc_lits.iter().enumerate().map(|(i, doc_lit)| {
        let fn_name = &fn_names[i];
        let fn_ptr_ident = &fn_ptr_idents[i];
        let fn_doc_ident = &fn_doc_idents[i];
        let fn_line_ident = &fn_line_idents[i];
        let fn_file_ident = &fn_file_idents[i];
        if let Some(fn_doc_lit) = doc_lit {
            quote! {
                ::janetrs::lowlevel::JanetRegExt {
                    name: #fn_name.as_ptr() as *const _,
                    cfun: Some(#fn_ptr_ident),
                    documentation: #fn_doc_lit.as_ptr() as *const _,
                    source_file: #fn_file_ident.as_ptr() as *const _,
                    source_line: #fn_line_ident as i32,
                },
            }
        } else {
            quote! {
                ::janetrs::lowlevel::JanetRegExt {
                    name: #fn_name.as_ptr() as *const _,
                    cfun: Some(#fn_ptr_ident),
                    documentation: #fn_doc_ident.as_ptr() as *const _,
                    source_file: #fn_file_ident.as_ptr() as *const _,
                    source_line: #fn_line_ident as i32,
                },
            }
        }
    });

    let regs = fn_doc_lits.iter().enumerate().map(|(i, doc_lit)| {
        let fn_name = &fn_names[i];
        let fn_ptr_ident = &fn_ptr_idents[i];
        let fn_doc_ident = &fn_doc_idents[i];

        if let Some(fn_doc_lit) = doc_lit {
            quote! {
                ::janetrs::lowlevel::JanetReg {
                    name: #fn_name.as_ptr() as *const _,
                    cfun: Some(#fn_ptr_ident),
                    documentation: #fn_doc_lit.as_ptr() as *const _,
                },
            }
        } else {
            quote! {
                ::janetrs::lowlevel::JanetReg {
                    name: #fn_name.as_ptr() as *const _,
                    cfun: Some(#fn_ptr_ident),
                    documentation: #fn_doc_ident.as_ptr() as *const _,
                },
            }
        }
    });

    let ts = quote! {
        #[no_mangle]
        pub unsafe extern "C-unwind" fn _janet_mod_config() -> ::janetrs::lowlevel::JanetBuildConfig {
            ::janetrs::lowlevel::JanetBuildConfig {
                major: ::janetrs::lowlevel::JANET_VERSION_MAJOR,
                minor: ::janetrs::lowlevel::JANET_VERSION_MINOR,
                patch: ::janetrs::lowlevel::JANET_VERSION_PATCH,
                bits:  ::janetrs::lowlevel::JANET_CURRENT_CONFIG_BITS,
            }
        }

        #[::janetrs::cjvg("1.17.0")]
        #[no_mangle]
        pub unsafe extern "C-unwind" fn _janet_init(env: *mut ::janetrs::lowlevel::JanetTable) {
            ::janetrs::lowlevel::janet_cfuns_ext(env, #mod_name.as_ptr() as *const _, [
                #(
                    #regs_ext
                )*

                ::janetrs::lowlevel::JanetRegExt {
                    name: std::ptr::null(),
                    cfun: None,
                    documentation: std::ptr::null(),
                    source_file: std::ptr::null(),
                    source_line: 0
                },
            ].as_ptr())
        }

        #[::janetrs::cjvg("1.0.0", "1.17.0")]
        #[no_mangle]
        pub unsafe extern "C-unwind" fn _janet_init(env: *mut ::janetrs::lowlevel::JanetTable) {
            ::janetrs::lowlevel::janet_cfuns(env, #mod_name.as_ptr() as *const _, [
                #(
                    #regs
                )*

                ::janetrs::lowlevel::JanetReg {
                    name: std::ptr::null(),
                    cfun: None,
                    documentation: std::ptr::null(),
                },
            ].as_ptr())
        }
    };

    ts.into()
}

/// Checks the specified version of Janet is equal of the used Janet. Emits a boolean.
///
/// **Usage:** `check_janet_version!(<MIN_VERSION>, [MAX_VERSION])` where `MIN_VERSION`
/// and `MAX_VERSION` are string literals.
///
/// That means that the range is open in the maximum version: [MIN_VERSION, MAX_VERSION).
#[proc_macro]
pub fn check_janet_version(args: TokenStream) -> TokenStream {
    let global_span = proc_macro2::TokenStream::from(args.clone()).span();
    let args = parse_macro_input!(args as JanetVersionArgs);

    match parse_args(&args.min_version.value()) {
        Ok(req_min_ver) => {
            if let Some(max_lit) = args.max_version {
                match parse_args(&max_lit.value()) {
                    Ok(req_max_ver) => {
                        if req_min_ver <= CURRENT_JANET && req_max_ver > CURRENT_JANET {
                            syn::LitBool::new(true, global_span)
                                .to_token_stream()
                                .into()
                        } else {
                            syn::LitBool::new(false, global_span)
                                .to_token_stream()
                                .into()
                        }
                    },
                    Err(err) => {
                        let err = format!("invalid string literal: {err}");
                        (quote_spanned! {max_lit.span() => compile_error!(#err);}).into()
                    },
                }
            } else if req_min_ver <= CURRENT_JANET {
                syn::LitBool::new(true, global_span)
                    .to_token_stream()
                    .into()
            } else {
                syn::LitBool::new(false, global_span)
                    .to_token_stream()
                    .into()
            }
        },
        Err(err) => {
            let err = format!("invalid string literal: {err}");
            (quote_spanned! {args.min_version.span() => compile_error!(#err);}).into()
        },
    }
}