Skip to main content

luau_vm/libs/
mod.rs

1mod base;
2mod bit32;
3mod buffer;
4mod class;
5mod coroutine;
6mod debug;
7mod integer;
8mod math;
9mod os;
10mod string;
11mod table;
12mod utf8;
13mod vector;
14
15use crate::native::NativeCallResult;
16use crate::thread::Thread;
17use luau_common::flags;
18pub(crate) const LUA_COLIB_NAME: &str = "coroutine";
19pub(crate) const LUA_TABLIB_NAME: &str = "table";
20pub(crate) const LUA_OSLIB_NAME: &str = "os";
21pub(crate) const LUA_STRLIB_NAME: &str = "string";
22pub(crate) const LUA_BITLIB_NAME: &str = "bit32";
23pub(crate) const LUA_BUFFERLIB_NAME: &str = "buffer";
24pub(crate) const LUA_UTF8LIB_NAME: &str = "utf8";
25pub(crate) const LUA_CLASSLIB_NAME: &str = "class";
26pub(crate) const LUA_MATHLIB_NAME: &str = "math";
27pub(crate) const LUA_DBLIB_NAME: &str = "debug";
28pub(crate) const LUA_VECLIB_NAME: &str = "vector";
29pub(crate) const LUA_INTLIB_NAME: &str = "integer";
30
31type LibraryOpen = unsafe fn(&Thread) -> NativeCallResult;
32
33const LUA_LIBS: &[LibraryOpen] = &[
34    Thread::open_base,
35    Thread::open_coroutine,
36    Thread::open_table,
37    Thread::open_os,
38    Thread::open_string,
39    Thread::open_math,
40    Thread::open_debug,
41    Thread::open_utf8,
42    Thread::open_bit32,
43    Thread::open_buffer,
44    Thread::open_vector,
45    Thread::open_integer,
46];
47
48const LUA_LIBS_NOINTEGER: &[LibraryOpen] = &[
49    Thread::open_base,
50    Thread::open_coroutine,
51    Thread::open_table,
52    Thread::open_os,
53    Thread::open_string,
54    Thread::open_math,
55    Thread::open_debug,
56    Thread::open_utf8,
57    Thread::open_bit32,
58    Thread::open_buffer,
59    Thread::open_vector,
60];
61
62unsafe fn call_library(thread: &Thread, open: LibraryOpen) -> NativeCallResult {
63    unsafe {
64        let top = thread.get_top();
65        open(thread)?;
66        thread.restore_top(top);
67    }
68    Ok(0)
69}
70
71impl Thread {
72    /// `luaL_openlibs`
73    pub unsafe fn open_libs(&self) -> NativeCallResult {
74        let libraries = if flags::LuauIntegerLibrary.get() {
75            LUA_LIBS
76        } else {
77            LUA_LIBS_NOINTEGER
78        };
79
80        for open in libraries {
81            unsafe { call_library(self, *open)? };
82        }
83
84        if flags::DebugLuauUserDefinedClassesRuntime.get() {
85            unsafe { call_library(self, Thread::open_class)? };
86        }
87        Ok(0)
88    }
89}