use crate::state_stub::{LuaState, LuaStateStubExt as _};
use lua_types::error::LuaError;
type LuaCFunction = fn(&mut LuaState) -> Result<usize, LuaError>;
static LOADED_LIBS: &[(&[u8], LuaCFunction)] = &[
(b"_G", crate::base::open),
#[cfg(feature = "package")]
(b"package", crate::loadlib::luaopen_package),
#[cfg(feature = "coroutine")]
(b"coroutine", crate::coro_lib::open_coroutine),
(b"table", crate::table_lib::open_table),
#[cfg(feature = "io")]
(b"io", crate::io_lib::luaopen_io),
#[cfg(feature = "os")]
(b"os", crate::os_lib::open_os),
(b"string", crate::string_lib::luaopen_string),
(b"math", crate::math_lib::luaopen_math),
#[cfg(feature = "utf8")]
(b"utf8", crate::utf8_lib::open_utf8),
#[cfg(feature = "debug")]
(b"debug", crate::debug_lib::open_debug),
];
pub fn open_libs(state: &mut LuaState) -> Result<(), LuaError> {
let has_utf8 = state
.global()
.lua_version
.supports(lua_types::Feature::Utf8Lib);
for &(name, func) in LOADED_LIBS {
if name == b"utf8".as_slice() && !has_utf8 {
continue;
}
state.require_lib(name, func, true)?;
state.pop_n(1);
}
#[cfg(feature = "bit32")]
if state
.global()
.lua_version
.supports(lua_types::Feature::Bit32Lib)
{
state.require_lib(b"bit32", crate::bit32_lib::open_bit32, true)?;
state.pop_n(1);
}
Ok(())
}