Browse Source

new semantics for pushuserdata (no more different userdatas with same value)

Roberto Ierusalimschy 24 years ago
parent
commit
6875fdc8be
5 changed files with 34 additions and 25 deletions
  1. 5 6
      lapi.c
  2. 8 8
      lstring.c
  3. 2 2
      lstring.h
  4. 16 5
      ltests.c
  5. 3 4
      lua.h

+ 5 - 6
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;
 }
 
 

+ 8 - 8
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;
 }
 

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

+ 16 - 5
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},

+ 3 - 4
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: [email protected]
@@ -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))