lua_strlen' upgraded to lua_objsize' (which also works with userdata)

This commit is contained in:
Roberto Ierusalimschy 2004-05-31 16:41:52 -03:00
parent 1e0aaf2156
commit 0dfd04eb60
3 changed files with 11 additions and 23 deletions

6
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.8 2004/05/11 16:52:08 roberto Exp roberto $ ** $Id: lapi.c,v 2.9 2004/05/14 19:25:09 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -323,10 +323,12 @@ LUA_API const char *lua_tostring (lua_State *L, int idx) {
} }
LUA_API size_t lua_strlen (lua_State *L, int idx) { LUA_API size_t lua_objsize (lua_State *L, int idx) {
StkId o = luaA_index(L, idx); StkId o = luaA_index(L, idx);
if (ttisstring(o)) if (ttisstring(o))
return tsvalue(o)->len; return tsvalue(o)->len;
else if (ttisuserdata(o))
return uvalue(o)->len;
else { else {
size_t l; size_t l;
lua_lock(L); /* `luaV_tostring' may create a new string */ lua_lock(L); /* `luaV_tostring' may create a new string */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.6 2004/05/10 17:50:51 roberto Exp roberto $ ** $Id: ltests.c,v 2.7 2004/05/31 18:50:48 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -760,16 +760,6 @@ static int int2fb_aux (lua_State *L) {
} }
static int test_do (lua_State *L) {
const char *p = luaL_checkstring(L, 1);
if (*p == '@')
lua_dofile(L, p+1);
else
lua_dostring(L, p);
return lua_gettop(L);
}
/* /*
** {====================================================== ** {======================================================
@ -858,8 +848,8 @@ static int testC (lua_State *L) {
const char *s = lua_tostring(L, getnum); const char *s = lua_tostring(L, getnum);
lua_pushstring(L, s); lua_pushstring(L, s);
} }
else if EQ("strlen") { else if EQ("objsize") {
lua_pushinteger(L, lua_strlen(L, getnum)); lua_pushinteger(L, lua_objsize(L, getnum));
} }
else if EQ("tocfunction") { else if EQ("tocfunction") {
lua_pushcfunction(L, lua_tocfunction(L, getnum)); lua_pushcfunction(L, lua_tocfunction(L, getnum));
@ -1033,7 +1023,6 @@ static const struct luaL_reg tests_funcs[] = {
{"stacklevel", stacklevel}, {"stacklevel", stacklevel},
{"querystr", string_query}, {"querystr", string_query},
{"querytab", table_query}, {"querytab", table_query},
{"doit", test_do},
{"testC", testC}, {"testC", testC},
{"checkmemory", lua_checkmemory}, {"checkmemory", lua_checkmemory},
{"gccolor", get_gccolor}, {"gccolor", get_gccolor},

11
lua.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.188 2004/03/24 13:55:46 roberto Exp roberto $ ** $Id: lua.h,v 1.189 2004/04/30 20:13:38 roberto Exp roberto $
** Lua - An Extensible Extension Language ** Lua - An Extensible Extension Language
** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
** http://www.lua.org mailto:info@lua.org ** http://www.lua.org mailto:info@lua.org
@ -145,7 +145,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 lua_Integer lua_tointeger (lua_State *L, int idx);
LUA_API int lua_toboolean (lua_State *L, int idx); LUA_API int lua_toboolean (lua_State *L, int idx);
LUA_API const char *lua_tostring (lua_State *L, int idx); LUA_API const char *lua_tostring (lua_State *L, int idx);
LUA_API size_t lua_strlen (lua_State *L, int idx); LUA_API size_t lua_objsize (lua_State *L, int idx);
LUA_API lua_CFunction lua_tocfunction (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 void *lua_touserdata (lua_State *L, int idx);
LUA_API lua_State *lua_tothread (lua_State *L, int idx); LUA_API lua_State *lua_tothread (lua_State *L, int idx);
@ -244,11 +244,6 @@ LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud);
** =============================================================== ** ===============================================================
*/ */
#define lua_boxpointer(L,u) \
(*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
#define lua_pop(L,n) lua_settop(L, -(n)-1) #define lua_pop(L,n) lua_settop(L, -(n)-1)
#define lua_newtable(L) lua_createtable(L, 0, 0) #define lua_newtable(L) lua_createtable(L, 0, 0)
@ -257,6 +252,8 @@ LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud);
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0) #define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
#define lua_strlen(L,i) lua_objsize(L,i)
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION) #define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE) #define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
#define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA) #define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA)