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
use std::any::Any;
use std::marker::PhantomData;
use std::rc::Rc;

use mica_language::bytecode::{
   DispatchTable, Environment, Function, FunctionKind, FunctionSignature,
};
use mica_language::gc::{Gc, Memory};
use mica_language::value::{self, Closure};

use crate::userdata::Type;
use crate::{
   ffvariants, Error, ForeignFunction, Object, ObjectConstructor, RawForeignFunction, Value,
};

type Constructor<T> = Box<dyn FnOnce(Rc<dyn ObjectConstructor<T>>) -> RawForeignFunction>;

/// A descriptor for a dispatch table. Defines which methods are available on the table, as well
/// as their implementations.
#[derive(Default)]
pub(crate) struct DispatchTableDescriptor {
   methods: Vec<(FunctionSignature, FunctionKind)>,
}

impl DispatchTableDescriptor {
   fn add_function_to_dtable(
      env: &mut Environment,
      gc: &mut Memory,
      dtable: &mut DispatchTable,
      signature: FunctionSignature,
      f: FunctionKind,
   ) -> Result<(), Error> {
      let function_id = env
         .create_function(Function {
            name: Rc::from(format!("{}.{}", &dtable.pretty_name, signature.name)),
            parameter_count: signature.arity,
            kind: f,
         })
         .map_err(|_| Error::TooManyFunctions)?;
      let index = env.get_method_index(&signature).map_err(|_| Error::TooManyMethods)?;
      dtable.set_method(
         index,
         gc.allocate(Closure {
            function_id,
            captures: Vec::new(),
         }),
      );
      Ok(())
   }

   /// Builds a dispatch table from this descriptor.
   pub(crate) fn build_dtable(
      self,
      mut dtable: DispatchTable,
      env: &mut Environment,
      gc: &mut Memory,
   ) -> Result<DispatchTable, Error> {
      for (signature, f) in self.methods {
         Self::add_function_to_dtable(env, gc, &mut dtable, signature, f)?;
      }
      Ok(dtable)
   }
}

/// A builder that allows for binding APIs with user-defined types.
pub struct TypeBuilder<T>
where
   T: ?Sized,
{
   type_name: Rc<str>,
   type_dtable: DispatchTableDescriptor,
   instance_dtable: DispatchTableDescriptor,
   constructors: Vec<(FunctionSignature, Constructor<T>)>,
   _data: PhantomData<T>,
}

