Implemented heap-functionality for object-types

This commit is contained in:
vegowotenks 2025-01-12 23:30:57 +01:00
parent 43f0ed0250
commit ed5d627e3f
5 changed files with 68 additions and 44 deletions

View file

@ -1,4 +1,5 @@
#include "object-type.h"
#include "value.h"
int ObjectType_Create(ObjectType* self, StringView name, ObjectType* supertype, size_t attribute_count_guess, allocator_t* allocator)
{
@ -9,16 +10,12 @@ int ObjectType_Create(ObjectType* self, StringView name, ObjectType* supertype,
return hashmap_code;
}
self->name = name;
self->reference_count = 0;
supertype->reference_count++;
return EXIT_SUCCESS;
}
void ObjectType_Destroy(ObjectType* self)
{
self->supertype->reference_count--;
// TODO: Emit a warning when there are still references?
HashMap_Destroy(&self->attributes);
}
@ -48,3 +45,46 @@ size_t ObjectType_GetAttributeCount(ObjectType* self)
{
return HashMap_Size(&self->attributes);
}
int ObjectType_ContextualDestroy(void* arg, ObjectType* self)
{
(void) arg;
ObjectType_Destroy(self);
return EXIT_SUCCESS;
}
size_t ObjectType_SizeQuery(void* arg, ObjectType* self)
{
return sizeof(*self);
}
typedef struct AttributeVisitorContext_s {
TracingHeapVisit visit;
void* visit_context;
} AttributeVisitorContext;
static int _ObjectType_VisitAttribute(AttributeVisitorContext* context, char* name, size_t name_length, ObjectTypeAttribute* attribute)
{
if (attribute->type == VALUETYPE_OBJECT) {
return context->visit(context->visit_context, attribute->object_type);
}
return EXIT_SUCCESS;
}
int ObjectType_TraceReferences(void* context, ObjectType* type, TracingHeapVisit trace_callback, void* callback_context)
{
int trace_code = trace_callback(callback_context, type->supertype);
if (trace_code != EXIT_SUCCESS) {
return trace_code;
}
AttributeVisitorContext visitor_context = {
.visit = trace_callback,
.visit_context = callback_context,
};
HashMap_ForEach(&type->attributes, (HashMapVisitFunction) _ObjectType_VisitAttribute, &visitor_context);
return EXIT_SUCCESS;
}