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
use core::ptr::null_mut;

use crate::{
    convert::*,
    error::{Result, ToLuaResult},
    ffi::*,
    lua::ArcLuaInner,
    luaapi::{ThreadStatus, Type, UnsafeLuaApi, NOREF},
    prelude::Reference,
    state::State,
    value::{Function, ValRef},
};

pub struct Coroutine {
    inner: Option<ArcLuaInner>,
    pub(crate) state: State,
    nres: i32,
}

unsafe impl Send for Coroutine {}

impl core::ops::Deref for Coroutine {
    type Target = State;

    fn deref(&self) -> &Self::Target {
        &self.state
    }
}

impl Drop for Coroutine {
    fn drop(&mut self) {
        self.push_nil();
        self.raw_setp(LUA_REGISTRYINDEX, self.as_ptr());
    }
}

impl Coroutine {
    // [-0, +0]
    pub fn empty(s: &State) -> Self {
        let result = s.new_thread();
        let result = unsafe { Self::init(result) };
        s.pop(1);
        result
    }

    unsafe fn init(l: *mut lua_State) -> Self {
        let state = State::from_raw_state(l);
        // assert_eq!(state.type_of(1), Type::Function);

        state.push_thread();
        state.raw_setp(LUA_REGISTRYINDEX, l);

        let inner = state.try_lua_inner();
        #[cfg(feature = "log")]
        if inner.is_none() {
            log::warn!("lua inner is not available");
        }
        Self {
            inner,
            state,
            nres: 0,
        }
    }

    pub fn new(fun: ValRef) -> Result<Self> {
        Ok(match fun.type_of() {
            Type::Function => unsafe {
                let s = fun.state;
                let l = s.new_thread();
                fun.ensure_top();
                s.xmove(l, 1);
                Self::init(l)
            },
            Type::Thread => unsafe {
                let l = fun
                    .state
                    .to_thread(fun.index)
                    .ok_or("to_thread")
                    .convert_error()?;
                Self::init(l)
            },
            _ => return Err("invalid coroutine type").convert_error(),
        })
    }

    /// Get the function associated to this coroutine
    #[inline(always)]
    pub fn func(&self) -> Result<Function> {
        self.val(1).try_into()
    }

    #[doc(hidden)]
    pub fn resume<'a, A: ToLuaMulti, R: FromLuaMulti<'a>>(&'a mut self, args: A) -> Result<R> {
        self.pop(self.nres);
        self.state
            .check_stack(args.value_count().unwrap_or(10) as i32)?;
        match self
            .state
            .resume(null_mut(), self.push_multi(args)? as _, &mut self.nres)
        {
            ThreadStatus::Ok | ThreadStatus::Yield => {
                let fidx = self.get_top() - self.nres;
                self.set_top(fidx + R::COUNT as i32);
                self.nres = R::COUNT as i32;
                R::from_lua_multi(self, self.abs_index(-(R::COUNT as i32)))
            }
            err => Err(self.status_to_error(err).unwrap_err()),
        }
    }
}

impl FromLua<'_> for Coroutine {
    fn from_lua(s: &State, val: ValRef) -> Result<Self> {
        Self::new(val)
    }
}

/// Coroutine with a lua value reference, which will be unreference when drop
pub struct CoroutineWithRef(pub Coroutine, pub Reference);

impl CoroutineWithRef {
    pub fn take<'l>(&mut self, lua: &'l State) -> Result<ValRef<'l>> {
        let result = lua.registry().take_reference(self.1)?;
        self.1 = NOREF;
        Ok(result)
    }
}

impl Drop for CoroutineWithRef {
    fn drop(&mut self) {
        if !self.1.is_no_ref() {
            self.0.registry().unreference(self.1);
        }
    }
}