intuicio-core 0.53.1

Core module for Intuicio scripting platform
Documentation
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
#![allow(unpredictable_function_pointer_comparisons)]

//! Callable units, and how to describe and find them.
//!
//! Every function, whether it came from Rust or from a script, is a
//! [`Function`]: a [`FunctionSignature`] describing it and a
//! [`FunctionBody`] doing the work. The body always has the same shape,
//! `fn(&mut Context, &Registry)`, which is why the caller cannot tell the two
//! kinds apart.
//!
//! # Argument order
//!
//! A body pops its arguments in declaration order, first to last, and pushes
//! its results in reverse. A caller therefore has to push arguments in reverse
//! order. [`Function::call`] does that for you, [`Function::invoke`] does not.
//! Prefer `call` unless you already manage the stack yourself.
use crate::{
    Filter, Visibility,
    context::Context,
    meta::Meta,
    registry::Registry,
    types::{Type, TypeHandle, TypeQuery},
};
use intuicio_data::data_stack::DataStackPack;
use rustc_hash::FxHasher;
use std::{
    borrow::Cow,
    hash::{Hash, Hasher},
    sync::Arc,
};

/// Shared function, as a registry holds it.
pub type FunctionHandle = Arc<Function>;
/// Predicate over a function's metadata, used inside queries.
pub type FunctionMetaQuery = fn(&Meta) -> bool;

/// The code a function runs.
///
/// Both variants take the context to move data through and the registry to
/// look up anything else they need to call.
pub enum FunctionBody {
    /// A plain function pointer.
    Pointer(fn(&mut Context, &Registry)),
    /// A closure, for bodies that capture state such as a compiled script.
    #[allow(clippy::type_complexity)]
    Closure(Arc<dyn Fn(&mut Context, &Registry) + Send + Sync>),
}

impl FunctionBody {
    /// Wraps a function pointer.
    pub fn pointer(pointer: fn(&mut Context, &Registry)) -> Self {
        Self::Pointer(pointer)
    }

    /// Wraps a closure.
    pub fn closure<T>(closure: T) -> Self
    where
        T: Fn(&mut Context, &Registry) + Send + Sync + 'static,
    {
        Self::Closure(Arc::new(closure))
    }

    /// Runs the body. Prefer [`Function::invoke`], which also scopes registers.
    pub fn invoke(&self, context: &mut Context, registry: &Registry) {
        match self {
            Self::Pointer(pointer) => pointer(context, registry),
            Self::Closure(closure) => closure(context, registry),
        }
    }
}

impl std::fmt::Debug for FunctionBody {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pointer(_) => write!(f, "<Pointer>"),
            Self::Closure(_) => write!(f, "<Closure>"),
        }
    }
}

/// One input or output of a function, with its name and type.
#[derive(Clone, PartialEq)]
pub struct FunctionParameter {
    /// Metadata attached to this parameter.
    pub meta: Option<Meta>,
    /// Parameter name.
    pub name: String,
    /// Type of the value this parameter carries.
    pub type_handle: TypeHandle,
}

impl FunctionParameter {
    /// Builds a parameter.
    pub fn new(name: impl ToString, type_handle: TypeHandle) -> Self {
        Self {
            meta: None,
            name: name.to_string(),
            type_handle,
        }
    }

    /// Attaches metadata, builder style.
    pub fn with_meta(mut self, meta: Meta) -> Self {
        self.meta = Some(meta);
        self
    }
}

impl std::fmt::Debug for FunctionParameter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FunctionParameter")
            .field("meta", &self.meta)
            .field("name", &self.name)
            .field("type_handle", &self.type_handle.name())
            .finish()
    }
}

/// Everything about a function except its body.
///
/// The signature is also the identity of a function: a registry refuses to
/// hold two functions whose signatures are equal.
///
/// `type_handle` is set when the function belongs to a type, which is how
/// methods are modelled.
#[derive(Clone, PartialEq)]
pub struct FunctionSignature {
    /// Metadata attached to this function.
    pub meta: Option<Meta>,
    /// Name the function is registered under.
    pub name: String,
    /// Module the function belongs to.
    pub module_name: Option<String>,
    /// Type the function is a method of, if any.
    pub type_handle: Option<TypeHandle>,
    /// How widely the function is visible.
    pub visibility: Visibility,
    /// Arguments, in declaration order.
    pub inputs: Vec<FunctionParameter>,
    /// Results, in declaration order.
    pub outputs: Vec<FunctionParameter>,
}

