Jump to content

  • Log In with Google      Sign In   
  • Create Account

Code stepping in Lua


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Josh Klint   GDNet+   -  Reputation: 1354

Like
0Likes
Like

Posted 27 February 2012 - 07:35 PM

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:
http://pgl.yoyo.org/luai/i/lua_sethook

However, the LUA_MASKLINE function description has one giant caveat in its description:
This event only happens while Lua is executing a Lua function.

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:
local a = 1 --hook executed!
local b = a + 1 --hook executed!
b = b + 2 --hook executed!

Is the functionality I want supported in Lua? Thanks.

Build mobile games with native code

http://www.leadwerks.com


Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 20990

Like
0Likes
Like

Posted 27 February 2012 - 10:44 PM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 Hodgman   Moderators   -  Reputation: 31799

Like
0Likes
Like

Posted 28 February 2012 - 01:52 AM

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.)

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS