finalize port objects when collected

This commit is contained in:
Yuichi Nishiwaki 2013-10-22 15:40:36 +09:00
parent d2e09fa2c7
commit 6245ec7e05
2 changed files with 9 additions and 1 deletions

View File

@ -17,7 +17,7 @@ struct pic_port {
PIC_OBJECT_HEADER
FILE *file;
short flags;
char status;
enum pic_port_status status;
};
#define pic_port_ptr(v) ((struct pic_port_t *)v.u.data)

View File

@ -5,6 +5,7 @@
#include "picrin/irep.h"
#include "picrin/proc.h"
#include "picrin/symbol.h"
#include "picrin/port.h"
#if GC_DEBUG
# include <stdio.h>
@ -265,6 +266,13 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
pic_free(pic, (void*)((struct pic_string *)obj)->str);
break;
}
case PIC_TT_PORT: {
struct pic_port *port = (struct pic_port *)obj;
if (port->status == PIC_OPEN) {
fclose(port->file);
}
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT: