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);
}