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
//! Module of the JanetAbstract abstractions.
//!
//! In this module you can find the definitions of types and traits to allow to work with
//! [`JanetAbstract`]. Most of those are re-exported at the supermodule of this module.
use core::{
    cell::Cell, cmp::Ordering, ffi::c_void, fmt, marker::PhantomData, mem::ManuallyDrop, ptr,
};

pub use evil_janet::JanetAbstractType;

/// Possible error trying to get the abstract value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AbstractError {
    /// [`JanetAbstract`] head size information not the same as the requested
    /// [`IsJanetAbstract`] type
    MismatchedSize,
    /// [`JanetAbstract`] head [`JanetAbstractType`] information not the same as the
    /// requested [`IsJanetAbstract`] [`JanetAbstractType`]
    MismatchedAbstractType,
    /// Pointer to the data is NULL
    NullDataPointer,
}

impl fmt::Display for AbstractError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MismatchedSize => f.pad("Mismatched size between requested type and actual type"),
            Self::MismatchedAbstractType => {
                f.pad("Mismatched fn pointers between requested type and actual type")
            },
            Self::NullDataPointer => f.pad("Data pointer is NULL"),
        }
    }
}

#[cfg(feature = "std")]
#[cfg_attr(_doc, doc(cfg(feature = "std")))]
impl std::error::Error for AbstractError {}

/// Type that represents the Janet Abstract type.
///
/// Janet Abstract types is the way to expose non-native types to the Janet Runtime and
/// allow the Janet user to interact with them.
///
/// It works like a `*mut c_void` pointer, but the memory it uses are tracked by the Janet
/// Garbage Collector.
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct JanetAbstract {
    pub(crate) raw: *mut c_void,
    phantom: PhantomData<Cell<()>>,
}

impl JanetAbstract {
    /// Creates a `JanetAbstract` using information from the type that can be used as
    /// `JanetAbstract`.
    ///
    /// This function manually wraps the `value` in a [`ManuallyDrop`] to avoid the
    /// value being dropped when the `value` is assigned to the `JanetAbstract` internal
    /// raw pointer.
    ///
    /// Note that [`IsJanetAbstract`] is implemented for [`ManuallyDrop`] of any type that
    /// implements [`IsJanetAbstract`].
    #[inline]
    pub fn new<A: IsJanetAbstract>(value: A) -> Self {
        let s = Self {
            raw:     unsafe { evil_janet::janet_abstract(A::type_info(), A::SIZE as _) },
            phantom: PhantomData,
        };

        // SAFETY: The type are the same since `s` was created with `A` type data.
        unsafe {
            ptr::write(s.raw as *mut _, value);
        }

        s
    }

    /// Creates a JanetAbstract from a raw pointer
    ///
    /// # Safety
    /// This function doesn't check anything.
    #[inline]
    pub unsafe fn from_raw(raw: *mut c_void) -> Self {
        Self {
            raw,
            phantom: PhantomData,
        }
    }

    /// Returns a reference to the abstract type data as `A`
    ///
    /// # Safety
    /// This function doesn't check if the underlying data of the `JanetAbstract` object
    /// and the requested type `A` are the same.
    #[inline]
    #[must_use]
    pub unsafe fn get_unchecked<A: IsJanetAbstract>(&self) -> &A::Get {
        &*(self.raw as *const A::Get)
    }

    /// Returns a mutable reference to the abstract type data as `A`
    ///
    /// # Safety
    /// This function doesn't check if the underlying data of the `JanetAbstract` object
    /// and the requested type `A` are the same.
    #[inline]
    pub unsafe fn get_mut_unchecked<A: IsJanetAbstract>(&mut self) -> &mut A::Get {
        &mut *(self.raw as *mut A::Get)
    }

    /// Check if the `JanetAbstract` data is of the type `A`.
    #[inline]
    #[must_use]
    pub fn is<A: IsJanetAbstract>(&self) -> bool {
        if self.size() != A::SIZE {
            return false;
        }
        let ty_self = self.type_info();
        let ty_a = A::type_info();

        if ty_self.call != ty_a.call
            || ty_self.compare != ty_a.compare
            || ty_self.tostring != ty_a.tostring
            || ty_self.marshal != ty_a.marshal
            || ty_self.unmarshal != ty_a.unmarshal
            || ty_self.hash != ty_a.hash
            || ty_self.next != ty_a.next
            || ty_self.put != ty_a.put
            || ty_self.get != ty_a.get
            || ty_self.gc != ty_a.gc
            || ty_self.gcmark != ty_a.gcmark
        {
            return false;
        }

        true
    }

