lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, 2011-04-08 at 14:49 +0200, Benoit Germain wrote:
> There is also the problem of expression evaluation within a context
> while the VM is suspended in a line hook / OP_BREAK handler. When a
> breakpoint is hit, if I wanted to evaluate an expression in the
> debugger, be it for break condition evaluation or simple expression
> inspection I would do it that way:
> * create a thread with lua_newthread
> * change its environment with an empty one that uses a metatable with
> __index to access the global VM's environment
> * get the list of locals and upvalues in the current call frame, add
> them in the thread's environment (-> as globals)
> * compile the expression in the thread, run it, fetch the results,
> display them

In the meantime, you can take a look at the snippet I wrote [1], which
creates this kind of environment. The created environment can access
local and global variables, you can even change the variables via
__newindex. So instead of setting a breakpoint, I usually just call
DEBUG(), because mostly I only inspect/change variables.

I don't think the code is entirely correct (it fails on multiple
variables of the same name in one scope), but I went for simplicity and
it works correctly for variables of the same name in different scopes.
Also, some tinkering had to be done with the level for locals, because
when accessed, the `level` parameter for a local is different than when
created.

[1] http://snippets.luacode.org/snippets/title_88