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
//! Records - prolog terms in heap storage.
//!
//! SWI-Prolog allows the storing of terms into records, which are
//! opaque handles which remain valid until they are explicitely
//! erased. This module wraps such records, making the erase happen
//! automatically on drop of a wrapper object.

use super::fli;
use super::result::*;
use super::term::*;
use crate::{term_getable, term_putable, unifiable};

/// A recorded term.
///
/// Recorded terms live off the stack, and remain valid until
/// explicitely dropped. They can be used to copy terms to other
/// threads, or to keep a template of a term around for repeated use.
pub struct Record {
    record: fli::record_t,
}

impl Record {
    /// Extract a record from the given term.
    pub fn from_term(term: &Term) -> Record {
        term.assert_term_handling_possible();
        unsafe {
            let record = fli::PL_record(term.term_ptr());

            Record { record }
        }
    }

    /// Copy the recorded term into the given term reference.
    pub fn recorded(&self, term: &Term) -> PrologResult<()> {
        term.assert_term_handling_possible();
        unsafe { into_prolog_result(fli::PL_recorded(self.record, term.term_ptr()) != 0) }
    }
}

impl Clone for Record {
    fn clone(&self) -> Self {
        unsafe {
            let new_record = fli::PL_duplicate_record(self.record);
            assert!(self.record == new_record);

            Record { record: new_record }
        }
    }
}

impl Drop for Record {
    fn drop(&mut self) {
        unsafe {
            fli::PL_erase(self.record);
        }
    }
}

term_putable! {
    (self: Record, term) => {
        self.recorded(term).expect("expected record to be putable");
    }
}

term_getable! {
    (Record, term) => {
        Some(Record::from_term(term))
    }
}

unifiable! {
    (self:Record, term) => {
        // we need an extra term for this, so we have to be very careful about clearing it up before we're out of here.
        unsafe {
            let extra_term = fli::PL_new_term_ref();
            fli::PL_recorded(self.record, extra_term);
            let result = fli::PL_unify(term.term_ptr(), extra_term);
            fli::PL_reset_term_refs(extra_term);

            result != 0
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::*;
    use crate::engine::*;
    use crate::term;

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

        let term1 = term! {context: foo(bar(baz, quux))}.unwrap();

        let record = term1.record();

        let term2 = context.new_term_ref();
        term2.put(&record).unwrap();

        assert!(term1 == term2);
    }

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

        let term1 = term! {context: foo(bar(baz, _))}.unwrap();

        let record = term1.record();

        let term2 = context.new_term_ref();
        term2.put(&record).unwrap();

        // these two terms are not equal, as the variables should be different
        assert!(term1 != term2);
        // but they should still be unifiable
        term1.unify(term2).unwrap();
    }

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

        let term1 = term! {context: foo(bar(baz, quux))}.unwrap();

        let record: Record = term1.get().unwrap();

        let term2 = context.new_term_ref();
        term2.put(&record).unwrap();

        assert!(term1 == term2);
    }

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

            let term = term! {context: foo(bar(baz, quux))}.unwrap();
            record = term.record();
        }

        // engine has entirely been dropped at this point.
        // but we should still have the same term in our record!
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term1 = term! {context: foo(bar(baz, quux))}.unwrap();
        let term2 = context.new_term_ref();
        term2.put(&record).unwrap();

        assert!(term1 == term2);
    }

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

        let term1 = term! {context: foo(bar(baz, quux))}.unwrap();
        let record1 = term1.record();

        let record2 = record1.clone();
        let record3 = record2.clone();

        std::mem::drop(record1);
        std::mem::drop(record2);

        let term2 = context.new_term_ref();
        term2.put(&record3).unwrap();

        assert!(term1 == term2);
    }

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

        let term = term! {context: foo(bar(baz, quux))}.unwrap();
        let record = term.record();

        term.unify(&record).unwrap();
    }

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

        let term1 = term! {context: foo(bar(baz, quux))}.unwrap();
        let record = term1.record();
        let term2 = context.new_term_ref();

        term2.unify(&record).unwrap();
    }

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

        let term1 = term! {context: foo(bar(baz, quux))}.unwrap();
        let record = term1.record();
        let term2 = term! {context: something(completely(different))}.unwrap();

        assert!(!attempt(term2.unify(&record)).unwrap());
    }
}