    fn check<A: IsJanetAbstract>(&self) -> Result<(), AbstractError> {
        if self.size() != A::SIZE {
            return Err(AbstractError::MismatchedSize);
        }
        let ty_self = self.type_info();
        let ty_a = A::type_info();

        if ty_self.call != ty_a.call
            || ty_self.compare != ty_a.compare
            || ty_self.tostring != ty_a.tostring
            || ty_self.marshal != ty_a.marshal
            || ty_self.unmarshal != ty_a.unmarshal
            || ty_self.hash != ty_a.hash
            || ty_self.next != ty_a.next
            || ty_self.put != ty_a.put
            || ty_self.get != ty_a.get
            || ty_self.gc != ty_a.gc
            || ty_self.gcmark != ty_a.gcmark
        {
            return Err(AbstractError::MismatchedAbstractType);
        }

        Ok(())
    }

    /// Returns a reference to value if it's the same kind of abstract.
    ///
    /// # Error
    /// This function may return [`AbstractError::MismatchedSize`] if this object size is
    /// different of requested type `A` size, [`AbstractError::MismatchedAbstractType`]
    /// if any of the function pointer in the [`JanetAbstractType`] are different, or
    /// [`AbstractError::NullDataPointer`] if the JanetAbstract is in a uninitialized
    /// state.
    #[inline]
    pub fn get<A: IsJanetAbstract>(&self) -> Result<&A::Get, AbstractError> {
        self.check::<A>()?;

        let ptr = self.raw as *const A::Get;
        if ptr.is_null() {
            return Err(AbstractError::NullDataPointer);
        }

        // SAFETY: The pointer was checked if it's NULL
        Ok(unsafe { &*ptr })
    }

    /// Returns a exclusive reference to value if it's the same kind of abstract.
    ///
    /// # Error
    /// This function may return [`AbstractError::MismatchedSize`] if this object size is
    /// different of requested type `A` size, [`AbstractError::MismatchedAbstractType`]
    /// if any of the function pointer in the [`JanetAbstractType`] are different, or
    /// [`AbstractError::NullDataPointer`] if the JanetAbstract is in a uninitialized
    /// state.
    #[inline]
    pub fn get_mut<A: IsJanetAbstract>(&mut self) -> Result<&mut A::Get, AbstractError> {
        self.check::<A>()?;

        let ptr = self.raw as *mut A::Get;
        if ptr.is_null() {
            return Err(AbstractError::NullDataPointer);
        }

        // SAFETY: The pointer was checked if it's NULL
        Ok(unsafe { &mut *(ptr) })
    }

    /// Takes the value out of this JanetAbstract, moving it back to an uninitialized
    /// state.
    ///
    /// # Error
    /// This function may return [`AbstractError::MismatchedSize`] if this object size is
    /// different of requested type `A` size, [`AbstractError::MismatchedAbstractType`]
    /// if any of the function pointer in the [`JanetAbstractType`] are different, or
    /// [`AbstractError::NullDataPointer`] if the JanetAbstract is in a uninitialized
    /// state.
    pub fn take<A: IsJanetAbstract>(&mut self) -> Result<A, AbstractError> {
        self.check::<A>()?;

        let ptr = self.raw as *mut A;
        if ptr.is_null() {
            return Err(AbstractError::NullDataPointer);
        }

        // SAFETY: The pointer was checked if it's NULL
        let value = unsafe { ptr::read(ptr) };
        self.raw = ptr::null_mut();
        Ok(value)
    }

    /// Extracts the value from the ManuallyDrop container.
    ///
    /// This allows the value to be dropped again.
    ///
    /// # Error
    /// This function may return [`AbstractError::MismatchedSize`] if this object size is
    /// different of requested type `A` size, [`AbstractError::MismatchedAbstractType`]
    /// if any of the function pointer in the [`JanetAbstractType`] are different, or
    /// [`AbstractError::NullDataPointer`] if the JanetAbstract is in a uninitialized
    /// state.
    pub fn into_inner<A: IsJanetAbstract>(self) -> Result<A, AbstractError> {
        self.check::<A>()?;

        let ptr = self.raw as *mut A;
        if ptr.is_null() {
            return Err(AbstractError::NullDataPointer);
        }

        // SAFETY: The pointer was checked if it's NULL
        Ok(unsafe { ptr::read(ptr) })
    }