impl FunctionSignature {
    /// Builds a signature with just a name.
    pub fn new(name: impl ToString) -> Self {
        Self {
            meta: None,
            name: name.to_string(),
            module_name: None,
            type_handle: None,
            visibility: Visibility::default(),
            inputs: vec![],
            outputs: vec![],
        }
    }

    /// Attaches metadata, builder style.
    pub fn with_meta(mut self, meta: Meta) -> Self {
        self.meta = Some(meta);
        self
    }

    /// Sets the owning module, builder style.
    pub fn with_module_name(mut self, name: impl ToString) -> Self {
        self.module_name = Some(name.to_string());
        self
    }

    /// Makes this a method of the given type, builder style.
    pub fn with_type_handle(mut self, handle: TypeHandle) -> Self {
        self.type_handle = Some(handle);
        self
    }

    /// Sets visibility, builder style.
    pub fn with_visibility(mut self, visibility: Visibility) -> Self {
        self.visibility = visibility;
        self
    }

    /// Appends an input, builder style.
    pub fn with_input(mut self, parameter: FunctionParameter) -> Self {
        self.inputs.push(parameter);
        self
    }

    /// Appends an output, builder style.
    pub fn with_output(mut self, parameter: FunctionParameter) -> Self {
        self.outputs.push(parameter);
        self
    }
}

impl std::fmt::Debug for FunctionSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FunctionSignature")
            .field("meta", &self.meta)
            .field("name", &self.name)
            .field("module_name", &self.module_name)
            .field(
                "type_handle",
                &match self.type_handle.as_ref() {
                    Some(type_handle) => type_handle.name().to_owned(),
                    None => "!".to_owned(),
                },
            )
            .field("visibility", &self.visibility)
            .field("inputs", &self.inputs)
            .field("outputs", &self.outputs)
            .finish()
    }
}

impl std::fmt::Display for FunctionSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(meta) = self.meta.as_ref() {
            write!(f, "#{meta} ")?;
        }
        if let Some(module_name) = self.module_name.as_ref() {
            write!(f, "mod {module_name} ")?;
        }
        if let Some(type_handle) = self.type_handle.as_ref() {
            match &**type_handle {
                Type::Struct(value) => {
                    write!(f, "struct {} ", value.type_name())?;
                }
                Type::Enum(value) => {
                    write!(f, "enum {} ", value.type_name())?;
                }
            }
        }
        write!(f, "fn {}(", self.name)?;
        for (index, parameter) in self.inputs.iter().enumerate() {
            if index > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{}: {}",
                parameter.name,
                parameter.type_handle.type_name()
            )?;
        }
        write!(f, ") -> (")?;
        for (index, parameter) in self.outputs.iter().enumerate() {
            if index > 0 {
                write!(f, ", ")?;
            }
            write!(
                f,
                "{}: {}",
                parameter.name,
                parameter.type_handle.type_name()
            )?;
        }
        write!(f, ")")
    }
}

/// A callable unit: a signature plus a body.
///
/// See the [module docs](self).
#[derive(Debug)]
pub struct Function {
    signature: FunctionSignature,
    body: FunctionBody,
}

impl Function {
    /// Pairs a signature with a body.
    pub fn new(signature: FunctionSignature, body: FunctionBody) -> Self {
        Self { signature, body }
    }

    /// Returns the signature.
    pub fn signature(&self) -> &FunctionSignature {
        &self.signature
    }

    /// Runs the function on data already on the stack.
    ///
    /// Arguments must be pushed in reverse order beforehand, and results are
    /// left on the stack. Registers are scoped around the body, so the callee
    /// cannot reach the caller's.
    pub fn invoke(&self, context: &mut Context, registry: &Registry) {
        context.store_registers();
        self.body.invoke(context, registry);
        context.restore_registers();
    }

    /// Runs the function with Rust values, taking care of stack order.
    ///
    /// With `verify` set, argument and result types are checked against the
    /// signature first.
    ///
    /// # Panics
    ///
    /// Panics when `verify` is set and the types do not match, or when the
    /// results on the stack are not of type `O`.
    pub fn call<O: DataStackPack, I: DataStackPack>(
        &self,
        context: &mut Context,
        registry: &Registry,
        inputs: I,
        verify: bool,
    ) -> O {
        if verify {
            self.verify_inputs_outputs::<O, I>();
        }
        inputs.stack_push_reversed(context.stack());
        self.invoke(context, registry);
        O::stack_pop(context.stack())
    }

