luaur-rt 0.1.1

Safe, ergonomic, mlua-style API for luaur (pure-Rust Luau).
Documentation
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! The [`Thread`] handle and [`ThreadStatus`]. Mirrors `mlua::Thread` /
//! `mlua::ThreadStatus`.
//!
//! A thread is a Luau coroutine. It is created from a [`Function`] via
//! [`Lua::create_thread`] (or surfaces from `coroutine.create(...)` evaluated
//! in Lua) and driven with [`Thread::resume`].
//!
//! ## Implementation
//!
//! The thread is a first-class Lua value, so the handle holds a registry
//! reference (like every other handle) keeping the coroutine alive. We also
//! cache the raw `*mut lua_State` of the coroutine for the resume/xmove dance.
//!
//! `resume` mirrors mlua: push the args onto the *parent* state, `lua_xmove`
//! them to the coroutine, `lua_resume(co, parent, nargs)`, then `lua_xmove` the
//! results back and convert them. Status is derived from `lua_status` +
//! `lua_costatus`, matching mlua's `Resumable`/`Running`/`Normal`/`Finished`/
//! `Error` mapping.

use crate::error::{Error, Result};
use crate::function::Function;
use crate::multi::MultiValue;
use crate::state::{Lua, LuaRef};
use crate::sync::{NotSync, XRc, NOT_SYNC};
use crate::sys::*;
use crate::traits::{FromLuaMulti, IntoLua, IntoLuaMulti};

/// Status of a Lua thread (coroutine). Mirrors `mlua::ThreadStatus`.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ThreadStatus {
    /// The thread was just created or is suspended (yielded) and can be resumed.
    Resumable,
    /// The thread is currently running.
    Running,
    /// The thread is active but not running (it resumed another thread).
    Normal,
    /// The thread has finished executing.
    Finished,
    /// The thread raised a Lua error during execution.
    Error,
}

/// The raw outcome of one async resume. See [`Thread::resume_for_async`].
#[cfg(feature = "async")]
pub(crate) enum AsyncResume {
    /// The coroutine yielded the internal "future pending" marker.
    Pending,
    /// The coroutine yielded values via `coroutine.yield` (a Stream item).
    Yielded(MultiValue),
    /// The coroutine finished, returning these values.
    Returned(MultiValue),
}

/// A handle to a Lua thread (coroutine). Mirrors `mlua::Thread`.
///
/// Under the `send` feature it is `Send` but never `Sync` — see
/// [`crate::sync::NotSync`].
#[derive(Clone)]
pub struct Thread {
    pub(crate) reference: XRc<LuaRef>,
    /// The raw coroutine state pointer (cached from the referenced value).
    pub(crate) thread_state: *mut lua_State,
    pub(crate) _not_sync: NotSync,
}

// `Thread` caches a raw `*mut lua_State` (the coroutine), which is `!Send` by
// default. Under the move-only `send` contract it is sound to move a `Thread`
// to another thread (the cached pointer stays valid; the VM is single-threaded
// in use). `!Sync` is preserved by the `NotSync` marker.
#[cfg(feature = "send")]
unsafe impl Send for Thread {}

impl Thread {
    /// Build a [`Thread`] from a registry ref to a thread value. Caches the
    /// coroutine's raw state via `lua_tothread`.
    pub(crate) fn from_ref(reference: LuaRef) -> Thread {
        let state = reference.state();
        let thread_state = unsafe {
            reference.push();
            let ts = lua_tothread(state, -1);
            lua_pop(state, 1);
            ts
        };
        Thread {
            reference: XRc::new(reference),
            thread_state,
            _not_sync: NOT_SYNC,
        }
    }

    pub(crate) unsafe fn push_to_stack(&self) {
        self.reference.push();
    }

    /// The owning [`Lua`].
    pub fn lua(&self) -> Lua {
        self.reference.lua()
    }

    /// The raw coroutine state pointer. Mirrors `mlua::Thread::state`.
    pub fn state(&self) -> *mut lua_State {
        self.thread_state
    }

