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
use crate::real_std::{any::Any, fmt, marker::PhantomData, sync::Mutex};

use futures::{
    channel::oneshot,
    future::{self, Either, Shared},
    prelude::*,
    Future,
};

use crate::{
    api::{
        generic::A, Getable, OpaqueValue, OwnedFunction, Pushable, Pushed, RuntimeResult, Userdata,
        VmType, WithVM,
    },
    base::types::{self, ArcType},
    gc::{CloneUnrooted, GcPtr, GcRef, Move, Trace},
    thread::{RootedThread, ThreadInternal},
    value::{Cloner, Value},
    vm::Thread,
    Error, ExternModule, Result,
};

pub struct Lazy<T> {
    value: Mutex<Lazy_>,
    // No need to traverse this thread reference as any thread having a reference to this `Sender`
    // would also directly own a reference to the `Thread`
    thread: GcPtr<Thread>,
    _marker: PhantomData<T>,
}

impl<T> Userdata for Lazy<T>
where
    T: Any + Send + Sync,
{
    fn deep_clone<'gc>(
        &self,
        deep_cloner: &'gc mut Cloner,
    ) -> Result<GcRef<'gc, Box<dyn Userdata>>> {
        let value = self.value.lock().unwrap();

        // SAFETY During the `alloc` call the unrooted values are scanned through the `DataDef`
        unsafe {
            let cloned_value = match *value {
                Lazy_::Blackhole(..) => return Err(Error::Message("<<loop>>".into())),
                Lazy_::Thunk(ref value) => Lazy_::Thunk(deep_cloner.deep_clone(value)?.unrooted()),
                Lazy_::Value(ref value) => Lazy_::Value(deep_cloner.deep_clone(value)?.unrooted()),
            };
            let data: Box<dyn Userdata> = Box::new(Lazy {
                value: Mutex::new(cloned_value),
                thread: GcPtr::from_raw(deep_cloner.thread()),
                _marker: PhantomData::<A>,
            });
            deep_cloner.gc().alloc(Move(data))
        }
    }
}

impl<T> fmt::Debug for Lazy<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Lazy({:?})", *self.value.lock().unwrap())
    }
}

#[derive(Debug)]
enum Lazy_ {
    Blackhole(
        usize,
        Option<(oneshot::Sender<()>, Shared<oneshot::Receiver<()>>)>,
    ),
    Thunk(Value),
    Value(Value),
}

unsafe impl<T> Trace for Lazy<T> {
    impl_trace! { self, gc,
        match &mut *self.value.lock().unwrap() {
            Lazy_::Blackhole(..) => (),
            Lazy_::Thunk(value) => mark(value, gc),
            Lazy_::Value(value) => mark(value, gc),
        }
    }
}

impl<T> VmType for Lazy<T>
where
    T: VmType,
    T::Type: Sized,
{
    type Type = Lazy<T::Type>;

    fn make_type(vm: &Thread) -> ArcType {
        let env = vm.get_env();
        let alias = env.find_type_info("std.lazy.Lazy").unwrap().into_type();
        types::Type::app(alias, collect![T::make_type(vm)])
    }
}

fn force(
    WithVM { vm, value: lazy }: WithVM<&Lazy<A>>,
) -> impl Future<Output = RuntimeResult<Pushed<A>, Error>> {
    let mut lazy_lock = lazy.value.lock().unwrap();
    let lazy: GcPtr<Lazy<A>> = unsafe { GcPtr::from_raw(lazy) };
    let thunk = match *lazy_lock {
        Lazy_::Thunk(ref value) => Some(value),
        _ => None,
    };
    match thunk {
        Some(value) => {
            let mut function = OwnedFunction::<fn(()) -> OpaqueValue<RootedThread, A>>::from_value(
                vm,
                value.get_variants(),
            );
            *lazy_lock = Lazy_::Blackhole(vm as *const Thread as usize, None);
            drop(lazy_lock);
            let vm = vm.root_thread();
            Either::Right(Either::Left(async move {
                match function.call_async(()).await {
                    Ok(value) => {
                        {
                            let value = match lazy.thread.deep_clone_value(&vm, value.get_value()) {
                                Ok(value) => value,
                                Err(err) => return RuntimeResult::Panic(err.to_string().into()),
                            };
                            let mut lazy_lock = lazy.value.lock().unwrap();
                            match *lazy_lock {
                                Lazy_::Blackhole(_, ref mut x) => {
                                    if let Some((sender, _receiver)) = x.take() {
                                        sender.send(()).unwrap();
                                    }
                                }
                                _ => unreachable!(),
                            }
                            // SAFETY Rooted by being stored in the lazy value
                            unsafe {
                                *lazy_lock = Lazy_::Value(value.get_variant().unrooted());
                            }
                        }
                        value.vm_push(&mut vm.current_context()).unwrap();
                        RuntimeResult::Return(Pushed::default())
                    }
                    Err(err) => RuntimeResult::Panic(format!("{}", err).into()),
                }
            }))
        }
        None => match *lazy_lock {
            Lazy_::Blackhole(ref evaluating_thread, _)
                if *evaluating_thread == vm as *const Thread as usize =>
            {
                Either::Left(future::ready(RuntimeResult::Panic(
                    "<<loop>>".to_string().into(),
                )))
            }
            Lazy_::Blackhole(_, ref mut opt) => {
                // The current thread was not the one that started evaluating the lazy value.
                // Store a oneshot future which that thread fires once the lazy value has been
                // evaluated
                if opt.is_none() {
                    let (tx, rx) = oneshot::channel();
                    *opt = Some((tx, rx.shared()));
                }
                let ready = opt.as_ref().unwrap().1.clone();
                let vm = vm.root_thread();
                Either::Right(Either::Right(
                    ready
                        .map(move |_| {
                            let lazy_lock = lazy.value.lock().unwrap();
                            match *lazy_lock {
                                Lazy_::Value(ref value) => {
                                    vm.current_context().push(value);
                                    Pushed::default()
                                }
                                _ => unreachable!(),
                            }
                        })
                        .map(RuntimeResult::Return),
                ))
            }
            Lazy_::Value(ref value) => {
                vm.current_context().push(value);
                Either::Left(future::ready(RuntimeResult::Return(Pushed::default())))
            }
            _ => unreachable!(),
        },
    }
}

fn lazy(f: OpaqueValue<&Thread, fn(()) -> A>) -> Lazy<A> {
    // SAFETY We get rooted immediately on returning
    unsafe {
        Lazy {
            value: Mutex::new(Lazy_::Thunk(f.get_value().clone_unrooted())),
            thread: GcPtr::from_raw(f.vm()),
            _marker: PhantomData,
        }
    }
}

mod std {
    pub use crate::lazy;
}

pub fn load(vm: &Thread) -> Result<ExternModule> {
    ExternModule::new(
        vm,
        record! {
            type std::lazy::Lazy a => Lazy<A>,
            lazy => primitive!(1, std::lazy::lazy),
            force => primitive!(1, async fn std::lazy::force)
        },
    )
}