aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-08-17 16:05:04 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-08-17 16:05:04 -0300
commit2f2b4a42a95c7a96e5e16b74ba1167690fcd6231 (patch)
tree9a5ae551f28f24f6b9204c76cec49dc28386e16b
parent074352911f3fe3102e4103c2b7140d1cf3d48492 (diff)
downloadlua-2f2b4a42a95c7a96e5e16b74ba1167690fcd6231.tar.gz
lua-2f2b4a42a95c7a96e5e16b74ba1167690fcd6231.tar.bz2
lua-2f2b4a42a95c7a96e5e16b74ba1167690fcd6231.zip
luaL_checkudata raises an error if value is not correct
(like other luaL_check functions)
-rw-r--r--lauxlib.c25
-rw-r--r--liolib.c20
-rw-r--r--loadlib.c8
3 files changed, 25 insertions, 28 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 93cbff51..a828e612 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.143 2005/08/10 18:47:09 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.144 2005/08/15 14:12:32 roberto Exp roberto $
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*/
@@ -130,18 +130,19 @@ LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) {
130 130
131 131
132LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { 132LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
133 const char *tn; 133 void *p = NULL;
134 if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */ 134 if (lua_getmetatable(L, ud)) {
135 lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */ 135 const char *tn;
136 tn = lua_tostring(L, -1); 136 lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */
137 if (tn && (strcmp(tn, tname) == 0)) { 137 tn = lua_tostring(L, -1);
138 lua_pop(L, 1); 138 if (tn && (strcmp(tn, tname) == 0)) {
139 return lua_touserdata(L, ud); 139 lua_pop(L, 1);
140 } 140 p = lua_touserdata(L, ud);
141 else { 141 }
142 lua_pop(L, 1);
143 return NULL;
144 } 142 }
143 if (p == NULL)
144 luaL_typerror(L, ud, tname);
145 return p;
145} 146}
146 147
147 148
diff --git a/liolib.c b/liolib.c
index a029190d..ceee5371 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.64 2005/07/12 14:32:08 roberto Exp roberto $ 2** $Id: liolib.c,v 2.65 2005/08/15 14:12:32 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -50,17 +50,17 @@ static void fileerror (lua_State *L, int arg, const char *filename) {
50} 50}
51 51
52 52
53static FILE **topfile (lua_State *L) { 53#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
54 FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
55 if (f == NULL) luaL_argerror(L, 1, "bad file");
56 return f;
57}
58 54
59 55
60static int io_type (lua_State *L) { 56static int io_type (lua_State *L) {
61 FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); 57 void *ud;
62 if (f == NULL) lua_pushnil(L); 58 luaL_checkany(L, 1);
63 else if (*f == NULL) 59 ud = lua_touserdata(L, 1);
60 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
61 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
62 lua_pushnil(L); /* not a file */
63 else if (*((FILE **)ud) == NULL)
64 lua_pushliteral(L, "closed file"); 64 lua_pushliteral(L, "closed file");
65 else 65 else
66 lua_pushliteral(L, "file"); 66 lua_pushliteral(L, "file");
@@ -173,7 +173,6 @@ static int io_tmpfile (lua_State *L) {
173static FILE *getiofile (lua_State *L, int findex) { 173static FILE *getiofile (lua_State *L, int findex) {
174 FILE *f; 174 FILE *f;
175 lua_rawgeti(L, LUA_ENVIRONINDEX, findex); 175 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
176 lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE));
177 f = *(FILE **)lua_touserdata(L, -1); 176 f = *(FILE **)lua_touserdata(L, -1);
178 if (f == NULL) 177 if (f == NULL)
179 luaL_error(L, "standard %s file is closed", fnames[findex - 1]); 178 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
@@ -194,7 +193,6 @@ static int g_iofile (lua_State *L, int f, const char *mode) {
194 tofile(L); /* check that it's a valid file handle */ 193 tofile(L); /* check that it's a valid file handle */
195 lua_pushvalue(L, 1); 194 lua_pushvalue(L, 1);
196 } 195 }
197 lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE));
198 lua_rawseti(L, LUA_ENVIRONINDEX, f); 196 lua_rawseti(L, LUA_ENVIRONINDEX, f);
199 } 197 }
200 /* return current value */ 198 /* return current value */
diff --git a/loadlib.c b/loadlib.c
index d50a587a..81d46172 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.37 2005/08/10 18:06:58 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.38 2005/08/15 14:12:32 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -289,10 +289,8 @@ static void **ll_register (lua_State *L, const char *path) {
289*/ 289*/
290static int gctm (lua_State *L) { 290static int gctm (lua_State *L) {
291 void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); 291 void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
292 if (lib) { 292 if (*lib) ll_unloadlib(*lib);
293 if (*lib) ll_unloadlib(*lib); 293 *lib = NULL; /* mark library as closed */
294 *lib = NULL; /* mark library as closed */
295 }
296 return 0; 294 return 0;
297} 295}
298 296