    /// Resume the coroutine, passing `args` and converting its yielded/returned
    /// values to `R`. Mirrors `mlua::Thread::resume`.
    ///
    /// Returns [`Error::CoroutineUnresumable`] if the thread has finished,
    /// errored, or is otherwise not resumable.
    pub fn resume<R: FromLuaMulti>(&self, args: impl IntoLuaMulti) -> Result<R> {
        let lua = self.lua();
        // Convert the args first so a failing `IntoLua` (e.g. a bad argument)
        // surfaces *before* we touch any Lua stack — matching mlua.
        let args: MultiValue = args.into_lua_multi(&lua)?;

        if !matches!(self.status(), ThreadStatus::Resumable) {
            return Err(Error::CoroutineUnresumable);
        }

        let parent = lua.state();
        let co = self.thread_state;
        unsafe {
            let nargs = args.len() as c_int;
            if lua_checkstack(co, nargs.saturating_add(2)) == 0 {
                return Err(Error::RuntimeError(
                    "stack overflow: too many arguments to coroutine resume".to_string(),
                ));
            }
            // Push args onto the parent, then move them to the coroutine.
            for v in args.iter() {
                lua.push_value(v)?;
            }
            if nargs > 0 {
                lua_xmove(parent, co, nargs);
            }

            self.resume_inner::<R>(&lua, nargs)
        }
    }

    /// Resume the coroutine, immediately raising `error` inside it.
    /// Mirrors `mlua::Thread::resume_error` (a Luau extension).
    pub fn resume_error<R: FromLuaMulti>(&self, error: impl IntoLua) -> Result<R> {
        let lua = self.lua();
        let err_value = error.into_lua(&lua)?;

        if !matches!(self.status(), ThreadStatus::Resumable) {
            return Err(Error::CoroutineUnresumable);
        }

        let parent = lua.state();
        let co = self.thread_state;
        unsafe {
            if lua_checkstack(co, 2) == 0 {
                return Err(Error::RuntimeError("stack overflow".to_string()));
            }
            lua.push_value(&err_value)?;
            lua_xmove(parent, co, 1);
            // lua_resumeerror does the resume-with-error and returns the status.
            let status = lua_resumeerror(co, parent);
            self.finish_resume::<R>(&lua, status)
        }
    }

    /// Run `lua_resume` and collect/convert the results. Expects `nargs` already
    /// moved onto the coroutine stack.
    unsafe fn resume_inner<R: FromLuaMulti>(&self, lua: &Lua, nargs: c_int) -> Result<R> {
        let parent = lua.state();
        let co = self.thread_state;
        let status = unsafe { lua_resume(co, parent, nargs) };
        unsafe { self.finish_resume::<R>(lua, status) }
    }

    /// Common tail of `resume`/`resume_error`: inspect the status, move results
    /// back to the parent, and convert.
    unsafe fn finish_resume<R: FromLuaMulti>(&self, lua: &Lua, status: c_int) -> Result<R> {
        let parent = lua.state();
        let co = self.thread_state;
        unsafe {
            if status != status::OK && status != status::YIELD && status != status::BREAK {
                // Error: the coroutine left the error object on its own stack.
                let nres = lua_gettop(co);
                if nres > 0 {
                    lua_xmove(co, parent, nres);
                }
                let err = lua.pop_error(status);
                // Clear any extra values the coroutine left on the parent.
                return Err(err);
            }
            // `LUA_BREAK` is an interrupt-driven yield: the coroutine produced
            // no values and its entire register window is still *live* (it must
            // continue from the break point on the next resume). We must NOT
            // touch its stack — moving any values off would strip live
            // registers and corrupt the re-entry. Return an empty result.
            if status == status::BREAK {
                return R::from_lua_multi(MultiValue::with_capacity(0), lua);
            }
            // Success/yield: the produced values sit on the coroutine stack.
            let nres = lua_gettop(co);
            if lua_checkstack(parent, nres.saturating_add(1)) == 0 {
                return Err(Error::RuntimeError("stack overflow".to_string()));
            }
            let base = lua_gettop(parent);
            if nres > 0 {
                lua_xmove(co, parent, nres);
            }
            let mut results = MultiValue::with_capacity(nres.max(0) as usize);
            for i in 0..nres {
                results.push_back(lua.value_from_stack(base + 1 + i)?);
            }
            lua_settop(parent, base);
            R::from_lua_multi(results, lua)
        }
    }

