// // Created by Nils Polek on 23.01.24. // #ifndef RECORD #define RECORD #include "stackslot.c" #include "instruktion.c" ObjRef newRecord(int size){ ObjRef record; unsigned int objSize; objSize = sizeof(*record) + (size * sizeof(void *)); if((record = my_malloc(objSize)) == NULL){ perror("malloc"); } record->size = MSB; record->size = record->size + size; for(int i = 0; i < size; i++) { GET_REFS_PTR(record)[i] = NULL; } return record; } int getSize(ObjRef arr){ if(arr == NULL) return 0; return GET_ELEMENT_COUNT(arr); } ObjRef getField(ObjRef arr, int point){ if(arr == NULL) perror("Record is null"); if(0 > point || point > getSize(arr)){ printf("Index %i out of bounds for length %i\n",point, getSize(arr)); } return *(ObjRef *)GET_REFS_PTR(arr)[point]->data; } void setField(ObjRef arr, int point, ObjRef value){ bool isNull = false; if(value == NULL) isNull = true; if(0 > point || point >= getSize(arr)){ printf("Index %i out of bounds for length %i\n",point, getSize(arr)); exit(EXIT_FAILURE); } if(arr == NULL) perror("Record is null"); if(IS_PRIMITIVE(arr)) perror("Record is a primitive"); int size; if (isNull) size = sizeof(void *); else { if (IS_PRIMITIVE(value)) size = value->size; else size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *)); } if((GET_REFS_PTR(arr)[point] = my_malloc(size)) == NULL) perror("malloc"); GET_REFS_PTR(arr)[point] ->size = size; * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; } #endif