diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-31 17:40:14 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-31 17:40:14 -0200 |
commit | 46347d768e571ba9b36581c36d11d2de1dee2cfb (patch) | |
tree | 70818496b180f09e98693b9b2cf726bcf559076c /lauxlib.c | |
parent | 36eb6658599f7ec158b819f259eca338ee9a0d1a (diff) | |
download | lua-46347d768e571ba9b36581c36d11d2de1dee2cfb.tar.gz lua-46347d768e571ba9b36581c36d11d2de1dee2cfb.tar.bz2 lua-46347d768e571ba9b36581c36d11d2de1dee2cfb.zip |
`ref' support goes to auxlib
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 39 |
1 files changed, 35 insertions, 4 deletions
@@ -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 | ||
45 | static void type_error (lua_State *L, int narg, const l_char *tname) { | 45 | LUALIB_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 | ||
52 | static void tag_error (lua_State *L, int narg, int tag) { | 52 | static 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) { | |||
75 | LUALIB_API void *luaL_check_userdata (lua_State *L, int narg, | 75 | LUALIB_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 | |||
228 | LUALIB_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 | |||
248 | LUALIB_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 | |||