use dictionary for rec->data

This commit is contained in:
Yuichi Nishiwaki 2015-01-18 21:40:14 +09:00
parent c86e97094d
commit 14a93ccf0f
3 changed files with 10 additions and 16 deletions

View File

@ -466,11 +466,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
} }
case PIC_TT_RECORD: { case PIC_TT_RECORD: {
struct pic_record *rec = (struct pic_record *)obj; struct pic_record *rec = (struct pic_record *)obj;
xh_entry *it;
for (it = xh_begin(&rec->hash); it != NULL; it = xh_next(it)) { gc_mark_object(pic, (struct pic_object *)rec->data);
gc_mark(pic, xh_val(it, pic_value));
}
break; break;
} }
case PIC_TT_NIL: case PIC_TT_NIL:
@ -662,8 +659,6 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
break; break;
} }
case PIC_TT_RECORD: { case PIC_TT_RECORD: {
struct pic_record *rec = (struct pic_record *)obj;
xh_destroy(&rec->hash);
break; break;
} }
case PIC_TT_NIL: case PIC_TT_NIL:

View File

@ -11,7 +11,7 @@ extern "C" {
struct pic_record { struct pic_record {
PIC_OBJECT_HEADER PIC_OBJECT_HEADER
xhash hash; struct pic_dict *data;
}; };
#define pic_record_p(v) (pic_type(v) == PIC_TT_RECORD) #define pic_record_p(v) (pic_type(v) == PIC_TT_RECORD)

View File

@ -4,14 +4,18 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/record.h" #include "picrin/record.h"
#include "picrin/dict.h"
struct pic_record * struct pic_record *
pic_make_record(pic_state *pic, pic_value rectype) pic_make_record(pic_state *pic, pic_value rectype)
{ {
struct pic_record *rec; struct pic_record *rec;
struct pic_dict *data;
data = pic_make_dict(pic);
rec = (struct pic_record *)pic_obj_alloc(pic, sizeof(struct pic_record), PIC_TT_RECORD); rec = (struct pic_record *)pic_obj_alloc(pic, sizeof(struct pic_record), PIC_TT_RECORD);
xh_init_int(&rec->hash, sizeof(pic_value)); rec->data = data;
pic_record_set(pic, rec, pic_intern_cstr(pic, "@@type"), rectype); pic_record_set(pic, rec, pic_intern_cstr(pic, "@@type"), rectype);
@ -27,21 +31,16 @@ pic_record_type(pic_state *pic, struct pic_record *rec)
pic_value pic_value
pic_record_ref(pic_state *pic, struct pic_record *rec, pic_sym slot) pic_record_ref(pic_state *pic, struct pic_record *rec, pic_sym slot)
{ {
xh_entry *e; if (! pic_dict_has(pic, rec->data, slot)) {
e = xh_get_int(&rec->hash, slot);
if (! e) {
pic_errorf(pic, "slot named ~s is not found for record: ~s", pic_sym_value(slot), rec); pic_errorf(pic, "slot named ~s is not found for record: ~s", pic_sym_value(slot), rec);
} }
return xh_val(e, pic_value); return pic_dict_ref(pic, rec->data, slot);
} }
void void
pic_record_set(pic_state *pic, struct pic_record *rec, pic_sym slot, pic_value val) pic_record_set(pic_state *pic, struct pic_record *rec, pic_sym slot, pic_value val)
{ {
PIC_UNUSED(pic); pic_dict_set(pic, rec->data, slot, val);
xh_put_int(&rec->hash, slot, &val);
} }
static pic_value static pic_value