aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-10-31 17:40:14 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-10-31 17:40:14 -0200
commit46347d768e571ba9b36581c36d11d2de1dee2cfb (patch)
tree70818496b180f09e98693b9b2cf726bcf559076c
parent36eb6658599f7ec158b819f259eca338ee9a0d1a (diff)
downloadlua-46347d768e571ba9b36581c36d11d2de1dee2cfb.tar.gz
lua-46347d768e571ba9b36581c36d11d2de1dee2cfb.tar.bz2
lua-46347d768e571ba9b36581c36d11d2de1dee2cfb.zip
`ref' support goes to auxlib
-rw-r--r--lapi.c36
-rw-r--r--lauxlib.c39
-rw-r--r--lauxlib.h5
-rw-r--r--lua.h25
4 files changed, 55 insertions, 50 deletions
diff --git a/lapi.c b/lapi.c
index e941139a..c0212e79 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.156 2001/10/17 21:17:45 roberto Exp $ 2** $Id: lapi.c,v 1.157 2001/10/25 19:14:14 roberto Exp $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -458,30 +458,6 @@ LUA_API void lua_setglobals (lua_State *L) {
458} 458}
459 459
460 460
461LUA_API int lua_ref (lua_State *L, int lock) {
462 int ref;
463 if (lock == 0) lua_error(L, l_s("unlocked references are obsolete"));
464 if (lua_isnil(L, -1)) {
465 lua_pop(L, 1);
466 return LUA_REFNIL;
467 }
468 lua_rawgeti(L, LUA_REGISTRYINDEX, 0); /* get first free element */
469 ref = cast(int, lua_tonumber(L, -1));
470 lua_pop(L, 1); /* remove it from stack */
471 if (ref != 0) { /* some free element? */
472 lua_rawgeti(L, LUA_REGISTRYINDEX, ref); /* remove it from list */
473 lua_rawseti(L, LUA_REGISTRYINDEX, 0);
474 }
475 else { /* no free elements */
476 ref = lua_getn(L, LUA_REGISTRYINDEX) + 1; /* use next `n' */
477 lua_pushliteral(L, l_s("n"));
478 lua_pushnumber(L, ref);
479 lua_settable(L, LUA_REGISTRYINDEX); /* n = n+1 */
480 }
481 lua_rawseti(L, LUA_REGISTRYINDEX, ref);
482 return ref;
483}
484
485 461
486/* 462/*
487** `do' functions (run Lua code) 463** `do' functions (run Lua code)
@@ -635,16 +611,6 @@ LUA_API void lua_error (lua_State *L, const l_char *s) {
635} 611}
636 612
637 613
638LUA_API void lua_unref (lua_State *L, int ref) {
639 if (ref >= 0) {
640 lua_rawgeti(L, LUA_REGISTRYINDEX, 0);
641 lua_pushnumber(L, ref);
642 lua_rawseti(L, LUA_REGISTRYINDEX, 0);
643 lua_rawseti(L, LUA_REGISTRYINDEX, ref);
644 }
645}
646
647
648LUA_API int lua_next (lua_State *L, int index) { 614LUA_API int lua_next (lua_State *L, int index) {
649 StkId t; 615 StkId t;
650 int more; 616 int more;
diff --git a/lauxlib.c b/lauxlib.c
index cc1390b9..7fef8721 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.51 2001/07/12 18:11:58 roberto Exp $ 2** $Id: lauxlib.c,v 1.52 2001/10/26 17:33:30 roberto Exp $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -42,7 +42,7 @@ LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) {
42} 42}
43 43
44 44
45static void type_error (lua_State *L, int narg, const l_char *tname) { 45LUALIB_API void luaL_typerror (lua_State *L, int narg, const l_char *tname) {
46 l_char buff[80]; 46 l_char buff[80];
47 sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg)); 47 sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
48 luaL_argerror(L, narg, buff); 48 luaL_argerror(L, narg, buff);
@@ -50,7 +50,7 @@ static void type_error (lua_State *L, int narg, const l_char *tname) {
50 50
51 51
52static void tag_error (lua_State *L, int narg, int tag) { 52static void tag_error (lua_State *L, int narg, int tag) {
53 type_error(L, narg, lua_typename(L, tag)); 53 luaL_typerror(L, narg, lua_typename(L, tag));
54} 54}
55 55
56 56
@@ -75,7 +75,7 @@ LUALIB_API void luaL_check_any (lua_State *L, int narg) {
75LUALIB_API void *luaL_check_userdata (lua_State *L, int narg, 75LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
76 const l_char *name) { 76 const l_char *name) {
77 if (strcmp(lua_type(L, narg), name) != 0) 77 if (strcmp(lua_type(L, narg), name) != 0)
78 type_error(L, narg, name); 78 luaL_typerror(L, narg, name);
79 return lua_touserdata(L, narg); 79 return lua_touserdata(L, narg);
80} 80}
81 81
@@ -223,3 +223,34 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
223} 223}
224 224
225/* }====================================================== */ 225/* }====================================================== */
226
227
228LUALIB_API int luaL_ref (lua_State *L, int t) {
229 int ref;
230 lua_rawgeti(L, t, 0); /* get first free element */
231 ref = (int)lua_tonumber(L, -1);
232 lua_pop(L, 1); /* remove it from stack */
233 if (ref != 0) { /* any free element? */
234 lua_rawgeti(L, t, ref); /* remove it from list */
235 lua_rawseti(L, t, 0);
236 }
237 else { /* no free elements */
238 ref = lua_getn(L, t) + 1; /* use next `n' */
239 lua_pushliteral(L, l_s("n"));
240 lua_pushnumber(L, ref);
241 lua_settable(L, t); /* n = n+1 */
242 }
243 lua_rawseti(L, t, ref);
244 return ref;
245}
246
247
248LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
249 if (ref >= 0) {
250 lua_rawgeti(L, t, 0);
251 lua_pushnumber(L, ref);
252 lua_rawseti(L, t, 0);
253 lua_rawseti(L, t, ref);
254 }
255}
256
diff --git a/lauxlib.h b/lauxlib.h
index e3da34f3..9bc51695 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.36 2001/07/22 00:59:36 roberto Exp $ 2** $Id: lauxlib.h,v 1.37 2001/10/26 17:33:30 roberto Exp $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,6 +27,7 @@ typedef struct luaL_reg {
27 27
28 28
29LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n); 29LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n);
30LUALIB_API void luaL_typerror (lua_State *L, int narg, const lua_char *tname);
30LUALIB_API void luaL_argerror (lua_State *L, int numarg, 31LUALIB_API void luaL_argerror (lua_State *L, int numarg,
31 const lua_char *extramsg); 32 const lua_char *extramsg);
32LUALIB_API const lua_char *luaL_check_lstr (lua_State *L, int numArg, 33LUALIB_API const lua_char *luaL_check_lstr (lua_State *L, int numArg,
@@ -46,6 +47,8 @@ LUALIB_API void luaL_verror (lua_State *L, const lua_char *fmt, ...);
46LUALIB_API int luaL_findstring (const lua_char *name, 47LUALIB_API int luaL_findstring (const lua_char *name,
47 const lua_char *const list[]); 48 const lua_char *const list[]);
48 49
50LUALIB_API int luaL_ref (lua_State *L, int t);
51LUALIB_API void luaL_unref (lua_State *L, int t, int ref);
49 52
50 53
51/* 54/*
diff --git a/lua.h b/lua.h
index 1325f5f4..1691b07f 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.104 2001/10/11 21:41:21 roberto Exp $ 2** $Id: lua.h,v 1.105 2001/10/17 21:12:57 roberto Exp $
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: info@lua.org 5** e-mail: info@lua.org
@@ -26,11 +26,6 @@
26#define LUA_ERRORMESSAGE "_ERRORMESSAGE" 26#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
27 27
28 28
29/* pre-defined references */
30#define LUA_NOREF (-2)
31#define LUA_REFNIL (-1)
32
33
34/* option for multiple returns in `lua_call' */ 29/* option for multiple returns in `lua_call' */
35#define LUA_MULTRET (-1) 30#define LUA_MULTRET (-1)
36 31
@@ -180,7 +175,6 @@ LUA_API void lua_rawset (lua_State *L, int index);
180LUA_API void lua_rawseti (lua_State *L, int index, int n); 175LUA_API void lua_rawseti (lua_State *L, int index, int n);
181LUA_API void lua_setglobals (lua_State *L); 176LUA_API void lua_setglobals (lua_State *L);
182LUA_API void lua_settagmethod (lua_State *L, int tag, const lua_char *event); 177LUA_API void lua_settagmethod (lua_State *L, int tag, const lua_char *event);
183LUA_API int lua_ref (lua_State *L, int lock);
184 178
185 179
186/* 180/*
@@ -214,8 +208,6 @@ LUA_API const lua_char *lua_tag2name (lua_State *L, int tag);
214 208
215LUA_API void lua_error (lua_State *L, const lua_char *s); 209LUA_API void lua_error (lua_State *L, const lua_char *s);
216 210
217LUA_API void lua_unref (lua_State *L, int ref);
218
219LUA_API int lua_next (lua_State *L, int index); 211LUA_API int lua_next (lua_State *L, int index);
220LUA_API int lua_getn (lua_State *L, int index); 212LUA_API int lua_getn (lua_State *L, int index);
221 213
@@ -253,7 +245,6 @@ LUA_API int lua_getweakmode (lua_State *L, int index);
253 245
254#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX); 246#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX);
255 247
256#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
257 248
258 249
259/* 250/*
@@ -264,6 +255,20 @@ LUA_API int lua_getweakmode (lua_State *L, int index);
264 255
265LUA_API void lua_pushupvalues (lua_State *L); 256LUA_API void lua_pushupvalues (lua_State *L);
266 257
258
259/* compatibility with ref system */
260
261/* pre-defined references */
262#define LUA_NOREF (-2)
263#define LUA_REFNIL (-1)
264
265#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
266 (lua_error(L, l_s("unlocked references are obsolete")), 0))
267
268#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
269
270#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
271
267#endif 272#endif
268 273
269 274