Skip to main content

luaur_vm/functions/
lua_ref.rs

1use core::ffi::c_int;
2
3use crate::functions::index_2_addr::index2addr;
4use crate::functions::lua_h_getn::lua_h_getn;
5use crate::functions::lua_h_setnum::luaH_setnum;
6use crate::macros::api_check::api_check;
7use crate::macros::hvalue::hvalue;
8use crate::macros::lua_c_barriert::luaC_barriert;
9use crate::macros::lua_refnil::LUA_REFNIL;
10use crate::macros::lua_registryindex::LUA_REGISTRYINDEX;
11use crate::macros::nvalue::nvalue;
12use crate::macros::registry::registry;
13use crate::macros::setobj_2_t::setobj2t;
14use crate::macros::ttisnil::ttisnil;
15use crate::records::lua_state::lua_State;
16use crate::type_aliases::lua_table::LuaTable;
17use crate::type_aliases::stk_id::StkId;
18use crate::type_aliases::t_value::TValue;
19
20#[allow(non_snake_case)]
21pub unsafe fn lua_ref(L: *mut lua_State, idx: c_int) -> c_int {
22    api_check!(L, idx != LUA_REGISTRYINDEX);
23
24    let mut ref_ = LUA_REFNIL;
25    let g = (*L).global;
26    let p: StkId = index2addr(L, idx);
27
28    if !ttisnil!(p) {
29        let reg: *mut LuaTable = hvalue!(registry!(L)) as *mut LuaTable;
30
31        if (*g).registryfree != 0 {
32            ref_ = (*g).registryfree;
33        } else {
34            // The dependency card for lua_h_getn shows an empty signature: pub fn lua_h_getn();
35            // In Luau VM, luaH_getn(t) returns int. We transmute to the real signature.
36            let lua_h_getn_real: unsafe extern "C" fn(*mut LuaTable) -> c_int =
37                core::mem::transmute(lua_h_getn as *const core::ffi::c_void);
38            ref_ = lua_h_getn_real(reg);
39            ref_ += 1;
40        }
41
42        let slot: *mut TValue = luaH_setnum(L, reg, ref_);
43        if (*g).registryfree != 0 {
44            (*g).registryfree = nvalue!(slot) as c_int;
45        }
46
47        setobj2t!(L, slot, p);
48
49        luaC_barriert!(L, reg, p);
50    }
51
52    ref_
53}