From 6875fdc8be9029b1bb29379c59d5409a0df42c10 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Feb 2001 17:53:16 -0200 Subject: [PATCH] new semantics for pushuserdata (no more different userdatas with same value) --- lapi.c | 11 +++++------ lstring.c | 16 ++++++++-------- lstring.h | 4 ++-- ltests.c | 21 ++++++++++++++++----- lua.h | 7 +++---- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/lapi.c b/lapi.c index e58fe789..c0c9f234 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.125 2001/02/02 15:13:05 roberto Exp roberto $ +** $Id: lapi.c,v 1.126 2001/02/07 18:13:49 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -335,14 +335,13 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { } -LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) { +LUA_API int lua_pushuserdata (lua_State *L, void *u) { + int isnew; LUA_LOCK(L); - /* ORDER LUA_T */ - if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag))) - luaO_verror(L, "invalid tag for a userdata (%d)", tag); - setuvalue(L->top, luaS_createudata(L, u, tag)); + isnew = luaS_createudata(L, u, L->top); api_incr_top(L); LUA_UNLOCK(L); + return isnew; } diff --git a/lstring.c b/lstring.c index fe87f5c2..48ffb3d7 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 1.54 2001/02/01 13:56:49 roberto Exp roberto $ +** $Id: lstring.c,v 1.55 2001/02/01 17:40:48 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -102,17 +102,17 @@ TString *luaS_newudata (lua_State *L, size_t s, void *udata) { } -TString *luaS_createudata (lua_State *L, void *udata, int tag) { +int luaS_createudata (lua_State *L, void *udata, TObject *o) { int h1 = lmod(IntPoint(udata), G(L)->udt.size); TString *ts; for (ts = G(L)->udt.hash[h1]; ts; ts = ts->nexthash) { - if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) - return ts; + if (udata == ts->u.d.value) { + setuvalue(o, ts); + return 0; + } } /* not found */ - ts = luaS_newudata(L, 0, udata); - if (tag != LUA_ANYTAG) - ts->u.d.tag = tag; - return ts; + setuvalue(o, luaS_newudata(L, 0, udata)); + return 1; } diff --git a/lstring.h b/lstring.h index 5bcf33aa..5c9c7c36 100644 --- a/lstring.h +++ b/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.26 2000/12/28 12:55:41 roberto Exp roberto $ +** $Id: lstring.h,v 1.27 2001/01/10 17:41:50 roberto Exp roberto $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -42,7 +42,7 @@ union L_UTString { void luaS_init (lua_State *L); void luaS_resize (lua_State *L, stringtable *tb, int newsize); TString *luaS_newudata (lua_State *L, size_t s, void *udata); -TString *luaS_createudata (lua_State *L, void *udata, int tag); +int luaS_createudata (lua_State *L, void *udata, TObject *o); void luaS_freeall (lua_State *L); TString *luaS_newlstr (lua_State *L, const char *str, size_t l); diff --git a/ltests.c b/ltests.c index 7f431343..7845716f 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.63 2001/02/06 16:01:29 roberto Exp roberto $ +** $Id: ltests.c,v 1.64 2001/02/06 18:18:58 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -348,11 +348,17 @@ static int unref (lua_State *L) { } static int newuserdata (lua_State *L) { - if (lua_isnumber(L, 2)) - lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2)); - else + if (lua_isnumber(L, 2)) { + int tag = luaL_check_int(L, 2); + int res = lua_pushuserdata(L, (void *)luaL_check_int(L, 1)); + if (tag) lua_settag(L, tag); + lua_pushnumber(L, res); + return 2; + } + else { lua_newuserdata(L, luaL_check_int(L, 1)); - return 1; + return 1; + } } static int udataval (lua_State *L) { @@ -361,6 +367,10 @@ static int udataval (lua_State *L) { return 1; } +static int newtag (lua_State *L) { + lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1), lua_tonumber(L, 2))); + return 1; +} static int doonnewstack (lua_State *L) { lua_State *L1 = lua_open(L, luaL_check_int(L, 1)); @@ -631,6 +641,7 @@ static const struct luaL_reg tests_funcs[] = { {"unref", unref}, {"newuserdata", newuserdata}, {"udataval", udataval}, + {"newtag", newtag}, {"doonnewstack", doonnewstack}, {"newstate", newstate}, {"closestate", closestate}, diff --git a/lua.h b/lua.h index e706eaa9..b42c6ad5 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.84 2001/01/25 16:45:36 roberto Exp roberto $ +** $Id: lua.h,v 1.85 2001/01/26 11:45:51 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: lua@tecgraf.puc-rio.br @@ -32,7 +32,6 @@ #define LUA_REFREGISTRY 0 /* pre-defined tags */ -#define LUA_ANYTAG (-1) #define LUA_NOTAG (-2) @@ -137,7 +136,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n); LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len); LUA_API void lua_pushstring (lua_State *L, const char *s); LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); -LUA_API void lua_pushusertag (lua_State *L, void *u, int tag); +LUA_API int lua_pushuserdata (lua_State *L, void *u); /* @@ -210,7 +209,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size); #define lua_pop(L,n) lua_settop(L, -(n)-1) #define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n)) -#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0) +#define lua_pushusertag(L,u,t) (lua_pushuserdata(L, u), lua_settag(L, t)) #define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0) #define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))