Skip to main content

luaur_vm/functions/
lua_touserdata.rs

1use core::ffi::c_int;
2
3use crate::functions::index_2_addr::index2addr;
4use crate::macros::ttislightuserdata::ttislightuserdata;
5use crate::macros::ttisuserdata::ttisuserdata;
6use crate::macros::uvalue::uvalue;
7use crate::type_aliases::lua_state::lua_State;
8use crate::type_aliases::stk_id::StkId;
9
10#[allow(non_snake_case)]
11pub unsafe fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut core::ffi::c_void {
12    let o: StkId = index2addr(L, idx);
13
14    if ttisuserdata!(o) {
15        // uvalue(o) returns a pointer to the Udata struct.
16        // The data field is a char[1] at the end of the struct.
17        // We return the address of that array as a void pointer.
18        uvalue!(o).data.as_ptr() as *mut core::ffi::c_void
19    } else if ttislightuserdata!(o) {
20        // pvalue(o) is defined as a constant in the provided context, but the C++ macro
21        // accesses (o)->value.p. Based on the provided PVALUE constant and the
22        // requirement to follow the C++ logic, we access the pointer value.
23        (*o).value.p
24    } else {
25        core::ptr::null_mut()
26    }
27}