    /// Low-level resume used by the async driver. Pushes `args` to the
    /// coroutine, resumes it once, and returns the raw outcome:
    ///
    /// * `Err(e)` — the coroutine raised an error.
    /// * `Ok(AsyncResume::Pending)` — the coroutine yielded the internal
    ///   "future pending" marker (a single light-userdata == `poll_pending()`);
    ///   the stack is left cleared.
    /// * `Ok(AsyncResume::Yielded(vals))` — the coroutine yielded `vals`
    ///   (a `coroutine.yield`, i.e. a Stream item).
    /// * `Ok(AsyncResume::Returned(vals))` — the coroutine finished, returning
    ///   `vals`.
    ///
    /// The coroutine stack is fully consumed/cleared on every path.
    #[cfg(feature = "async")]
    pub(crate) fn resume_for_async(&self, args: MultiValue) -> Result<AsyncResume> {
        let lua = self.lua();
        let parent = lua.state();
        let co = self.thread_state;
        unsafe {
            let nargs = args.len() as c_int;
            if lua_checkstack(co, nargs.saturating_add(2)) == 0 {
                return Err(Error::RuntimeError(
                    "stack overflow: too many arguments to coroutine resume".to_string(),
                ));
            }
            for v in args.iter() {
                lua.push_value(v)?;
            }
            if nargs > 0 {
                lua_xmove(parent, co, nargs);
            }
            let status = lua_resume(co, parent, nargs);

            if status != status::OK && status != status::YIELD {
                let nres = lua_gettop(co);
                if nres > 0 {
                    lua_xmove(co, parent, nres);
                }
                return Err(lua.pop_error(status));
            }

            let yielded = status == status::YIELD;
            let nres = lua_gettop(co);

            // Detect the single-light-userdata pending marker (top of the
            // coroutine stack) on a yield.
            if yielded
                && nres == 1
                && crate::sys::lua_tolightuserdata(co, -1) == crate::async_support::poll_pending()
            {
                lua_settop(co, 0);
                return Ok(AsyncResume::Pending);
            }

            // Otherwise move the produced values to the parent and convert.
            if lua_checkstack(parent, nres.saturating_add(1)) == 0 {
                return Err(Error::RuntimeError("stack overflow".to_string()));
            }
            let base = lua_gettop(parent);
            if nres > 0 {
                lua_xmove(co, parent, nres);
            }
            let mut results = MultiValue::with_capacity(nres.max(0) as usize);
            for i in 0..nres {
                results.push_back(lua.value_from_stack(base + 1 + i)?);
            }
            lua_settop(parent, base);
            lua_settop(co, 0);

            if yielded {
                Ok(AsyncResume::Yielded(results))
            } else {
                Ok(AsyncResume::Returned(results))
            }
        }
    }

    /// Resume a yielded async coroutine with the "terminate" signal so it drops
    /// its in-flight future and parks. Best-effort; ignores errors. Used when an
    /// [`AsyncThread`](crate::async_support::AsyncThread) is dropped mid-flight.
    #[cfg(feature = "async")]
    pub(crate) fn terminate_async(&self) {
        if !self.is_resumable() {
            return;
        }
        let lua = self.lua();
        let parent = lua.state();
        let co = self.thread_state;
        unsafe {
            if lua_checkstack(co, 2) == 0 {
                return;
            }
            crate::sys::lua_pushlightuserdatatagged(
                parent,
                crate::async_support::poll_terminate(),
                0,
            );
            lua_xmove(parent, co, 1);
            let _ = lua_resume(co, parent, 1);
            lua_settop(co, 0);
        }
    }

    /// The thread's status. Mirrors `mlua::Thread::status`.
    pub fn status(&self) -> ThreadStatus {
        let lua = self.lua();
        let parent = lua.state();
        let co = self.thread_state;
        // A thread whose state is the currently-running state is "Running".
        if co == parent {
            return ThreadStatus::Running;
        }
        unsafe {
            // A coroutine yielded by an interrupt (`lua_break`) has raw status
            // `LUA_BREAK`; `lua_costatus` reports that as "normal", but the
            // coroutine is in fact resumable (it continues from the break point
            // on the next resume). Detect it directly.
            if lua_status(co) == status::BREAK {
                return ThreadStatus::Resumable;
            }
            let cos = lua_costatus(parent, co);
            match cos {
                costatus::SUSPENDED => ThreadStatus::Resumable,
                costatus::RUNNING => ThreadStatus::Running,
                costatus::NORMAL => ThreadStatus::Normal,
                costatus::FINISHED => ThreadStatus::Finished,
                costatus::ERROR => ThreadStatus::Error,
                _ => {
                    // Fall back to lua_status for any unexpected code.
                    let s = lua_status(co);
                    if s == status::YIELD {
                        ThreadStatus::Resumable
                    } else if s == status::OK {
                        // New (function on stack) vs finished (empty stack).
                        if lua_gettop(co) > 0 {
                            ThreadStatus::Resumable
                        } else {
                            ThreadStatus::Finished
                        }
                    } else {
                        ThreadStatus::Error
                    }
                }
            }
        }
    }

    /// Whether the thread can be resumed. Mirrors `mlua::Thread::is_resumable`.
    pub fn is_resumable(&self) -> bool {
        self.status() == ThreadStatus::Resumable
    }

    /// Whether the thread is currently running. Mirrors `mlua::Thread::is_running`.
    pub fn is_running(&self) -> bool {
        self.status() == ThreadStatus::Running
    }

    /// Whether the thread is active but not running. Mirrors
    /// `mlua::Thread::is_normal`.
    pub fn is_normal(&self) -> bool {
        self.status() == ThreadStatus::Normal
    }

    /// Whether the thread has finished executing. Mirrors
    /// `mlua::Thread::is_finished`.
    pub fn is_finished(&self) -> bool {
        self.status() == ThreadStatus::Finished
    }

    /// Whether the thread raised an error. Mirrors `mlua::Thread::is_error`.
    pub fn is_error(&self) -> bool {
        self.status() == ThreadStatus::Error
    }

    /// Reset the thread to a fresh state and install `func` as its body.
    /// Mirrors `mlua::Thread::reset` (Luau semantics: any non-running thread can
    /// be reset).
    pub fn reset(&self, func: Function) -> Result<()> {
        let status = self.status();
        match status {
            ThreadStatus::Running => {
                return Err(Error::runtime("cannot reset a running thread"));
            }
            ThreadStatus::Normal => {
                return Err(Error::runtime("cannot reset a normal thread"));
            }
            _ => {}
        }
        let lua = self.lua();
        let parent = lua.state();
        let co = self.thread_state;
        unsafe {
            lua_resetthread(co);
            // Push the new body function onto the coroutine stack.
            func.push_to_stack();
            lua_xmove(parent, co, 1);
            // Re-inherit the *main* globals table into the coroutine, dropping
            // any sandbox proxy global a prior `Thread::sandbox` had installed
            // (matches mlua's Luau `reset`: a reset thread sees the main env).
            lua_pushvalue(parent, LUA_GLOBALSINDEX);
            lua_xmove(parent, co, 1);
            lua_replace(co, LUA_GLOBALSINDEX);
        }
        Ok(())
    }

    /// A raw pointer identifying this thread. Mirrors `mlua::Thread::to_pointer`.
    pub fn to_pointer(&self) -> *const c_void {
        let state = self.reference.state();
        unsafe {
            self.reference.push();
            let p = lua_topointer(state, -1);
            lua_pop(state, 1);
            p
        }
    }
}

