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
use std::marker::PhantomData;

use gc_arena::{barrier, Mutation, Root, Rootable};
use piccolo::{
    Callback, CallbackReturn, Context, Error, Execution, FromMultiValue, IntoMultiValue, IntoValue,
    MetaMethod, Table, UserData, Value,
};

pub struct UserMethods<'gc, U: for<'a> Rootable<'a>> {
    table: Table<'gc>,
    _marker: PhantomData<U>,
}

impl<'gc, U: for<'a> Rootable<'a>> Copy for UserMethods<'gc, U> {}

impl<'gc, U: for<'a> Rootable<'a>> Clone for UserMethods<'gc, U> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'gc, U: for<'a> Rootable<'a>> UserMethods<'gc, U> {
    pub fn new(mc: &Mutation<'gc>) -> Self {
        Self {
            table: Table::new(mc),
            _marker: PhantomData,
        }
    }

    pub fn add<F, A, R>(self, name: &'static str, ctx: Context<'gc>, method: F) -> bool
    where
        F: Fn(&Root<'gc, U>, Context<'gc>, Execution<'gc, '_>, A) -> Result<R, Error<'gc>>
            + 'static,
        A: FromMultiValue<'gc>,
        R: IntoMultiValue<'gc>,
    {
        let callback = Callback::from_fn(&ctx, move |ctx, exec, mut stack| {
            let userdata: UserData = stack.from_front(ctx)?;
            let args: A = stack.consume(ctx)?;
            let this = userdata.downcast::<U>()?;
            let ret = method(&this, ctx, exec, args)?;
            stack.replace(ctx, ret);
            Ok(CallbackReturn::Return)
        });

        !self.table.set(ctx, name, callback).unwrap().is_nil()
    }

    pub fn add_write<F, A, R>(self, name: &'static str, ctx: Context<'gc>, method: F) -> bool
    where
        F: Fn(
                &barrier::Write<Root<'gc, U>>,
                Context<'gc>,
                Execution<'gc, '_>,
                A,
            ) -> Result<R, Error<'gc>>
            + 'static,
        A: FromMultiValue<'gc>,
        R: IntoMultiValue<'gc>,
    {
        let callback = Callback::from_fn(&ctx, move |ctx, exec, mut stack| {
            let userdata: UserData = stack.from_front(ctx)?;
            let args: A = stack.consume(ctx)?;
            let mut this = userdata.downcast_write::<U>(&ctx)?;
            let ret = method(&mut this, ctx, exec, args)?;
            stack.replace(ctx, ret);
            Ok(CallbackReturn::Return)
        });

        !self.table.set(ctx, name, callback).unwrap().is_nil()
    }

    pub fn metatable(self, ctx: Context<'gc>) -> Table<'gc> {
        let metatable = Table::new(&ctx);
        metatable.set(ctx, MetaMethod::Index, self.table).unwrap();
        metatable
    }

    pub fn wrap(self, ctx: Context<'gc>, ud: Root<'gc, U>) -> UserData<'gc> {
        let ud = UserData::new::<U>(&ctx, ud);
        ud.set_metatable(&ctx, Some(self.metatable(ctx)));
        ud
    }
}

impl<'gc, U: for<'a> Rootable<'a>> IntoValue<'gc> for UserMethods<'gc, U> {
    fn into_value(self, _: Context<'gc>) -> Value<'gc> {
        self.table.into()
    }
}

pub struct StaticUserMethods<'gc, U: 'static> {
    table: Table<'gc>,
    _marker: PhantomData<U>,
}

impl<'gc, U: 'static> Copy for StaticUserMethods<'gc, U> {}

impl<'gc, U: 'static> Clone for StaticUserMethods<'gc, U> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'gc, U: 'static> StaticUserMethods<'gc, U> {
    pub fn new(mc: &Mutation<'gc>) -> Self {
        Self {
            table: Table::new(mc),
            _marker: PhantomData,
        }
    }

    pub fn add<F, A, R>(self, name: &'static str, ctx: Context<'gc>, method: F) -> bool
    where
        F: Fn(&U, Context<'gc>, Execution<'gc, '_>, A) -> Result<R, Error<'gc>> + 'static,
        A: FromMultiValue<'gc>,
        R: IntoMultiValue<'gc>,
    {
        let callback = Callback::from_fn(&ctx, move |ctx, exec, mut stack| {
            let userdata: UserData = stack.from_front(ctx)?;
            let args: A = stack.consume(ctx)?;
            let this = userdata.downcast_static::<U>()?;
            let ret = method(&this, ctx, exec, args)?;
            stack.replace(ctx, ret);
            Ok(CallbackReturn::Return)
        });

        !self.table.set(ctx, name, callback).unwrap().is_nil()
    }

    pub fn metatable(self, ctx: Context<'gc>) -> Table<'gc> {
        let metatable = Table::new(&ctx);
        metatable.set(ctx, MetaMethod::Index, self.table).unwrap();
        metatable
    }

    pub fn wrap(self, ctx: Context<'gc>, ud: U) -> UserData<'gc> {
        let ud = UserData::new_static(&ctx, ud);
        ud.set_metatable(&ctx, Some(self.metatable(ctx)));
        ud
    }
}

impl<'gc, U: 'static> IntoValue<'gc> for StaticUserMethods<'gc, U> {
    fn into_value(self, _: Context<'gc>) -> Value<'gc> {
        self.table.into()
    }
}