@@ -5,7 +5,9 @@ const testing = std.testing;
5
5
const ziglua = @import ("ziglua.zig" );
6
6
7
7
const Buffer = ziglua .Buffer ;
8
+ const DebugInfo = ziglua .DebugInfo ;
8
9
const Error = ziglua .Error ;
10
+ const Event = ziglua .Event ;
9
11
const Integer = ziglua .Integer ;
10
12
const Lua = ziglua .Lua ;
11
13
const LuaType = ziglua .LuaType ;
@@ -983,45 +985,82 @@ test "debug interface" {
983
985
984
986
try lua .doString (
985
987
\\f = function(x)
986
- \\ return x + 1
988
+ \\ local y = x * 2
989
+ \\ y = y + 2
990
+ \\ return x + y
987
991
\\end
988
992
);
989
993
_ = lua .getGlobal ("f" );
990
994
991
- var info = lua .getInfo (.{
995
+ var info : DebugInfo = undefined ;
996
+ lua .getInfo (.{
992
997
.@">" = true ,
993
998
.l = true ,
994
999
.S = true ,
995
1000
.n = true ,
996
1001
.u = true ,
997
1002
.t = true ,
998
- });
1003
+ }, & info );
999
1004
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 );
1002
1008
const len = std .mem .len (@ptrCast ([* :0 ]u8 , & info .short_src ));
1003
1009
try expectEqualStrings ("[string \" f = function(x)...\" ]" , info .short_src [0.. len ]);
1004
1010
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 );
1006
1012
try expectEqual (@as (u8 , 1 ), info .num_params );
1007
1013
try expectEqual (@as (u8 , 0 ), info .num_upvalues );
1008
1014
try expect (! info .is_tail_call );
1009
1015
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 );
1010
1055
}
1011
1056
1012
1057
test "refs" {
1013
1058
// temporary test that includes a reference to all functions so
1014
1059
// they will be type-checked
1015
1060
1016
1061
// debug
1017
- _ = Lua .getHook ;
1018
- _ = Lua .getHookCount ;
1019
- _ = Lua .getHookMask ;
1020
- _ = Lua .getLocal ;
1021
1062
_ = Lua .getStack ;
1022
1063
_ = Lua .getUpvalue ;
1023
- _ = Lua .setHook ;
1024
- _ = Lua .setLocal ;
1025
1064
_ = Lua .setUpvalue ;
1026
1065
_ = Lua .upvalueId ;
1027
1066
_ = Lua .upvalueJoin ;
0 commit comments