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/// A registry miss is normally a genuinely dead coroutine (`cannot resume dead
274/// coroutine`). The one exception is the main thread, which is deliberately
275/// never stored in `GlobalState::threads` (`main_thread_id == 0`): resuming it
276/// is a non-suspended error, not a dead one, matching the reference's
277/// `cannot resume non-suspended coroutine` (5.2+) / `cannot resume running
278/// coroutine` (5.1). This mirrors `aux_status`, which already classifies the
279/// main thread as `COS_NORM` rather than `COS_DEAD`.
280///
281/// Cross-thread open-upvalue mirroring rides the resume boundary: before
282/// yielding control, the parent's open-upvalue values are snapshotted into
283/// `GlobalState::cross_thread_upvals` so the coroutine body can read and write
284/// them through `LuaState::upvalue_get` / `upvalue_set`. On resume return, the
285/// (possibly mutated) cache entries are flushed back into the parent's stack.
286/// This is the alternative to a stack-refactor that would let the parent's
287/// `LuaState` be reached through `Rc<RefCell<_>>` while it is held by `&mut`
288/// further up the call stack. Load-bearing: do not collapse the snapshot /
289/// flush handshake — it is what keeps cross-thread upvalues coherent and
290/// rooted across the resume.
291fn aux_resume(state: &mut LuaState, co: GcRef<lua_types::value::LuaThread>, narg: i32) -> i32 {
292 let co_id = co.id;
293 let entry_rc = {
294 let g = state.global();
295 match g.threads.get(&co_id) {
296 Some(e) => e.state.clone(),
297 None => {
298 let is_main = co_id == g.main_thread_id;
299 drop(g);
300 if is_main {
301 let msg = non_suspended_resume_message(state);
302 push_lit_or_nil(state, msg);
303 } else {
304 push_lit_or_nil(state, b"cannot resume dead coroutine");
305 }
306 return -1;
307 }
308 }
309 };
310 let parent_thread_id = state.global().current_thread_id;
311 let top_before = state.get_top();
312 if top_before < narg {
313 push_lit_or_nil(state, b"not enough arguments to resume");
314 return -1;
315 }
316 let first_arg_idx = top_before - narg + 1;
317 let mut args = pop_resume_value_buf(state);
318 args.extend((first_arg_idx..=top_before).map(|i| state.value_at(i)));
319 lua_vm::api::set_top(state, (top_before - narg) as i32).ok();
320
321 let mut parent_open_upval_slots = pop_resume_slot_buf(state);
322 parent_open_upval_slots.extend(state.openupval.iter().filter_map(|uv| {
323 uv.try_open_payload()
324 .map(|(thread_id, idx)| (thread_id as u64, idx))
325 }));
326 {
327 let mut g = state.global_mut();
328 for (tid, idx) in &parent_open_upval_slots {
329 let val = state.get_at(*idx);
330 g.cross_thread_upvals.insert((*tid, *idx), val);
331 }
332 }
333
334 push_parent_gc_snapshot(state);
335
336 let (status, results_or_err): (LuaStatus, Vec<LuaValue>) = {
337 let mut co_state = match entry_rc.try_borrow_mut() {
338 Ok(b) => b,
339 Err(_) => {
340 pop_parent_gc_snapshot(state);
341 let mut g = state.global_mut();
342 for (tid, idx) in &parent_open_upval_slots {
343 g.cross_thread_upvals.remove(&(*tid, *idx));
344 }
345 drop(g);
346 return_resume_slot_buf(state, parent_open_upval_slots);
347 return_resume_value_buf(state, args);
348 let msg = non_suspended_resume_message(state);
349 push_lit_or_nil(state, msg);
350 return -1;
351 }
352 };
353 if co_state.check_stack(narg + 1).is_err() {
354 drop(co_state);
355 pop_parent_gc_snapshot(state);
356 let mut g = state.global_mut();
357 for (tid, idx) in &parent_open_upval_slots {
358 g.cross_thread_upvals.remove(&(*tid, *idx));
359 }
360 drop(g);
361 return_resume_slot_buf(state, parent_open_upval_slots);
362 return_resume_value_buf(state, args);
363 push_lit_or_nil(state, b"too many arguments to resume");
364 return -1;
365 }
366 for v in args.drain(..) {
367 co_state.push(v);
368 }
369 return_resume_value_buf(state, args);
370 co_state.global_mut().current_thread_id = co_id;
371 let mut nres: i32 = 0;
372 ensure_chaining_panic_hook();
373 let resume_result = {
374 let _suppress = SuppressGuard::new();
375 catch_unwind(AssertUnwindSafe(|| {
376 lua_vm::do_::lua_resume(&mut *co_state, Some(state), narg, &mut nres)
377 }))
378 };
379 co_state.global_mut().current_thread_id = parent_thread_id;
380 let status = match resume_result {
381 Ok(status) => status,
382 Err(payload) => {
383 if let Some(close) = payload.downcast_ref::<LuaThreadClose>() {
384 close.0
385 } else {
386 resume_unwind(payload);
387 }
388 }
389 };
390 let co_top = co_state.top_idx().0 as i32;
391 let ci_func = co_state.current_call_info().func.0 as i32;
392 let count = if status == LuaStatus::Ok || status == LuaStatus::Yield {
393 nres
394 } else {
395 1
396 };
397 let start = co_top - count;
398 let mut vals = pop_resume_value_buf(state);
399 vals.extend((start..co_top).map(|i| co_state.get_at(lua_vm::state::StackIdx(i as u32))));
400 let new_co_top = if status == LuaStatus::Ok || status == LuaStatus::Yield {
401 (co_top - count).max(ci_func + 1)
402 } else {
403 co_top - count
404 };
405 co_state.set_top(lua_vm::state::StackIdx(new_co_top.max(0) as u32));
406 (status, vals)
407 };
408
409 // Pop the parent stack snapshot — the coroutine has yielded or returned.
410 pop_parent_gc_snapshot(state);
411
412 {
413 let mut flush = pop_resume_flush_buf(state);
414 let mut g = state.global_mut();
415 for (tid, idx) in &parent_open_upval_slots {
416 if let Some(v) = g.cross_thread_upvals.remove(&(*tid, *idx)) {
417 flush.push((*idx, v));
418 }
419 }
420 drop(g);
421 for (idx, v) in flush.drain(..) {
422 state.set_at(idx, v);
423 }
424 return_resume_flush_buf(state, flush);
425 }
426 return_resume_slot_buf(state, parent_open_upval_slots);
427
428 let mut results_or_err = results_or_err;
429 match status {
430 LuaStatus::Ok | LuaStatus::Yield => {
431 if state.check_stack(results_or_err.len() as i32 + 1).is_err() {
432 return_resume_value_buf(state, results_or_err);
433 push_lit_or_nil(state, b"too many results to resume");
434 return -1;
435 }
436 let n = results_or_err.len();
437 for v in results_or_err.drain(..) {
438 state.push(v);
439 }
440 return_resume_value_buf(state, results_or_err);
441 n as i32
442 }
443 _ => {
444 for v in results_or_err.drain(..) {
445 state.push(v);
446 }
447 return_resume_value_buf(state, results_or_err);
448 -1
449 }
450 }
451}
452
453fn push_parent_gc_snapshot(state: &mut LuaState) {
454 let top = (state.top_idx().0 as usize).min(state.stack.len());
455 let (mut stack_snapshot, mut upval_snapshot) = {
456 let mut g = state.global_mut();
457 (
458 g.snapshot_stack_pool.pop().unwrap_or_default(),
459 g.snapshot_upval_pool.pop().unwrap_or_default(),
460 )
461 };
462 stack_snapshot.extend(state.stack[..top].iter().map(|sv| sv.val));
463 upval_snapshot.extend(state.openupval.iter().cloned());
464 let mut g = state.global_mut();
465 g.suspended_parent_stacks.push(stack_snapshot);
466 g.suspended_parent_open_upvals.push(upval_snapshot);
467}
468
469fn pop_parent_gc_snapshot(state: &mut LuaState) {
470 let mut g = state.global_mut();
471 if let Some(mut v) = g.suspended_parent_open_upvals.pop() {
472 v.clear();
473 g.snapshot_upval_pool.push(v);
474 }
475 if let Some(mut v) = g.suspended_parent_stacks.pop() {
476 v.clear();
477 g.snapshot_stack_pool.push(v);
478 }
479}
480
481/// Borrow an empty open-upvalue slot buffer from the resume pool, or a fresh
482/// one if the pool is empty (first resume at this nesting depth). The returned
483/// buffer must be parked with [`return_resume_slot_buf`] on every exit path so
484/// the capacity is retained instead of freed.
485fn pop_resume_slot_buf(state: &mut LuaState) -> Vec<(u64, lua_vm::state::StackIdx)> {
486 state.global_mut().resume_upval_slot_pool.pop().unwrap_or_default()
487}
488
489/// Park a (drained) open-upvalue slot buffer back in the resume pool, clearing
490/// it first so the pooled buffer is always empty and roots nothing.
491fn return_resume_slot_buf(state: &mut LuaState, mut buf: Vec<(u64, lua_vm::state::StackIdx)>) {
492 buf.clear();
493 state.global_mut().resume_upval_slot_pool.push(buf);
494}
495
496/// Borrow an empty `LuaValue` buffer from the resume pool for an argument or
497/// result list, or a fresh one if the pool is empty (first use at this nesting
498/// depth). Park with [`return_resume_value_buf`] once the buffer is drained.
499fn pop_resume_value_buf(state: &mut LuaState) -> Vec<LuaValue> {
500 state.global_mut().resume_value_pool.pop().unwrap_or_default()
501}
502
503/// Park a (drained) `LuaValue` buffer back in the resume pool, clearing it so
504/// the pooled buffer is always empty and roots nothing.
505fn return_resume_value_buf(state: &mut LuaState, mut buf: Vec<LuaValue>) {
506 buf.clear();
507 state.global_mut().resume_value_pool.push(buf);
508}
509
510/// Borrow an empty cross-thread upvalue flush buffer from the resume pool, or a
511/// fresh one if the pool is empty. Park with [`return_resume_flush_buf`] once
512/// the buffer has been drained back onto the parent stack.
513fn pop_resume_flush_buf(state: &mut LuaState) -> Vec<(lua_vm::state::StackIdx, LuaValue)> {
514 state.global_mut().resume_flush_pool.pop().unwrap_or_default()
515}
516
517/// Park a (drained) flush buffer back in the resume pool, clearing it so the
518/// pooled buffer is always empty and roots nothing.
519fn return_resume_flush_buf(state: &mut LuaState, mut buf: Vec<(lua_vm::state::StackIdx, LuaValue)>) {
520 buf.clear();
521 state.global_mut().resume_flush_pool.push(buf);
522}
523
524/// RAII borrow of another thread's `LuaState` that keeps the thread's stack
525/// rooted while the borrow is held.
526///
527/// A coroutine whose `RefCell` is mutably borrowed at collect time cannot be
528/// traced by `trace_reachable_threads` — its stack is invisible to the
529/// marker for that whole cycle, so any object only it references is swept
530/// while still live (issue #140 bug A: `debug.traceback(co)` held the borrow
531/// across `push_vfstring`'s GC checkpoint). This guard rides the same
532/// rooting structure as `coroutine.resume`: it pushes a snapshot of the
533/// target's live stack and open upvalues onto `suspended_parent_stacks` for
534/// the lifetime of the borrow and pops it on drop. Snapshots are strictly
535/// LIFO — callers must not resume a coroutine while a guard is alive.
536///
537/// If the guarded section pushes new values onto the *target's* stack and
538/// then allocates before consuming them (`lua_getinfo`'s 'L'/'f' pushes),
539/// call [`RootedThreadBorrow::resnapshot`] after the pushes so the snapshot
540/// covers them too.
541#[cfg(feature = "debug")]
542pub(crate) struct RootedThreadBorrow<'a> {
543 inner: std::cell::RefMut<'a, LuaState>,
544}
545
546#[cfg(feature = "debug")]
547impl RootedThreadBorrow<'_> {
548 /// Re-copy the target's current live stack and open upvalues into the
549 /// snapshot pushed at borrow time, covering values pushed onto the
550 /// target since then.
551 pub(crate) fn resnapshot(&mut self) {
552 let top = (self.inner.top_idx().0 as usize).min(self.inner.stack.len());
553 let stack_copy: Vec<LuaValue> = self.inner.stack[..top].iter().map(|sv| sv.val).collect();
554 let upval_copy: Vec<GcRef<lua_types::UpVal>> = self.inner.openupval.to_vec();
555 let mut g = self.inner.global_mut();
556 if let Some(slot) = g.suspended_parent_stacks.last_mut() {
557 slot.clear();
558 slot.extend(stack_copy);
559 }
560 if let Some(slot) = g.suspended_parent_open_upvals.last_mut() {
561 slot.clear();
562 slot.extend(upval_copy);
563 }
564 }
565}
566
567#[cfg(feature = "debug")]
568impl std::ops::Deref for RootedThreadBorrow<'_> {
569 type Target = LuaState;
570 fn deref(&self) -> &LuaState {
571 &self.inner
572 }
573}
574
575#[cfg(feature = "debug")]
576impl std::ops::DerefMut for RootedThreadBorrow<'_> {
577 fn deref_mut(&mut self) -> &mut LuaState {
578 &mut self.inner
579 }
580}
581
582#[cfg(feature = "debug")]
583impl Drop for RootedThreadBorrow<'_> {
584 fn drop(&mut self) {
585 let mut g = self.inner.global_mut();
586 if let Some(mut v) = g.suspended_parent_open_upvals.pop() {
587 v.clear();
588 g.snapshot_upval_pool.push(v);
589 }
590 if let Some(mut v) = g.suspended_parent_stacks.pop() {
591 v.clear();
592 g.snapshot_stack_pool.push(v);
593 }
594 }
595}
596
597/// Borrow `cell`'s thread state mutably with its stack rooted for the
598/// duration (see [`RootedThreadBorrow`]). Panics if the cell is already
599/// borrowed, matching the bare `borrow_mut()` call sites this replaces.
600#[cfg(feature = "debug")]
601pub(crate) fn borrow_thread_rooted<'a>(
602 state: &mut LuaState,
603 cell: &'a std::cell::RefCell<LuaState>,
604) -> RootedThreadBorrow<'a> {
605 let inner = cell.borrow_mut();
606 let top = (inner.top_idx().0 as usize).min(inner.stack.len());
607 let (mut stack_snapshot, mut upval_snapshot) = {
608 let mut g = state.global_mut();
609 (
610 g.snapshot_stack_pool.pop().unwrap_or_default(),
611 g.snapshot_upval_pool.pop().unwrap_or_default(),
612 )
613 };
614 stack_snapshot.extend(inner.stack[..top].iter().map(|sv| sv.val));
615 upval_snapshot.extend(inner.openupval.iter().cloned());
616 let mut g = state.global_mut();
617 g.suspended_parent_stacks.push(stack_snapshot);
618 g.suspended_parent_open_upvals.push(upval_snapshot);
619 drop(g);
620 RootedThreadBorrow { inner }
621}
622
623/// The wording for "tried to resume a coroutine that is the running (or an
624/// active normal) thread", which changed between versions: Lua 5.1 says
625/// `cannot resume running coroutine`; 5.2 generalized it to
626/// `cannot resume non-suspended coroutine` (the same message now covers a
627/// normal coroutine too). Pinned by `double_resume_running_message_by_version`
628/// in `tests/coro_strengthen.rs` against lua5.1.5 vs lua5.2.4+.
629fn non_suspended_resume_message(state: &LuaState) -> &'static [u8] {
630 if matches!(state.global().lua_version, lua_types::LuaVersion::V51) {
631 b"cannot resume running coroutine"
632 } else {
633 b"cannot resume non-suspended coroutine"
634 }
635}
636
637/// Helper: push a string literal or fall back to Nil on intern failure.
638fn push_lit_or_nil(state: &mut LuaState, bytes: &[u8]) {
639 match state.intern_str(bytes) {
640 Ok(s) => state.push(LuaValue::Str(s)),
641 Err(_) => state.push(LuaValue::Nil),
642 }
643}
644
645// ── Public library functions ──────────────────────────────────────────────────
646
647/// `coroutine.resume(co [, val1, ...])` — attempt to resume coroutine `co`.
648///
649/// On success pushes `true` followed by all values yielded or returned by `co`.
650/// On failure pushes `false` followed by the error object.
651///
652/// The argument count handed to [`aux_resume`] is the stack top minus one: the
653/// coroutine itself sits at index 1 and is not forwarded as an argument.
654///
655/// A sandbox budget trip is uncatchable: it re-raises into the caller frame
656/// instead of returning `false, msg`, so code cannot keep a runaway coroutine
657/// alive by resuming it in a loop.
658pub fn co_resume(state: &mut LuaState) -> Result<usize, LuaError> {
659 let co = get_co(state)?;
660 let narg = state.get_top() - 1;
661 let r = aux_resume(state, co, narg);
662 if r < 0 {
663 if state.sandbox_aborting() {
664 let top = state.get_top();
665 let err_val = state.value_at(top);
666 return Err(LuaError::from_value(err_val));
667 }
668 state.push(LuaValue::Bool(false));
669 state.insert(-2)?;
670 Ok(2)
671 } else {
672 state.push(LuaValue::Bool(true));
673 state.insert(-(r + 1))?;
674 Ok((r + 1) as usize)
675 }
676}
677
678/// Closure body installed by `coroutine.wrap`. The wrapped coroutine
679/// thread is stored in upvalue slot 1 as a `LuaValue::Thread`.
680///
681/// On call: forwards all args to `aux_resume` on the captured thread. On
682/// success returns the yielded/returned values; on coroutine error raises
683/// the error (matching `select(2, assert(resume(co, ...)))` semantics).
684///
685fn aux_wrap(state: &mut LuaState) -> Result<usize, LuaError> {
686 let up = state.value_at(upvalue_index(1));
687 let co = match up {
688 LuaValue::Thread(t) => t,
689 _ => {
690 return Err(LuaError::runtime(format_args!(
691 "coroutine.wrap: upvalue is not a thread"
692 )))
693 }
694 };
695 let narg = state.get_top();
696 let r = aux_resume(state, co.clone(), narg);
697 if r < 0 {
698 let top = state.get_top();
699 let mut err_val = state.value_at(top);
700 if aux_status(state, &co) == COS_DEAD {
701 let old_err = state.pop();
702 let nclose = close_suspended_or_dead(state, co)?;
703 err_val = if nclose >= 2 {
704 let top = state.get_top();
705 state.value_at(top)
706 } else {
707 old_err
708 };
709 state.pop_n(nclose);
710 }
711 Err(LuaError::from_value(err_val))
712 } else {
713 Ok(r as usize)
714 }
715}
716
717/// `coroutine.create(f)` — create a new coroutine that will run function `f`.
718///
719/// Allocates a real `LuaState` registered in `GlobalState::threads`, with `f`
720/// staged on the new thread's stack so `coroutine.status` reports
721/// `"suspended"`. Pushes the new thread value and returns 1.
722pub fn co_create(state: &mut LuaState) -> Result<usize, LuaError> {
723 state.check_arg_type(1, LuaType::Function)?;
724 // 5.1's `luaB_cocreate` additionally rejects C functions
725 // (`luaL_argcheck(L, ... && !lua_iscfunction(L, 1), 1, "Lua function
726 // expected")`); only Lua closures may become coroutine bodies. 5.2 moved
727 // coroutines to `lcorolib.c` and dropped that restriction, so a C function
728 // is accepted from 5.2 on. Verified against lua5.1.5 / lua5.2.4.
729 if matches!(state.global().lua_version, lua_types::LuaVersion::V51)
730 && state.is_c_function_at(1)
731 {
732 return Err(lua_vm::debug::arg_error_impl(
733 state,
734 1,
735 b"Lua function expected",
736 ));
737 }
738 let body = state.value_at(1);
739 let _nl = state.new_thread(Some(body))?;
740 Ok(1)
741}
742
743/// `coroutine.wrap(f)` — create a coroutine and return a resuming function.
744///
745/// The returned function, when called, resumes the coroutine as if by
746/// `coroutine.resume`, but raises an error rather than returning `false`.
747///
748///
749/// Captures the new coroutine thread as upvalue 1 of `aux_wrap`.
750pub fn co_wrap(state: &mut LuaState) -> Result<usize, LuaError> {
751 co_create(state)?;
752 state.push_cclosure(aux_wrap, 1)?;
753 Ok(1)
754}
755
756/// `coroutine.yield([...])` — suspend the running coroutine.
757///
758/// All arguments are passed back as results of the corresponding `resume`.
759///
760/// → `return lua_yield(L, lua_gettop(L));`
761/// → `lua_yield(L,n)` is `lua_yieldk(L, n, 0, NULL)` (lua.h:316)
762pub fn co_yield(state: &mut LuaState) -> Result<usize, LuaError> {
763 let n = state.get_top();
764 let r = lua_vm::do_::lua_yieldk(state, n, 0, None)?;
765 Ok(r as usize)
766}
767
768/// `coroutine.status(co)` — return a string describing `co`'s current status.
769///
770/// Returns one of `"running"`, `"dead"`, `"suspended"`, or `"normal"`.
771///
772pub fn co_status(state: &mut LuaState) -> Result<usize, LuaError> {
773 let co = get_co(state)?;
774 let idx = aux_status(state, &co) as usize;
775 let name: &[u8] = STAT_NAMES[idx];
776 let interned = state.intern_str(name)?;
777 state.push(LuaValue::Str(interned));
778 Ok(1)
779}
780
781/// `coroutine.isyieldable([co])` — test whether a coroutine (default: current)
782/// is in a yieldable state.
783///
784pub fn co_isyieldable(state: &mut LuaState) -> Result<usize, LuaError> {
785 let is_yieldable = if matches!(state.type_at(1), LuaType::None) {
786 state.is_yieldable()
787 } else {
788 let co = get_co(state)?;
789 let co_id = co.id;
790 let (is_main, is_current) = {
791 let g = state.global();
792 (co_id == g.main_thread_id, co_id == g.current_thread_id)
793 };
794 if is_main {
795 false
796 } else if is_current {
797 state.is_yieldable()
798 } else {
799 let entry_rc = {
800 let g = state.global();
801 g.threads
802 .get(&co_id)
803 .expect("thread value carries an id that must resolve in GlobalState::threads")
804 .state
805 .clone()
806 };
807 let target_is_yieldable = match entry_rc.try_borrow() {
808 Ok(b) => b.is_yieldable(),
809 Err(_) => false,
810 };
811 target_is_yieldable
812 }
813 };
814 state.push(LuaValue::Bool(is_yieldable));
815 Ok(1)
816}
817
818/// `coroutine.running()` — return the current coroutine plus a boolean.
819///
820/// `push_thread` pushes the current `LuaState` as a thread value and returns
821/// `true` iff it is the main thread.
822///
823/// The return arity is version-gated (pinned by `running_in_*_arity_by_version`
824/// in `tests/coro_strengthen.rs`). From 5.2 the result is `(thread, ismain)`
825/// where `ismain` is `true` for the main thread. Lua 5.1 has no `ismain`
826/// boolean: it returns `nil` in the main coroutine and only the running thread
827/// (one value) inside a coroutine (verified against lua5.1.5; see
828/// `specs/followup/5.1-roster-syntax.md` §1).
829pub fn co_running(state: &mut LuaState) -> Result<usize, LuaError> {
830 let is_main = state.push_thread()?;
831 if matches!(state.global().lua_version, lua_types::LuaVersion::V51) {
832 if is_main {
833 state.pop_n(1);
834 state.push(LuaValue::Nil);
835 }
836 return Ok(1);
837 }
838 state.push(LuaValue::Bool(is_main));
839 Ok(2)
840}
841
842/// `coroutine.close(co)` — close a dead or suspended coroutine.
843///
844/// Closes a coroutine, running any pending to-be-closed variables via
845/// `__close` and resetting its status. Valid only when the target is
846/// suspended (`Yield`) or dead (`Ok` with no active frames).
847/// Calling on a running or normal coroutine raises an error.
848///
849pub fn co_close(state: &mut LuaState) -> Result<usize, LuaError> {
850 lua_vm::state::inc_c_stack(state)?;
851 let result = (|| {
852 let co = get_opt_co(state)?;
853 let status = aux_status(state, &co);
854 match status {
855 COS_DEAD | COS_YIELD => close_suspended_or_dead(state, co),
856 _ => {
857 if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
858 && status == COS_RUN
859 && state.global().closing_thread_id == Some(co.id)
860 {
861 state.push(LuaValue::Bool(true));
862 return Ok(1);
863 }
864 if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
865 && status == COS_RUN
866 && co.id == state.global().main_thread_id
867 {
868 return Err(LuaError::runtime(format_args!("cannot close main thread")));
869 }
870 if matches!(state.global().lua_version, lua_types::LuaVersion::V55)
871 && status == COS_RUN
872 && co.id == state.global().current_thread_id
873 {
874 state.global_mut().closing_thread_id = Some(co.id);
875 let in_status = state.status as i32;
876 let s = lua_vm::state::reset_thread(state, in_status);
877 state.global_mut().closing_thread_id = None;
878 state.n_ccalls = state.n_ccalls.saturating_sub(1);
879 std::panic::panic_any(LuaThreadClose(LuaStatus::from_raw(s)));
880 }
881 let name = if status == COS_RUN {
882 "running"
883 } else {
884 "normal"
885 };
886 Err(LuaError::runtime(format_args!(
887 "cannot close a {} coroutine",
888 name
889 )))
890 }
891 }
892 })();
893 state.n_ccalls -= 1;
894 result
895}
896
897/// Performs the actual close for a suspended or dead coroutine.
898fn close_suspended_or_dead(
899 state: &mut LuaState,
900 co: GcRef<lua_types::value::LuaThread>,
901) -> Result<usize, LuaError> {
902 let co_id = co.id;
903 let entry_rc_opt = {
904 let g = state.global();
905 g.threads.get(&co_id).map(|e| e.state.clone())
906 };
907 let entry_rc = match entry_rc_opt {
908 Some(rc) => rc,
909 None => {
910 state.push(LuaValue::Bool(true));
911 return Ok(1);
912 }
913 };
914 let parent_thread_id = state.global().current_thread_id;
915 let caller_c_calls = state.c_calls();
916
917 let mut parent_open_upval_slots = pop_resume_slot_buf(state);
918 parent_open_upval_slots.extend(state.openupval.iter().filter_map(|uv| {
919 uv.try_open_payload()
920 .map(|(thread_id, idx)| (thread_id as u64, idx))
921 }));
922 {
923 let mut g = state.global_mut();
924 for (tid, idx) in &parent_open_upval_slots {
925 let val = state.get_at(*idx);
926 g.cross_thread_upvals.insert((*tid, *idx), val);
927 }
928 }
929
930 push_parent_gc_snapshot(state);
931
932 let (status, err_value): (i32, Option<LuaValue>) = {
933 let mut co_state = entry_rc.borrow_mut();
934 co_state.global_mut().current_thread_id = co_id;
935 co_state.global_mut().closing_thread_id = Some(co_id);
936 co_state.n_ccalls = caller_c_calls;
937 let in_status = co_state.status as i32;
938 let s = lua_vm::state::reset_thread(&mut *co_state, in_status);
939 co_state.global_mut().closing_thread_id = None;
940 co_state.global_mut().current_thread_id = parent_thread_id;
941 if s == LuaStatus::Ok as i32 {
942 (s, None)
943 } else {
944 let top = co_state.top_idx().0;
945 if top > 0 {
946 let err = co_state.get_at(lua_vm::state::StackIdx(top - 1));
947 co_state.set_top(lua_vm::state::StackIdx(top - 1));
948 (s, Some(err))
949 } else {
950 (s, Some(LuaValue::Nil))
951 }
952 }
953 };
954
955 pop_parent_gc_snapshot(state);
956
957 {
958 let mut flush = pop_resume_flush_buf(state);
959 let mut g = state.global_mut();
960 for (tid, idx) in &parent_open_upval_slots {
961 if let Some(v) = g.cross_thread_upvals.remove(&(*tid, *idx)) {
962 flush.push((*idx, v));
963 }
964 }
965 drop(g);
966 for (idx, v) in flush.drain(..) {
967 state.set_at(idx, v);
968 }
969 return_resume_flush_buf(state, flush);
970 }
971 return_resume_slot_buf(state, parent_open_upval_slots);
972
973 if status == LuaStatus::Ok as i32 {
974 state.push(LuaValue::Bool(true));
975 Ok(1)
976 } else {
977 state.push(LuaValue::Bool(false));
978 if let Some(v) = err_value {
979 state.push(v);
980 } else {
981 state.push(LuaValue::Nil);
982 }
983 Ok(2)
984 }
985}
986
987// ── Module entry point ────────────────────────────────────────────────────────
988
989/// Opens the `coroutine` standard library by pushing a new table containing
990/// all `coroutine.*` functions.
991///
992pub fn open_coroutine(state: &mut LuaState) -> Result<usize, LuaError> {
993 // `coroutine.close` is a Lua 5.4 addition tied to to-be-closed variables
994 // (`specs/research/5.3-upstream-delta.md` delta #9). Under 5.3 it is absent
995 // from the roster entirely.
996 use lua_types::LuaVersion;
997 let version = state.global().lua_version;
998 let has_close = !matches!(version, LuaVersion::V51 | LuaVersion::V52 | LuaVersion::V53);
999 // `coroutine.isyieldable` is a Lua 5.3 addition; it is absent in 5.1 and 5.2
1000 // (verified against lua5.1.5 and lua5.2.4: `type(coroutine.isyieldable)` ==
1001 // "nil"). See specs/followup/5.1-roster-syntax.md §1.
1002 let has_isyieldable = !matches!(version, LuaVersion::V51 | LuaVersion::V52);
1003 if has_close && has_isyieldable {
1004 state.new_lib(CO_FUNCS)?;
1005 } else {
1006 let filtered: Vec<(&[u8], lua_CFunction)> = CO_FUNCS
1007 .iter()
1008 .filter(|(name, _)| {
1009 (has_close || *name != b"close".as_slice())
1010 && (has_isyieldable || *name != b"isyieldable".as_slice())
1011 })
1012 .copied()
1013 .collect();
1014 state.new_lib(&filtered)?;
1015 }
1016 Ok(1)
1017}
1018
1019// ──────────────────────────────────────────────────────────────────────────────
1020// PORT STATUS
1021// target_crate: lua-stdlib
1022// unsafe_blocks: 0
1023// load-bearing: this module is the cold shell — arg checking, the COS_*
1024// status mapping, the wrap closure, the cross-thread
1025// argument/result transfer scaffolding, and version-gated
1026// registration. The resume/yield CONTROL TRANSFER (stack save
1027// and restore) lives in lua-vm (lua_vm::do_::lua_resume /
1028// lua_yieldk) and is load-bearing; so are the cross-thread
1029// rooting machinery (RootedThreadBorrow, the resume-pool
1030// buffers, the GC stack snapshots), the LuaThreadClose
1031// panic-unwind that implements 5.5 self-close, and every
1032// version gate.
1033// net: behavior is pinned by tests/coro_strengthen.rs (the version
1034// seams), the official coroutine.lua suite, multiversion
1035// oracle, and check.sh 5.1-5.5. See GRADUATED.md "coroutine".
1036// version-gated: get_co/thread_arg_error emit the calling function's name and
1037// the per-version "expected" body (coroutine vs thread, with vs
1038// without ", got <type>"). co_create rejects C-function bodies
1039// on 5.1 only ("Lua function expected").
1040// known-gap: the 5.1 yield-from-outside / yield-across-C-call wording is
1041// "attempt to yield across metamethod/C-call boundary" in the
1042// reference but "attempt to yield from outside a coroutine"
1043// here — the message originates in lua-vm's lua_yieldk (a
1044// cross-cutting yield guard, not this module). NOT fixed here:
1045// the single-source fix is a version gate in lua-vm/src/do_.rs.
1046// known-gap: a 5.1 arg error raised through pcall (no resolvable namewhat)
1047// names '?' in the reference but the qualified function here
1048// ('coroutine.resume', 'coroutine.create', ...). Single-source
1049// fix is to gate lua-vm arg_error_impl's find_func_name_in_loaded
1050// fallback off for V51 (same gap hits base/math arg errors).
1051// ──────────────────────────────────────────────────────────────────────────────