Garbage-collect dictionaries properly

This commit is contained in:
Konstantin Nazarov 2024-08-03 02:54:53 +01:00
parent 737dc03054
commit 1f14624664
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 15 additions and 2 deletions

View file

@ -146,7 +146,7 @@ Result<PodObject*> Arena::gc_pair(PodPair* obj) {
}
Result<PodObject*> Arena::gc_array(PodArray* obj) {
auto nobj = TRY(alloc<PodArray>(obj->size));
auto nobj = TRY(alloc<PodArray>(sizeof(OffPtr<PodObject>) * obj->size));
nobj->header.tag = Tag::Array;
nobj->size = obj->size;
for (uint64_t i = 0; i < obj->size; i++) {
@ -165,5 +165,13 @@ Result<PodObject*> Arena::gc_bytearray(PodByteArray* obj) {
}
Result<PodObject*> Arena::gc_dict(PodDict* obj) {
return ErrorCode::NotImplemented;
auto nobj = TRY(alloc<PodDict>(obj->size * 2 * sizeof(OffPtr<PodObject>)));
nobj->header.tag = Tag::Dict;
nobj->size = obj->size;
for (uint64_t i = 0; i < obj->size * 2; i++) {
PodObject* val = obj->data[i].get(obj);
nobj->data[i] = OffPtr<PodObject>(nobj, TRY(gc_pod(val)));
}
return nobj;
// return ErrorCode::NotImplemented;
}

View file

@ -216,6 +216,7 @@ Result<Dict> Dict::insert(Arena& arena, Value& key, Value& value) {
}
auto pod = TRY(arena.alloc<PodDict>(2 * s * sizeof(OffPtr<PodObject>)));
pod->header.tag = Tag::Dict;
pod->size = s;
auto vpod = _value.get();

View file

@ -15,7 +15,11 @@ TEST_CASE(dict_insert) {
d = DIEX(d.insert(arena, 0, 5));
d = DIEX(d.insert(arena, 2, 6));
DIEX(arena.gc());
auto s = DIEX(write_one(arena, d));
DIEX(arena.gc());
ASSERT_EQUALS(s, "{0 5 1 3 2 6 3 3}");
}