Advertisement

Code stepping in Lua

Started by February 28, 2012 01:35 AM
1 comment, last by Hodgman 12 years, 9 months ago

[color=#000000][font=Helvetica]I am looking for a way to call my own C++ function automatically at the end of each line in a Lua script. The Lua SetHook function seems to do this:[/font]
[color=#000000][font=Helvetica]http://pgl.yoyo.org/luai/i/lua_sethook[/font]

[color=#000000][font=Helvetica]However, the LUA_MASKLINE function description has one giant caveat in its description:[/font]
[color=#000000][font=Helvetica]This event only happens while Lua is executing a Lua function.[/font]

[color=#000000][font=Helvetica]I am not sure what this means, but the hook doesn't seem to get called in the manner I expect. If I had a script like below, I would expect the hook to be called after executing each line:[/font]
[color=#000000][font=Helvetica]local a = 1 --hook executed![/font]
[color=#000000][font=Helvetica]local b = a + 1 --hook executed![/font]
[color=#000000][font=Helvetica]b = b + 2 --hook executed![/font]

[color=#000000][font=Helvetica]Is the functionality I want supported in Lua? Thanks.[/font]

10x Faster Performance for VR: www.ultraengine.com

I'm no Lua expert, and this might have bad performance issues, but why not load your lua script like you'd load a normal text file, break it up into lines, and execute each line with luaL_dostring. Inbetween each luaL_dostring() call, you can run whatever C++ code you like. You could wrap the entire thing in a function taking a callback, if that's what you want. (MyLuaDoFile(scriptFile, myCallback()).

For optimization, if you routinely run the same lua script, you might want to pre-load and pre-compile the script portions using luaL_loadstring (I'm guessing - I've never done it before), to reduce the overhead of parsing the data before running it.
Advertisement
At work we use Tilde, which does use these hooks to implement its line-by-line debugger, so yeah it should be possible for you to receive a callback for each line of code the VM runs.
However, the LUA_MASKLINE function description has one giant caveat in its description:
This event only happens while Lua is executing a Lua function.
The line hook: is called when the interpreter is about to start the execution of a new line of code, or when it jumps back in the code (even to the same line). (This event only happens while Lua is executing a Lua function.)[/quote]To me, that caveat sounds like it's implying that when Lua calls into a C function, no callbacks will be triggered, but when it returns from the C function, it will.

This topic is closed to new replies.

Advertisement