瀏覽代碼

'lua_objlen' replaced by 'lua_rawlen', 'lua_len', and 'luaL_len'

Roberto Ierusalimschy 15 年之前
父節點
當前提交
c3a6f3fa1c
共有 11 個文件被更改,包括 69 次插入42 次删除
  1. 17 14
      lapi.c
  2. 15 4
      lauxlib.c
  3. 2 1
      lauxlib.h
  4. 3 3
      lbaselib.c
  5. 2 2
      liolib.c
  6. 3 3
      ltablib.c
  7. 11 5
      ltests.c
  8. 5 3
      lua.c
  9. 6 3
      lua.h
  10. 3 3
      lvm.c
  11. 2 1
      lvm.h

+ 17 - 14
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);

+ 15 - 4
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)) {

+ 2 - 1
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);

+ 3 - 3
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 */

+ 2 - 2
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')

+ 3 - 3
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);

+ 11 - 5
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") {

+ 5 - 3
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 <readline/history.h>
 #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;

+ 6 - 3
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()
 

+ 3 - 3
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: {

+ 2 - 1
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