Skip to main content

luaur_require/functions/
is_cached.rs

1use alloc::string::String;
2use core::ffi::c_char;
3
4use luaur_vm::functions::lua_getfield::lua_getfield;
5use luaur_vm::functions::lua_l_findtable::luaL_findtable;
6use luaur_vm::functions::lua_type::lua_type;
7use luaur_vm::macros::lua_isnil::lua_isnil;
8use luaur_vm::macros::lua_pop::lua_pop;
9use luaur_vm::macros::lua_registryindex::LUA_REGISTRYINDEX;
10use luaur_vm::records::lua_state::lua_State;
11
12const required_cache_table_key: *const c_char = c"requiredCacheTableKey".as_ptr();
13
14pub fn is_cached(l: *mut lua_State, key: &String) -> bool {
15    unsafe {
16        luaL_findtable(l, LUA_REGISTRYINDEX, required_cache_table_key, 1);
17
18        let key_c = alloc::ffi::CString::new(key.as_str()).unwrap();
19        lua_getfield(l, -1, key_c.as_ptr());
20
21        let cached = lua_type(l, -1) != (luaur_vm::enums::lua_type::lua_Type::LUA_TNIL as i32);
22
23        lua_pop(l, 2);
24
25        cached
26    }
27}