    /// Acquires the underlying pointer as const pointer.
    #[allow(clippy::wrong_self_convention)] // false positive lint
    #[inline]
    #[must_use]
    pub const fn as_raw(&self) -> *const c_void {
        self.raw
    }

    /// Acquires the underlying pointer.
    // false positive lint
    #[allow(clippy::wrong_self_convention)]
    #[inline]
    pub fn as_mut_raw(&mut self) -> *mut c_void {
        self.raw
    }

    /// Casts to a pointer of another type.
    #[inline]
    pub fn cast<U: IsJanetAbstract>(self) -> *mut U {
        self.raw as *mut U
    }

    /// Return the size of the type it holds.
    #[inline]
    #[must_use]
    pub fn size(&self) -> usize {
        unsafe { (*evil_janet::janet_abstract_head(self.raw)).size }
    }

    /// Return the struct that holds the type name and all possible polymorphic function
    /// pointer that a Abstract type can have in Janet.
    #[inline]
    #[must_use]
    pub fn type_info(&self) -> JanetAbstractType {
        unsafe { *(*evil_janet::janet_abstract_head(self.raw)).type_ }
    }

    #[inline]
    fn raw_type_info(&self) -> *const JanetAbstractType {
        unsafe { (*evil_janet::janet_abstract_head(self.raw)).type_ }
    }
}

impl fmt::Debug for JanetAbstract {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JanetAbstract")
            .field("mem_size", &self.size())
            .finish()
    }
}

impl PartialOrd for JanetAbstract {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for JanetAbstract {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        if self.raw == other.raw {
            return Ordering::Equal;
        }
        let self_ty = self.raw_type_info();
        let other_ty = self.raw_type_info();

        if self_ty != other_ty && self_ty > other_ty {
            return Ordering::Greater;
        }

        let self_ty = self.type_info();

        if let Some(f) = self_ty.compare {
            let res = unsafe { f(self.raw, other.raw) };
            match res {
                -1 => Ordering::Less,
                0 => Ordering::Equal,
                1 => Ordering::Greater,
                _ => unreachable!(),
            }
        } else if self.raw > other.raw {
            Ordering::Greater
        } else {
            Ordering::Less
        }
    }
}

/// The trait that encodes the information required to instantiate the implementer as
/// [`JanetAbstract`]
pub trait IsJanetAbstract {
    /// The type that you get when you call [`JanetAbstract::get`] family of functions.
    ///
    /// This is usually set to `Self` when the type does not implement [`Drop`], or
    /// `ManuallyDrop<Self>` if the type implements [`Drop`].
    type Get: IsJanetAbstract;

    /// The size of the type that is being transformed as [`JanetAbstract`].
    ///
    /// Usually `mem::size_of<Self>()`
    const SIZE: usize;

    /// Returns the table of the name of the `Self` and all possible polymorphic function
    /// pointer that a Abstract type can have in Janet.
    fn type_info() -> &'static JanetAbstractType;
}

/// Register the [`JanetAbstractType`] of a type `T` that implements [`IsJanetAbstract`].
///
/// Registering the type is required to be able to marshal the type.
pub fn register<T: IsJanetAbstract>() {
    let at = T::type_info();
    unsafe {
        let syn = evil_janet::janet_wrap_symbol(evil_janet::janet_csymbol(at.name));

        // If `abs_type_ptr` is NULL, the type is not registered, so we then register it
        let abs_type_ptr = evil_janet::janet_get_abstract_type(syn);
        if abs_type_ptr.is_null() {
            evil_janet::janet_register_abstract_type(at);
        }
    }
}

impl IsJanetAbstract for i64 {
    type Get = i64;

    const SIZE: usize = core::mem::size_of::<Self>();

    #[inline]
    fn type_info() -> &'static JanetAbstractType {
        unsafe { &evil_janet::janet_s64_type }
    }
}

impl IsJanetAbstract for u64 {
    type Get = u64;

    const SIZE: usize = core::mem::size_of::<Self>();

    #[inline]
    fn type_info() -> &'static JanetAbstractType {
        unsafe { &evil_janet::janet_u64_type }
    }
}

impl<A> IsJanetAbstract for ManuallyDrop<A>
where
    A: IsJanetAbstract,
{
    type Get = ManuallyDrop<A::Get>;

    const SIZE: usize = A::SIZE;

    #[inline]
    fn type_info() -> &'static JanetAbstractType {
        A::type_info()
    }
}

#[cfg(all(test, any(feature = "amalgation", feature = "link-system")))]
mod tests {
    use super::*;

    #[test]
    fn creation_and_getting_value() -> Result<(), crate::client::Error> {
        let _client = crate::client::JanetClient::init()?;

        let test = JanetAbstract::new(10u64);
        let test2 = JanetAbstract::new(12i64);

        let val = test.get::<u64>();
        let val2 = test2.get::<i64>();
        let val3 = test.get::<i64>();

        assert_eq!(Ok(&10), val);
        assert_eq!(Ok(&12), val2);
        assert_eq!(Err(AbstractError::MismatchedAbstractType), val3);

        Ok(())
    }

    #[test]
    fn get_mut() -> Result<(), crate::client::Error> {
        let _client = crate::client::JanetClient::init()?;

        let mut test = JanetAbstract::new(10u64);
        let mut test2 = JanetAbstract::new(12i64);

        let val = test.get_mut::<u64>();
        let val2 = test2.get_mut::<i64>();

        assert_eq!(Ok(&mut 10), val);
        assert_eq!(Ok(&mut 12), val2);

        if let Ok(val) = val {
            *val = 1000;
            assert_eq!(&mut 1000, val);
        }

        if let Ok(val2) = val2 {
            *val2 = 2000;
            assert_eq!(&mut 2000, val2);
        }
        Ok(())
    }

    #[test]
    fn size() -> Result<(), crate::client::Error> {
        let _client = crate::client::JanetClient::init()?;

        let test = JanetAbstract::new(10u64);
        let test2 = JanetAbstract::new(12i64);

        assert_eq!(u64::SIZE, test.size());
        assert_eq!(u64::SIZE, test2.size());

        Ok(())
    }

    #[derive(Debug, PartialEq)]
    struct TestDrop(bool);
    static mut TEST_DROP: JanetAbstractType = JanetAbstractType {
        name: c"TestDrop".as_ptr().cast::<i8>(),
        gc: None,
        gcmark: None,
        get: None,
        put: None,
        marshal: None,
        unmarshal: None,
        tostring: None,
        compare: None,
        hash: None,
        next: None,
        call: None,
        length: None,
        bytes: None,
    };

    impl IsJanetAbstract for TestDrop {
        type Get = ManuallyDrop<Self>;

        const SIZE: usize = core::mem::size_of::<Self>();

        #[inline]
        fn type_info() -> &'static JanetAbstractType {
            unsafe { &*ptr::addr_of!(TEST_DROP) }
        }
    }

    impl Drop for TestDrop {
        fn drop(&mut self) {
            self.0 = false;
            panic!("Dropping TestNonCopy");
        }
    }

    #[test]
    fn non_copy() -> Result<(), crate::client::Error> {
        let _client = crate::client::JanetClient::init()?;

        let test = JanetAbstract::new(TestDrop(true));
        let val = test.get::<TestDrop>();
        assert_eq!(Ok(&ManuallyDrop::new(TestDrop(true))), val);

        Ok(())
    }

    #[derive(Debug, PartialEq)]
    struct TestDrop2(bool);
    static mut TEST_DROP2: JanetAbstractType = JanetAbstractType {
        name: c"TestDrop2".as_ptr().cast::<i8>(),
        gc: None,
        gcmark: None,
        get: None,
        put: None,
        marshal: None,
        unmarshal: None,
        tostring: None,
        compare: None,
        hash: None,
        next: None,
        call: None,
        length: None,
        bytes: None,
    };

    impl IsJanetAbstract for TestDrop2 {
        type Get = Self;

        const SIZE: usize = core::mem::size_of::<Self>();

        #[inline]
        fn type_info() -> &'static JanetAbstractType {
            unsafe { &*ptr::addr_of!(TEST_DROP2) }
        }
    }

    impl Drop for TestDrop2 {
        fn drop(&mut self) {
            self.0 = false;
            panic!("Dropping TestNonCopy");
        }
    }

    #[test]
    #[should_panic]
    fn non_copy_ill_implemented() {
        let _client = crate::client::JanetClient::init().unwrap();

        let test = JanetAbstract::new(TestDrop2(true));
        let val = test.get::<TestDrop2>();
        assert_eq!(Ok(&TestDrop2(true)), val);
    }
}