    /// Checks that `I` and `O` match the signature.
    ///
    /// # Panics
    ///
    /// Panics with a message naming the offending parameter when they do not.
    pub fn verify_inputs_outputs<O: DataStackPack, I: DataStackPack>(&self) {
        let input_types = I::pack_types();
        if input_types.len() != self.signature.inputs.len() {
            panic!("Function: {} got wrong inputs number!", self.signature.name);
        }
        let output_types = O::pack_types();
        if output_types.len() != self.signature.outputs.len() {
            panic!(
                "Function: {} got wrong outputs number!",
                self.signature.name
            );
        }
        for (parameter, type_hash) in self.signature.inputs.iter().zip(input_types) {
            if parameter.type_handle.type_hash() != type_hash {
                panic!(
                    "Function: {} input parameter: {} got wrong value type!",
                    self.signature.name, parameter.name
                );
            }
        }
        for (parameter, type_hash) in self.signature.outputs.iter().zip(output_types) {
            if parameter.type_handle.type_hash() != type_hash {
                panic!(
                    "Function: {} output parameter: {} got wrong value type!",
                    self.signature.name, parameter.name
                );
            }
        }
    }

    /// Wraps this function in a shared handle.
    pub fn into_handle(self) -> FunctionHandle {
        self.into()
    }
}

/// Search filter for one function parameter.
///
/// Every field is optional. An empty filter matches anything.
#[derive(Debug, Default, Clone, PartialEq, Hash)]
pub struct FunctionQueryParameter<'a> {
    /// Required parameter name.
    pub name: Option<Cow<'a, str>>,
    /// Filter on the parameter type.
    pub type_query: Option<TypeQuery<'a>>,
    /// Predicate the parameter metadata must satisfy.
    pub meta: Option<FunctionMetaQuery>,
}

impl FunctionQueryParameter<'_> {
    /// Returns `true` when `parameter` satisfies every set field.
    pub fn is_valid(&self, parameter: &FunctionParameter) -> bool {
        self.name
            .as_ref()
            .map(|name| name.as_ref() == parameter.name)
            .unwrap_or(true)
            && self
                .type_query
                .as_ref()
                .map(|query| query.is_valid(&parameter.type_handle))
                .unwrap_or(true)
            && self
                .meta
                .as_ref()
                .map(|query| parameter.meta.as_ref().map(query).unwrap_or(false))
                .unwrap_or(true)
    }

    /// Copies borrowed names into owned ones, so the filter can outlive them.
    pub fn to_static(&self) -> FunctionQueryParameter<'static> {
        FunctionQueryParameter {
            name: self
                .name
                .as_ref()
                .map(|name| name.as_ref().to_owned().into()),
            type_query: self.type_query.as_ref().map(|query| query.to_static()),
            meta: self.meta,
        }
    }
}

/// Search filter for a function's parameter list.
///
/// Three settings, because "the first two are ints" and "it takes exactly two
/// ints" are different questions. A bare list can only ask one of them.
///
/// [`Self::Prefix`] rejects a list **longer** than the function's. A filter
/// with no parameter to match cannot be satisfied, so it fails instead of
/// being ignored.
///
/// ```
/// # use intuicio_core::function::Parameters;
/// // Say nothing about the parameters at all.
/// let any = Parameters::default();
/// assert!(matches!(any, Parameters::Any));
/// ```
#[derive(Debug, Default, Clone, PartialEq, Hash)]
pub enum Parameters<'a> {
    /// Matches any parameter list.
    #[default]
    Any,
    /// Matches from the front. The function may take more, but not fewer.
    Prefix(Cow<'a, [FunctionQueryParameter<'a>]>),
    /// Matches one for one. The function takes exactly these.
    Exact(Cow<'a, [FunctionQueryParameter<'a>]>),
}

/// A plain list is matched from the front. The function may take more
/// parameters.
impl<'a> From<Vec<FunctionQueryParameter<'a>>> for Parameters<'a> {
    fn from(value: Vec<FunctionQueryParameter<'a>>) -> Self {
        Self::Prefix(value.into())
    }
}

impl<'a> From<Cow<'a, [FunctionQueryParameter<'a>]>> for Parameters<'a> {
    fn from(value: Cow<'a, [FunctionQueryParameter<'a>]>) -> Self {
        Self::Prefix(value)
    }
}

impl<'a> From<&'a [FunctionQueryParameter<'a>]> for Parameters<'a> {
    fn from(value: &'a [FunctionQueryParameter<'a>]) -> Self {
        Self::Prefix(value.into())
    }
}

impl<'a> Parameters<'a> {
    /// Returns `true` when `parameters` satisfies this filter.
    pub fn is_valid(&self, parameters: &[FunctionParameter]) -> bool {
        let (queries, exact) = match self {
            Self::Any => return true,
            Self::Prefix(queries) => (queries, false),
            Self::Exact(queries) => (queries, true),
        };
        let fits = if exact {
            queries.len() == parameters.len()
        } else {
            queries.len() <= parameters.len()
        };
        fits && queries
            .iter()
            .zip(parameters.iter())
            .all(|(query, parameter)| query.is_valid(parameter))
    }

    /// The filters themselves, empty for [`Self::Any`].
    pub fn queries(&self) -> &[FunctionQueryParameter<'a>] {
        match self {
            Self::Any => &[],
            Self::Prefix(queries) | Self::Exact(queries) => queries,
        }
    }

    /// Copies borrowed names into owned ones, so the filter can outlive them.
    pub fn to_static(&self) -> Parameters<'static> {
        let queries = |queries: &Cow<'_, [FunctionQueryParameter<'_>]>| {
            queries
                .iter()
                .map(|query| query.to_static())
                .collect::<Vec<_>>()
                .into()
        };
        match self {
            Self::Any => Parameters::Any,
            Self::Prefix(inner) => Parameters::Prefix(queries(inner)),
            Self::Exact(inner) => Parameters::Exact(queries(inner)),
        }
    }
}

/// Search filter for functions in a [`crate::registry::Registry`].
///
/// Every field is optional and an empty query matches everything.
///
/// Three of the fields are a [`Filter`] rather than an `Option`, because the
/// signature's own field is optional and a query must be able to ask for its
/// **absence**. `type_query: Filter::Absent` asks for a free function, not a
/// method, which an `Option` cannot express.
///
/// ```
/// # use intuicio_core::{Filter, function::{FunctionQuery, Parameters}};
/// // A free function called `add`, taking exactly two parameters.
/// let query = FunctionQuery {
///     name: Some("add".into()),
///     module_name: Filter::Matching("lib".into()),
///     type_query: Filter::Absent,
///     inputs: Parameters::Exact(vec![Default::default(), Default::default()].into()),
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Default, Clone, PartialEq, Hash)]
pub struct FunctionQuery<'a> {
    /// Required function name.
    pub name: Option<Cow<'a, str>>,
    /// Filter on the module the function belongs to.
    pub module_name: Filter<Cow<'a, str>>,
    /// Filter on the type the function is a method of.
    pub type_query: Filter<TypeQuery<'a>>,
    /// Required visibility.
    pub visibility: Option<Visibility>,
    /// Filter on the argument list.
    pub inputs: Parameters<'a>,
    /// Filter on the result list.
    pub outputs: Parameters<'a>,
    /// Predicate the function metadata must satisfy.
    pub meta: Filter<FunctionMetaQuery>,
}

impl FunctionQuery<'_> {
    /// Returns `true` when `signature` satisfies every set field.
    pub fn is_valid(&self, signature: &FunctionSignature) -> bool {
        self.name
            .as_ref()
            .map(|name| name.as_ref() == signature.name)
            .unwrap_or(true)
            && self
                .module_name
                .is_valid(signature.module_name.as_ref(), |name, module_name| {
                    name.as_ref() == module_name
                })
            && self
                .type_query
                .is_valid(signature.type_handle.as_ref(), |query, handle| {
                    query.is_valid(handle)
                })
            && self
                .visibility
                .map(|visibility| signature.visibility.is_visible(visibility))
                .unwrap_or(true)
            && self.inputs.is_valid(&signature.inputs)
            && self.outputs.is_valid(&signature.outputs)
            && self
                .meta
                .is_valid(signature.meta.as_ref(), |query, meta| query(meta))
    }

    /// Hashes the query, which is the key the registry caches results under.
    pub fn as_hash(&self) -> u64 {
        let mut hasher = FxHasher::default();
        self.hash(&mut hasher);
        hasher.finish()
    }

    /// Copies borrowed names into owned ones, so the query can outlive them.
    pub fn to_static(&self) -> FunctionQuery<'static> {
        FunctionQuery {
            name: self
                .name
                .as_ref()
                .map(|name| name.as_ref().to_owned().into()),
            module_name: self.module_name.map(|name| name.as_ref().to_owned().into()),
            type_query: self.type_query.map(|query| query.to_static()),
            visibility: self.visibility,
            inputs: self.inputs.to_static(),
            outputs: self.outputs.to_static(),
            meta: self.meta,
        }
    }
}

/// Builds a [`FunctionSignature`], looking every type up in a registry.
///
/// ```ignore
/// function_signature! {
///     registry => mod lib fn add(a: i32, b: i32) -> (result: i32)
/// }
/// ```
///
/// # Panics
///
/// Panics when a type used in the signature is not registered.
#[macro_export]
macro_rules! function_signature {
    (
        $registry:expr
        =>
        $(mod $module_name:ident)?
        $(type ($type:ty))?
        fn
        $name:ident
        ($( $input_name:ident : $input_type:ty ),*)
        ->
        ($( $output_name:ident : $output_type:ty ),*)
    ) => {{
        let mut result = $crate::function::FunctionSignature::new(stringify!($name));
        $(
            result.module_name = Some(stringify!($module_name).to_owned());
        )?
        $(
            result.type_handle = Some($registry.find_type($crate::types::TypeQuery::of::<$type>()).unwrap());
        )?
        $(
            result.inputs.push(
                $crate::function::FunctionParameter::new(
                    stringify!($input_name).to_owned(),
                    $registry.find_type($crate::types::TypeQuery::of::<$input_type>()).unwrap()
                )
            );
        )*
        $(
            result.outputs.push(
                $crate::function::FunctionParameter::new(
                    stringify!($output_name).to_owned(),
                    $registry.find_type($crate::types::TypeQuery::of::<$output_type>()).unwrap()
                )
            );
        )*
        result
    }};
}

/// Builds a whole [`Function`] from a signature and a Rust body.
///
/// The body is a block whose value is a tuple of the outputs. Arguments arrive
/// as ordinary local variables.
///
/// ```ignore
/// define_function! {
///     registry => mod lib fn add(a: i32, b: i32) -> (result: i32) {
///         (a + b,)
///     }
/// }
/// ```
#[macro_export]
macro_rules! define_function {
    (
        $registry:expr
        =>
        $(mod $module_name:ident)?
        $(type ($type:ty))?
        fn
        $name:ident
        ($( $input_name:ident : $input_type:ty),*)
        ->
        ($( $output_name:ident : $output_type:ty),*)
        $code:block
    ) => {
        $crate::function::Function::new(
            $crate::function_signature! {
                $registry
                =>
                $(mod $module_name)?
                $(type ($type))?
                fn
                $name
                ($($input_name : $input_type),*)
                ->
                ($($output_name : $output_type),*)
            },
            $crate::function::FunctionBody::closure(move |context, registry| {
                use intuicio_data::data_stack::DataStackPack;
                #[allow(unused_mut)]
                let ($(mut $input_name,)*) = <($($input_type,)*)>::stack_pop(context.stack());
                $code.stack_push_reversed(context.stack());
            }),
        )
    };
}

#[cfg(test)]
mod tests {
    use crate as intuicio_core;
    use crate::{context::*, function::*, registry::*, types::struct_type::*};
    use intuicio_data;
    use intuicio_derive::*;

    #[intuicio_function(meta = "foo", args_meta(_bar = "foo"))]
    fn function_meta(_bar: bool) {}

    #[intuicio_function(name = "+", module_name = "core/ops")]
    fn function_non_ident_name(a: i32, b: i32) -> i32 {
        a + b
    }

