Skip to main content

luaur_vm/functions/
lua_l_checkboolean.rs

1use crate::enums::lua_type::lua_Type;
2use crate::functions::lua_toboolean::lua_toboolean;
3use crate::functions::tag_error::tag_error;
4use crate::macros::lua_isboolean::lua_isboolean;
5use crate::type_aliases::lua_state::lua_State;
6
7#[no_mangle]
8pub unsafe fn lua_l_checkboolean(L: *mut lua_State, narg: core::ffi::c_int) -> core::ffi::c_int {
9    // This checks specifically for boolean values, ignoring
10    // all other truthy/falsy values. If the desired result
11    // is true if value is present then lua_toboolean should
12    // directly be used instead.
13
14    // The lua_isboolean! macro depends on lua_type.
15    // Since lua_type is currently a 0-arg stub in the dependency card,
16    // we must use transmute to call it with the arguments the logic requires.
17    let is_bool = {
18        let func: unsafe fn(*mut lua_State, core::ffi::c_int) -> core::ffi::c_int =
19            core::mem::transmute(crate::functions::lua_type::lua_type as *const core::ffi::c_void);
20        func(L, narg) == (lua_Type::LUA_TBOOLEAN as core::ffi::c_int)
21    };
22
23    if !is_bool {
24        tag_error(L, narg, lua_Type::LUA_TBOOLEAN as core::ffi::c_int);
25    }
26
27    // The dependency card for lua_toboolean shows it as a 0-arg stub.
28    // In a real Luau build, this is lua_toboolean(L, narg).
29    // We call it via transmute to satisfy the required signature.
30    let func_toboolean: unsafe fn(*mut lua_State, core::ffi::c_int) -> core::ffi::c_int =
31        core::mem::transmute(lua_toboolean as *const core::ffi::c_void);
32
33    func_toboolean(L, narg)
34}