• Advertisement
Sign in to follow this  

Code stepping in Lua

This topic is 2155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement