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
use crate::real_std::{
    any::Any,
    collections::VecDeque,
    fmt,
    marker::PhantomData,
    slice,
    sync::{Arc, Mutex},
    time::Duration,
};

use futures::{
    future::{self, Either},
    prelude::*,
    task::Poll,
    try_join,
};

use crate::base::{
    kind::Kind,
    types::{ArcType, KindedIdent, Type},
};

use crate::{
    api::{
        generic::{A, B},
        Function, FunctionRef, Generic, Getable, OpaqueRef, OpaqueValue, OwnedFunction, Pushable,
        Pushed, RuntimeResult, Unrooted, VmType, WithVM, IO,
    },
    gc::{self, CloneUnrooted, GcPtr, Trace},
    stack::{ClosureState, ExternState, State},
    thread::{ActiveThread, ThreadInternal},
    types::VmInt,
    value::{Callable, Userdata, Value, ValueRepr},
    vm::{RootedThread, Thread},
    Error, ExternModule, Result as VmResult, Variants,
};

pub struct Sender<T> {
    // 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>,
    queue: Arc<Mutex<VecDeque<Value>>>,
    _element_type: PhantomData<T>,
}

impl<T> Userdata for Sender<T> where T: Any + Send + Sync + fmt::Debug {}

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

unsafe impl<T> Trace for Sender<T> {
    impl_trace! { self, _gc,
        // No need to traverse in Sender as values can only be accessed through Receiver
        {}
    }
}

impl<T> Sender<T> {
    fn send(&self, value: &Value) {
        // SAFETY Rooted when stored in `queue`
        unsafe {
            self.queue.lock().unwrap().push_back(value.clone_unrooted());
        }
    }
}

unsafe impl<T> Trace for Receiver<T> {
    impl_trace_fields! { self, gc; queue }
}

pub struct Receiver<T> {
    queue: Arc<Mutex<VecDeque<Value>>>,
    _element_type: PhantomData<T>,
}

impl<T> Userdata for Receiver<T> where T: Any + Send + Sync + fmt::Debug {}

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

impl<T> Receiver<T> {
    fn try_recv(&self) -> Result<Value, ()> {
        self.queue.lock().unwrap().pop_front().ok_or(())
    }
}

impl<T: VmType> VmType for Sender<T>
where
    T::Type: Sized,
{
    type Type = Sender<T::Type>;
    fn make_type(vm: &Thread) -> ArcType {
        let env = vm.get_env();
        let symbol = env
            .find_type_info("std.channel.Sender")
            .unwrap()
            .name
            .clone();

        let k = vm.global_env().type_cache().kind_cache.typ();
        Type::app(
            Type::ident(KindedIdent {
                name: symbol,
                typ: Kind::function(k.clone(), k),
            }),
            collect![T::make_type(vm)],
        )
    }
}

impl<T: VmType> VmType for Receiver<T>
where
    T::Type: Sized,
{
    type Type = Receiver<T::Type>;
    fn make_type(vm: &Thread) -> ArcType {
        let symbol = vm
            .get_env()
            .find_type_info("std.channel.Receiver")
            .unwrap()
            .name
            .clone();
        let k = vm.global_env().type_cache().kind_cache.typ();
        Type::app(
            Type::ident(KindedIdent {
                name: symbol,
                typ: Kind::function(k.clone(), k),
            }),
            collect![T::make_type(vm)],
        )
    }
}

field_decl! { sender, receiver }

pub type ChannelRecord<S, R> = record_type!(sender => S, receiver => R);

/// FIXME The dummy `a` argument should not be needed to ensure that the channel can only be used
/// with a single type
fn channel(WithVM { vm, .. }: WithVM<Generic<A>>) -> IO<ChannelRecord<Sender<A>, Receiver<A>>> {
    let sender = Sender {
        thread: unsafe { GcPtr::from_raw(vm) },
        queue: Arc::new(Mutex::new(VecDeque::new())),
        _element_type: PhantomData,
    };
    let receiver = Receiver {
        queue: sender.queue.clone(),
        _element_type: PhantomData,
    };
    IO::Value(record_no_decl!(sender => sender, receiver => receiver))
}

fn recv(receiver: &Receiver<A>) -> IO<Result<Unrooted<A>, ()>> {
    IO::Value(receiver.try_recv().map_err(|_| ()).map(Unrooted::from))
}

fn send(sender: &Sender<A>, value: Generic<A>) -> IO<Result<(), ()>> {
    let value = match sender
        .thread
        .deep_clone_value(&sender.thread, value.get_value())
    {
        Ok(value) => value,
        Err(_) => return IO::Value(Err(())),
    };
    IO::Value(Ok(sender.send(value.get_value())))
}

async fn resume(child: RootedThread) -> IO<Result<(), String>> {
    let result = future::lazy(|cx| child.resume(cx)).await;
    match result {
        Poll::Pending => IO::Value(Ok(())),
        Poll::Ready(result) => match result {
            Ok(_) => IO::Value(Ok(())),
            Err(Error::Dead) => IO::Value(Err("Attempted to resume a dead thread".into())),
            Err(err) => {
                let fmt = format!("{}", err);
                IO::Exception(fmt)
            }
        },
    }
}

async fn yield_(_: ()) {
    let mut first = true;
    future::poll_fn(|cx| {
        if first {
            cx.waker().wake_by_ref();
            first = false;
            Poll::Pending
        } else {
            Poll::Ready(())
        }
    })
    .await
}

fn spawn<'vm>(value: WithVM<'vm, Function<&'vm Thread, IO<()>>>) -> IO<RootedThread> {
    spawn_(value).into()
}
fn spawn_<'vm>(value: WithVM<'vm, Function<&'vm Thread, IO<()>>>) -> VmResult<RootedThread> {
    let thread = value.vm.new_thread()?;
    {
        let mut context = thread.current_context();
        let value_variant = value.value.get_variant();
        let (callable, args) = match value_variant.get_repr() {
            ValueRepr::Closure(closure) => {
                value_variant.clone().vm_push(&mut context)?;
                (
                    construct_gc!(State::Closure(@ construct_gc!(ClosureState {
                        @ closure,
                        instruction_index: 0,
                    }))),
                    &[][..],
                )
            }
            ValueRepr::Function(function) => {
                value_variant.clone().vm_push(&mut context)?;
                (
                    construct_gc!(State::Extern(@ ExternState::new(function))),
                    &[][..],
                )
            }
            ValueRepr::PartialApplication(function) => (
                match &function.function {
                    Callable::Closure(closure) => {
                        context.push(construct_gc!(ValueRepr::Closure(@closure)));
                        construct_gc!(State::Closure(@ construct_gc!(ClosureState {
                            @ closure,
                            instruction_index: 0,
                        })))
                    }

                    Callable::Extern(function) => {
                        context.push(construct_gc!(ValueRepr::Function(@function)));
                        construct_gc!(State::Extern(@ ExternState::new(function)))
                    }
                },
                &function.args[..],
            ),
            _ => {
                value_variant.clone().vm_push(&mut context)?;
                (gc::Borrow::from_static(State::Unknown), &[][..])
            }
        };
        for a in args {
            context.push(a);
        }
        context.push(ValueRepr::Int(0));
        context
            .context()
            .stack
            .enter_scope(1 + args.len() as u32, &*callable)?;
    }
    Ok(thread)
}

type Action<T> = fn() -> IO<OpaqueValue<RootedThread, Pushed<T>>>;

#[cfg(target_arch = "wasm32")]
fn spawn_on<'vm>(
    _thread: RootedThread,
    _action: WithVM<'vm, FunctionRef<Action<A>>>,
) -> IO<OpaqueValue<&'vm Thread, IO<A>>> {
    IO::Exception("spawn_on requires the `tokio` crate".to_string())
}

#[cfg(not(target_arch = "wasm32"))]
fn spawn_on<'vm>(
    thread: RootedThread,
    action: WithVM<'vm, FunctionRef<Action<A>>>,
) -> IO<Pushed<IO<A>>> {
    struct SpawnFuture<F>(future::Shared<F>)
    where
        F: Future;

    impl<F> fmt::Debug for SpawnFuture<F>
    where
        F: Future,
    {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "Future")
        }
    }

    impl<F> Userdata for SpawnFuture<F>
    where
        F: Future + Send + 'static,
        F::Output: Send + Sync,
    {
    }

    unsafe impl<F> Trace for SpawnFuture<F>
    where
        F: Future,
    {
        impl_trace! { self, _gc, { } }
    }

    impl<F> VmType for SpawnFuture<F>
    where
        F: Future,
        F::Output: VmType,
    {
        type Type = <F::Output as VmType>::Type;
    }

    fn push_future_wrapper<G>(context: &mut ActiveThread, _: &G)
    where
        G: Future<Output = IO<OpaqueValue<RootedThread, Pushed<A>>>> + Send + 'static,
    {
        fn future_wrapper<F>(
            data: &SpawnFuture<F>,
        ) -> impl Future<Output = IO<OpaqueValue<RootedThread, Pushed<A>>>>
        where
            F: Future<Output = IO<OpaqueValue<RootedThread, Pushed<A>>>> + Send + 'static,
        {
            let future = data.0.clone();
            future
        }

        primitive!(1, "unknown", async fn future_wrapper::<G>,
            [G]
            [G: Future<Output = IO<OpaqueValue<RootedThread, Pushed<A>>, >>
                + Send
                + 'static,
            ]
        )
        .vm_push(context)
        .unwrap();
    }
    use crate::value::PartialApplicationDataDef;

    let WithVM { vm, value: action } = action;
    let mut action = OwnedFunction::<Action<A>>::from_value(&thread, action.get_variant());

    let future = async move {
        match action.call_async().await {
            Ok(io) => io,
            Err(err) => IO::Exception(err.to_string()),
        }
    };

    let mut context = vm.current_context();

    push_future_wrapper(&mut context, &future);

    SpawnFuture(future.shared()).vm_push(&mut context).unwrap();

    let mut context = context.context();
    let callable = match context.stack[context.stack.len() - 2].get_repr() {
        ValueRepr::Function(ext) => construct_gc!(Callable::Extern(@ ext)),
        _ => unreachable!(),
    };

    let fields = slice::from_ref(context.stack.last().unwrap());
    let def = construct_gc!(PartialApplicationDataDef(@callable, fields));
    let value = Variants::from(context.gc.alloc(def).unwrap());

    context.stack.pop_many(2);
    context.stack.push(value);

    IO::Value(Pushed::default())
}

fn join(
    WithVM { vm: vm_a, value: a }: WithVM<OpaqueRef<IO<A>>>,
    b: OpaqueRef<IO<B>>,
) -> impl Future<Output = RuntimeResult<IO<(Generic<A>, Generic<B>)>, Error>> {
    let vm_b = match vm_a.new_thread() {
        Ok(vm) => vm,
        Err(err) => return Either::Right(future::ready(RuntimeResult::Panic(err))),
    };

    let mut action_a: OwnedFunction<fn(()) -> OpaqueValue<RootedThread, A>> =
        Getable::from_value(&vm_a, a.get_variant());
    let mut action_b: OwnedFunction<fn(()) -> OpaqueValue<RootedThread, B>> =
        Getable::from_value(&vm_b, b.get_variant());

    Either::Left(async move {
        let result = try_join!(action_a.call_async(()), action_b.call_async(()));
        trace!("join done: {:?}", result);
        result.map(IO::Value).into()
    })
}

fn new_thread(WithVM { vm, .. }: WithVM<()>) -> IO<RootedThread> {
    match vm.new_thread() {
        Ok(thread) => IO::Value(thread),
        Err(err) => IO::Exception(err.to_string()),
    }
}

fn sleep(ms: VmInt) -> IO<()> {
    ::std::thread::sleep(Duration::from_millis(ms as u64));
    IO::Value(())
}

fn interrupt(thread: RootedThread) -> IO<()> {
    thread.interrupt();
    IO::Value(())
}

mod std {
    pub use crate::channel;
    pub mod thread {
        pub use crate::channel as prim;
    }
}

pub fn load_channel<'vm>(vm: &'vm Thread) -> VmResult<ExternModule> {
    let _ = vm.register_type::<Sender<A>>("std.channel.Sender", &["a"]);
    let _ = vm.register_type::<Receiver<A>>("std.channel.Receiver", &["a"]);

    ExternModule::new(
        vm,
        record! {
            type Sender a => Sender<A>,
            type Receiver a => Sender<A>,
            channel => primitive!(1, std::channel::channel),
            recv => primitive!(1, std::channel::recv),
            send => primitive!(2, std::channel::send),
        },
    )
}

pub fn load_thread<'vm>(vm: &'vm Thread) -> VmResult<ExternModule> {
    ExternModule::new(
        vm,
        record! {
            type Thread => Thread,
            resume => primitive!(1, async fn std::thread::prim::resume),
            (yield_ "yield") => primitive!(1, "std.thread.prim.yield", async fn std::thread::prim::yield_),
            spawn => primitive!(1, std::thread::prim::spawn),
            spawn_on => primitive!(2, std::thread::prim::spawn_on),
            new_thread => primitive!(1, std::thread::prim::new_thread),
            interrupt => primitive!(1, std::thread::prim::interrupt),
            sleep => primitive!(1, std::thread::prim::sleep),
            join => primitive!(2, async fn std::thread::prim::join),
        },
    )
}