use core::ffi::c_char;
use luaur_vm::functions::lua_l_getmetafield::lua_l_getmetafield;
use luaur_vm::functions::lua_pushnil::lua_pushnil;
use luaur_vm::functions::lua_pushvalue::lua_pushvalue;
use luaur_vm::functions::lua_rawget::lua_rawget;
use luaur_vm::functions::lua_remove::lua_remove;
use luaur_vm::functions::lua_replace::lua_replace;
use luaur_vm::macros::lua_isnil::lua_isnil;
use luaur_vm::macros::lua_istable::lua_istable;
use luaur_vm::macros::lua_pop::lua_pop;
use luaur_vm::type_aliases::lua_state::lua_State;
const MAX_TRAVERSAL_LIMIT: i32 = 50;
pub unsafe fn safe_get_table(l: *mut lua_State, table_index: i32) {
lua_pushvalue(l, table_index);
let mut loop_count = 0;
loop {
lua_pushvalue(l, -2); lua_rawget(l, -2); if !lua_isnil!(l, -1) || loop_count >= MAX_TRAVERSAL_LIMIT {
break;
} else {
lua_pop(l, 1); if lua_l_getmetafield(l, -1, c"__index".as_ptr() as *const c_char) == 0 {
lua_pushnil(l);
break;
} else if lua_istable!(l, -1) {
lua_replace(l, -2);
} else {
lua_pop(l, 1); lua_pushnil(l);
break;
}
}
loop_count += 1;
}
lua_remove(l, -2); lua_remove(l, -2); }