From 6245ec7e05eb19f8392ade98fddb68e561cb4140 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 22 Oct 2013 15:40:36 +0900 Subject: [PATCH] finalize port objects when collected --- include/picrin/port.h | 2 +- src/gc.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/picrin/port.h b/include/picrin/port.h index 914680f3..d1bbcd82 100644 --- a/include/picrin/port.h +++ b/include/picrin/port.h @@ -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) diff --git a/src/gc.c b/src/gc.c index 6a59179a..969c771b 100644 --- a/src/gc.c +++ b/src/gc.c @@ -5,6 +5,7 @@ #include "picrin/irep.h" #include "picrin/proc.h" #include "picrin/symbol.h" +#include "picrin/port.h" #if GC_DEBUG # include @@ -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: