forked from ground/ground
fixed some issues with the collections lib
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user