Skip to main content

lua_stdlib/
coro_lib.rs

1//! Coroutine library — the `coroutine.*` standard-library table: `create`,
2//! `resume`, `running`, `status`, `wrap`, `yield`, `isyieldable`, and `close`.
3//!
4//! This module is the **cold shell** around coroutine execution: argument
5//! checking, the `COS_*` status-string mapping, the `wrap` closure setup, the
6//! cross-thread argument/result transfer scaffolding, and version-gated
7//! registration. The actual control transfer — resume/yield stack save and
8//! restore — lives in `lua-vm` (`lua_vm::do_::lua_resume` / `lua_yieldk`) and is
9//! load-bearing; this module calls into it but does not implement it.
10//!
11//! # Graduation (Idiomatization Sprint 2, Phase 2 — `coroutine`)
12//!
13//! Idiomatized AROUND the resume/yield machinery, never through it. The
14//! behavioral net guarding this module's cold surface is
15//! `crates/lua-stdlib/tests/coro_strengthen.rs` (the version seams:
16//! `running` arity 5.1-vs-5.2+, `isyieldable` 5.3+, `close` 5.4+ + its
17//! suspended→dead transition and the 5.4-errors/5.5-unwinds self-close, the
18//! resume/wrap error wording, status transitions across a yield) plus the
19//! official `coroutine.lua` suite and `multiversion_oracle`. Net-strengthening
20//! caught one real bug: the resume-of-running error used the 5.2+ wording on
21//! 5.1 — fixed via `non_suspended_resume_message`. Left load-bearing: the
22//! cross-thread snapshot/rooting (`RootedThreadBorrow`, the resume-pool
23//! buffers, the GC stack snapshots), the `LuaThreadClose` panic-unwind path
24//! that implements 5.5 self-close, and every version gate.
25
26use std::cell::Cell;
27use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
28use std::sync::OnceLock;
29
30use crate::state_stub::{lua_CFunction, upvalue_index, LuaState, LuaStateStubExt as _};
31use lua_types::{error::LuaError, gc::GcRef, value::LuaValue, LuaStatus, LuaThreadClose, LuaType};
32
33thread_local! {
34    /// Per-thread suppression depth for [`LuaThreadClose`] unwind payloads.
35    ///
36    /// Incremented for the duration of each `catch_unwind` resume window by a
37    /// [`SuppressGuard`], decremented (on every path, including a panic
38    /// unwinding through the guard) when the guard drops. The process-global
39    /// chaining hook installed by [`ensure_chaining_panic_hook`] silently
40    /// swallows a `LuaThreadClose` payload only while this counter is non-zero
41    /// **on the panicking thread**, and delegates every other payload — and
42    /// `LuaThreadClose` outside a resume window — to the previously installed
43    /// hook.
44    ///
45    /// It is a counter rather than a bool because resumes nest: a coroutine
46    /// resumed from inside another resume must keep the suppression active for
47    /// the outer window after the inner one exits. Because the state is
48    /// thread-local, a `LuaThreadClose` unwind suppressed on one OS thread
49    /// never silences a simultaneous unrelated panic on another OS thread —
50    /// that thread reads its own zero counter and reaches the previous hook.
51    static THREAD_CLOSE_SUPPRESS: Cell<u32> = const { Cell::new(0) };
52}
53
54/// One-shot install guard for the process-global chaining panic hook.
55static CHAINING_HOOK_INSTALLED: OnceLock<()> = OnceLock::new();
56
57/// RAII increment of [`THREAD_CLOSE_SUPPRESS`] for one resume window.
58///
59/// Constructing the guard increments the per-thread counter; dropping it
60/// decrements. `catch_unwind` returns normally even when it catches a panic,
61/// so the decrement in `Drop` covers both the caught-panic and the
62/// normal-return paths; an uncaught panic unwinding through the guard runs the
63/// same `Drop`, so the counter invariant holds on every exit.
64struct SuppressGuard;
65
66impl SuppressGuard {
67    fn new() -> Self {
68        THREAD_CLOSE_SUPPRESS.with(|c| c.set(c.get() + 1));
69        SuppressGuard
70    }
71}
72
73impl Drop for SuppressGuard {
74    fn drop(&mut self) {
75        THREAD_CLOSE_SUPPRESS.with(|c| c.set(c.get().saturating_sub(1)));
76    }
77}
78
79/// Install — exactly once for the process — a chaining panic hook that
80/// suppresses the default panic printout for [`LuaThreadClose`] unwind
81/// payloads while a resume window is active on the panicking thread, and
82/// delegates everything else to the hook that was current at install time.
83///
84/// `LuaThreadClose` is the internal unwind used by `coroutine.close` (5.5
85/// self-close) and coroutine teardown; it is control flow, not a Rust runtime
86/// fault, so it must never reach the default printer. The previous per-resume
87/// implementation paid 3–4 heap allocations plus four global hook-lock
88/// operations on every resume to install and tear this suppression down around
89/// each `catch_unwind`. This installs the hook once and scopes the suppression
90/// with a thread-local counter ([`THREAD_CLOSE_SUPPRESS`]) instead, so the
91/// per-resume cost is two TLS counter writes.
92///
93/// Suppression is gated on the counter so it is active only inside a resume
94/// window: a `LuaThreadClose` that somehow escaped a resume would still reach
95/// the previous hook, and — because the counter is thread-local — a
96/// `LuaThreadClose` suppressed on one OS thread never silences a simultaneous
97/// unrelated panic on another OS thread.
98///
99/// Accepted tradeoff (T2-B2): an embedder that calls `std::panic::set_hook`
100/// **after** lua-rs's first resume displaces this chained hook permanently —
101/// the previous implementation re-installed the suppression on every resume,
102/// so it won each resume window even against a later embedder hook. Embedders
103/// that need a custom hook should install it before the first resume; the
104/// chaining hook then captures and delegates to it.
105fn ensure_chaining_panic_hook() {
106    CHAINING_HOOK_INSTALLED.get_or_init(|| {
107        let previous = std::panic::take_hook();
108        std::panic::set_hook(Box::new(move |info| {
109            let suppress = info.payload().downcast_ref::<LuaThreadClose>().is_some()
110                && THREAD_CLOSE_SUPPRESS.with(|c| c.get()) > 0;
111            if !suppress {
112                previous(info);
113            }
114        }));
115    });
116}
117
118// ── Coroutine status codes ────────────────────────────────────────────────────
119
120/// Coroutine is the currently running thread.
121const COS_RUN: i32 = 0;
122
123/// Coroutine has finished execution or encountered an error.
124const COS_DEAD: i32 = 1;
125
126/// Coroutine is suspended — either yielded or not yet started.
127const COS_YIELD: i32 = 2;
128
129/// Coroutine is normal — it resumed another coroutine and is waiting.
130const COS_NORM: i32 = 3;
131
132/// Human-readable status strings indexed by the `COS_*` constants above,
133/// pushed onto the Lua stack as byte strings by `coroutine.status`.
134const STAT_NAMES: [&[u8]; 4] = [b"running", b"dead", b"suspended", b"normal"];
135
136// ── Registration table ────────────────────────────────────────────────────────
137
138/// Registration table for the `coroutine` standard library — one
139/// `(name_bytes, function_pointer)` entry per `coroutine.*` function. The
140/// per-version roster (which entries actually register) is filtered in
141/// [`open_coroutine`]; this table is the full superset.
142pub const CO_FUNCS: &[(&[u8], lua_CFunction)] = &[
143    (b"create", co_create),
144    (b"resume", co_resume),
145    (b"running", co_running),
146    (b"status", co_status),
147    (b"wrap", co_wrap),
148    (b"yield", co_yield),
149    (b"isyieldable", co_isyieldable),
150    (b"close", co_close),
151];
152
153// ── Internal helpers ──────────────────────────────────────────────────────────
154
155/// Retrieves the coroutine thread at stack index 1, raising a type error if
156/// the argument is absent or not a thread.
157///
158/// The error routes through `arg_error_impl` so it carries the calling
159/// function's name (`bad argument #1 to 'coroutine.resume' (...)` on 5.2+; `'?'`
160/// on 5.1). The `extramsg` body is version-gated to match each reference:
161/// 5.1/5.2 say `coroutine expected`, 5.3 says `thread expected`, and 5.4/5.5 use
162/// `luaL_argexpected` which appends `, got <type>`.
163fn get_co(state: &mut LuaState) -> Result<GcRef<lua_types::value::LuaThread>, LuaError> {
164    let co = state.to_thread(1);
165    if let Some(co) = co {
166        return Ok(co);
167    }
168    Err(thread_arg_error(state, 1))
169}
170
171/// Build the version-correct "expected a coroutine/thread" argument error for
172/// argument `arg`, carrying the calling function's name via `arg_error_impl`.
173///
174/// See [`get_co`] for the per-version message forms.
175fn thread_arg_error(state: &mut LuaState, arg: i32) -> LuaError {
176    use lua_types::LuaVersion;
177    let version = state.global().lua_version;
178    if matches!(version, LuaVersion::V51 | LuaVersion::V52) {
179        return lua_vm::debug::arg_error_impl(state, arg, b"coroutine expected");
180    }
181    if matches!(version, LuaVersion::V53) {
182        return lua_vm::debug::arg_error_impl(state, arg, b"thread expected");
183    }
184    let got = state.value_at(arg);
185    let got_name = match state.full_type_name(&got) {
186        Ok(n) => n,
187        Err(e) => return e,
188    };
189    let mut extramsg = b"thread expected, got ".to_vec();
190    extramsg.extend_from_slice(&got_name);
191    lua_vm::debug::arg_error_impl(state, arg, &extramsg)
192}
193
194fn get_opt_co(state: &mut LuaState) -> Result<GcRef<lua_types::value::LuaThread>, LuaError> {
195    if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
196        && state.type_at(1) == LuaType::None
197    {
198        let id = state.global().current_thread_id;
199        return state
200            .global()
201            .thread_value_for(id)
202            .ok_or_else(|| LuaError::runtime(format_args!("current thread is not registered")));
203    }
204    get_co(state)
205}
206
207/// Returns one of the `COS_*` status codes describing `co` relative to the
208/// calling thread `state`, reading the target coroutine's `status`,
209/// call-frame depth, and stack top through `GlobalState::threads`:
210///
211/// - `co` is the current thread → `COS_RUN` (running).
212/// - `co` is the main thread (never stored in the registry) → `COS_NORM`.
213/// - `co` is not in the registry → `COS_DEAD`.
214/// - otherwise classify by the registered thread's `status`: a yielded thread
215///   is `COS_YIELD`; a thread with live frames (it resumed a child) is
216///   `COS_NORM`; an `Ok` thread with no frames is `COS_DEAD` if its stack is
217///   empty, else `COS_YIELD` (suspended at its initial frame, function still
218///   staged on the stack).
219///
220/// The transition table this produces is pinned by `status_transitions_*` in
221/// `tests/coro_strengthen.rs`.
222fn aux_status(state: &mut LuaState, co: &GcRef<lua_types::value::LuaThread>) -> i32 {
223    let co_id = co.id;
224    let entry_rc = {
225        let g = state.global();
226        if co_id == g.current_thread_id {
227            return COS_RUN;
228        }
229        if co_id == g.main_thread_id {
230            return COS_NORM;
231        }
232        match g.threads.get(&co_id) {
233            Some(e) => e.state.clone(),
234            None => return COS_DEAD,
235        }
236    };
237    let co_state = match entry_rc.try_borrow() {
238        Ok(state) => state,
239        Err(_) => {
240            // A thread already mutably borrowed is one that resumed a child and
241            // is waiting up the call stack — i.e. a normal (active, not
242            // suspended/dead) coroutine, so report COS_NORM.
243            return COS_NORM;
244        }
245    };
246    let raw_status = co_state.status;
247    if raw_status == LuaStatus::Yield as u8 {
248        return COS_YIELD;
249    }
250    if raw_status != LuaStatus::Ok as u8 {
251        return COS_DEAD;
252    }
253    let has_frames = co_state.ci.as_usize() > 0;
254    if has_frames {
255        return COS_NORM;
256    }
257    let ci_func = co_state.call_info[0].func.0;
258    let top = co_state.top.0;
259    let lua_gettop = top as i64 - ci_func as i64 - 1;
260    if lua_gettop == 0 {
261        COS_DEAD
262    } else {
263        COS_YIELD
264    }
265}
266
267/// Transfers `narg` arguments from `state` to `co`, resumes the coroutine,
268/// then transfers results (or error message) back to `state`.
269///
270/// Returns the number of result values (≥ 0) on success, or `-1` on error
271/// with the error object left on top of `state`'s stack.
272///
273/// Cross-thread open-upvalue mirroring rides the resume boundary: before
274/// yielding control, the parent's open-upvalue values are snapshotted into
275/// `GlobalState::cross_thread_upvals` so the coroutine body can read and write
276/// them through `LuaState::upvalue_get` / `upvalue_set`. On resume return, the
277/// (possibly mutated) cache entries are flushed back into the parent's stack.
278/// This is the alternative to a stack-refactor that would let the parent's
279/// `LuaState` be reached through `Rc<RefCell<_>>` while it is held by `&mut`
280/// further up the call stack. Load-bearing: do not collapse the snapshot /
281/// flush handshake — it is what keeps cross-thread upvalues coherent and
282/// rooted across the resume.
283fn aux_resume(state: &mut LuaState, co: GcRef<lua_types::value::LuaThread>, narg: i32) -> i32 {
284    let co_id = co.id;
285    let entry_rc = {
286        let g = state.global();
287        match g.threads.get(&co_id) {
288            Some(e) => e.state.clone(),
289            None => {
290                drop(g);
291                push_lit_or_nil(state, b"cannot resume dead coroutine");
292                return -1;
293            }
294        }
295    };
296    let parent_thread_id = state.global().current_thread_id;
297    let top_before = state.get_top();
298    if top_before < narg {
299        push_lit_or_nil(state, b"not enough arguments to resume");
300        return -1;
301    }
302    let first_arg_idx = top_before - narg + 1;
303    let mut args = pop_resume_value_buf(state);
304    args.extend((first_arg_idx..=top_before).map(|i| state.value_at(i)));
305    lua_vm::api::set_top(state, (top_before - narg) as i32).ok();
306
307    let mut parent_open_upval_slots = pop_resume_slot_buf(state);
308    parent_open_upval_slots.extend(state.openupval.iter().filter_map(|uv| {
309        uv.try_open_payload()
310            .map(|(thread_id, idx)| (thread_id as u64, idx))
311    }));
312    {
313        let mut g = state.global_mut();
314        for (tid, idx) in &parent_open_upval_slots {
315            let val = state.get_at(*idx);
316            g.cross_thread_upvals.insert((*tid, *idx), val);
317        }
318    }
319
320    push_parent_gc_snapshot(state);
321
322    let (status, results_or_err): (LuaStatus, Vec<LuaValue>) = {
323        let mut co_state = match entry_rc.try_borrow_mut() {
324            Ok(b) => b,
325            Err(_) => {
326                pop_parent_gc_snapshot(state);
327                let mut g = state.global_mut();
328                for (tid, idx) in &parent_open_upval_slots {
329                    g.cross_thread_upvals.remove(&(*tid, *idx));
330                }
331                drop(g);
332                return_resume_slot_buf(state, parent_open_upval_slots);
333                return_resume_value_buf(state, args);
334                let msg = non_suspended_resume_message(state);
335                push_lit_or_nil(state, msg);
336                return -1;
337            }
338        };
339        if co_state.check_stack(narg + 1).is_err() {
340            drop(co_state);
341            pop_parent_gc_snapshot(state);
342            let mut g = state.global_mut();
343            for (tid, idx) in &parent_open_upval_slots {
344                g.cross_thread_upvals.remove(&(*tid, *idx));
345            }
346            drop(g);
347            return_resume_slot_buf(state, parent_open_upval_slots);
348            return_resume_value_buf(state, args);
349            push_lit_or_nil(state, b"too many arguments to resume");
350            return -1;
351        }
352        for v in args.drain(..) {
353            co_state.push(v);
354        }
355        return_resume_value_buf(state, args);
356        co_state.global_mut().current_thread_id = co_id;
357        let mut nres: i32 = 0;
358        ensure_chaining_panic_hook();
359        let resume_result = {
360            let _suppress = SuppressGuard::new();
361            catch_unwind(AssertUnwindSafe(|| {
362                lua_vm::do_::lua_resume(&mut *co_state, Some(state), narg, &mut nres)
363            }))
364        };
365        co_state.global_mut().current_thread_id = parent_thread_id;
366        let status = match resume_result {
367            Ok(status) => status,
368            Err(payload) => {
369                if let Some(close) = payload.downcast_ref::<LuaThreadClose>() {
370                    close.0
371                } else {
372                    resume_unwind(payload);
373                }
374            }
375        };
376        let co_top = co_state.top_idx().0 as i32;
377        let ci_func = co_state.current_call_info().func.0 as i32;
378        let count = if status == LuaStatus::Ok || status == LuaStatus::Yield {
379            nres
380        } else {
381            1
382        };
383        let start = co_top - count;
384        let mut vals = pop_resume_value_buf(state);
385        vals.extend((start..co_top).map(|i| co_state.get_at(lua_vm::state::StackIdx(i as u32))));
386        let new_co_top = if status == LuaStatus::Ok || status == LuaStatus::Yield {
387            (co_top - count).max(ci_func + 1)
388        } else {
389            co_top - count
390        };
391        co_state.set_top(lua_vm::state::StackIdx(new_co_top.max(0) as u32));
392        (status, vals)
393    };
394
395    // Pop the parent stack snapshot — the coroutine has yielded or returned.
396    pop_parent_gc_snapshot(state);
397
398    {
399        let mut flush = pop_resume_flush_buf(state);
400        let mut g = state.global_mut();
401        for (tid, idx) in &parent_open_upval_slots {
402            if let Some(v) = g.cross_thread_upvals.remove(&(*tid, *idx)) {
403                flush.push((*idx, v));
404            }
405        }
406        drop(g);
407        for (idx, v) in flush.drain(..) {
408            state.set_at(idx, v);
409        }
410        return_resume_flush_buf(state, flush);
411    }
412    return_resume_slot_buf(state, parent_open_upval_slots);
413
414    let mut results_or_err = results_or_err;
415    match status {
416        LuaStatus::Ok | LuaStatus::Yield => {
417            if state.check_stack(results_or_err.len() as i32 + 1).is_err() {
418                return_resume_value_buf(state, results_or_err);
419                push_lit_or_nil(state, b"too many results to resume");
420                return -1;
421            }
422            let n = results_or_err.len();
423            for v in results_or_err.drain(..) {
424                state.push(v);
425            }
426            return_resume_value_buf(state, results_or_err);
427            n as i32
428        }
429        _ => {
430            for v in results_or_err.drain(..) {
431                state.push(v);
432            }
433            return_resume_value_buf(state, results_or_err);
434            -1
435        }
436    }
437}
438
439fn push_parent_gc_snapshot(state: &mut LuaState) {
440    let top = (state.top_idx().0 as usize).min(state.stack.len());
441    let (mut stack_snapshot, mut upval_snapshot) = {
442        let mut g = state.global_mut();
443        (
444            g.snapshot_stack_pool.pop().unwrap_or_default(),
445            g.snapshot_upval_pool.pop().unwrap_or_default(),
446        )
447    };
448    stack_snapshot.extend(state.stack[..top].iter().map(|sv| sv.val));
449    upval_snapshot.extend(state.openupval.iter().cloned());
450    let mut g = state.global_mut();
451    g.suspended_parent_stacks.push(stack_snapshot);
452    g.suspended_parent_open_upvals.push(upval_snapshot);
453}
454
455fn pop_parent_gc_snapshot(state: &mut LuaState) {
456    let mut g = state.global_mut();
457    if let Some(mut v) = g.suspended_parent_open_upvals.pop() {
458        v.clear();
459        g.snapshot_upval_pool.push(v);
460    }
461    if let Some(mut v) = g.suspended_parent_stacks.pop() {
462        v.clear();
463        g.snapshot_stack_pool.push(v);
464    }
465}
466
467/// Borrow an empty open-upvalue slot buffer from the resume pool, or a fresh
468/// one if the pool is empty (first resume at this nesting depth). The returned
469/// buffer must be parked with [`return_resume_slot_buf`] on every exit path so
470/// the capacity is retained instead of freed.
471fn pop_resume_slot_buf(state: &mut LuaState) -> Vec<(u64, lua_vm::state::StackIdx)> {
472    state.global_mut().resume_upval_slot_pool.pop().unwrap_or_default()
473}
474
475/// Park a (drained) open-upvalue slot buffer back in the resume pool, clearing
476/// it first so the pooled buffer is always empty and roots nothing.
477fn return_resume_slot_buf(state: &mut LuaState, mut buf: Vec<(u64, lua_vm::state::StackIdx)>) {
478    buf.clear();
479    state.global_mut().resume_upval_slot_pool.push(buf);
480}
481
482/// Borrow an empty `LuaValue` buffer from the resume pool for an argument or
483/// result list, or a fresh one if the pool is empty (first use at this nesting
484/// depth). Park with [`return_resume_value_buf`] once the buffer is drained.
485fn pop_resume_value_buf(state: &mut LuaState) -> Vec<LuaValue> {
486    state.global_mut().resume_value_pool.pop().unwrap_or_default()
487}
488
489/// Park a (drained) `LuaValue` buffer back in the resume pool, clearing it so
490/// the pooled buffer is always empty and roots nothing.
491fn return_resume_value_buf(state: &mut LuaState, mut buf: Vec<LuaValue>) {
492    buf.clear();
493    state.global_mut().resume_value_pool.push(buf);
494}
495
496/// Borrow an empty cross-thread upvalue flush buffer from the resume pool, or a
497/// fresh one if the pool is empty. Park with [`return_resume_flush_buf`] once
498/// the buffer has been drained back onto the parent stack.
499fn pop_resume_flush_buf(state: &mut LuaState) -> Vec<(lua_vm::state::StackIdx, LuaValue)> {
500    state.global_mut().resume_flush_pool.pop().unwrap_or_default()
501}
502
503/// Park a (drained) flush buffer back in the resume pool, clearing it so the
504/// pooled buffer is always empty and roots nothing.
505fn return_resume_flush_buf(state: &mut LuaState, mut buf: Vec<(lua_vm::state::StackIdx, LuaValue)>) {
506    buf.clear();
507    state.global_mut().resume_flush_pool.push(buf);
508}
509
510/// RAII borrow of another thread's `LuaState` that keeps the thread's stack
511/// rooted while the borrow is held.
512///
513/// A coroutine whose `RefCell` is mutably borrowed at collect time cannot be
514/// traced by `trace_reachable_threads` — its stack is invisible to the
515/// marker for that whole cycle, so any object only it references is swept
516/// while still live (issue #140 bug A: `debug.traceback(co)` held the borrow
517/// across `push_vfstring`'s GC checkpoint). This guard rides the same
518/// rooting structure as `coroutine.resume`: it pushes a snapshot of the
519/// target's live stack and open upvalues onto `suspended_parent_stacks` for
520/// the lifetime of the borrow and pops it on drop. Snapshots are strictly
521/// LIFO — callers must not resume a coroutine while a guard is alive.
522///
523/// If the guarded section pushes new values onto the *target's* stack and
524/// then allocates before consuming them (`lua_getinfo`'s 'L'/'f' pushes),
525/// call [`RootedThreadBorrow::resnapshot`] after the pushes so the snapshot
526/// covers them too.
527pub(crate) struct RootedThreadBorrow<'a> {
528    inner: std::cell::RefMut<'a, LuaState>,
529}
530
531impl RootedThreadBorrow<'_> {
532    /// Re-copy the target's current live stack and open upvalues into the
533    /// snapshot pushed at borrow time, covering values pushed onto the
534    /// target since then.
535    pub(crate) fn resnapshot(&mut self) {
536        let top = (self.inner.top_idx().0 as usize).min(self.inner.stack.len());
537        let stack_copy: Vec<LuaValue> = self.inner.stack[..top].iter().map(|sv| sv.val).collect();
538        let upval_copy: Vec<GcRef<lua_types::UpVal>> = self.inner.openupval.to_vec();
539        let mut g = self.inner.global_mut();
540        if let Some(slot) = g.suspended_parent_stacks.last_mut() {
541            slot.clear();
542            slot.extend(stack_copy);
543        }
544        if let Some(slot) = g.suspended_parent_open_upvals.last_mut() {
545            slot.clear();
546            slot.extend(upval_copy);
547        }
548    }
549}
550
551impl std::ops::Deref for RootedThreadBorrow<'_> {
552    type Target = LuaState;
553    fn deref(&self) -> &LuaState {
554        &self.inner
555    }
556}
557
558impl std::ops::DerefMut for RootedThreadBorrow<'_> {
559    fn deref_mut(&mut self) -> &mut LuaState {
560        &mut self.inner
561    }
562}
563
564impl Drop for RootedThreadBorrow<'_> {
565    fn drop(&mut self) {
566        let mut g = self.inner.global_mut();
567        if let Some(mut v) = g.suspended_parent_open_upvals.pop() {
568            v.clear();
569            g.snapshot_upval_pool.push(v);
570        }
571        if let Some(mut v) = g.suspended_parent_stacks.pop() {
572            v.clear();
573            g.snapshot_stack_pool.push(v);
574        }
575    }
576}
577
578/// Borrow `cell`'s thread state mutably with its stack rooted for the
579/// duration (see [`RootedThreadBorrow`]). Panics if the cell is already
580/// borrowed, matching the bare `borrow_mut()` call sites this replaces.
581pub(crate) fn borrow_thread_rooted<'a>(
582    state: &mut LuaState,
583    cell: &'a std::cell::RefCell<LuaState>,
584) -> RootedThreadBorrow<'a> {
585    let inner = cell.borrow_mut();
586    let top = (inner.top_idx().0 as usize).min(inner.stack.len());
587    let (mut stack_snapshot, mut upval_snapshot) = {
588        let mut g = state.global_mut();
589        (
590            g.snapshot_stack_pool.pop().unwrap_or_default(),
591            g.snapshot_upval_pool.pop().unwrap_or_default(),
592        )
593    };
594    stack_snapshot.extend(inner.stack[..top].iter().map(|sv| sv.val));
595    upval_snapshot.extend(inner.openupval.iter().cloned());
596    let mut g = state.global_mut();
597    g.suspended_parent_stacks.push(stack_snapshot);
598    g.suspended_parent_open_upvals.push(upval_snapshot);
599    drop(g);
600    RootedThreadBorrow { inner }
601}
602
603/// The wording for "tried to resume a coroutine that is the running (or an
604/// active normal) thread", which changed between versions: Lua 5.1 says
605/// `cannot resume running coroutine`; 5.2 generalized it to
606/// `cannot resume non-suspended coroutine` (the same message now covers a
607/// normal coroutine too). Pinned by `double_resume_running_message_by_version`
608/// in `tests/coro_strengthen.rs` against lua5.1.5 vs lua5.2.4+.
609fn non_suspended_resume_message(state: &LuaState) -> &'static [u8] {
610    if matches!(state.global().lua_version, lua_types::LuaVersion::V51) {
611        b"cannot resume running coroutine"
612    } else {
613        b"cannot resume non-suspended coroutine"
614    }
615}
616
617/// Helper: push a string literal or fall back to Nil on intern failure.
618fn push_lit_or_nil(state: &mut LuaState, bytes: &[u8]) {
619    match state.intern_str(bytes) {
620        Ok(s) => state.push(LuaValue::Str(s)),
621        Err(_) => state.push(LuaValue::Nil),
622    }
623}
624
625// ── Public library functions ──────────────────────────────────────────────────
626
627/// `coroutine.resume(co [, val1, ...])` — attempt to resume coroutine `co`.
628///
629/// On success pushes `true` followed by all values yielded or returned by `co`.
630/// On failure pushes `false` followed by the error object.
631///
632/// The argument count handed to [`aux_resume`] is the stack top minus one: the
633/// coroutine itself sits at index 1 and is not forwarded as an argument.
634///
635/// A sandbox budget trip is uncatchable: it re-raises into the caller frame
636/// instead of returning `false, msg`, so code cannot keep a runaway coroutine
637/// alive by resuming it in a loop.
638pub fn co_resume(state: &mut LuaState) -> Result<usize, LuaError> {
639    let co = get_co(state)?;
640    let narg = state.get_top() - 1;
641    let r = aux_resume(state, co, narg);
642    if r < 0 {
643        if state.sandbox_aborting() {
644            let top = state.get_top();
645            let err_val = state.value_at(top);
646            return Err(LuaError::from_value(err_val));
647        }
648        state.push(LuaValue::Bool(false));
649        state.insert(-2)?;
650        Ok(2)
651    } else {
652        state.push(LuaValue::Bool(true));
653        state.insert(-(r + 1))?;
654        Ok((r + 1) as usize)
655    }
656}
657
658/// Closure body installed by `coroutine.wrap`. The wrapped coroutine
659/// thread is stored in upvalue slot 1 as a `LuaValue::Thread`.
660///
661/// On call: forwards all args to `aux_resume` on the captured thread. On
662/// success returns the yielded/returned values; on coroutine error raises
663/// the error (matching `select(2, assert(resume(co, ...)))` semantics).
664///
665fn aux_wrap(state: &mut LuaState) -> Result<usize, LuaError> {
666    let up = state.value_at(upvalue_index(1));
667    let co = match up {
668        LuaValue::Thread(t) => t,
669        _ => {
670            return Err(LuaError::runtime(format_args!(
671                "coroutine.wrap: upvalue is not a thread"
672            )))
673        }
674    };
675    let narg = state.get_top();
676    let r = aux_resume(state, co.clone(), narg);
677    if r < 0 {
678        let top = state.get_top();
679        let mut err_val = state.value_at(top);
680        if aux_status(state, &co) == COS_DEAD {
681            let old_err = state.pop();
682            let nclose = close_suspended_or_dead(state, co)?;
683            err_val = if nclose >= 2 {
684                let top = state.get_top();
685                state.value_at(top)
686            } else {
687                old_err
688            };
689            state.pop_n(nclose);
690        }
691        Err(LuaError::from_value(err_val))
692    } else {
693        Ok(r as usize)
694    }
695}
696
697/// `coroutine.create(f)` — create a new coroutine that will run function `f`.
698///
699/// Allocates a real `LuaState` registered in `GlobalState::threads`, with `f`
700/// staged on the new thread's stack so `coroutine.status` reports
701/// `"suspended"`. Pushes the new thread value and returns 1.
702pub fn co_create(state: &mut LuaState) -> Result<usize, LuaError> {
703    state.check_arg_type(1, LuaType::Function)?;
704    // 5.1's `luaB_cocreate` additionally rejects C functions
705    // (`luaL_argcheck(L, ... && !lua_iscfunction(L, 1), 1, "Lua function
706    // expected")`); only Lua closures may become coroutine bodies. 5.2 moved
707    // coroutines to `lcorolib.c` and dropped that restriction, so a C function
708    // is accepted from 5.2 on. Verified against lua5.1.5 / lua5.2.4.
709    if matches!(state.global().lua_version, lua_types::LuaVersion::V51)
710        && state.is_c_function_at(1)
711    {
712        return Err(lua_vm::debug::arg_error_impl(
713            state,
714            1,
715            b"Lua function expected",
716        ));
717    }
718    let body = state.value_at(1);
719    let _nl = state.new_thread(Some(body))?;
720    Ok(1)
721}
722
723/// `coroutine.wrap(f)` — create a coroutine and return a resuming function.
724///
725/// The returned function, when called, resumes the coroutine as if by
726/// `coroutine.resume`, but raises an error rather than returning `false`.
727///
728///
729/// Captures the new coroutine thread as upvalue 1 of `aux_wrap`.
730pub fn co_wrap(state: &mut LuaState) -> Result<usize, LuaError> {
731    co_create(state)?;
732    state.push_cclosure(aux_wrap, 1)?;
733    Ok(1)
734}
735
736/// `coroutine.yield([...])` — suspend the running coroutine.
737///
738/// All arguments are passed back as results of the corresponding `resume`.
739///
740/// → `return lua_yield(L, lua_gettop(L));`
741/// → `lua_yield(L,n)` is `lua_yieldk(L, n, 0, NULL)` (lua.h:316)
742pub fn co_yield(state: &mut LuaState) -> Result<usize, LuaError> {
743    let n = state.get_top();
744    let r = lua_vm::do_::lua_yieldk(state, n, 0, None)?;
745    Ok(r as usize)
746}
747
748/// `coroutine.status(co)` — return a string describing `co`'s current status.
749///
750/// Returns one of `"running"`, `"dead"`, `"suspended"`, or `"normal"`.
751///
752pub fn co_status(state: &mut LuaState) -> Result<usize, LuaError> {
753    let co = get_co(state)?;
754    let idx = aux_status(state, &co) as usize;
755    let name: &[u8] = STAT_NAMES[idx];
756    let interned = state.intern_str(name)?;
757    state.push(LuaValue::Str(interned));
758    Ok(1)
759}
760
761/// `coroutine.isyieldable([co])` — test whether a coroutine (default: current)
762/// is in a yieldable state.
763///
764pub fn co_isyieldable(state: &mut LuaState) -> Result<usize, LuaError> {
765    let is_yieldable = if matches!(state.type_at(1), LuaType::None) {
766        state.is_yieldable()
767    } else {
768        let co = get_co(state)?;
769        let co_id = co.id;
770        let (is_main, is_current) = {
771            let g = state.global();
772            (co_id == g.main_thread_id, co_id == g.current_thread_id)
773        };
774        if is_main {
775            false
776        } else if is_current {
777            state.is_yieldable()
778        } else {
779            let entry_rc = {
780                let g = state.global();
781                g.threads
782                    .get(&co_id)
783                    .expect("thread value carries an id that must resolve in GlobalState::threads")
784                    .state
785                    .clone()
786            };
787            let target_is_yieldable = match entry_rc.try_borrow() {
788                Ok(b) => b.is_yieldable(),
789                Err(_) => false,
790            };
791            target_is_yieldable
792        }
793    };
794    state.push(LuaValue::Bool(is_yieldable));
795    Ok(1)
796}
797
798/// `coroutine.running()` — return the current coroutine plus a boolean.
799///
800/// `push_thread` pushes the current `LuaState` as a thread value and returns
801/// `true` iff it is the main thread.
802///
803/// The return arity is version-gated (pinned by `running_in_*_arity_by_version`
804/// in `tests/coro_strengthen.rs`). From 5.2 the result is `(thread, ismain)`
805/// where `ismain` is `true` for the main thread. Lua 5.1 has no `ismain`
806/// boolean: it returns `nil` in the main coroutine and only the running thread
807/// (one value) inside a coroutine (verified against lua5.1.5; see
808/// `specs/followup/5.1-roster-syntax.md` §1).
809pub fn co_running(state: &mut LuaState) -> Result<usize, LuaError> {
810    let is_main = state.push_thread()?;
811    if matches!(state.global().lua_version, lua_types::LuaVersion::V51) {
812        if is_main {
813            state.pop_n(1);
814            state.push(LuaValue::Nil);
815        }
816        return Ok(1);
817    }
818    state.push(LuaValue::Bool(is_main));
819    Ok(2)
820}
821
822/// `coroutine.close(co)` — close a dead or suspended coroutine.
823///
824/// Closes a coroutine, running any pending to-be-closed variables via
825/// `__close` and resetting its status. Valid only when the target is
826/// suspended (`Yield`) or dead (`Ok` with no active frames).
827/// Calling on a running or normal coroutine raises an error.
828///
829pub fn co_close(state: &mut LuaState) -> Result<usize, LuaError> {
830    lua_vm::state::inc_c_stack(state)?;
831    let result = (|| {
832        let co = get_opt_co(state)?;
833        let status = aux_status(state, &co);
834        match status {
835            COS_DEAD | COS_YIELD => close_suspended_or_dead(state, co),
836            _ => {
837                if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
838                    && status == COS_RUN
839                    && state.global().closing_thread_id == Some(co.id)
840                {
841                    state.push(LuaValue::Bool(true));
842                    return Ok(1);
843                }
844                if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
845                    && status == COS_RUN
846                    && co.id == state.global().main_thread_id
847                {
848                    return Err(LuaError::runtime(format_args!("cannot close main thread")));
849                }
850                if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
851                    && status == COS_RUN
852                    && co.id == state.global().current_thread_id
853                {
854                    state.global_mut().closing_thread_id = Some(co.id);
855                    let in_status = state.status as i32;
856                    let s = lua_vm::state::reset_thread(state, in_status);
857                    state.global_mut().closing_thread_id = None;
858                    state.n_ccalls = state.n_ccalls.saturating_sub(1);
859                    std::panic::panic_any(LuaThreadClose(LuaStatus::from_raw(s)));
860                }
861                let name = if status == COS_RUN {
862                    "running"
863                } else {
864                    "normal"
865                };
866                Err(LuaError::runtime(format_args!(
867                    "cannot close a {} coroutine",
868                    name
869                )))
870            }
871        }
872    })();
873    state.n_ccalls -= 1;
874    result
875}
876
877/// Performs the actual close for a suspended or dead coroutine.
878fn close_suspended_or_dead(
879    state: &mut LuaState,
880    co: GcRef<lua_types::value::LuaThread>,
881) -> Result<usize, LuaError> {
882    let co_id = co.id;
883    let entry_rc_opt = {
884        let g = state.global();
885        g.threads.get(&co_id).map(|e| e.state.clone())
886    };
887    let entry_rc = match entry_rc_opt {
888        Some(rc) => rc,
889        None => {
890            state.push(LuaValue::Bool(true));
891            return Ok(1);
892        }
893    };
894    let parent_thread_id = state.global().current_thread_id;
895    let caller_c_calls = state.c_calls();
896
897    let mut parent_open_upval_slots = pop_resume_slot_buf(state);
898    parent_open_upval_slots.extend(state.openupval.iter().filter_map(|uv| {
899        uv.try_open_payload()
900            .map(|(thread_id, idx)| (thread_id as u64, idx))
901    }));
902    {
903        let mut g = state.global_mut();
904        for (tid, idx) in &parent_open_upval_slots {
905            let val = state.get_at(*idx);
906            g.cross_thread_upvals.insert((*tid, *idx), val);
907        }
908    }
909
910    push_parent_gc_snapshot(state);
911
912    let (status, err_value): (i32, Option<LuaValue>) = {
913        let mut co_state = entry_rc.borrow_mut();
914        co_state.global_mut().current_thread_id = co_id;
915        co_state.global_mut().closing_thread_id = Some(co_id);
916        co_state.n_ccalls = caller_c_calls;
917        let in_status = co_state.status as i32;
918        let s = lua_vm::state::reset_thread(&mut *co_state, in_status);
919        co_state.global_mut().closing_thread_id = None;
920        co_state.global_mut().current_thread_id = parent_thread_id;
921        if s == LuaStatus::Ok as i32 {
922            (s, None)
923        } else {
924            let top = co_state.top_idx().0;
925            if top > 0 {
926                let err = co_state.get_at(lua_vm::state::StackIdx(top - 1));
927                co_state.set_top(lua_vm::state::StackIdx(top - 1));
928                (s, Some(err))
929            } else {
930                (s, Some(LuaValue::Nil))
931            }
932        }
933    };
934
935    pop_parent_gc_snapshot(state);
936
937    {
938        let mut flush = pop_resume_flush_buf(state);
939        let mut g = state.global_mut();
940        for (tid, idx) in &parent_open_upval_slots {
941            if let Some(v) = g.cross_thread_upvals.remove(&(*tid, *idx)) {
942                flush.push((*idx, v));
943            }
944        }
945        drop(g);
946        for (idx, v) in flush.drain(..) {
947            state.set_at(idx, v);
948        }
949        return_resume_flush_buf(state, flush);
950    }
951    return_resume_slot_buf(state, parent_open_upval_slots);
952
953    if status == LuaStatus::Ok as i32 {
954        state.push(LuaValue::Bool(true));
955        Ok(1)
956    } else {
957        state.push(LuaValue::Bool(false));
958        if let Some(v) = err_value {
959            state.push(v);
960        } else {
961            state.push(LuaValue::Nil);
962        }
963        Ok(2)
964    }
965}
966
967// ── Module entry point ────────────────────────────────────────────────────────
968
969/// Opens the `coroutine` standard library by pushing a new table containing
970/// all `coroutine.*` functions.
971///
972pub fn open_coroutine(state: &mut LuaState) -> Result<usize, LuaError> {
973    // `coroutine.close` is a Lua 5.4 addition tied to to-be-closed variables
974    // (`specs/research/5.3-upstream-delta.md` delta #9). Under 5.3 it is absent
975    // from the roster entirely.
976    use lua_types::LuaVersion;
977    let version = state.global().lua_version;
978    let has_close = !matches!(version, LuaVersion::V51 | LuaVersion::V52 | LuaVersion::V53);
979    // `coroutine.isyieldable` is a Lua 5.3 addition; it is absent in 5.1 and 5.2
980    // (verified against lua5.1.5 and lua5.2.4: `type(coroutine.isyieldable)` ==
981    // "nil"). See specs/followup/5.1-roster-syntax.md §1.
982    let has_isyieldable = !matches!(version, LuaVersion::V51 | LuaVersion::V52);
983    if has_close && has_isyieldable {
984        state.new_lib(CO_FUNCS)?;
985    } else {
986        let filtered: Vec<(&[u8], lua_CFunction)> = CO_FUNCS
987            .iter()
988            .filter(|(name, _)| {
989                (has_close || *name != b"close".as_slice())
990                    && (has_isyieldable || *name != b"isyieldable".as_slice())
991            })
992            .copied()
993            .collect();
994        state.new_lib(&filtered)?;
995    }
996    Ok(1)
997}
998
999// ──────────────────────────────────────────────────────────────────────────────
1000// PORT STATUS
1001//   target_crate:  lua-stdlib
1002//   unsafe_blocks: 0
1003//   load-bearing:  this module is the cold shell — arg checking, the COS_*
1004//                  status mapping, the wrap closure, the cross-thread
1005//                  argument/result transfer scaffolding, and version-gated
1006//                  registration. The resume/yield CONTROL TRANSFER (stack save
1007//                  and restore) lives in lua-vm (lua_vm::do_::lua_resume /
1008//                  lua_yieldk) and is load-bearing; so are the cross-thread
1009//                  rooting machinery (RootedThreadBorrow, the resume-pool
1010//                  buffers, the GC stack snapshots), the LuaThreadClose
1011//                  panic-unwind that implements 5.5 self-close, and every
1012//                  version gate.
1013//   net:           behavior is pinned by tests/coro_strengthen.rs (the version
1014//                  seams), the official coroutine.lua suite, multiversion
1015//                  oracle, and check.sh 5.1-5.5. See GRADUATED.md "coroutine".
1016//   version-gated: get_co/thread_arg_error emit the calling function's name and
1017//                  the per-version "expected" body (coroutine vs thread, with vs
1018//                  without ", got <type>"). co_create rejects C-function bodies
1019//                  on 5.1 only ("Lua function expected").
1020//   known-gap:     the 5.1 yield-from-outside / yield-across-C-call wording is
1021//                  "attempt to yield across metamethod/C-call boundary" in the
1022//                  reference but "attempt to yield from outside a coroutine"
1023//                  here — the message originates in lua-vm's lua_yieldk (a
1024//                  cross-cutting yield guard, not this module). NOT fixed here:
1025//                  the single-source fix is a version gate in lua-vm/src/do_.rs.
1026//   known-gap:     a 5.1 arg error raised through pcall (no resolvable namewhat)
1027//                  names '?' in the reference but the qualified function here
1028//                  ('coroutine.resume', 'coroutine.create', ...). Single-source
1029//                  fix is to gate lua-vm arg_error_impl's find_func_name_in_loaded
1030//                  fallback off for V51 (same gap hits base/math arg errors).
1031// ──────────────────────────────────────────────────────────────────────────────