From 0dfd04eb60a7b9d54a6348ef4d9a3fa3963682aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 31 May 2004 16:41:52 -0300 Subject: `lua_strlen' upgraded to `lua_objsize' (which also works with userdata) --- lapi.c | 6 ++++-- ltests.c | 17 +++-------------- lua.h | 11 ++++------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/lapi.c b/lapi.c index 7a1a72d3..ab0278b4 100644 --- a/lapi.c +++ b/lapi.c @@ -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 ** 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); if (ttisstring(o)) return tsvalue(o)->len; + else if (ttisuserdata(o)) + return uvalue(o)->len; else { size_t l; lua_lock(L); /* `luaV_tostring' may create a new string */ diff --git a/ltests.c b/ltests.c index 456c9d91..afdfb515 100644 --- a/ltests.c +++ b/ltests.c @@ -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 ** 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); lua_pushstring(L, s); } - else if EQ("strlen") { - lua_pushinteger(L, lua_strlen(L, getnum)); + else if EQ("objsize") { + lua_pushinteger(L, lua_objsize(L, getnum)); } else if EQ("tocfunction") { lua_pushcfunction(L, lua_tocfunction(L, getnum)); @@ -1033,7 +1023,6 @@ static const struct luaL_reg tests_funcs[] = { {"stacklevel", stacklevel}, {"querystr", string_query}, {"querytab", table_query}, - {"doit", test_do}, {"testC", testC}, {"checkmemory", lua_checkmemory}, {"gccolor", get_gccolor}, diff --git a/lua.h b/lua.h index 7317bc47..48b69b5f 100644 --- a/lua.h +++ b/lua.h @@ -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 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil ** 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 int lua_toboolean (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 void *lua_touserdata (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_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_strlen(L,i) lua_objsize(L,i) + #define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION) #define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE) #define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA) -- cgit v1.2.3-55-g6feb