fixed some issues with the collections lib

This commit is contained in:
2026-04-17 10:09:40 +10:00
parent 2c5c64671c
commit 54a0f6058a
2 changed files with 39 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
#include "hashmap.h"
#include <groundext.h>
#include <groundvm.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <uthash.h>
@@ -142,6 +143,26 @@ GroundValue destroyHashmapStruct(GroundScope* scope, List args) {
return groundCreateValue(NONE);
}
GroundValue duplicateHashmapStruct(GroundScope* scope, List args) {
GroundObject* self = args.values[0].data.customVal;
GroundValue newSelf = groundCreateValue(CUSTOM, &hashmapStruct);
HashmapItem* src, *dst = NULL, *item, *tmp;
HASH_ITER(hh, src, item, tmp) {
HashmapItem *copy = malloc(sizeof(HashmapItem));
*copy = *item;
HASH_ADD_KEYPTR(hh, dst, copy->key, strlen(copy->key), copy);
}
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
if (ptrField == NULL) {
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
}
ptrField->value.data.intVal = (int64_t)src;
return newSelf;
}
void initHashmaps(GroundScope* scope) {
hashmapStruct = groundCreateStruct();
@@ -154,6 +175,7 @@ void initHashmaps(GroundScope* scope) {
groundAddFunctionToStruct(&hashmapStruct, "removeIfPresent", hashmapStructRemoveIfPresent, BOOL, 1, STRING, "key"); // remove a key from the hashmap, returns true if the key was removed or false if it didn't exist
groundAddFunctionToStruct(&hashmapStruct, "destructor", destroyHashmapStruct, ANY, 0);
groundAddFunctionToStruct(&hashmapStruct, "duplicator", duplicateHashmapStruct, ANY, 1, CUSTOM);
groundAddNativeFunction(scope, "newHashmap", hashmapStructConstructor, CUSTOM, 0);
groundAddNativeFunction(scope, "Hashmap_SOLS_CONSTRUCTOR", hashmapStructConstructor, CUSTOM, 0);
}

View File

@@ -2,6 +2,7 @@
#include <groundext.h>
#include <groundvm.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -36,25 +37,27 @@ GroundValue appendToListStruct(GroundScope* scope, List args) {
ERROR("A field called \"capacity\" was not found", "FieldNotFound");
}
int64_t capacity = capacityField->value.data.intVal;
int64_t size = sizeField->value.data.intVal;
sizeField->value.data.intVal++;
if (size+1 > capacity) {
if (capacity == 0) capacity = 1;
else capacity *= 2;
if (capacity <= sizeField->value.data.intVal) {
capacity *= 2;
capacityField->value.data.intVal = capacity;
int64_t newSize = sizeof(GroundValue) * capacity;
memSizeField->value.data.intVal = newSize;
items = realloc(items, newSize);
if (items == NULL) {
GroundValue* newItems = realloc(items, newSize);
if (newItems == NULL) {
ERROR("Failed to allocate memory when increasing list size!", "AllocFail");
}
items = newItems;
ptrField->value.data.intVal = (int64_t)items;
}
items[sizeField->value.data.intVal - 1] = newValue;
items[size] = newValue;
sizeField->value.data.intVal = size + 1;
return groundCreateValue(INT, 0);
}
@@ -86,22 +89,6 @@ GroundValue listStructAt(GroundScope* scope, List args) {
return items[index];
}
GroundValue listStructToString(GroundScope* scope, List args) {
GroundVariable* sizeField = groundFindVariable(scope, "size");
if (sizeField == NULL) {
ERROR("A field called \"size\" was not found", "FieldNotFound");
}
int64_t size = sizeField->value.data.intVal;
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
if (ptrField == NULL) {
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
}
GroundValue* items = (GroundValue*)ptrField->value.data.intVal;
return groundCreateValue(STRING, "TODO");
}
GroundValue clearListStruct(GroundScope* scope, List args) {
// grab fields we need from struct
GroundVariable* sizeField = groundFindVariable(scope, "size");
@@ -489,13 +476,12 @@ GroundValue listStructConstructor(GroundScope* scope, List args) {
}
GroundValue destroyListStruct(GroundScope* scope, List args) {
printf("destroying\n");
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
if (ptrField == NULL) {
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
}
free((GroundValue*)ptrField->value.data.intVal);
//free((GroundValue*)ptrField->value.data.intVal);
return groundCreateValue(NONE);
}
@@ -515,16 +501,19 @@ GroundValue duplicateListStruct(GroundScope* scope, List args) {
GroundObjectField *newMemSizeField = groundFindField(*newSelf.data.customVal, "memSize");
GroundObjectField *newPtrField = groundFindField(*newSelf.data.customVal, "ptr");
newSizeField->value.data.intVal = sizeField->value.data.intVal;
newCapacityField->value.data.intVal = capacityField->value.data.intVal;
newMemSizeField->value.data.intVal = memSizeField->value.data.intVal;
groundAddValueToScope(scope, "size", sizeField->value);
groundAddValueToScope(scope, "capacity", capacityField->value);
groundAddValueToScope(scope, "memSize", memSizeField->value);
GroundValue* newPtr = calloc(newCapacityField->value.data.intVal, sizeof(GroundValue));
GroundValue* newPtr = calloc(capacityField->value.data.intVal, sizeof(GroundValue));
if (!newPtr) {
ERROR("Failed to allocate memory while calling copy constructor of List!", "AllocFail");
}
memcpy(newPtr, (GroundValue*)ptrField->value.data.intVal, newMemSizeField->value.data.intVal);
memcpy(newPtr, (GroundValue*)ptrField->value.data.intVal, sizeField->value.data.intVal * sizeof(GroundValue));
newPtrField->value.data.intVal = (int64_t)newPtr;
return newSelf;
@@ -554,5 +543,4 @@ void initLists(GroundScope* scope) {
groundAddFunctionToStruct(&listStruct, "duplicator", duplicateListStruct, CUSTOM, 1, CUSTOM, "self");
groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
groundAddNativeFunction(scope, "string_SOLS_AS", listStructToString, STRING, 0);
}