Skip to main content

luaur_vm/functions/
lua_b_select.rs

1use crate::enums::lua_type::lua_Type;
2use crate::functions::lua_gettop::lua_gettop;
3use crate::functions::lua_l_checkinteger::lua_l_checkinteger;
4use crate::functions::lua_pushinteger::lua_pushinteger;
5use crate::functions::lua_type::lua_type;
6use crate::macros::lua_l_argcheck::luaL_argcheck;
7use crate::macros::lua_tostring::lua_tostring;
8use crate::type_aliases::lua_state::lua_State;
9
10pub unsafe fn lua_b_select(L: *mut lua_State) -> core::ffi::c_int {
11    let n = lua_gettop(L);
12    let first_type = lua_type(L, 1);
13    if first_type == lua_Type::LUA_TSTRING as i32 {
14        let str_ptr = lua_tostring!(L, 1);
15        let first_char = *str_ptr;
16        if first_char == b'#' as core::ffi::c_char {
17            lua_pushinteger(L, n - 1);
18            return 1;
19        }
20    }
21
22    let i = lua_l_checkinteger(L, 1);
23    let i = if i < 0 {
24        n + i
25    } else if i > n {
26        n
27    } else {
28        i
29    };
30
31    luaL_argcheck!(L, 1 <= i, 1, "index out of range");
32    n - i
33}