impl<T> TypeBuilder<T>
where
   T: ?Sized,
{
   /// Creates a new `TypeBuilder`.
   pub fn new(type_name: impl Into<Rc<str>>) -> Self
   where
      T: Any,
   {
      let type_name = type_name.into();
      Self {
         type_dtable: Default::default(),
         instance_dtable: Default::default(),
         constructors: Vec::new(),
         type_name,
         _data: PhantomData,
      }
   }

   /// Adds a _raw_ instance function to the type.
   ///
   /// You should generally prefer [`add_function`][`Self::add_function`] instead of this.
   ///
   /// `parameter_count` should reflect the parameter count of the function. Pass `None` if the
   /// function accepts a variable number of arguments. Note that _unlike with bare raw functions_
   /// there can be two functions with the same name defined on a type, as long as they have
   /// different arities. Functions with specific arities take priority over varargs.
   ///
   /// Note that this function _consumes_ the builder; this is because calls to functions that add
   /// into the type are meant to be chained together in one expression.
   pub fn add_raw_function(
      mut self,
      name: &str,
      parameter_count: Option<u16>,
      f: FunctionKind,
   ) -> Self {
      self.instance_dtable.methods.push((
         FunctionSignature {
            name: Rc::from(name),
            arity: parameter_count,
         },
         f,
      ));
      self
   }

   /// Adds a _raw_ static function to the type.
   ///
   /// You should generally prefer [`add_static`][`Self::add_static`] instead of this.
   ///
   /// `parameter_count` should reflect the parameter count of the function. Pass `None` if the
   /// function accepts a variable number of arguments. Note that _unlike with bare raw functions_
   /// there can be two functions with the same name defined on a type, as long as they have
   /// different arities. Functions with specific arities take priority over varargs.
   ///
   /// Note that this function _consumes_ the builder; this is because calls to functions that add
   /// into the type are meant to be chained together in one expression.
   pub fn add_raw_static(
      mut self,
      name: &str,
      parameter_count: Option<u16>,
      f: FunctionKind,
   ) -> Self {
      self.type_dtable.methods.push((
         FunctionSignature {
            name: Rc::from(name),
            arity: parameter_count,
         },
         f,
      ));
      self
   }

   /// Adds a _raw_ constructor to the type.
   ///
   /// You should generally prefer [`add_constructor`][`Self::add_constructor`] instead of this.
   ///
   /// `parameter_count` should reflect the parameter count of the function. Pass `None` if the
   /// function accepts a variable number of arguments. Note that _unlike with bare raw functions_
   /// there can be two functions with the same name defined on a type, as long as they have
   /// different arities. Functions with specific arities take priority over varargs.
   ///
   /// Note that this function _consumes_ the builder; this is because calls to functions that add
   /// into the type are meant to be chained together in one expression.
   pub fn add_raw_constructor(
      mut self,
      name: &str,
      parameter_count: Option<u16>,
      f: Constructor<T>,
   ) -> Self {
      self.constructors.push((
         FunctionSignature {
            name: Rc::from(name),
            arity: parameter_count,
         },
         f,
      ));
      self
   }

   /// Adds an instance function to the struct.
   ///
   /// The function must follow the "method" calling convention, in that it accepts `&T` or
   /// `&mut T` as its first parameter.
   pub fn add_function<F, V>(self, name: &str, f: F) -> Self
   where
      V: ffvariants::Method<T>,
      F: ForeignFunction<V>,
   {
      self.add_raw_function(
         name,
         F::parameter_count(),
         FunctionKind::Foreign(f.into_raw_foreign_function()),
      )
   }

   /// Adds a static function to the struct.
   ///
   /// The function must follow the "bare" calling convention, in that it doesn't accept a reference
   /// to `T` as its first parameter.
   pub fn add_static<F, V>(self, name: &str, f: F) -> Self
   where
      V: ffvariants::Bare,
      F: ForeignFunction<V>,
   {
      self.add_raw_static(
         name,
         F::parameter_count().map(|x| {
            // Add 1 for the static receiver, which isn't counted into the bare function's
            // signature.
            x + 1
         }),
         FunctionKind::Foreign(f.into_raw_foreign_function()),
      )
   }

   /// Adds a constructor to the type.
   ///
   /// A constructor is a static function responsible for creating instances of a type. The function
   /// passed to this one must return another function that actually constructs the object.
   ///
   /// Constructors use the "bare" calling convention, in that they don't accept a `self` parameter.
   pub fn add_constructor<F, G, V>(self, name: &str, f: F) -> Self
   where
      V: ffvariants::Bare,
      F: FnOnce(Rc<dyn ObjectConstructor<T>>) -> G,
      F: 'static,
      G: ForeignFunction<V>,
   {
      self.add_raw_constructor(
         name,
         G::parameter_count().map(|x| x + 1),
         Box::new(|ctor| f(ctor).into_raw_foreign_function()),
      )
   }

   /// Builds the struct builder into its type dtable and instance dtable, respectively.
   pub(crate) fn build(self, env: &mut Environment, gc: &mut Memory) -> Result<BuiltType<T>, Error>
   where
      T: Any + Sized,
   {
      // Build the dtables.
      let mut type_dtable = self.type_dtable.build_dtable(
         DispatchTable::new_for_type(Rc::clone(&self.type_name)),
         env,
         gc,
      )?;
      let instance_dtable = self.instance_dtable.build_dtable(
         DispatchTable::new_for_instance(Rc::clone(&self.type_name)),
         env,
         gc,
      )?;
      let instance_dtable = Gc::new(instance_dtable);

      // The type dtable also contains constructors, so build those while we're at it.
      // We implement a helper here that fulfills the ObjectConstructor trait.

      struct Instancer<T>
      where
         T: Sized,
      {
         instance_dtable: Gc<DispatchTable>,
         _data: PhantomData<T>,
      }

      impl<T> ObjectConstructor<T> for Instancer<T> {
         fn construct(&self, instance: T) -> crate::Object<T> {
            Object::new(Gc::as_raw(&self.instance_dtable), instance)
         }
      }

      let instancer: Rc<dyn ObjectConstructor<T>> = Rc::new(Instancer {
         instance_dtable: Gc::clone(&instance_dtable),
         _data: PhantomData,
      });
      for (signature, constructor) in self.constructors {
         let f = constructor(Rc::clone(&instancer));
         DispatchTableDescriptor::add_function_to_dtable(
            env,
            gc,
            &mut type_dtable,
            signature,
            FunctionKind::Foreign(f),
         )?;
      }

      // Build the instance dtable.
      type_dtable.instance = Some(Gc::as_raw(&instance_dtable));

      let type_dtable = Gc::new(type_dtable);
      Ok(BuiltType {
         type_dtable,
         instance_dtable,
         type_name: self.type_name,
         _data: PhantomData,
      })
   }
}

/// Dispatch tables for a finished type.
pub(crate) struct BuiltType<T>
where
   T: ?Sized,
{
   pub(crate) type_name: Rc<str>,
   pub(crate) type_dtable: Gc<DispatchTable>,
   pub(crate) instance_dtable: Gc<DispatchTable>,
   _data: PhantomData<T>,
}

impl<T> BuiltType<T> {
   /// Makes a `Type<T>` user data value from the built type.
   pub(crate) fn make_type(&self) -> Value
   where
      T: Any,
   {
      let user_data: Box<dyn value::UserData> =
         Box::new(Type::<T>::new(Gc::clone(&self.type_dtable)));
      Value::UserData(Gc::new(user_data))
   }
}