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
use std::rc::Rc;

use crate::model::*;

/// Different types of classes
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum ClassType {
    /// A normal user class which will have a constructor, destructor, methods, etc
    Normal,
    /// An iterator class
    Iterator,
    /// A collection class
    Collection,
}

/// C-style structure forward declaration
#[derive(Debug)]
pub struct ClassDeclaration {
    pub(crate) name: Name,
    pub(crate) class_type: ClassType,
    pub(crate) settings: Rc<LibrarySettings>,
}

#[derive(Debug, Clone)]
pub(crate) struct IteratorClassDeclaration {
    pub(crate) inner: ClassDeclarationHandle,
}

impl IteratorClassDeclaration {
    pub(crate) fn new(inner: ClassDeclarationHandle) -> Self {
        Self { inner }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct CollectionClassDeclaration {
    pub(crate) inner: ClassDeclarationHandle,
}

impl CollectionClassDeclaration {
    pub(crate) fn new(inner: ClassDeclarationHandle) -> Self {
        Self { inner }
    }
}

impl ClassDeclaration {
    pub(crate) fn new(name: Name, class_type: ClassType, settings: Rc<LibrarySettings>) -> Self {
        Self {
            name,
            class_type,
            settings,
        }
    }
}

pub type ClassDeclarationHandle = Handle<ClassDeclaration>;

/// Represents an instance method on a class
#[derive(Debug, Clone)]
pub struct Method<T>
where
    T: DocReference,
{
    pub(crate) name: Name,
    pub(crate) associated_class: Handle<ClassDeclaration>,
    pub(crate) native_function: Handle<Function<T>>,
}

impl Method<Unvalidated> {
    pub(crate) fn new(
        name: Name,
        associated_class: Handle<ClassDeclaration>,
        function: Handle<Function<Unvalidated>>,
    ) -> Self {
        Self {
            name,
            associated_class,
            native_function: function,
        }
    }

    pub(crate) fn validate(&self, lib: &LibraryFields) -> BindResult<Method<Validated>> {
        Ok(Method {
            name: self.name.clone(),
            associated_class: self.associated_class.clone(),
            native_function: self.native_function.validate(lib)?,
        })
    }
}

pub type MethodHandle = Method<Unvalidated>;

/// represents a static method associated with a class
///
/// name given to the class method may differ from the name of the native function
#[derive(Debug)]
pub struct StaticMethod<T>
where
    T: DocReference,
{
    pub(crate) name: Name,
    pub(crate) native_function: Handle<Function<T>>,
}

impl StaticMethod<Unvalidated> {
    pub(crate) fn validate(&self, lib: &LibraryFields) -> BindResult<StaticMethod<Validated>> {
        Ok(StaticMethod {
            name: self.name.clone(),
            native_function: self.native_function.validate(lib)?,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum DestructionMode {
    /// Object is automatically deleted by the GC
    Automatic,
    /// Object is disposed of manually by calling a custom method
    ///
    /// For safety, if the user never calls the destruction method, the object
    /// will still be deleted by the GC at some point. However, it is
    /// strongly advised to take care of the destruction manually.
    Custom(Name),
    /// Object is disposed of manually by calling a dispose()/close() method
    ///
    /// For safety, if the user never calls the destruction method, the object
    /// will still be deleted by the GC at some point. However, it is
    /// strongly advised to take care of the destruction manually.
    Dispose,
}

impl DestructionMode {
    pub(crate) fn is_manual_destruction(&self) -> bool {
        match self {
            Self::Automatic => false,
            Self::Custom(_) => true,
            Self::Dispose => true,
        }
    }
}

/// Object-oriented class definition
#[derive(Debug)]
pub struct Class<T>
where
    T: DocReference,
{
    pub(crate) declaration: ClassDeclarationHandle,
    pub(crate) constructor: Option<ClassConstructor<T>>,
    pub(crate) destructor: Option<ClassDestructor<T>>,
    pub(crate) methods: Vec<Method<T>>,
    pub(crate) static_methods: Vec<StaticMethod<T>>,
    pub(crate) future_methods: Vec<FutureMethod<T>>,
    pub(crate) doc: Doc<T>,
    pub(crate) destruction_mode: DestructionMode,
    pub(crate) settings: Rc<LibrarySettings>,
}

impl Class<Unvalidated> {
    pub(crate) fn validate(&self, lib: &LibraryFields) -> BindResult<Handle<Class<Validated>>> {
        let constructor = match &self.constructor {
            None => None,
            Some(x) => Some(x.validate(lib)?),
        };
        let destructor = match &self.destructor {
            None => None,
            Some(x) => Some(x.validate(lib)?),
        };
        let methods: BindResult<Vec<Method<Validated>>> =
            self.methods.iter().map(|x| x.validate(lib)).collect();
        let static_methods: BindResult<Vec<StaticMethod<Validated>>> = self
            .static_methods
            .iter()
            .map(|x| x.validate(lib))
            .collect();
        let async_methods: BindResult<Vec<FutureMethod<Validated>>> = self
            .future_methods
            .iter()
            .map(|x| x.validate(lib))
            .collect();

        Ok(Handle::new(Class {
            declaration: self.declaration.clone(),
            constructor,
            destructor,
            methods: methods?,
            static_methods: static_methods?,
            future_methods: async_methods?,
            doc: self.doc.validate(self.name(), lib)?,
            destruction_mode: self.destruction_mode.clone(),
            settings: self.settings.clone(),
        }))
    }
}

impl<T> Class<T>
where
    T: DocReference,
{
    pub fn name(&self) -> &Name {
        &self.declaration.name
    }

    pub fn declaration(&self) -> ClassDeclarationHandle {
        self.declaration.clone()
    }
}

impl Class<Unvalidated> {
    pub(crate) fn find_method<S: AsRef<str>>(
        &self,
        method_name: S,
    ) -> Option<(Name, FunctionHandle)> {
        let method_name = method_name.as_ref();

        for method in &self.methods {
            if method.name.as_ref() == method_name {
                return Some((method.name.clone(), method.native_function.clone()));
            }
        }

        for method in &self.static_methods {
            if method.name.as_ref() == method_name {
                return Some((method.name.clone(), method.native_function.clone()));
            }
        }

        for async_method in &self.future_methods {
            if async_method.name.as_ref() == method_name {
                return Some((
                    async_method.name.clone(),
                    async_method.native_function.clone(),
                ));
            }
        }

        None
    }
}

pub type ClassHandle = Handle<Class<Unvalidated>>;

/// Static class definition
#[derive(Debug)]
pub struct StaticClass<T>
where
    T: DocReference,
{
    pub(crate) name: Name,
    pub(crate) static_methods: Vec<StaticMethod<T>>,
    pub(crate) doc: Doc<T>,
}

impl StaticClass<Unvalidated> {
    pub(crate) fn validate(
        &self,
        lib: &LibraryFields,
    ) -> BindResult<Handle<StaticClass<Validated>>> {
        let methods: BindResult<Vec<StaticMethod<Validated>>> = self
            .static_methods
            .iter()
            .map(|x| x.validate(lib))
            .collect();
        Ok(Handle::new(StaticClass {
            name: self.name.clone(),
            static_methods: methods?,
            doc: self.doc.validate(&self.name, lib)?,
        }))
    }
}

pub type StaticClassHandle = Handle<StaticClass<Unvalidated>>;