Skip to content

Commit db385a4

Browse files
committed
tests: garbage collection (gc)
Tests the gc function with all of it's variants
1 parent d01c349 commit db385a4

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

docs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ All functions and types that deal with continuations have been renamed. For exam
7676

7777
In general, just replace the "k" with the word "cont". This is just to make the API more clear and Zig-like.
7878

79+
### `lua_error` and `luaL_error`
80+
81+
Because `error` is a reserved word in Zig, these functions have been renamed to `raiseError` and `raiseErrorAux` respectively.
82+
7983
### `lua_tostring` and `lua_tolstring`
8084

8185
These functions have been combined into `Lua.toString()`. The function `lua_tostring` is a macro around `lua_tolstring` and does not return the length of the string.

src/ziglua.zig

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ pub const GCAction = enum(u5) {
9090
countb = c.LUA_GCCOUNTB,
9191
step = c.LUA_GCSTEP,
9292
is_running = c.LUA_GCISRUNNING,
93-
inc = c.LUA_GCINC,
94-
gen = c.LUA_GCGEN,
93+
set_incremental = c.LUA_GCINC,
94+
set_generational = c.LUA_GCGEN,
9595
};
9696

9797
/// Type for debugging hook functions
@@ -393,11 +393,37 @@ pub const Lua = struct {
393393
unreachable;
394394
}
395395

396+
fn TypeOfGC(comptime action: GCAction) type {
397+
return switch (action) {
398+
.stop, .collect, .restart, .step => void,
399+
.count, .countb => i32,
400+
.is_running, .set_incremental, .set_generational => bool,
401+
};
402+
}
403+
396404
/// Controls the garbage collector
397-
/// The purpose of the return value is dependent on the given action
398-
/// TODO: perhaps `action` could be comptime known to enforce specific return types
399-
pub fn gc(lua: *Lua, action: GCAction, args: anytype) i32 {
400-
return @call(.{}, c.lua_gc, .{ lua.state, @enumToInt(action) } ++ args);
405+
/// The return value type is dependent on the given action
406+
pub fn gc(lua: *Lua, comptime action: GCAction, args: anytype) TypeOfGC(action) {
407+
const val = @enumToInt(action);
408+
switch (action) {
409+
.stop, .collect, .restart => _ = c.lua_gc(lua.state, val),
410+
.step => _ = c.lua_gc(lua.state, val, @as(i32, args.@"0")),
411+
.count, .countb => return c.lua_gc(lua.state, val),
412+
.is_running => return c.lua_gc(lua.state, val) != 0,
413+
.set_incremental => return c.lua_gc(
414+
lua.state,
415+
val,
416+
@as(i32, args.@"0"),
417+
@as(i32, args.@"1"),
418+
@as(i32, args.@"2"),
419+
) != @enumToInt(GCAction.set_incremental),
420+
.set_generational => return c.lua_gc(
421+
lua.state,
422+
val,
423+
@as(i32, args.@"0"),
424+
@as(i32, args.@"1"),
425+
) != @enumToInt(GCAction.set_generational),
426+
}
401427
}
402428

403429
/// Returns the memory allocation function of a given state
@@ -2267,6 +2293,24 @@ test "concat" {
22672293
try expectEqualStrings("hello 10.0 wow!", lua.toString(-1).?);
22682294
}
22692295

2296+
test "garbage collector" {
2297+
var lua = try Lua.init(testing.allocator);
2298+
defer lua.deinit();
2299+
2300+
// because the garbage collector is an opaque, unmanaged
2301+
// thing, it is hard to test, so just run each function
2302+
lua.gc(.stop, .{});
2303+
lua.gc(.collect, .{});
2304+
lua.gc(.restart, .{});
2305+
lua.gc(.step, .{10});
2306+
_ = lua.gc(.count, .{});
2307+
_ = lua.gc(.countb, .{});
2308+
_ = lua.gc(.is_running, .{});
2309+
try expect(lua.gc(.set_generational, .{0, 10}));
2310+
try expect(lua.gc(.set_incremental, .{0, 0, 0}));
2311+
try expect(!lua.gc(.set_incremental, .{0, 0, 0}));
2312+
}
2313+
22702314
test "refs" {
22712315
// temporary test that includes a reference to all functions so
22722316
// they will be type-checked

0 commit comments

Comments
 (0)