luaur_vm/functions/
lua_getfenv.rs1use core::ffi::c_int;
2
3use crate::enums::lua_type::lua_Type;
4use crate::functions::index_2_addr::index2addr;
5use crate::functions::lua_concat::lua_c_threadbarrier_lapi;
6use crate::macros::api_check::api_check;
7use crate::macros::api_incr_top::api_incr_top;
8use crate::macros::clvalue::clvalue;
9use crate::macros::lua_o_nilobject::luaO_nilobject;
10use crate::macros::sethvalue::sethvalue;
11use crate::macros::setnilvalue::setnilvalue;
12use crate::macros::thvalue::thvalue;
13use crate::macros::ttype::ttype;
14use crate::records::lua_state::lua_State;
15use crate::type_aliases::stk_id::StkId;
16
17#[allow(non_snake_case)]
18pub unsafe fn lua_getfenv(L: *mut lua_State, idx: c_int) {
19 lua_c_threadbarrier_lapi(L);
20
21 let o: StkId = index2addr(L, idx);
22 api_check!(L, o != luaO_nilobject as StkId);
23
24 match ttype!(o) {
25 x if x == lua_Type::LUA_TFUNCTION as c_int => {
26 sethvalue!(L, (*L).top, (*clvalue!(o)).env);
27 }
28 x if x == lua_Type::LUA_TTHREAD as c_int => {
29 sethvalue!(L, (*L).top, (*thvalue!(o)).gt);
30 }
31 _ => {
32 setnilvalue!((*L).top);
33 }
34 }
35
36 api_incr_top!(L);
37}