diff --git a/lang/object.c b/lang/object.c index f1ff90a..07b1504 100644 --- a/lang/object.c +++ b/lang/object.c @@ -2,6 +2,7 @@ // Copyright (c) 2023 John Watts and the LuminaSensum contributors #include "object.h" +#include "error.h" #include "vm.h" #include #include @@ -74,3 +75,20 @@ } vm_abort_msg(state, "dispatch_call: no call found to dispatch"); } + +struct object_list *object_list_create(int length) { + size_t elem_size = sizeof(Object) * length; + size_t size = sizeof(struct object_list) + elem_size; + struct object_list *list = calloc(size, 1); + if (list == NULL) + abort_print("object_list_create: not enough memory"); + list->length = length; + for (int i = 0; i < length; ++i) + list->elems[i] = object_none(); + return list; +} + +void object_list_free(struct object_list **list) { + free(*list); + *list = NULL; +} diff --git a/lang/object.h b/lang/object.h index bf0a7b4..ebc16a2 100644 --- a/lang/object.h +++ b/lang/object.h @@ -39,4 +39,20 @@ // Calls a method on an object void dispatch_call(VmState state, Object obj, const char *name); +// List of objects +struct object_list { + int length; + Object elems[]; +}; + +// Creates a list of objects +// This doesn't hold any references to objects +// All objects are initialized to object_none +struct object_list *object_list_create(int length); + +// Frees a list of objects +// This doesn't drop any references to objects +// Sets list to NULL +void object_list_free(struct object_list **list); + #endif