diff options
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 34 |
1 files changed, 17 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.56 2004/08/09 14:35:59 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.57 2004/08/13 19:52:13 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 | */ |
@@ -105,8 +105,8 @@ static int aux_close (lua_State *L) { | |||
105 | 105 | ||
106 | 106 | ||
107 | static int io_close (lua_State *L) { | 107 | static int io_close (lua_State *L) { |
108 | if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE) | 108 | if (lua_isnone(L, 1)) |
109 | lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT); | 109 | lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); |
110 | return pushresult(L, aux_close(L), NULL); | 110 | return pushresult(L, aux_close(L), NULL); |
111 | } | 111 | } |
112 | 112 | ||
@@ -147,7 +147,7 @@ static int io_tmpfile (lua_State *L) { | |||
147 | 147 | ||
148 | static FILE *getiofile (lua_State *L, int findex) { | 148 | static FILE *getiofile (lua_State *L, int findex) { |
149 | FILE *f; | 149 | FILE *f; |
150 | lua_rawgeti(L, lua_upvalueindex(1), findex); | 150 | lua_rawgeti(L, LUA_ENVIRONINDEX, findex); |
151 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); | 151 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); |
152 | f = *(FILE **)lua_touserdata(L, -1); | 152 | f = *(FILE **)lua_touserdata(L, -1); |
153 | if (f == NULL) | 153 | if (f == NULL) |
@@ -170,10 +170,10 @@ static int g_iofile (lua_State *L, int f, const char *mode) { | |||
170 | lua_pushvalue(L, 1); | 170 | lua_pushvalue(L, 1); |
171 | } | 171 | } |
172 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); | 172 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); |
173 | lua_rawseti(L, lua_upvalueindex(1), f); | 173 | lua_rawseti(L, LUA_ENVIRONINDEX, f); |
174 | } | 174 | } |
175 | /* return current value */ | 175 | /* return current value */ |
176 | lua_rawgeti(L, lua_upvalueindex(1), f); | 176 | lua_rawgeti(L, LUA_ENVIRONINDEX, f); |
177 | return 1; | 177 | return 1; |
178 | } | 178 | } |
179 | 179 | ||
@@ -192,10 +192,9 @@ static int io_readline (lua_State *L); | |||
192 | 192 | ||
193 | 193 | ||
194 | static void aux_lines (lua_State *L, int idx, int close) { | 194 | static void aux_lines (lua_State *L, int idx, int close) { |
195 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); | ||
196 | lua_pushvalue(L, idx); | 195 | lua_pushvalue(L, idx); |
197 | lua_pushboolean(L, close); /* close/not close file when finished */ | 196 | lua_pushboolean(L, close); /* close/not close file when finished */ |
198 | lua_pushcclosure(L, io_readline, 3); | 197 | lua_pushcclosure(L, io_readline, 2); |
199 | } | 198 | } |
200 | 199 | ||
201 | 200 | ||
@@ -209,7 +208,7 @@ static int f_lines (lua_State *L) { | |||
209 | static int io_lines (lua_State *L) { | 208 | static int io_lines (lua_State *L) { |
210 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ | 209 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ |
211 | /* will iterate over default input */ | 210 | /* will iterate over default input */ |
212 | lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); | 211 | lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); |
213 | return f_lines(L); | 212 | return f_lines(L); |
214 | } | 213 | } |
215 | else { | 214 | else { |
@@ -349,7 +348,7 @@ static int f_read (lua_State *L) { | |||
349 | 348 | ||
350 | 349 | ||
351 | static int io_readline (lua_State *L) { | 350 | static int io_readline (lua_State *L) { |
352 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2)); | 351 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); |
353 | int sucess; | 352 | int sucess; |
354 | if (f == NULL) /* file is already closed? */ | 353 | if (f == NULL) /* file is already closed? */ |
355 | luaL_error(L, "file is already closed"); | 354 | luaL_error(L, "file is already closed"); |
@@ -358,9 +357,9 @@ static int io_readline (lua_State *L) { | |||
358 | luaL_error(L, "%s", strerror(errno)); | 357 | luaL_error(L, "%s", strerror(errno)); |
359 | if (sucess) return 1; | 358 | if (sucess) return 1; |
360 | else { /* EOF */ | 359 | else { /* EOF */ |
361 | if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ | 360 | if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ |
362 | lua_settop(L, 0); | 361 | lua_settop(L, 0); |
363 | lua_pushvalue(L, lua_upvalueindex(2)); | 362 | lua_pushvalue(L, lua_upvalueindex(1)); |
364 | aux_close(L); /* close it */ | 363 | aux_close(L); /* close it */ |
365 | } | 364 | } |
366 | return 0; | 365 | return 0; |
@@ -489,12 +488,13 @@ LUALIB_API int luaopen_io (lua_State *L) { | |||
489 | createmeta(L); | 488 | createmeta(L); |
490 | createupval(L); | 489 | createupval(L); |
491 | lua_pushvalue(L, -1); | 490 | lua_pushvalue(L, -1); |
492 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); | 491 | lua_replace(L, LUA_ENVIRONINDEX); |
492 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 0); | ||
493 | /* put predefined file handles into `io' table */ | 493 | /* put predefined file handles into `io' table */ |
494 | lua_rawgeti(L, -2, IO_INPUT); /* get current input from metatable */ | 494 | lua_rawgeti(L, -2, IO_INPUT); /* get current input from upval */ |
495 | lua_setfield(L, -2, "stdin"); /* io.stdin = metatable[IO_INPUT] */ | 495 | lua_setfield(L, -2, "stdin"); /* io.stdin = upval[IO_INPUT] */ |
496 | lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from metatable */ | 496 | lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from upval */ |
497 | lua_setfield(L, -2, "stdout"); /* io.stdout = metatable[IO_OUTPUT] */ | 497 | lua_setfield(L, -2, "stdout"); /* io.stdout = upval[IO_OUTPUT] */ |
498 | *newfile(L) = stderr; | 498 | *newfile(L) = stderr; |
499 | lua_setfield(L, -2, "stderr"); /* io.stderr = newfile(stderr) */ | 499 | lua_setfield(L, -2, "stderr"); /* io.stderr = newfile(stderr) */ |
500 | return 1; | 500 | return 1; |