Skip to content

Commit 56a8235

Browse files
committed
tests: add more debug library tests
1 parent 737beb7 commit 56a8235

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

src/tests.zig

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const testing = std.testing;
55
const ziglua = @import("ziglua.zig");
66

77
const Buffer = ziglua.Buffer;
8+
const DebugInfo = ziglua.DebugInfo;
89
const Error = ziglua.Error;
10+
const Event = ziglua.Event;
911
const Integer = ziglua.Integer;
1012
const Lua = ziglua.Lua;
1113
const LuaType = ziglua.LuaType;
@@ -983,45 +985,82 @@ test "debug interface" {
983985

984986
try lua.doString(
985987
\\f = function(x)
986-
\\ return x + 1
988+
\\ local y = x * 2
989+
\\ y = y + 2
990+
\\ return x + y
987991
\\end
988992
);
989993
_ = lua.getGlobal("f");
990994

991-
var info = lua.getInfo(.{
995+
var info: DebugInfo = undefined;
996+
lua.getInfo(.{
992997
.@">" = true,
993998
.l = true,
994999
.S = true,
9951000
.n = true,
9961001
.u = true,
9971002
.t = true,
998-
});
1003+
}, &info);
9991004

1000-
try expectEqual(ziglua.DebugInfo.FnType.lua, info.what);
1001-
try expectEqual(ziglua.DebugInfo.NameType.other, info.name_what);
1005+
// get information about the function
1006+
try expectEqual(DebugInfo.FnType.lua, info.what);
1007+
try expectEqual(DebugInfo.NameType.other, info.name_what);
10021008
const len = std.mem.len(@ptrCast([*:0]u8, &info.short_src));
10031009
try expectEqualStrings("[string \"f = function(x)...\"]", info.short_src[0..len]);
10041010
try expectEqual(@as(?i32, 1), info.first_line_defined);
1005-
try expectEqual(@as(?i32, 3), info.last_line_defined);
1011+
try expectEqual(@as(?i32, 5), info.last_line_defined);
10061012
try expectEqual(@as(u8, 1), info.num_params);
10071013
try expectEqual(@as(u8, 0), info.num_upvalues);
10081014
try expect(!info.is_tail_call);
10091015
try expectEqual(@as(?i32, null), info.current_line);
1016+
1017+
// create a hook
1018+
const hook = struct {
1019+
fn inner(l: *Lua, event: Event, i: *DebugInfo) void {
1020+
switch (event) {
1021+
.call => {
1022+
l.getInfo(.{ .l = true, .r = true }, i);
1023+
if (i.current_line.? != 2) panic("Expected line to be 2", .{});
1024+
_ = l.getLocal(i, i.first_transfer) catch unreachable;
1025+
if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected x to equal 3", .{});
1026+
},
1027+
.line => if (i.current_line.? == 4) {
1028+
// modify the value of y to be 0 right before returning
1029+
l.pushNumber(0);
1030+
_ = l.setLocal(i, 2) catch unreachable;
1031+
},
1032+
.ret => {
1033+
l.getInfo(.{ .l = true, .r = true }, i);
1034+
if (i.current_line.? != 4) panic("Expected line to be 4", .{});
1035+
_ = l.getLocal(i, i.first_transfer) catch unreachable;
1036+
if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected result to equal 3", .{});
1037+
},
1038+
else => unreachable,
1039+
}
1040+
}
1041+
}.inner;
1042+
1043+
// run the hook when a function is called
1044+
try expectEqual(@as(?ziglua.CHookFn, null), lua.getHook());
1045+
try expectEqual(ziglua.HookMask{}, lua.getHookMask());
1046+
try expectEqual(@as(i32, 0), lua.getHookCount());
1047+
1048+
lua.setHook(ziglua.wrap(hook), .{ .call = true, .line = true, .ret = true }, 0);
1049+
try expectEqual(@as(?ziglua.CHookFn, ziglua.wrap(hook)), lua.getHook());
1050+
try expectEqual(ziglua.HookMask{ .call = true, .line = true, .ret = true }, lua.getHookMask());
1051+
1052+
_ = lua.getGlobal("f");
1053+
lua.pushNumber(3);
1054+
try lua.protectedCall(1, 1, 0);
10101055
}
10111056

10121057
test "refs" {
10131058
// temporary test that includes a reference to all functions so
10141059
// they will be type-checked
10151060

10161061
// debug
1017-
_ = Lua.getHook;
1018-
_ = Lua.getHookCount;
1019-
_ = Lua.getHookMask;
1020-
_ = Lua.getLocal;
10211062
_ = Lua.getStack;
10221063
_ = Lua.getUpvalue;
1023-
_ = Lua.setHook;
1024-
_ = Lua.setLocal;
10251064
_ = Lua.setUpvalue;
10261065
_ = Lua.upvalueId;
10271066
_ = Lua.upvalueJoin;

src/ziglua.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub const DebugInfo = struct {
106106
index += 1;
107107
}
108108
}
109+
while (index < str.len) : (index += 1) str[index] = 0;
109110

110111
return str;
111112
}
@@ -1127,15 +1128,17 @@ pub const Lua = struct {
11271128
/// Gets information about a specific function or function invocation
11281129
/// Returns an error if an invalid option was given, but the valid options
11291130
/// are still handled
1130-
pub fn getInfo(lua: *Lua, options: DebugInfo.Options) DebugInfo {
1131+
pub fn getInfo(lua: *Lua, options: DebugInfo.Options, info: *DebugInfo) void {
11311132
const str = options.toString();
1133+
11321134
var ar: Debug = undefined;
1135+
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
11331136

11341137
// should never fail because we are controlling options with the struct param
1135-
std.debug.assert(c.lua_getinfo(lua.state, &str, &ar) != 0);
1138+
_ = c.lua_getinfo(lua.state, &str, &ar);
1139+
// std.debug.assert( != 0);
11361140

11371141
// copy data into a struct
1138-
var info: DebugInfo = undefined;
11391142
if (options.l) info.current_line = if (ar.currentline == -1) null else ar.currentline;
11401143
if (options.n) {
11411144
info.name = if (ar.name != null) std.mem.span(ar.name) else null;
@@ -1173,7 +1176,6 @@ pub const Lua = struct {
11731176
info.num_params = ar.nparams;
11741177
info.is_vararg = ar.isvararg != 0;
11751178
}
1176-
return info;
11771179
}
11781180

11791181
/// Gets information about a local variable
@@ -1833,9 +1835,9 @@ fn wrapZigHookFn(comptime f: ZigHookFn) CHookFn {
18331835
var lua: Lua = .{ .state = state.? };
18341836
var info: DebugInfo = .{
18351837
.current_line = if (ar.?.currentline == -1) null else ar.?.currentline,
1836-
.private = ar.i_ci,
1838+
.private = @ptrCast(*anyopaque, ar.?.i_ci),
18371839
};
1838-
@call(.{ .modifier = .always_inline }, f, .{ &lua, @intToEnum(Event, ar.?.event), info });
1840+
@call(.{ .modifier = .always_inline }, f, .{ &lua, @intToEnum(Event, ar.?.event), &info });
18391841
}
18401842
}.inner;
18411843
}

0 commit comments

Comments
 (0)