impl std::fmt::Debug for Thread {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Thread")
    }
}

// ---------------------------------------------------------------------------
// Async: drive a coroutine as a Rust `Future` / `Stream` (the `async` feature)
// ---------------------------------------------------------------------------

#[cfg(feature = "async")]
impl Thread {
    /// Convert this (resumable) thread into an
    /// [`AsyncThread`](crate::AsyncThread) that implements
    /// [`Future`](std::future::Future) and
    /// [`Stream`](futures_util::stream::Stream).
    ///
    /// Mirrors `mlua::Thread::into_async`. `args` are passed to the coroutine on
    /// its first resume. As a `Future` the thread is driven to completion and
    /// resolves to its final return value(s); as a `Stream` each
    /// `coroutine.yield` produces an item.
    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
    pub fn into_async<R: FromLuaMulti>(
        self,
        args: impl IntoLuaMulti,
    ) -> Result<crate::async_support::AsyncThread<R>> {
        if !self.is_resumable() {
            return Err(Error::CoroutineUnresumable);
        }
        let lua = self.lua();
        let args = args.into_lua_multi(&lua)?;
        Ok(crate::async_support::AsyncThread::new(self, args))
    }
}

impl PartialEq for Thread {
    fn eq(&self, other: &Self) -> bool {
        self.to_pointer() == other.to_pointer()
    }
}

impl IntoLua for Thread {
    fn into_lua(self, _lua: &Lua) -> Result<crate::value::Value> {
        Ok(crate::value::Value::Thread(self))
    }
}

impl IntoLua for &Thread {
    fn into_lua(self, _lua: &Lua) -> Result<crate::value::Value> {
        Ok(crate::value::Value::Thread(self.clone()))
    }
}

impl FromLua for Thread {
    fn from_lua(value: crate::value::Value, _lua: &Lua) -> Result<Self> {
        match value {
            crate::value::Value::Thread(t) => Ok(t),
            other => Err(Error::FromLuaConversionError {
                from: other.type_name(),
                to: "Thread".to_string(),
                message: None,
            }),
        }
    }
}

use crate::traits::FromLua;

impl Lua {
    /// Create a new coroutine from a [`Function`]. Mirrors
    /// `mlua::Lua::create_thread`.
    pub fn create_thread(&self, func: Function) -> Result<Thread> {
        let state = self.state();
        unsafe {
            // Create a new thread; it is pushed on the parent stack.
            let co = lua_newthread(state);
            if co.is_null() {
                return Err(Error::runtime("luaur-rt: failed to create thread"));
            }
            // Take a ref to the thread value (still on the parent stack top).
            let thread = Thread::from_ref(self.pop_ref());
            // Move the body function onto the coroutine's stack so the first
            // resume invokes it.
            func.push_to_stack(); // pushes onto parent stack
            lua_xmove(state, co, 1);
            Ok(thread)
        }
    }

    /// The currently-running thread. Mirrors `mlua::Lua::current_thread`.
    ///
    /// Inside a Rust callback this is the coroutine (or main thread) that
    /// invoked it. Under the `async` feature, a coroutine created implicitly by
    /// `call_async` is transparent: this returns its *owner* thread instead, so
    /// `current_thread()` is stable across the implicit-coroutine boundary
    /// (matching mlua).
    pub fn current_thread(&self) -> Thread {
        let state = self.state();
        // If we are running on an implicit `call_async` coroutine, report the
        // owner thread that issued the call.
        #[cfg(feature = "async")]
        if let Some(owner) = crate::async_support::implicit_thread_owner(state) {
            unsafe {
                lua_pushthread(owner);
                // The owner-thread value is on the owner's stack; move it to this
                // state so we can take a ref to it from here.
                if owner != state {
                    lua_xmove(owner, state, 1);
                }
                return Thread::from_ref(self.pop_ref());
            }
        }
        unsafe {
            lua_pushthread(state);
            Thread::from_ref(self.pop_ref())
        }
    }
}