aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c30
1 files changed, 9 insertions, 21 deletions
diff --git a/liolib.c b/liolib.c
index 0e58a942..b1fddebe 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.86 2000/10/02 20:10:55 roberto Exp roberto $ 2** $Id: liolib.c,v 1.87 2000/10/20 16:39:03 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*/
@@ -159,18 +159,9 @@ static int io_close (lua_State *L) {
159 159
160static int file_collect (lua_State *L) { 160static int file_collect (lua_State *L) {
161 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); 161 IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
162 lua_pop(L, 1); /* remove upvalue */ 162 FILE *f = getnonullfile(L, ctrl, 1);
163 if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) { 163 if (f != stdin && f != stdout && f != stderr)
164 /* collecting `ctrl' itself */ 164 CLOSEFILE(L, f);
165 lua_unref(L, ctrl->ref[INFILE]);
166 lua_unref(L, ctrl->ref[OUTFILE]);
167 free(ctrl);
168 }
169 else { /* collecting a file: Close it */
170 FILE *f = getnonullfile(L, ctrl, 1);
171 if (f != stdin && f != stdout && f != stderr)
172 CLOSEFILE(L, f);
173 }
174 return 0; 165 return 0;
175} 166}
176 167
@@ -690,14 +681,13 @@ static const struct luaL_reg iolibtag[] = {
690 681
691 682
692static void openwithcontrol (lua_State *L) { 683static void openwithcontrol (lua_State *L) {
693 IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl)); 684 IOCtrl *ctrl = (IOCtrl *)lua_newuserdata(L, sizeof(IOCtrl));
694 unsigned int i; 685 unsigned int i;
695 int ctrltag = lua_newtag(L);
696 ctrl->iotag = lua_newtag(L); 686 ctrl->iotag = lua_newtag(L);
697 ctrl->closedtag = lua_newtag(L); 687 ctrl->closedtag = lua_newtag(L);
698 for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { 688 for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
699 /* put `ctrl' as upvalue for these functions */ 689 /* put `ctrl' as upvalue for these functions */
700 lua_pushusertag(L, ctrl, ctrltag); 690 lua_pushvalue(L, -1);
701 lua_pushcclosure(L, iolibtag[i].func, 1); 691 lua_pushcclosure(L, iolibtag[i].func, 1);
702 lua_setglobal(L, iolibtag[i].name); 692 lua_setglobal(L, iolibtag[i].name);
703 } 693 }
@@ -712,12 +702,10 @@ static void openwithcontrol (lua_State *L) {
712 setfilebyname(L, ctrl, stdin, "_STDIN"); 702 setfilebyname(L, ctrl, stdin, "_STDIN");
713 setfilebyname(L, ctrl, stdout, "_STDOUT"); 703 setfilebyname(L, ctrl, stdout, "_STDOUT");
714 setfilebyname(L, ctrl, stderr, "_STDERR"); 704 setfilebyname(L, ctrl, stderr, "_STDERR");
715 /* delete `ctrl' when collected */
716 lua_pushusertag(L, ctrl, ctrltag);
717 lua_pushcclosure(L, file_collect, 1);
718 lua_settagmethod(L, ctrltag, "gc");
719 /* close files when collected */ 705 /* close files when collected */
720 lua_copytagmethods(L, ctrl->iotag, ctrltag); 706 lua_pushcclosure(L, file_collect, 1); /* pops `ctrl' from stack */
707 lua_settagmethod(L, ctrl->iotag, "gc");
708 lua_pop(L, 1); /* remove tag method returned by previous call */
721} 709}
722 710
723 711