Browse Source

lua_objlen calls __len metamethod on tables (if present)

pull/9/head
Roberto Ierusalimschy 15 years ago
parent
commit
86312e1a7d
  1. 19
      lapi.c

19
lapi.c

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.100 2009/11/09 19:10:48 roberto Exp roberto $ ** $Id: lapi.c,v 2.101 2009/11/27 15:37:59 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -365,11 +365,18 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
LUA_API size_t lua_objlen (lua_State *L, int idx) { LUA_API size_t lua_objlen (lua_State *L, int idx) {
StkId o = index2addr(L, idx); StkId o = index2addr(L, idx);
switch (ttype(o)) { if (ttisuserdata(o)) return uvalue(o)->len;
case LUA_TSTRING: return tsvalue(o)->len; else {
case LUA_TUSERDATA: return uvalue(o)->len; size_t res;
case LUA_TTABLE: return luaH_getn(hvalue(o)); TValue temp;
default: return 0; lua_lock(L);
res = luaV_len(L, o, &temp);
if (res == cast(size_t, -1)) {
const TValue *t = &temp;
res = tonumber(t, &temp) ? nvalue(t) : 0;
}
lua_unlock(L);
return res;
} }
} }

Loading…
Cancel
Save