forked from ground/ground
added destructors to List and Hashmap in the collections lib
This commit is contained in:
@@ -477,12 +477,22 @@ GroundValue listStructConstructor(GroundScope* scope, List args) {
|
||||
return value;
|
||||
}
|
||||
|
||||
GroundValue destroyListStruct(GroundScope* scope, List args) {
|
||||
GroundVariable* ptrField = groundFindVariable(scope, "ptr");
|
||||
if (ptrField == NULL) {
|
||||
ERROR("A field called \"ptr\" was not found", "FieldNotFound");
|
||||
}
|
||||
|
||||
free((GroundValue*)ptrField->value.data.intVal);
|
||||
return groundCreateValue(NONE);
|
||||
}
|
||||
|
||||
void initLists(GroundScope* scope) {
|
||||
listStruct = groundCreateStruct();
|
||||
groundAddFieldToStruct(&listStruct, "size", groundCreateValue(INT, 0)); // number of elements
|
||||
groundAddFieldToStruct(&listStruct, "memSize", groundCreateValue(INT, sizeof(GroundValue) * STARTING_ELEMENTS)); // number of bytes allocated
|
||||
groundAddFieldToStruct(&listStruct, "capacity", groundCreateValue(INT, STARTING_ELEMENTS)); // number of elements that can fit in the currently allocated space
|
||||
groundAddFieldToStruct(&listStruct, "ptr", groundCreateValue(INT, 0)); // pointer to internal list struct
|
||||
groundAddFieldToStruct(&listStruct, "ptr", groundCreateValue(INT, 0)); // pointer to internal list struct
|
||||
|
||||
groundAddFunctionToStruct(&listStruct, "init", listStructConstructor, INT, 1, INT, "startingCapacity"); // init struct (ground constructor)
|
||||
groundAddFunctionToStruct(&listStruct, "append", appendToListStruct, INT, 1, ANY, "value"); // append item to end of list
|
||||
@@ -496,7 +506,9 @@ void initLists(GroundScope* scope) {
|
||||
groundAddFunctionToStruct(&listStruct, "reverse", reverseListStruct, INT, 0); // return list struct in reverse order
|
||||
groundAddFunctionToStruct(&listStruct, "find", findListStruct, INT, 1, ANY, "value"); // return index of value in list, if not found, returns -1
|
||||
groundAddFunctionToStruct(&listStruct, "reserve", reserveListStruct, BOOL, 1, INT, "amount"); // ensure list capacity >= amount. returns true if the list's capacity was expanded
|
||||
|
||||
|
||||
|
||||
groundAddFunctionToStruct(&listStruct, "destructor", destroyListStruct, ANY,0);
|
||||
groundAddNativeFunction(scope, "newList", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
||||
groundAddNativeFunction(scope, "List_SOLS_CONSTRUCTOR", listStructConstructor, CUSTOM, 1, INT, "startingCapacity");
|
||||
}
|
||||
Reference in New Issue
Block a user