lua-users home
lua-l archive

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



> My implementation of breakpoints is as follows: a table in 
> the registry
> contains strings of lua code of the form "return exp" indexed by the

Nice idea.

> The author also uses lua_beginblock() and
> lua_endblock(), which seems 3.x-specific. What does it do ? I 
> suppose it is
> equivalent to the pair int top = lua_gettop(L) / lua_settop(L,top) ? 

You used to have to enclose calls from Lua to C in in begin and end blocks.
>From what I can remember it was to do just as you say. You didnt have to
manage the stack size and ensure you cleanedup after yourself. Not using
begin/end block would often result in your stack overflowing. The current
system is probably is more efficient but you have to police the stack
yourself.

> I also see that when evaluating an expression, ldb takes all locals 
> of the current
> context, and makes globals of them, saving the masked global's value
> somewhere, and restores those values after the expression is 
> evaluated. Is
> there really no other means to access local values ? I will 
> do the same if
> it is the only way, but if there is another solution I am 
> interested :-)

The manual says that you get can local variables using the debug library:

<snip>
 const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
 const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);

    int listvars (lua_State *L, int level) {
         lua_Debug ar;
         int i = 1;
         const char *name;
         if (lua_getstack(L, level, &ar) == 0)
           return 0;  /* failure: no such level in the stack */
         while ((name = lua_getlocal(L, &ar, i++)) != NULL) {
           printf("%s\n", name);
           lua_pop(L, 1);  /* remove variable value */
         }
         return 1;
       }
</snip>

or through the debug interface:

<snip>
  getlocal (level, local)
  This function returns the name and the value of the local variable with
index local of the function at level level of the stack. (The first
parameter or local variable has index 1, and so on, until the last active
local variable.) The function returns nil if there is no local variable with
the given index, and raises an error when called with a level out of range.
(You can call getinfo to check whether the level is valid.) 

  setlocal (level, local, value)
This function assigns the value value to the local variable with index local
of the function at level level of the stack. The function returns nil if
there is no local variable with the given index, and raises an error when
called with a level out of range. 
</snip>

I know it wont be too quick, but you could build a table of locals each line
overwrite a global table copy as you say.

An alternative may be to use your "return exp" as "return
getlocal(level,local)==val" and work out what level and local index are. I
havent checked this but when the callhook is set you could build a locals
table eg { [1]="name1",[2]="name2" ... } and get the local index from this,
level you should also be able to read at this point. You might have a
debugger info stack which contains tables with all this information in and
mirrors the Lua call stack. I think you can cache the locals info per
function so you'll only receive the retrieve locals info hit on the first
call. Mmmm but then with block scoping locals should get created and
destroyed in a function so not sure you can do this. ie. I'd have thought
you'd get 2 locals with different indices and the same name in different
blocks in a function. You'll have to experiment.

Regards,
Nick