@@ -144,19 +144,6 @@ pub const FnReg = struct {
144
144
func : ? CFn ,
145
145
};
146
146
147
- /// Actions supported by `Lua.gc()`
148
- pub const GCAction = enum (u5 ) {
149
- stop = c .LUA_GCSTOP ,
150
- restart = c .LUA_GCRESTART ,
151
- collect = c .LUA_GCCOLLECT ,
152
- count = c .LUA_GCCOUNT ,
153
- countb = c .LUA_GCCOUNTB ,
154
- step = c .LUA_GCSTEP ,
155
- is_running = c .LUA_GCISRUNNING ,
156
- set_incremental = c .LUA_GCINC ,
157
- set_generational = c .LUA_GCGEN ,
158
- };
159
-
160
147
/// Type for debugging hook functions
161
148
pub const CHookFn = fn (state : ? * LuaState , ar : ? * Debug ) callconv (.C ) void ;
162
149
@@ -474,38 +461,51 @@ pub const Lua = struct {
474
461
unreachable ;
475
462
}
476
463
477
- fn TypeOfGC (comptime action : GCAction ) type {
478
- return switch (action ) {
479
- .stop , .collect , .restart , .step = > void ,
480
- .count , .countb = > i32 ,
481
- .is_running , .set_incremental , .set_generational = > bool ,
482
- };
464
+ /// Perform a full garbage-collection cycle
465
+ pub fn gcCollect (lua : * Lua ) void {
466
+ _ = c .lua_gc (lua .state , c .LUA_GCCOLLECT );
483
467
}
484
468
485
- /// Controls the garbage collector
486
- /// The return value type is dependent on the given action
487
- /// TODO: split into separate functions
488
- pub fn gc (lua : * Lua , comptime action : GCAction , args : anytype ) TypeOfGC (action ) {
489
- const val = @enumToInt (action );
490
- switch (action ) {
491
- .stop , .collect , .restart = > _ = c .lua_gc (lua .state , val ),
492
- .step = > _ = c .lua_gc (lua .state , val , @as (i32 , args .@"0" )),
493
- .count , .countb = > return c .lua_gc (lua .state , val ),
494
- .is_running = > return c .lua_gc (lua .state , val ) != 0 ,
495
- .set_incremental = > return c .lua_gc (
496
- lua .state ,
497
- val ,
498
- @as (i32 , args .@"0" ),
499
- @as (i32 , args .@"1" ),
500
- @as (i32 , args .@"2" ),
501
- ) != @enumToInt (GCAction .set_incremental ),
502
- .set_generational = > return c .lua_gc (
503
- lua .state ,
504
- val ,
505
- @as (i32 , args .@"0" ),
506
- @as (i32 , args .@"1" ),
507
- ) != @enumToInt (GCAction .set_generational ),
508
- }
469
+ /// Stops the garbage collector
470
+ pub fn gcStop (lua : * Lua ) void {
471
+ _ = c .lua_gc (lua .state , c .LUA_GCSTOP );
472
+ }
473
+
474
+ /// Restarts the garbage collector
475
+ pub fn gcRestart (lua : * Lua ) void {
476
+ _ = c .lua_gc (lua .state , c .LUA_GCRESTART );
477
+ }
478
+
479
+ /// Performs an incremental step of garbage collection corresponding to the allocation of `step_size` Kbytes
480
+ pub fn gcStep (lua : * Lua , step_size : i32 ) void {
481
+ _ = c .lua_gc (lua .state , c .LUA_GCSTEP , step_size );
482
+ }
483
+
484
+ /// Returns the current amount of memory (in Kbytes) in use by Lua
485
+ pub fn gcCount (lua : * Lua ) i32 {
486
+ return c .lua_gc (lua .state , c .LUA_GCCOUNT );
487
+ }
488
+
489
+ /// Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024
490
+ pub fn gcCountB (lua : * Lua ) i32 {
491
+ return c .lua_gc (lua .state , c .LUA_GCCOUNTB );
492
+ }
493
+
494
+ /// Returns a boolean that tells whether the garbage collector is running
495
+ pub fn gcIsRunning (lua : * Lua ) bool {
496
+ return c .lua_gc (lua .state , c .LUA_GCISRUNNING ) != 0 ;
497
+ }
498
+
499
+ /// Changes the collector to incremental mode
500
+ /// Returns true if the previous mode was generational
501
+ pub fn gcSetIncremental (lua : * Lua , pause : i32 , step_mul : i32 , step_size : i32 ) bool {
502
+ return c .lua_gc (lua .state , c .LUA_GCINC , pause , step_mul , step_size ) == c .LUA_GCGEN ;
503
+ }
504
+
505
+ /// Changes the collector to generational mode
506
+ /// Returns true if the previous mode was incremental
507
+ pub fn gcSetGenerational (lua : * Lua , minor_mul : i32 , major_mul : i32 ) bool {
508
+ return c .lua_gc (lua .state , c .LUA_GCGEN , minor_mul , major_mul ) == c .LUA_GCINC ;
509
509
}
510
510
511
511
/// Returns the memory allocation function of a given state
0 commit comments