Skip to main content

luaur_vm/functions/
lua_t_gettmbyobj.rs

1use crate::enums::lua_type::lua_Type;
2use crate::functions::lua_h_getstr::lua_h_getstr;
3use crate::macros::classvalue::classvalue;
4use crate::macros::hvalue::hvalue;
5use crate::macros::lua_o_nilobject::luaO_nilobject;
6use crate::macros::objectvalue::objectvalue;
7use crate::macros::ttype::ttype;
8use crate::macros::uvalue::uvalue;
9use crate::type_aliases::t_value::TValue;
10
11#[allow(non_snake_case)]
12pub unsafe fn lua_t_gettmbyobj(
13    L: *mut crate::type_aliases::lua_state::lua_State,
14    o: *const TValue,
15    event: crate::type_aliases::tms::TMS,
16) -> *const TValue {
17    /*
18      NB: Tag-methods were replaced by meta-methods in Lua 5.0, but the
19      old names are still around (this function, for example).
20    */
21    let mt: *mut crate::records::lua_table::LuaTable;
22
23    match ttype!(o) as i32 {
24        t if t == lua_Type::LUA_TTABLE as i32 => {
25            mt = (*hvalue!(o)).metatable;
26        }
27        t if t == lua_Type::LUA_TUSERDATA as i32 => {
28            mt = (*uvalue!(o)).metatable;
29        }
30        t if t == lua_Type::LUA_TCLASS as i32 => {
31            // We store a metatable for class objects on the
32            // class object itself, use that.
33            mt = (*classvalue!(o)).metatable;
34        }
35        t if t == lua_Type::LUA_TOBJECT as i32 => {
36            mt = (*(*objectvalue!(o)).lclass).instancemetatable;
37        }
38        _ => {
39            mt = (*(*L).global).mt[ttype!(o) as usize];
40        }
41    }
42
43    if !mt.is_null() {
44        lua_h_getstr(
45            mt,
46            (*(*L).global).tmname[event as usize] as *mut crate::records::t_string::TString,
47        )
48    } else {
49        luaO_nilobject
50    }
51}