luau-vm 0.732.0

Pure-Rust Luau virtual machine, garbage collector, and standard libraries
Documentation
mod base;
mod bit32;
mod buffer;
mod class;
mod coroutine;
mod debug;
mod integer;
mod math;
mod os;
mod string;
mod table;
mod utf8;
mod vector;

use crate::native::NativeCallResult;
use crate::thread::Thread;
use luau_common::flags;
pub(crate) const LUA_COLIB_NAME: &str = "coroutine";
pub(crate) const LUA_TABLIB_NAME: &str = "table";
pub(crate) const LUA_OSLIB_NAME: &str = "os";
pub(crate) const LUA_STRLIB_NAME: &str = "string";
pub(crate) const LUA_BITLIB_NAME: &str = "bit32";
pub(crate) const LUA_BUFFERLIB_NAME: &str = "buffer";
pub(crate) const LUA_UTF8LIB_NAME: &str = "utf8";
pub(crate) const LUA_CLASSLIB_NAME: &str = "class";
pub(crate) const LUA_MATHLIB_NAME: &str = "math";
pub(crate) const LUA_DBLIB_NAME: &str = "debug";
pub(crate) const LUA_VECLIB_NAME: &str = "vector";
pub(crate) const LUA_INTLIB_NAME: &str = "integer";

type LibraryOpen = unsafe fn(&Thread) -> NativeCallResult;

const LUA_LIBS: &[LibraryOpen] = &[
    Thread::open_base,
    Thread::open_coroutine,
    Thread::open_table,
    Thread::open_os,
    Thread::open_string,
    Thread::open_math,
    Thread::open_debug,
    Thread::open_utf8,
    Thread::open_bit32,
    Thread::open_buffer,
    Thread::open_vector,
    Thread::open_integer,
];

const LUA_LIBS_NOINTEGER: &[LibraryOpen] = &[
    Thread::open_base,
    Thread::open_coroutine,
    Thread::open_table,
    Thread::open_os,
    Thread::open_string,
    Thread::open_math,
    Thread::open_debug,
    Thread::open_utf8,
    Thread::open_bit32,
    Thread::open_buffer,
    Thread::open_vector,
];

unsafe fn call_library(thread: &Thread, open: LibraryOpen) -> NativeCallResult {
    unsafe {
        let top = thread.get_top();
        open(thread)?;
        thread.restore_top(top);
    }
    Ok(0)
}

impl Thread {
    /// `luaL_openlibs`
    pub unsafe fn open_libs(&self) -> NativeCallResult {
        let libraries = if flags::LuauIntegerLibrary.get() {
            LUA_LIBS
        } else {
            LUA_LIBS_NOINTEGER
        };

        for open in libraries {
            unsafe { call_library(self, *open)? };
        }

        if flags::DebugLuauUserDefinedClassesRuntime.get() {
            unsafe { call_library(self, Thread::open_class)? };
        }
        Ok(0)
    }
}