diff options
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 20 |
1 files changed, 9 insertions, 11 deletions
@@ -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 | ||
53 | static 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 | ||
60 | static int io_type (lua_State *L) { | 56 | static 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) { | |||
173 | static FILE *getiofile (lua_State *L, int findex) { | 173 | static 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 */ |