lua-users home
lua-l archive

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


D'oh, just send and see a bug XD please, remove the second "int result
= 0"... should be like this:
int mylen(lua_State *L, int p)
{
   int pos = (p > 0 || p <= LUA_REGISTRYINDEX ? p : lua_gettop(L) + p + 1);
   int result = 0;
   if (lua_isuserdata(L,pos) && !lua_islightuserdata(L,pos))
   {
       luaL_getmetafield(L, pos, "__len");
       if (lua_isfunction(L,-1))
       {
           lua_pushvalue(L,pos); // push the object
           lua_call(L, 1, 1); // call __len(obj)
           result = lua_tointeger(L,-1); // get the result
           lua_pop(L,1); // remove returned value
       }
       lua_pop(L,1); // remove metafield or nil
   }
   else
   {
       // default behaviour
       result = lua_objlen(L, pos);
   }
   return result;
}

But I have no compiler to test it now.... sorry.
--
 Jose L. Hidalgo Valiño (PpluX)
 ---- http://www.pplux.com ----