lua-users home
lua-l archive

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

> In Lua 5.1.2, lua_getinfo (or debug.getinfo) does not fill the
> lua_Debug.namewhat and fields for metamethod calls.  One
> implication is that luaL_check* calls, which rely on this information, do not
> give meaningful error messages when used in metamethods.
> [...]
> The solution I thought was to patch ldebug.c getfuncname so that metamethod
> calls result in namewhat having a new value "meta" and name having the key
> passed to __newindex, or probably more generally the name of the metamethod. 
> This is shown below for __newindex:
> [...]

We had already changed that to 5.2. Here is the new implementation:

static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  TMS tm = 0;
  Instruction i;
  if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
    return NULL;  /* calling function is not Lua (or is unknown) */
  ci--;  /* calling function */
  i = ci_func(ci)->l.p->code[currentpc(L, ci)];
  switch (GET_OPCODE(i)) {
    case OP_CALL:
    case OP_TAILCALL:
    case OP_TFORLOOP:
      return getobjname(L, ci, GETARG_A(i), name);
    case OP_GETGLOBAL:
    case OP_SELF:
    case OP_GETTABLE: tm = TM_INDEX; break;
    case OP_SETGLOBAL:
    case OP_SETTABLE: tm = TM_NEWINDEX; break;
    case OP_EQ: tm = TM_EQ; break;
    case OP_ADD: tm = TM_ADD; break;
    case OP_SUB: tm = TM_SUB; break;
    case OP_MUL: tm = TM_MUL; break;
    case OP_DIV: tm = TM_DIV; break;
    case OP_MOD: tm = TM_MOD; break;
    case OP_UNM: tm = TM_UNM; break;
    case OP_LEN: tm = TM_LEN; break;
    case OP_LT: tm = TM_LT; break;
    case OP_LE: tm = TM_LE; break;
    case OP_CONCAT: tm = TM_CONCAT; break;
      return NULL;  /* else no useful name can be found */
  *name = getstr(G(L)->tmname[tm]);
  return "metamethod";

-- Roberto