intuicio_frontend_simpleton/library/
closure.rs

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
use crate::{Array, Function, Reference};
use intuicio_core::{context::Context, registry::Registry, IntuicioStruct};
use intuicio_derive::{intuicio_method, intuicio_methods, IntuicioStruct};

#[derive(IntuicioStruct, Default)]
#[intuicio(name = "Closure", module_name = "closure", override_send = false)]
pub struct Closure {
    #[intuicio(ignore)]
    pub function: Function,
    #[intuicio(ignore)]
    pub captured: Array,
}

#[intuicio_methods(module_name = "closure")]
impl Closure {
    #[allow(clippy::new_ret_no_self)]
    #[intuicio_method(use_registry)]
    pub fn new(registry: &Registry, function: Reference, captured: Reference) -> Reference {
        Reference::new(
            Closure {
                function: function.read::<Function>().unwrap().clone(),
                captured: captured.read::<Array>().unwrap().clone(),
            },
            registry,
        )
    }

    pub fn invoke(
        &self,
        context: &mut Context,
        registry: &Registry,
        arguments: &[Reference],
    ) -> Reference {
        for argument in arguments.iter().rev() {
            context.stack().push(argument.clone());
        }
        for argument in self.captured.iter().rev() {
            context.stack().push(argument.clone());
        }
        self.function.handle().unwrap().invoke(context, registry);
        context.stack().pop::<Reference>().unwrap_or_default()
    }

    #[intuicio_method(use_context, use_registry)]
    pub fn call(
        context: &mut Context,
        registry: &Registry,
        closure: Reference,
        arguments: Reference,
    ) -> Reference {
        closure.read::<Closure>().unwrap().invoke(
            context,
            registry,
            arguments.read::<Array>().as_ref().unwrap(),
        )
    }
}

pub fn install(registry: &mut Registry) {
    registry.add_type(Closure::define_struct(registry));
    registry.add_function(Closure::new__define_function(registry));
    registry.add_function(Closure::call__define_function(registry));
}