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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Prolog atoms.
//!
//! Atoms are a core datatype in prolog. An atom is a string that is
//! kept around in a lookup table. Each occurence of an atom for the
//! same string points at the same object. This allows for easy
//! comparison.
//!
//! Atoms are reference-counted. When nothing refers to an atom
//! anymore, the atom may be garbage collected, freeing up the memory
//! of the string.
//!
//! This module provides functions and types for interacting with
//! prolog atoms.

use super::context::*;
use super::engine::*;
use super::fli::*;
use super::init::*;
use super::result::*;
use super::term::*;
use crate::{term_getable, term_putable, unifiable};
use std::convert::TryInto;
use std::os::raw::c_char;
use std::sync::atomic::{AtomicUsize, Ordering};

/// A wrapper for a prolog atom.
///
/// When created, the underlying atom will have its reference count
/// increased. When dropped, the reference count will decrease.
#[derive(PartialEq, Eq, Hash, Debug)]
pub struct Atom {
    atom: atom_t,
}

impl Atom {
    /// Wrap an `atom_t`, which is how the SWI-Prolog fli represents atoms.
    ///
    /// # Safety
    /// This is unsafe because no check is done to ensure that the
    /// atom_t indeed points at a valid atom. The caller will have to
    /// ensure that this is the case.
    pub unsafe fn wrap(atom: atom_t) -> Atom {
        Atom { atom }
    }

    /// Create a new atom from the given string.
    ///
    /// This will panic if no prolog engine is active on this thread.
    ///
    /// If the atom already exists, this will raise the reference
    /// count on that atom and then return the existing atom.
    pub fn new(name: &str) -> Atom {
        assert_swipl_is_initialized();
        // there's a worrying bit of information in the documentation.
        // It says that in some cases for small strings,
        // PL_new_atom_mbchars will recalculate the size of the string
        // using strlen. In that case we need to give it a
        // nul-terminated string.
        const S_USIZE: usize = std::mem::size_of::<usize>();
        let atom = if name.len() == S_USIZE - 1 {
            let mut buf: [u8; S_USIZE] = [0; S_USIZE];
            buf[..name.len()].clone_from_slice(name.as_bytes());

            unsafe {
                PL_new_atom_mbchars(
                    REP_UTF8.try_into().unwrap(),
                    name.len(),
                    buf.as_ptr() as *const c_char,
                )
            }
        } else {
            unsafe {
                PL_new_atom_mbchars(
                    REP_UTF8.try_into().unwrap(),
                    name.len(),
                    name.as_ptr() as *const c_char,
                )
            }
        };

        unsafe { Atom::wrap(atom) }
    }

    /// Return the underlying `atom_t` which SWI-Prolog uses to refer to the atom.
    pub fn atom_ptr(&self) -> atom_t {
        self.atom
    }

    /// Retrieve the name of this atom, that is, the string with which it was created.
    ///
    /// This will panic if no prolog engine is active on this thread.
    pub fn name(&self) -> String {
        // NOTE: This code is terrible. There should be no reason to
        // go through term to get a string out, except that's the only
        // way SWI-Prolog exposes auto-conversion to UTF8.
        assert_some_engine_is_active();

        let name;
        unsafe {
            let temp_term_ref = PL_new_term_ref();
            let unsafe_engine = unmanaged_engine_context();
            let temp_term = Term::new(temp_term_ref, unsafe_engine.as_term_origin());
            temp_term.put(self).unwrap();

            name = temp_term.get_atom_name(|name| name.unwrap().to_string());
            temp_term.reset();
        }

        name.unwrap()
    }

    /// Increase the reference counter for this atom.
    pub(crate) fn increment_refcount(&self) {
        unsafe { PL_register_atom(self.atom) }
    }
}

impl ToString for Atom {
    fn to_string(&self) -> String {
        self.name()
    }
}

impl From<Atom> for String {
    fn from(atom: Atom) -> String {
        atom.to_string()
    }
}

impl Clone for Atom {
    fn clone(&self) -> Self {
        assert_some_engine_is_active();
        unsafe { PL_register_atom(self.atom) };
        Atom { atom: self.atom }
    }
}

impl Drop for Atom {
    fn drop(&mut self) {
        assert_some_engine_is_active();
        unsafe {
            PL_unregister_atom(self.atom);
        }
    }
}

unifiable! {
    (self:Atom, term) => {
        let result = unsafe { PL_unify_atom(term.term_ptr(), self.atom) };

        result != 0
    }
}

/// Get an atom out of a term.
///
/// # Safety
/// This is unsafe because we don't check if the term is part of the
/// active engine.
#[allow(unused_unsafe)]
pub unsafe fn get_atom<F, R>(term: &Term, func: F) -> PrologResult<R>
where
    F: Fn(Option<&Atom>) -> R,
{
    let mut atom = 0;
    let result = unsafe { PL_get_atom(term.term_ptr(), &mut atom) };

    if unsafe { pl_default_exception() != 0 } {
        return Err(PrologError::Exception);
    }

    let arg = if result == 0 {
        None
    } else {
        let atom = unsafe { Atom::wrap(atom) };

        Some(atom)
    };

    let result = func(arg.as_ref());
    // prevent destructor from running since we never increased the refcount
    std::mem::forget(arg);

    Ok(result)
}

term_getable! {
    (Atom, "atom", term) => {
        match term.get_atom(|a| a.cloned()) {
            Ok(r) => r,
            // ignore this error - it'll be picked up again by the wrapper
            Err(_) => None
        }
    }
}

term_putable! {
    (self:Atom, term) => {
        unsafe { PL_put_atom(term.term_ptr(), self.atom); }
    }
}

/// A type that allows easy conversion of strings from and to an atom.
pub enum Atomable<'a> {
    Str(&'a str),
    String(String),
}

impl<'a> From<&'a str> for Atomable<'a> {
    fn from(s: &str) -> Atomable {
        Atomable::Str(s)
    }
}

impl<'a> From<String> for Atomable<'a> {
    fn from(s: String) -> Atomable<'static> {
        Atomable::String(s)
    }
}

impl<'a> Atomable<'a> {
    /// Create a new Atomable out of a String or an &str.
    pub fn new<T: Into<Atomable<'a>>>(s: T) -> Self {
        s.into()
    }

    /// Return the name.
    pub fn name(&self) -> &str {
        match self {
            Self::Str(s) => s,
            Self::String(s) => s,
        }
    }

    /// Convert this Atomable into a new Atomable which is guaranteed to own its data.
    pub fn owned(&self) -> Atomable<'static> {
        match self {
            Self::Str(s) => Atomable::String(s.to_string()),
            Self::String(s) => Atomable::String(s.clone()),
        }
    }
}

/// Wrapper for `Atomable::new`.
pub fn atomable<'a, T: Into<Atomable<'a>>>(s: T) -> Atomable<'a> {
    Atomable::new(s)
}

/// Trait for types which can be turned into an `Atom`.
pub trait IntoAtom {
    /// Turn this object into an `Atom`.
    fn into_atom(self) -> Atom;
}

impl IntoAtom for Atom {
    fn into_atom(self) -> Atom {
        self
    }
}

impl IntoAtom for &Atom {
    fn into_atom(self) -> Atom {
        self.clone()
    }
}

impl<'a> IntoAtom for &Atomable<'a> {
    fn into_atom(self) -> Atom {
        Atom::new(self.as_ref())
    }
}

impl<'a> IntoAtom for Atomable<'a> {
    fn into_atom(self) -> Atom {
        (&self).into_atom()
    }
}

impl<'a> IntoAtom for &'a str {
    fn into_atom(self) -> Atom {
        Atom::new(self)
    }
}

/// Trait for types which can be turned into an `Atom` from a borrow.
pub trait AsAtom {
    /// Turn the borrowed object into an `Atom`.
    ///
    /// The second argument ensures that this function is called in a
    /// context where atoms are allowed to be created.
    fn as_atom(&self) -> Atom;

    /// Turn the borrowed object into an `atom_t`, and returns an
    /// allocation which will keep this `atom_t` valid as long as it
    /// is not dropped.
    ///
    /// This allows code that takes an `AsAtom` to be a little bit
    /// smart about not cloning the underlying data, if the underlying
    /// data is already an atom.
    fn as_atom_ptr(&self) -> (atom_t, Option<Atom>) {
        let atom = self.as_atom();
        (atom.atom_ptr(), Some(atom))
    }
}

impl AsAtom for Atom {
    fn as_atom(&self) -> Atom {
        self.clone()
    }

