aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-09 17:53:16 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-02-09 17:53:16 -0200
commit6875fdc8be9029b1bb29379c59d5409a0df42c10 (patch)
treeaf7d5845b1e209473ecf8ad0f53a188974628b20
parentdc17a9cc24a52a298dbfb7ffe8aaad393f7c1bf9 (diff)
downloadlua-6875fdc8be9029b1bb29379c59d5409a0df42c10.tar.gz
lua-6875fdc8be9029b1bb29379c59d5409a0df42c10.tar.bz2
lua-6875fdc8be9029b1bb29379c59d5409a0df42c10.zip
new semantics for pushuserdata (no more different userdatas with same value)
-rw-r--r--lapi.c11
-rw-r--r--lstring.c16
-rw-r--r--lstring.h4
-rw-r--r--ltests.c21
-rw-r--r--lua.h7
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 @@
1/* 1/*
2** $Id: lapi.c,v 1.125 2001/02/02 15:13:05 roberto Exp roberto $ 2** $Id: lapi.c,v 1.126 2001/02/07 18:13:49 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -335,14 +335,13 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
335} 335}
336 336
337 337
338LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) { 338LUA_API int lua_pushuserdata (lua_State *L, void *u) {
339 int isnew;
339 LUA_LOCK(L); 340 LUA_LOCK(L);
340 /* ORDER LUA_T */ 341 isnew = luaS_createudata(L, u, L->top);
341 if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag)))
342 luaO_verror(L, "invalid tag for a userdata (%d)", tag);
343 setuvalue(L->top, luaS_createudata(L, u, tag));
344 api_incr_top(L); 342 api_incr_top(L);
345 LUA_UNLOCK(L); 343 LUA_UNLOCK(L);
344 return isnew;
346} 345}
347 346
348 347
diff --git a/lstring.c b/lstring.c
index fe87f5c2..48ffb3d7 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.54 2001/02/01 13:56:49 roberto Exp roberto $ 2** $Id: lstring.c,v 1.55 2001/02/01 17:40:48 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -102,17 +102,17 @@ TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
102} 102}
103 103
104 104
105TString *luaS_createudata (lua_State *L, void *udata, int tag) { 105int luaS_createudata (lua_State *L, void *udata, TObject *o) {
106 int h1 = lmod(IntPoint(udata), G(L)->udt.size); 106 int h1 = lmod(IntPoint(udata), G(L)->udt.size);
107 TString *ts; 107 TString *ts;
108 for (ts = G(L)->udt.hash[h1]; ts; ts = ts->nexthash) { 108 for (ts = G(L)->udt.hash[h1]; ts; ts = ts->nexthash) {
109 if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) 109 if (udata == ts->u.d.value) {
110 return ts; 110 setuvalue(o, ts);
111 return 0;
112 }
111 } 113 }
112 /* not found */ 114 /* not found */
113 ts = luaS_newudata(L, 0, udata); 115 setuvalue(o, luaS_newudata(L, 0, udata));
114 if (tag != LUA_ANYTAG) 116 return 1;
115 ts->u.d.tag = tag;
116 return ts;
117} 117}
118 118
diff --git a/lstring.h b/lstring.h
index 5bcf33aa..5c9c7c36 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.26 2000/12/28 12:55:41 roberto Exp roberto $ 2** $Id: lstring.h,v 1.27 2001/01/10 17:41:50 roberto Exp roberto $
3** String table (keep all strings handled by Lua) 3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -42,7 +42,7 @@ union L_UTString {
42void luaS_init (lua_State *L); 42void luaS_init (lua_State *L);
43void luaS_resize (lua_State *L, stringtable *tb, int newsize); 43void luaS_resize (lua_State *L, stringtable *tb, int newsize);
44TString *luaS_newudata (lua_State *L, size_t s, void *udata); 44TString *luaS_newudata (lua_State *L, size_t s, void *udata);
45TString *luaS_createudata (lua_State *L, void *udata, int tag); 45int luaS_createudata (lua_State *L, void *udata, TObject *o);
46void luaS_freeall (lua_State *L); 46void luaS_freeall (lua_State *L);
47TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 47TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
48 48
diff --git a/ltests.c b/ltests.c
index 7f431343..7845716f 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.63 2001/02/06 16:01:29 roberto Exp roberto $ 2** $Id: ltests.c,v 1.64 2001/02/06 18:18:58 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -348,11 +348,17 @@ static int unref (lua_State *L) {
348} 348}
349 349
350static int newuserdata (lua_State *L) { 350static int newuserdata (lua_State *L) {
351 if (lua_isnumber(L, 2)) 351 if (lua_isnumber(L, 2)) {
352 lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2)); 352 int tag = luaL_check_int(L, 2);
353 else 353 int res = lua_pushuserdata(L, (void *)luaL_check_int(L, 1));
354 if (tag) lua_settag(L, tag);
355 lua_pushnumber(L, res);
356 return 2;
357 }
358 else {
354 lua_newuserdata(L, luaL_check_int(L, 1)); 359 lua_newuserdata(L, luaL_check_int(L, 1));
355 return 1; 360 return 1;
361 }
356} 362}
357 363
358static int udataval (lua_State *L) { 364static int udataval (lua_State *L) {
@@ -361,6 +367,10 @@ static int udataval (lua_State *L) {
361 return 1; 367 return 1;
362} 368}
363 369
370static int newtag (lua_State *L) {
371 lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1), lua_tonumber(L, 2)));
372 return 1;
373}
364 374
365static int doonnewstack (lua_State *L) { 375static int doonnewstack (lua_State *L) {
366 lua_State *L1 = lua_open(L, luaL_check_int(L, 1)); 376 lua_State *L1 = lua_open(L, luaL_check_int(L, 1));
@@ -631,6 +641,7 @@ static const struct luaL_reg tests_funcs[] = {
631 {"unref", unref}, 641 {"unref", unref},
632 {"newuserdata", newuserdata}, 642 {"newuserdata", newuserdata},
633 {"udataval", udataval}, 643 {"udataval", udataval},
644 {"newtag", newtag},
634 {"doonnewstack", doonnewstack}, 645 {"doonnewstack", doonnewstack},
635 {"newstate", newstate}, 646 {"newstate", newstate},
636 {"closestate", closestate}, 647 {"closestate", closestate},
diff --git a/lua.h b/lua.h
index e706eaa9..b42c6ad5 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.84 2001/01/25 16:45:36 roberto Exp roberto $ 2** $Id: lua.h,v 1.85 2001/01/26 11:45:51 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: lua@tecgraf.puc-rio.br 5** e-mail: lua@tecgraf.puc-rio.br
@@ -32,7 +32,6 @@
32#define LUA_REFREGISTRY 0 32#define LUA_REFREGISTRY 0
33 33
34/* pre-defined tags */ 34/* pre-defined tags */
35#define LUA_ANYTAG (-1)
36#define LUA_NOTAG (-2) 35#define LUA_NOTAG (-2)
37 36
38 37
@@ -137,7 +136,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n);
137LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len); 136LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len);
138LUA_API void lua_pushstring (lua_State *L, const char *s); 137LUA_API void lua_pushstring (lua_State *L, const char *s);
139LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); 138LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
140LUA_API void lua_pushusertag (lua_State *L, void *u, int tag); 139LUA_API int lua_pushuserdata (lua_State *L, void *u);
141 140
142 141
143/* 142/*
@@ -210,7 +209,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
210#define lua_pop(L,n) lua_settop(L, -(n)-1) 209#define lua_pop(L,n) lua_settop(L, -(n)-1)
211 210
212#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n)) 211#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
213#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0) 212#define lua_pushusertag(L,u,t) (lua_pushuserdata(L, u), lua_settag(L, t))
214#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0) 213#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
215#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t)) 214#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
216 215