luau 0.732.0

Safe lifetime-bound Rust embedding API for the Luau runtime
use luau_vm::Thread as VmThread;

use crate::lua::runtime::RuntimeData;
use crate::thread::Thread;

/// A borrowed reference to the active Luau state.
///
/// Callbacks receive this value to create and access Luau values without
/// owning the state.
#[derive(Clone, Copy)]
pub struct LuaRef<'lua> {
    thread: &'lua VmThread,
    runtime: &'lua RuntimeData,
}

impl<'lua> LuaRef<'lua> {
    pub(crate) const fn new(thread: &'lua VmThread, runtime: &'lua RuntimeData) -> Self {
        Self { thread, runtime }
    }

    /// Returns a handle to the thread that supplied this reference.
    pub fn current_thread(&self) -> Thread<'lua> {
        Thread::new(self.thread, self.runtime)
    }

    pub(crate) const fn as_vm(&self) -> &'lua VmThread {
        self.thread
    }

    pub(crate) const fn runtime(&self) -> &'lua RuntimeData {
        self.runtime
    }

    /// Returns whether the active thread can yield.
    pub fn is_yieldable(&self) -> bool {
        unsafe { self.thread.is_yieldable() != 0 }
    }
}