Skip to main content

luaur_repl_cli/functions/
lua_collectgarbage.rs

1use core::ffi::CStr;
2
3use luaur_vm::enums::lua_gc_op::lua_GCOp;
4use luaur_vm::functions::lua_gc::lua_gc;
5use luaur_vm::functions::lua_l_optlstring::lua_l_optlstring;
6use luaur_vm::functions::lua_pushnumber::lua_pushnumber;
7use luaur_vm::macros::lua_l_error::luaL_error;
8use luaur_vm::type_aliases::lua_state::lua_State;
9
10pub unsafe fn lua_collectgarbage(l: *mut lua_State) -> i32 {
11    let option = lua_l_optlstring(l, 1, c"collect".as_ptr(), core::ptr::null_mut());
12    let option = CStr::from_ptr(option);
13
14    if option.to_bytes() == b"collect" {
15        lua_gc(l, lua_GCOp::LUA_GCCOLLECT as core::ffi::c_int, 0);
16        return 0;
17    }
18
19    if option.to_bytes() == b"count" {
20        let c = lua_gc(l, lua_GCOp::LUA_GCCOUNT as core::ffi::c_int, 0);
21        lua_pushnumber(l, c as f64);
22        return 1;
23    }
24
25    luaL_error!(l, "collectgarbage must be called with 'count' or 'collect'");
26    0
27}