Skip to main content

luaur_vm/functions/
lua_clonetable.rs

1use crate::functions::index_2_addr::index_2_addr;
2use crate::functions::lua_h_clone::lua_h_clone;
3use crate::macros::api_check::api_check;
4use crate::macros::api_incr_top::api_incr_top;
5use crate::macros::hvalue::hvalue;
6use crate::macros::sethvalue::sethvalue;
7use crate::macros::ttistable::ttistable;
8use crate::type_aliases::lua_state::lua_State;
9use crate::type_aliases::lua_table::LuaTable;
10use crate::type_aliases::stk_id::StkId;
11
12#[allow(non_snake_case)]
13pub unsafe fn lua_clonetable(L: *mut lua_State, idx: core::ffi::c_int) {
14    // The C++ source:
15    // StkId t = index2addr(L, idx);
16    // api_check(L, ttistable(t));
17    // LuaTable* tt = luaH_clone(L, hvalue(t));
18    // sethvalue(L, L->top, tt);
19    // api_incr_top(L);
20
21    // We must cast the stubbed function pointers to their real signatures to call them.
22    // Rust does not allow direct casting from fn item to fn pointer with different signature,
23    // so we cast through a usize.
24    let index2addr_fn: unsafe fn(*mut lua_State, core::ffi::c_int) -> StkId =
25        core::mem::transmute(index_2_addr as *const core::ffi::c_void);
26    let t: StkId = index2addr_fn(L, idx);
27
28    api_check!(L, ttistable!(t));
29
30    let lua_h_clone_fn: unsafe fn(*mut lua_State, *mut LuaTable) -> *mut LuaTable =
31        core::mem::transmute(lua_h_clone as *const core::ffi::c_void);
32    let tt: *mut LuaTable = lua_h_clone_fn(L, hvalue!(t));
33
34    sethvalue!(L, (*L).top, tt);
35    api_incr_top!(L);
36}