Skip to content

Commit d0d3a95

Browse files
committed
refactor: expand gc function into individual functions
While it was cool that Zig metaprogramming could expose all garbage collection functions with an enum parameter, it's much more straightforward and clear to just make each a separate function.
1 parent c64538d commit d0d3a95

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

src/tests.zig

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,17 @@ test "garbage collector" {
602602

603603
// because the garbage collector is an opaque, unmanaged
604604
// thing, it is hard to test, so just run each function
605-
lua.gc(.stop, .{});
606-
lua.gc(.collect, .{});
607-
lua.gc(.restart, .{});
608-
lua.gc(.step, .{10});
609-
_ = lua.gc(.count, .{});
610-
_ = lua.gc(.countb, .{});
611-
_ = lua.gc(.is_running, .{});
612-
try expect(lua.gc(.set_generational, .{ 0, 10 }));
613-
try expect(lua.gc(.set_incremental, .{ 0, 0, 0 }));
614-
try expect(!lua.gc(.set_incremental, .{ 0, 0, 0 }));
605+
lua.gcStop();
606+
lua.gcCollect();
607+
lua.gcRestart();
608+
lua.gcStep(10);
609+
_ = lua.gcCount();
610+
_ = lua.gcCountB();
611+
_ = lua.gcIsRunning();
612+
613+
try expect(lua.gcSetGenerational(0, 10));
614+
try expect(lua.gcSetIncremental(0, 0, 0));
615+
try expect(!lua.gcSetIncremental(0, 0, 0));
615616
}
616617

617618
test "extra space" {

src/ziglua.zig

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,6 @@ pub const FnReg = struct {
144144
func: ?CFn,
145145
};
146146

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-
160147
/// Type for debugging hook functions
161148
pub const CHookFn = fn (state: ?*LuaState, ar: ?*Debug) callconv(.C) void;
162149

@@ -474,38 +461,51 @@ pub const Lua = struct {
474461
unreachable;
475462
}
476463

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);
483467
}
484468

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;
509509
}
510510

511511
/// Returns the memory allocation function of a given state

0 commit comments

Comments
 (0)