From c3a6f3fa1c6360d4ea2e32f9415f51e8e55093b4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Dec 2009 14:20:01 -0200 Subject: [PATCH] 'lua_objlen' replaced by 'lua_rawlen', 'lua_len', and 'luaL_len' --- lapi.c | 31 +++++++++++++++++-------------- lauxlib.c | 19 +++++++++++++++---- lauxlib.h | 3 ++- lbaselib.c | 6 +++--- liolib.c | 4 ++-- ltablib.c | 6 +++--- ltests.c | 16 +++++++++++----- lua.c | 8 +++++--- lua.h | 9 ++++++--- lvm.c | 6 +++--- lvm.h | 3 ++- 11 files changed, 69 insertions(+), 42 deletions(-) diff --git a/lapi.c b/lapi.c index b26efbc0..3b24d70c 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.103 2009/12/08 16:15:43 roberto Exp roberto $ +** $Id: lapi.c,v 2.104 2009/12/15 11:25:36 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -374,20 +374,13 @@ 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_rawlen (lua_State *L, int idx) { StkId o = index2addr(L, idx); - if (ttisuserdata(o)) return uvalue(o)->len; - else { - size_t res; - TValue temp; - 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; + switch (ttype(o)) { + case LUA_TSTRING: return tsvalue(o)->len; + case LUA_TUSERDATA: return uvalue(o)->len; + case LUA_TTABLE: return luaH_getn(hvalue(o)); + default: return 0; } } @@ -1027,6 +1020,16 @@ LUA_API void lua_concat (lua_State *L, int n) { } +LUA_API void lua_len (lua_State *L, int idx) { + StkId t; + lua_lock(L); + t = index2addr(L, idx); + luaV_objlen(L, L->top, t); + api_incr_top(L); + lua_unlock(L); +} + + LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { lua_Alloc f; lua_lock(L); diff --git a/lauxlib.c b/lauxlib.c index 7611db7f..dcb913f0 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.193 2009/10/05 16:44:33 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.194 2009/11/25 15:27:51 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -370,9 +370,9 @@ static void adjuststack (luaL_Buffer *B) { if (B->lvl > 1) { lua_State *L = B->L; int toget = 1; /* number of levels to concat */ - size_t toplen = lua_objlen(L, -1); + size_t toplen = lua_rawlen(L, -1); do { - size_t l = lua_objlen(L, -(toget+1)); + size_t l = lua_rawlen(L, -(toget+1)); if (B->lvl - toget + 1 >= LIMIT || toplen > l) { toplen += l; toget++; @@ -463,7 +463,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ } else { /* no free elements */ - ref = (int)lua_objlen(L, t) + 1; /* get a new reference */ + ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ if (ref == FREELIST_REF) { /* FREELIST_REF not initialized? */ lua_pushinteger(L, 0); lua_rawseti(L, t, FREELIST_REF); @@ -627,6 +627,17 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { } +LUALIB_API int luaL_len (lua_State *L, int idx) { + int l; + lua_len(L, idx); + l = lua_tointeger(L, -1); + if (l == 0 && !lua_isnumber(L, -1)) + luaL_error(L, "object length is not a number"); + lua_pop(L, 1); /* remove object */ + return l; +} + + LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ switch (lua_type(L, idx)) { diff --git a/lauxlib.h b/lauxlib.h index 10de3d0b..38fcdece 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.95 2009/02/13 19:39:34 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.96 2009/06/18 18:59:58 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -73,6 +73,7 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); +LUALIB_API int luaL_len (lua_State *L, int idx); LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); diff --git a/lbaselib.c b/lbaselib.c index 419fdc52..80b0039d 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.231 2009/12/11 13:40:44 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.232 2009/12/15 11:25:16 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -258,7 +258,7 @@ static int ipairsaux (lua_State *L) { i++; /* next value */ lua_pushinteger(L, i); lua_rawgeti(L, 1, i); - return (lua_isnil(L, -1) && i > (int)lua_objlen(L, 1)) ? 0 : 2; + return (lua_isnil(L, -1) && i > luaL_len(L, 1)) ? 0 : 2; } @@ -416,7 +416,7 @@ static int luaB_unpack (lua_State *L) { int i, e, n; luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, (int)lua_objlen(L, 1)); + e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1)); if (i > e) return 0; /* empty range */ n = e - i + 1; /* number of elements */ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ diff --git a/liolib.c b/liolib.c index 718058f2..6282b5c4 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.83 2009/11/24 12:05:44 roberto Exp roberto $ +** $Id: liolib.c,v 2.84 2009/12/17 13:08:51 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -336,7 +336,7 @@ static int read_line (lua_State *L, FILE *f) { char *p = luaL_prepbuffer(&b); if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ luaL_pushresult(&b); /* close buffer */ - return (lua_objlen(L, -1) > 0); /* check whether read something */ + return (lua_rawlen(L, -1) > 0); /* check whether read something */ } l = strlen(p); if (l == 0 || p[l-1] != '\n') diff --git a/ltablib.c b/ltablib.c index 26a103da..2feffe54 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.49 2009/11/26 17:35:13 roberto Exp roberto $ +** $Id: ltablib.c,v 1.50 2009/12/07 15:50:27 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -16,7 +16,7 @@ #include "lualib.h" -#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), lua_objlen(L, n)) +#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), lua_rawlen(L, n)) static int foreachi (lua_State *L) { @@ -154,7 +154,7 @@ static int tconcat (lua_State *L) { const char *sep = luaL_optlstring(L, 2, "", &lsep); luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 3, 1); - last = luaL_opt(L, luaL_checkint, 4, (int)lua_objlen(L, 1)); + last = luaL_opt(L, luaL_checkint, 4, (int)lua_rawlen(L, 1)); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, i); diff --git a/ltests.c b/ltests.c index 7114a175..57493695 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.83 2009/12/11 19:14:59 roberto Exp roberto $ +** $Id: ltests.c,v 2.84 2009/12/16 16:42:58 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -946,7 +946,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0); } else if EQ("objsize") { - lua_pushinteger(L1, lua_objlen(L1, getindex)); + lua_pushinteger(L1, lua_rawlen(L1, getindex)); + } + else if EQ("len") { + lua_len(L1, getindex); + } + else if EQ("Llen") { + lua_pushinteger(L1, luaL_len(L1, getindex)); } else if EQ("tocfunction") { lua_pushcfunction(L1, lua_tocfunction(L1, getindex)); @@ -1135,13 +1141,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("type") { lua_pushstring(L1, luaL_typename(L1, getnum)); } - else if EQ("getn") { +/* else if EQ("getn") { int i = getindex; lua_pushinteger(L1, lua_objlen(L1, i)); - } + } */ else if EQ("append") { int t = getindex; - int i = lua_objlen(L1, t); + int i = lua_rawlen(L1, t); lua_rawseti(L1, t, i + 1); } else if EQ("getctx") { diff --git a/lua.c b/lua.c index a3b9e663..4875b54d 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.178 2009/12/17 12:26:09 roberto Exp roberto $ +** $Id: lua.c,v 1.179 2009/12/17 13:07:41 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -65,7 +65,7 @@ #include #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) #define lua_saveline(L,idx) \ - if (lua_objlen(L,idx) > 0) /* non-empty line? */ \ + if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ add_history(lua_tostring(L, idx)); /* add it to history */ #define lua_freeline(L,b) ((void)L, free(b)) @@ -271,7 +271,9 @@ static int loadline (lua_State *L) { if (!pushline(L, 1)) return -1; /* no input */ for (;;) { /* repeat until gets a complete line */ - status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_objlen(L, 1), "=stdin"); + size_t l; + const char *line = lua_tolstring(L, 1, &l); + status = luaL_loadbuffer(L, line, l, "=stdin"); if (!incomplete(L, status)) break; /* cannot try to add lines? */ if (!pushline(L, 0)) /* no more input? */ return -1; diff --git a/lua.h b/lua.h index 9cf92660..f41a6690 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.252 2009/11/26 11:39:20 roberto Exp roberto $ +** $Id: lua.h,v 1.253 2009/12/11 13:40:44 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -156,7 +156,7 @@ LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); LUA_API int (lua_toboolean) (lua_State *L, int idx); 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_rawlen) (lua_State *L, int idx); LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); LUA_API void *(lua_touserdata) (lua_State *L, int idx); LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); @@ -281,6 +281,7 @@ LUA_API int (lua_error) (lua_State *L); LUA_API int (lua_next) (lua_State *L, int idx); LUA_API void (lua_concat) (lua_State *L, int n); +LUA_API void (lua_len) (lua_State *L, int idx); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); @@ -326,7 +327,9 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); */ #if defined(LUA_COMPAT_API) -#define lua_strlen(L,i) lua_objlen(L, (i)) +#define lua_strlen(L,i) lua_rawlen(L, (i)) + +#define lua_objlen(L,i) lua_rawlen(L, (i)) #define lua_open() luaL_newstate() diff --git a/lvm.c b/lvm.c index 0b9eab47..c1d12f89 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.100 2009/10/28 12:20:07 roberto Exp roberto $ +** $Id: lvm.c,v 2.101 2009/11/25 15:27:51 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -307,7 +307,7 @@ void luaV_concat (lua_State *L, int total) { } -static void objlen (lua_State *L, StkId ra, const TValue *rb) { +void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; switch (ttype(rb)) { case LUA_TTABLE: { @@ -582,7 +582,7 @@ void luaV_execute (lua_State *L) { continue; } case OP_LEN: { - Protect(objlen(L, ra, RB(i))); + Protect(luaV_objlen(L, ra, RB(i))); continue; } case OP_CONCAT: { diff --git a/lvm.h b/lvm.h index 22d37e50..23e50798 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.12 2009/11/06 17:05:34 roberto Exp roberto $ +** $Id: lvm.h,v 2.13 2009/11/19 19:04:58 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -37,5 +37,6 @@ LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_concat (lua_State *L, int total); LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, const TValue *rc, TMS op); +LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); #endif