aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-07 13:49:47 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-07 13:49:47 -0200
commit86312e1a7d128be944a29bea7421eff58efb83be (patch)
treea1a29dc3dc5e2fac534b114a33a262174a9fb1ae /lapi.c
parentdf1dc3f1f54a75174cea7298d5c40bc7b99e60ea (diff)
downloadlua-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.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/lapi.c b/lapi.c
index 694fdc35..eef68536 100644
--- a/lapi.c
+++ b/lapi.c
@@ -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
366LUA_API size_t lua_objlen (lua_State *L, int idx) { 366LUA_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