    /// The three things `Option` and a bare parameter list could not ask, and
    /// the one they got wrong.
    #[test]
    fn test_query_filters() {
        let mut registry = Registry::default();
        registry.add_type(NativeStructBuilder::new::<i32>().build());
        let i32_type = registry.find_type(TypeQuery::of::<i32>()).unwrap();

        let free = FunctionSignature::new("f")
            .with_module_name("lib")
            .with_input(FunctionParameter::new("a", i32_type.clone()));
        let method = FunctionSignature::new("f")
            .with_module_name("lib")
            .with_type_handle(i32_type.clone())
            .with_input(FunctionParameter::new("a", i32_type.clone()));

        // Absence, which is how "a free function, not a method" is asked.
        let query = FunctionQuery {
            type_query: Filter::Absent,
            ..Default::default()
        };
        assert!(query.is_valid(&free));
        assert!(!query.is_valid(&method));

        // Ignore, the default, still matches either - so old queries behave the
        // way they did.
        let query = FunctionQuery::default();
        assert!(query.is_valid(&free));
        assert!(query.is_valid(&method));

        // Matching, on a field the signature may not have at all.
        let query = FunctionQuery {
            module_name: Filter::Matching("lib".into()),
            ..Default::default()
        };
        assert!(query.is_valid(&free));
        let query = FunctionQuery {
            module_name: Filter::Matching("other".into()),
            ..Default::default()
        };
        assert!(!query.is_valid(&free));

        let two = FunctionSignature::new("g")
            .with_input(FunctionParameter::new("a", i32_type.clone()))
            .with_input(FunctionParameter::new("b", i32_type.clone()));

        // Exact pins the count; prefix does not.
        let one_filter = vec![FunctionQueryParameter::default()];
        assert!(
            FunctionQuery {
                inputs: Parameters::Prefix(one_filter.to_owned().into()),
                ..Default::default()
            }
            .is_valid(&two)
        );
        assert!(
            !FunctionQuery {
                inputs: Parameters::Exact(one_filter.into()),
                ..Default::default()
            }
            .is_valid(&two)
        );

        // The bug: more filters than parameters used to match, because `zip`
        // stopped at the shorter list and left the extra filter unchecked.
        let three_filters = vec![
            FunctionQueryParameter::default(),
            FunctionQueryParameter::default(),
            FunctionQueryParameter::default(),
        ];
        assert!(
            !FunctionQuery {
                inputs: Parameters::Prefix(three_filters.into()),
                ..Default::default()
            }
            .is_valid(&two)
        );
    }

    #[test]
    fn test_function_non_ident_name() {
        let mut registry = Registry::default();
        registry.add_type(NativeStructBuilder::new::<i32>().build());
        let signature = function_non_ident_name::define_signature(&registry);
        assert_eq!(signature.name, "+");
        assert_eq!(signature.module_name.as_deref(), Some("core/ops"));
    }

    #[test]
    fn test_function() {
        fn add(context: &mut Context, _: &Registry) {
            let a = context.stack().pop::<i32>().unwrap();
            let b = context.stack().pop::<i32>().unwrap();
            context.stack().push(a + b);
        }

        let i32_handle = NativeStructBuilder::new::<i32>()
            .build()
            .into_type()
            .into_handle();
        let signature = FunctionSignature::new("add")
            .with_input(FunctionParameter::new("a", i32_handle.clone()))
            .with_input(FunctionParameter::new("b", i32_handle.clone()))
            .with_output(FunctionParameter::new("result", i32_handle));
        let function = Function::new(signature.to_owned(), FunctionBody::pointer(add));

        assert!(FunctionQuery::default().is_valid(&signature));
        assert!(
            FunctionQuery {
                name: Some("add".into()),
                ..Default::default()
            }
            .is_valid(&signature)
        );
        assert!(
            FunctionQuery {
                name: Some("add".into()),
                inputs: [
                    FunctionQueryParameter {
                        name: Some("a".into()),
                        ..Default::default()
                    },
                    FunctionQueryParameter {
                        name: Some("b".into()),
                        ..Default::default()
                    }
                ]
                .as_slice()
                .into(),
                outputs: [FunctionQueryParameter {
                    name: Some("result".into()),
                    ..Default::default()
                }]
                .as_slice()
                .into(),
                ..Default::default()
            }
            .is_valid(&signature)
        );
        assert!(
            !FunctionQuery {
                name: Some("add".into()),
                inputs: [
                    FunctionQueryParameter {
                        name: Some("b".into()),
                        ..Default::default()
                    },
                    FunctionQueryParameter {
                        name: Some("a".into()),
                        ..Default::default()
                    }
                ]
                .as_slice()
                .into(),
                ..Default::default()
            }
            .is_valid(&signature)
        );

        let mut context = Context::new(10240, 10240);
        let registry = Registry::default().with_basic_types();

        context.stack().push(2);
        context.stack().push(40);
        function.invoke(&mut context, &registry);
        assert_eq!(context.stack().pop::<i32>().unwrap(), 42);

        assert_eq!(
            function_meta::define_signature(&registry).meta,
            Some(Meta::Identifier("foo".to_owned()))
        );
    }
}