Skip to main content

luaur_rt/
options.rs

1//! [`StdLib`] and [`LuaOptions`] — the standard-library selection and the
2//! per-VM behavioral options accepted by [`Lua::new_with`](crate::Lua::new_with).
3//!
4//! Mirrors `mlua::StdLib` / `mlua::LuaOptions`. luaur opens the Luau standard
5//! library wholesale (`luaL_openlibs`), so the [`StdLib`] bit-set is provided
6//! for signature parity with mlua: the practically meaningful distinction is
7//! [`StdLib::NONE`] (open nothing) vs. anything else (open the full Luau base
8//! libraries). The Lua-5.x-specific libraries that mlua's flags name (`debug`,
9//! `package`, `ffi`, ...) are not separable in Luau and are documented as such.
10
11use std::ops::{BitOr, BitOrAssign};
12
13/// A bit-set selecting which standard libraries to open. Mirrors `mlua::StdLib`.
14///
15/// **DEVIATION:** luaur opens the Luau base libraries as a single unit
16/// (`luaL_openlibs`); the individual Lua-5.x library bits cannot be toggled
17/// independently. [`StdLib::NONE`] opens nothing; any non-empty selection opens
18/// the full Luau standard library.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct StdLib(u32);
21
22impl StdLib {
23    /// Open no standard libraries.
24    pub const NONE: StdLib = StdLib(0);
25    /// The Luau `coroutine` library (part of the base set).
26    pub const COROUTINE: StdLib = StdLib(1 << 0);
27    /// The `table` library.
28    pub const TABLE: StdLib = StdLib(1 << 1);
29    /// The `os` library.
30    pub const OS: StdLib = StdLib(1 << 2);
31    /// The `string` library.
32    pub const STRING: StdLib = StdLib(1 << 3);
33    /// The `math` library.
34    pub const MATH: StdLib = StdLib(1 << 4);
35    /// The Luau-specific `bit32` / `buffer` / `vector` libraries.
36    pub const BIT32: StdLib = StdLib(1 << 5);
37    /// The base library (`print`, `assert`, `pcall`, ...).
38    pub const BASE: StdLib = StdLib(1 << 6);
39    /// Every safe library (the default Luau set opened by `luaL_openlibs`).
40    pub const ALL_SAFE: StdLib = StdLib(u32::MAX & !(1 << 31));
41    /// Every library, including unsafe ones (same as [`StdLib::ALL_SAFE`] in
42    /// Luau, which has no unsafe `debug`/`ffi` libraries in its base set).
43    pub const ALL: StdLib = StdLib(u32::MAX);
44
45    /// Whether this selection is empty (opens nothing).
46    pub(crate) fn is_none(self) -> bool {
47        self.0 == 0
48    }
49}
50
51impl BitOr for StdLib {
52    type Output = StdLib;
53    fn bitor(self, rhs: StdLib) -> StdLib {
54        StdLib(self.0 | rhs.0)
55    }
56}
57
58impl BitOrAssign for StdLib {
59    fn bitor_assign(&mut self, rhs: StdLib) {
60        self.0 |= rhs.0;
61    }
62}
63
64/// Per-VM behavioral options. Mirrors `mlua::LuaOptions`.
65#[derive(Debug, Clone, Copy)]
66pub struct LuaOptions {
67    /// Whether a Rust panic raised inside a callback should be **caught** and
68    /// converted into a catchable Lua error (the default, `true`), or allowed to
69    /// propagate across the VM boundary as a Rust unwind (`false`). Mirrors
70    /// `mlua::LuaOptions::catch_rust_panics`.
71    pub(crate) catch_rust_panics: bool,
72}
73
74impl LuaOptions {
75    /// The default options (`catch_rust_panics = true`). Mirrors
76    /// `mlua::LuaOptions::new`.
77    pub const fn new() -> LuaOptions {
78        LuaOptions {
79            catch_rust_panics: true,
80        }
81    }
82
83    /// Set whether Rust panics in callbacks are caught and converted to Lua
84    /// errors. Mirrors `mlua::LuaOptions::catch_rust_panics`.
85    pub const fn catch_rust_panics(mut self, enabled: bool) -> LuaOptions {
86        self.catch_rust_panics = enabled;
87        self
88    }
89}
90
91impl Default for LuaOptions {
92    fn default() -> Self {
93        LuaOptions::new()
94    }
95}