Skip to main content

luaur_vm/functions/
lua_topointer.rs

1use core::ffi::c_int;
2use core::ffi::c_void;
3
4use crate::enums::lua_type::lua_Type;
5use crate::functions::index_2_addr::index2addr;
6use crate::macros::gcvalue::gcvalue;
7use crate::macros::iscollectable::iscollectable;
8use crate::macros::ttype::ttype;
9use crate::macros::uvalue::uvalue;
10use crate::type_aliases::lua_state::lua_State;
11use crate::type_aliases::stk_id::StkId;
12
13#[allow(non_snake_case)]
14pub unsafe fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void {
15    let o: StkId = index2addr(L, idx);
16    let tt = ttype!(o);
17
18    if tt == lua_Type::LUA_TUSERDATA as i32 {
19        (*uvalue!(o)).data.as_ptr() as *const c_void
20    } else if tt == lua_Type::LUA_TLIGHTUSERDATA as i32 {
21        // pvalue(o) is defined as check_exp(ttislightuserdata(o), (o)->value.p)
22        // In Rust, the pvalue macro is currently a stub or constant,
23        // so we access the union field directly to match the C++ logic.
24        (*o).value.p as *const c_void
25    } else {
26        if iscollectable!(o) {
27            gcvalue!(o) as *const c_void
28        } else {
29            core::ptr::null()
30        }
31    }
32}