1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use luau_vm::thread::StackGuard;
use luau_vm::{LUA_GC_COLLECT, LUA_GC_IS_RUNNING, LUA_GC_RESTART, LUA_GC_STEP, LUA_GC_STOP};
use super::{Lua, LuaRef};
use crate::Error;
impl Lua {
/// Returns whether automatic garbage collection is enabled.
pub fn gc_is_running(&self) -> Result<bool, Error> {
self.lua_ref().gc_is_running()
}
/// Stops automatic garbage collection.
pub fn gc_stop(&self) -> Result<(), Error> {
self.lua_ref().gc_stop()
}
/// Restarts automatic garbage collection.
pub fn gc_restart(&self) -> Result<(), Error> {
self.lua_ref().gc_restart()
}
/// Performs a full garbage-collection cycle.
///
/// Collecting every currently unreachable object can require two cycles:
/// one to finish the active cycle and another to complete the next one.
pub fn gc_collect(&self) -> Result<(), Error> {
self.lua_ref().gc_collect()
}
/// Performs one incremental collection step.
///
/// Returns `true` when the step completes a collection cycle.
pub fn gc_step(&self) -> Result<bool, Error> {
self.lua_ref().gc_step()
}
}
impl LuaRef<'_> {
/// Returns whether automatic garbage collection is enabled.
pub fn gc_is_running(&self) -> Result<bool, Error> {
let thread = self.as_vm();
unsafe {
thread
.gc(LUA_GC_IS_RUNNING, 0)
.map(|running| running != 0)
.map_err(|error| Error::from_thread_exit(thread, error))
}
}
/// Stops automatic garbage collection.
pub fn gc_stop(&self) -> Result<(), Error> {
let thread = self.as_vm();
unsafe {
thread
.gc(LUA_GC_STOP, 0)
.map(drop)
.map_err(|error| Error::from_thread_exit(thread, error))
}
}
/// Restarts automatic garbage collection.
pub fn gc_restart(&self) -> Result<(), Error> {
let thread = self.as_vm();
unsafe {
thread
.gc(LUA_GC_RESTART, 0)
.map(drop)
.map_err(|error| Error::from_thread_exit(thread, error))
}
}
/// Performs a full garbage-collection cycle.
///
/// Collecting every currently unreachable object can require two cycles:
/// one to finish the active cycle and another to complete the next one.
pub fn gc_collect(&self) -> Result<(), Error> {
let thread = self.as_vm();
unsafe {
let _stack = StackGuard::new(thread);
thread
.gc(LUA_GC_COLLECT, 0)
.map(|_| ())
.map_err(|error| Error::from_thread_exit(thread, error))
}
}
/// Performs one incremental collection step.
///
/// Returns `true` when the step completes a collection cycle.
pub fn gc_step(&self) -> Result<bool, Error> {
let thread = self.as_vm();
unsafe {
let _stack = StackGuard::new(thread);
thread
.gc(LUA_GC_STEP, 0)
.map(|finished| finished != 0)
.map_err(|error| Error::from_thread_exit(thread, error))
}
}
}