    fn as_atom_ptr(&self) -> (atom_t, Option<Atom>) {
        (self.atom_ptr(), None)
    }
}

impl AsAtom for &Atom {
    fn as_atom(&self) -> Atom {
        (*self).clone()
    }

    fn as_atom_ptr(&self) -> (atom_t, Option<Atom>) {
        (self.atom_ptr(), None)
    }
}

impl<'a> AsAtom for Atomable<'a> {
    fn as_atom(&self) -> Atom {
        self.into_atom()
    }
}

impl<'a> AsAtom for &'a str {
    fn as_atom(&self) -> Atom {
        self.into_atom()
    }
}

impl AsAtom for str {
    fn as_atom(&self) -> Atom {
        self.into_atom()
    }
}

unifiable! {
    (self:Atomable<'a>, term) => {
        let result = unsafe {
            PL_unify_chars(
                term.term_ptr(),
                (PL_ATOM | REP_UTF8).try_into().unwrap(),
                self.name().len(),
                self.name().as_bytes().as_ptr() as *const c_char,
            )
        };

        result != 0
    }
}

/// Get an atomable out of a term.
///
/// This is very much like `get_atom`, but instead of retrieving the
/// atom, we retrieve the atom's name as an &str, wrapped by an
/// `Atomable`. This means the atom reference count does not have to
/// be manipulated, and it should be slightly faster than first
/// getting the atom and then extracting its name.
pub fn get_atomable<F, R>(term: &Term, func: F) -> PrologResult<R>
where
    F: Fn(Option<&Atomable>) -> R,
{
    assert_some_engine_is_active();
    let mut ptr = std::ptr::null_mut();
    let mut len = 0;
    let result = unsafe {
        PL_get_nchars(
            term.term_ptr(),
            &mut len,
            &mut ptr,
            CVT_ATOM | REP_UTF8 | BUF_DISCARDABLE,
        )
    };

    if unsafe { pl_default_exception() != 0 } {
        return Err(PrologError::Exception);
    }

    let arg = if result == 0 {
        None
    } else {
        let swipl_string_ref = unsafe { std::slice::from_raw_parts(ptr as *const u8, len) };

        let swipl_string = std::str::from_utf8(swipl_string_ref).unwrap();
        let atomable = Atomable::new(swipl_string);

        Some(atomable)
    };

    let result = func(arg.as_ref());
    // prevent destructor from running since we never increased the refcount
    std::mem::forget(arg);

    Ok(result)
}

term_getable! {
    (Atomable<'static>, "atom", term) => {
        match get_atomable(term, |a|a.map(|a|a.owned())) {
            Ok(r) => r,
            // ignore error - it'll be picked up in the wrapper
            Err(_) => None
        }
    }
}

term_putable! {
    (self:Atomable<'a>, term) => {
        unsafe {
            PL_put_chars(
                term.term_ptr(),
                (PL_ATOM | REP_UTF8).try_into().unwrap(),
                self.name().len(),
                self.name().as_bytes().as_ptr() as *const c_char,
            );
        }
    }
}

impl<'a> AsRef<str> for Atomable<'a> {
    fn as_ref(&self) -> &str {
        self.name()
    }
}

/// A struct which provides a way to delay and cache atom creation.
///
/// This struct wraps a static string and uses it to construct a swipl
/// atom on the first invocation of `as_atom`. Subsequent invocations
/// will reuse the earlier constructed atom.
///
/// The purpose of this struct is to back the implementation of the
/// `atom!` macro, but it's also usable on its own.
pub struct LazyAtom {
    s: &'static str,
    a: AtomicUsize,
}

impl LazyAtom {
    /// Create a new LazyAtom.
    ///
    /// This constructor is const, as all it does is set up the
    /// struct. No actual calls into SWI-Prolog happen at this stage.
    pub const fn new(s: &'static str) -> Self {
        Self {
            s,
            a: AtomicUsize::new(0),
        }
    }

    pub fn as_atom_t(&self) -> atom_t {
        let ptr = self.a.load(Ordering::Relaxed);
        if ptr == 0 {
            // we've not yet allocated an atom. let's do it now.
            let atom = Atom::new(self.s);
            let atom_ptr = atom.atom_ptr();

            let swapped = self.a.swap(atom_ptr, Ordering::Relaxed);
            if swapped == 0 {
                // nobody raced us to store this atom. This means
                // we're the first ones here. We need to ensure that
                // the atom refcount is incremented so that our
                // reference here in static memory remains valid.

                std::mem::forget(atom);
            }

            atom_ptr
        } else {
            ptr
        }
    }
}

