Skip to main content

luaur_vm/functions/
lua_h_setp.rs

1use crate::functions::lua_h_getp::lua_h_getp;
2use crate::functions::newkey::newkey;
3use crate::macros::cast_to::cast_to;
4use crate::macros::lua_o_nilobject::luaO_nilobject;
5use crate::type_aliases::lua_state::lua_State;
6use crate::type_aliases::lua_table::LuaTable;
7use crate::type_aliases::t_value::TValue;
8
9#[allow(non_snake_case)]
10pub unsafe fn lua_h_setp(
11    L: *mut lua_State,
12    t: *mut LuaTable,
13    key: *mut core::ffi::c_void,
14    tag: i32,
15) -> *mut TValue {
16    // The dependency card for lua_h_getp shows a stub signature pub fn lua_h_getp();
17    // We must transmute it to the real signature (t, key, tag) -> *const TValue to call it.
18    type LuaHGetPFn = unsafe fn(*mut LuaTable, *mut core::ffi::c_void, i32) -> *const TValue;
19    let lua_h_getp_ptr =
20        core::mem::transmute::<_, LuaHGetPFn>(lua_h_getp as *const core::ffi::c_void);
21
22    let p = lua_h_getp_ptr(t, key, tag);
23
24    if p != luaO_nilobject {
25        cast_to!(*mut TValue, p)
26    } else {
27        let mut k: TValue = core::mem::zeroed();
28
29        // setpvalue(obj, x, tag) logic:
30        // i_o->value.p = (x); i_o->extra[0] = (tag); i_o->tt = LUA_TLIGHTUSERDATA;
31        k.value.p = key;
32        k.extra[0] = tag;
33        k.tt = 2; // LUA_TLIGHTUSERDATA
34
35        // The dependency card for newkey shows a stub signature pub fn newkey();
36        // We must transmute it to the real signature (L, t, key) -> *mut TValue to call it.
37        type NewKeyFn = unsafe fn(*mut lua_State, *mut LuaTable, *const TValue) -> *mut TValue;
38        let newkey_ptr = core::mem::transmute::<_, NewKeyFn>(newkey as *const core::ffi::c_void);
39
40        newkey_ptr(L, t, &k)
41    }
42}