diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-07 13:49:47 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-07 13:49:47 -0200 |
commit | 86312e1a7d128be944a29bea7421eff58efb83be (patch) | |
tree | a1a29dc3dc5e2fac534b114a33a262174a9fb1ae /lapi.c | |
parent | df1dc3f1f54a75174cea7298d5c40bc7b99e60ea (diff) | |
download | lua-86312e1a7d128be944a29bea7421eff58efb83be.tar.gz lua-86312e1a7d128be944a29bea7421eff58efb83be.tar.bz2 lua-86312e1a7d128be944a29bea7421eff58efb83be.zip |
lua_objlen calls __len metamethod on tables (if present)
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 19 |
1 files changed, 13 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.100 2009/11/09 19:10:48 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.101 2009/11/27 15:37:59 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -365,11 +365,18 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { | |||
365 | 365 | ||
366 | LUA_API size_t lua_objlen (lua_State *L, int idx) { | 366 | LUA_API size_t lua_objlen (lua_State *L, int idx) { |
367 | StkId o = index2addr(L, idx); | 367 | StkId o = index2addr(L, idx); |
368 | switch (ttype(o)) { | 368 | if (ttisuserdata(o)) return uvalue(o)->len; |
369 | case LUA_TSTRING: return tsvalue(o)->len; | 369 | else { |
370 | case LUA_TUSERDATA: return uvalue(o)->len; | 370 | size_t res; |
371 | case LUA_TTABLE: return luaH_getn(hvalue(o)); | 371 | TValue temp; |
372 | default: return 0; | 372 | lua_lock(L); |
373 | res = luaV_len(L, o, &temp); | ||
374 | if (res == cast(size_t, -1)) { | ||
375 | const TValue *t = &temp; | ||
376 | res = tonumber(t, &temp) ? nvalue(t) : 0; | ||
377 | } | ||
378 | lua_unlock(L); | ||
379 | return res; | ||
373 | } | 380 | } |
374 | } | 381 | } |
375 | 382 | ||