impl AsAtom for LazyAtom {
    fn as_atom(&self) -> Atom {
        let ptr = self.as_atom_t();
        let atom = unsafe { Atom::wrap(ptr) };

        atom.increment_refcount();

        atom
    }

    fn as_atom_ptr(&self) -> (atom_t, Option<Atom>) {
        let ptr = self.as_atom_t();
        (ptr, None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn create_atom_and_retrieve_name() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let atom = Atom::new("the cow says moo");
        let name = atom.name();

        assert_eq!(name, "the cow says moo");
    }
    #[test]
    fn create_and_compare_some_atoms() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let a1 = Atom::new("foo");
        let a2 = Atom::new("bar");
        assert!(a1 != a2);
        let a3 = Atom::new("foo");
        assert_eq!(a1, a3);
    }

    #[test]
    fn clone_atom() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let a1 = Atom::new("foo");
        let a2 = a1.clone();

        assert_eq!(a1, a2);
    }

    #[test]
    fn create_atom_of_magic_length() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let len = std::mem::size_of::<usize>() - 1;
        let name = (0..len).map(|_| "a").collect::<String>();

        let _atom = Atom::new(&name);
    }

    #[test]
    fn unify_atoms() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = Atom::new("foo");
        let a2 = Atom::new("bar");

        let term = context.new_term_ref();

        assert!(term.unify(&a1).is_ok());
        assert!(term.unify(a1).is_ok());
        assert!(term.unify(a2).is_err());
    }

    #[test]
    fn unify_atoms_from_string() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = Atom::new("foo");
        let a2 = Atom::new("bar");

        let term = context.new_term_ref();

        assert!(term.unify(atomable("foo")).is_ok());
        assert!(term.unify(atomable("foo")).is_ok());
        assert!(term.unify(a1).is_ok());
        assert!(term.unify(atomable("bar")).is_err());
        assert!(term.unify(a2).is_err());
    }

    #[test]
    fn unify_from_atomable_turned_atom() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = atomable("foo").as_atom();
        let a2 = atomable("bar").as_atom();

        assert_eq!("foo", a1.name());

        let term = context.new_term_ref();

        assert!(term.unify(&a1).is_ok());
        assert!(term.unify(&a1).is_ok());
        assert!(term.unify(&a2).is_err());
    }

    #[test]
    fn retrieve_atom_temporarily() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = "foo".as_atom();
        let term = context.new_term_ref();
        term.unify(&a1).unwrap();
        term.get_atom(|a2| assert_eq!(&a1, a2.unwrap())).unwrap();
    }

    #[test]
    fn retrieve_atom() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = "foo".as_atom();
        let term = context.new_term_ref();
        term.unify(&a1).unwrap();
        let a2: Atom = term.get().unwrap();

        assert_eq!(a1, a2);
    }

    #[test]
    fn retrieve_atomable_temporarily() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = "foo".as_atom();
        let term = context.new_term_ref();
        term.unify(&a1).unwrap();
        term.get_atom_name(|a2| assert_eq!("foo", a2.unwrap()))
            .unwrap();
    }

    #[test]
    fn retrieve_atomable() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let a1 = "foo".as_atom();
        let term = context.new_term_ref();
        term.unify(&a1).unwrap();
        let a2: Atomable = term.get().unwrap();

        assert_eq!("foo", a2.name());
    }

    #[test]
    fn lazy_atom_to_atom() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let lazy = LazyAtom::new("moo");
        let a1 = lazy.as_atom();
        let a2 = lazy.as_atom();

        assert_eq!(a1, a2);
        let a3 = "moo".as_atom();
        assert_eq!(a1, a3);
    }

    use swipl_macros::atom;
    #[test]
    fn inline_atom_through_macro_ident() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let a1 = atom!(foo);
        let a2 = "foo".as_atom();
        assert_eq!(a1, a2);
    }

    #[test]
    fn inline_atom_through_macro_str() {
        let engine = Engine::new();
        let _activation = engine.activate();

        let a1 = atom!("bar");
        let a2 = "bar".as_atom();
        assert_eq!(a1, a2);
    }
}