Debugging API
We don't provide lua_Debug or lua_Hook API bindings, because each Lua version introduces some changes to the API (especially the lua_Debug structure) and we can't cleanly support all of them.
Instead, all Lua versions already provide a debugging API with their debug library, and we recommend the users using them instead.
TIP
Because of our wrappers (in C or with __call), one might need to provide a different level number to debug.getinfo to access the correct frame. But otherwise, the usage generally follows the same pattern.
Here is an example of using Java functions (LuaFunction) as hooks:
java
L.openLibraries();
int[] count = new int[1];
boolean[] insideTestFunc = new boolean[1];
L.register("hook", (K, args) -> {
count[0]++;
// Use debug.getinfo to get frame info
insideTestFunc[0] |= K.eval("return debug.getinfo(3)")[0]
.get("name").toString().equals("test_func");
return null;
});
L.run("function test_func() return 42 end");
// Set hook
L.run("debug.sethook(hook, 'c', 1)");
L.run("test_func()");
L.run("debug.sethook()");
assertNotEquals(0, count[0]);
assertTrue(insideTestFunc[0]);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17