lua-users home
lua-l archive

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


Eike Decker wrote:
Hi

Is it possible to get the line number of an inspected local value? Currently, only the name and value is returned... however, retrieving the line number would be quite useful as well...

Eike
I don't think you can during runtime but I do this through an offline parser prior to running any of the Lua scripts.

In lparser.c "static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base)" you can find upvalue & local line numbers and I also grab global line numbers from lparser.c "static void singlevar (LexState *ls, expdesc *var)". Basically, the "Get*" functions you'll see referenced below convert the TString* to char* through the macro getstr() and from the LexState* I get the "lastline" member for the line number then dispatch those values back to the app.

*snip*

static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
 if (fs == NULL) {  /* no more levels? */
   init_exp(var, VGLOBAL, NO_REG);  /* default is global variable */
   return VGLOBAL;
 }
 else {
   int v = searchvar(fs, n);  /* look up at current level */
   if (v >= 0) {
     init_exp(var, VLOCAL, v);
     if (!base)
       markupval(fs, v);  /* local will be used as an upval */
     else // <-- added
       GetLocal(n, fs->ls); // <-- added
     return VLOCAL;
   }
   else {  /* not found at current level; try upper one */
     if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
       return VGLOBAL;
var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */
     var->k = VUPVAL;  /* upvalue in this level */
     GetUpvalue(n, fs->ls); // <-- added
     return VUPVAL;
   }
 }
}

*snip*

static void singlevar (LexState *ls, expdesc *var) {
 TString *varname = str_checkname(ls);
 FuncState *fs = ls->fs;
 if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
 { // <-- added
var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */
   GetGlobal(varname, ls); // <-- added
 } // <-- added
}