Destructor fixes

This commit is contained in:
2026-04-11 20:41:30 +10:00
parent 3427df2643
commit 2d02625e37
2 changed files with 14 additions and 5 deletions

View File

@@ -148,9 +148,16 @@ GroundVariable* findVariable(GroundVariable* head, const char *id) {
} }
void deleteVariable(GroundVariable** head, GroundVariable *item) { void deleteVariable(GroundVariable** head, GroundVariable *item) {
if (item == NULL) {
return;
}
if (strcmp(item->id, "self") == 0) { if (strcmp(item->id, "self") == 0) {
return; return;
} }
if (item->freed) {
return;
}
item->freed = true;
if (item->value.type == CUSTOM) { if (item->value.type == CUSTOM) {
GroundObjectField* destructor = findField(*item->value.data.customVal, "destructor"); GroundObjectField* destructor = findField(*item->value.data.customVal, "destructor");
if (destructor != NULL) { if (destructor != NULL) {
@@ -175,6 +182,7 @@ void deleteVariable(GroundVariable** head, GroundVariable *item) {
} }
} }
} }
freeGroundValue(&item->value);
HASH_DEL(*head, item); HASH_DEL(*head, item);
free(item); free(item);
} }
@@ -182,12 +190,12 @@ void deleteVariable(GroundVariable** head, GroundVariable *item) {
void addVariable(GroundVariable **head, const char *id, GroundValue data) { void addVariable(GroundVariable **head, const char *id, GroundValue data) {
GroundVariable* variable = findVariable(*head, id); GroundVariable* variable = findVariable(*head, id);
if (variable) { if (variable) {
freeGroundValue(&variable->value);
deleteVariable(head, variable); deleteVariable(head, variable);
} }
GroundVariable* item = malloc(sizeof(GroundVariable)); GroundVariable* item = malloc(sizeof(GroundVariable));
snprintf(item->id, MAX_ID_LEN, "%s", id); snprintf(item->id, MAX_ID_LEN, "%s", id);
item->value = copyGroundValue(&data); item->value = copyGroundValue(&data);
item->freed = false;
HASH_ADD_STR(*head, id, item); HASH_ADD_STR(*head, id, item);
} }
@@ -1904,7 +1912,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
} }
} else { } else {
if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) { if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
} }
} }
@@ -1934,7 +1942,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.args[i + 1].type != VALUE) { if (in->args.args[i + 1].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
} }
if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) { if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
} }
addVariable(newScope.variables, function->args[i].name, in->args.args[i + 1].value.value); addVariable(newScope.variables, function->args[i].name, in->args.args[i + 1].value.value);
@@ -2008,7 +2016,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
} }
} else { } else {
if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) { if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
} }
} }
@@ -2063,7 +2071,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
} }
//if (in->args.args[i + 1].value.value.type != function->args[i].type) { //if (in->args.args[i + 1].value.value.type != function->args[i].type) {
if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) { if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
} }
addVariable(newScope.variables, function->args[i].name, in->args.args[i + 2].value.value); addVariable(newScope.variables, function->args[i].name, in->args.args[i + 2].value.value);

View File

@@ -107,6 +107,7 @@ typedef struct GroundVariable {
char id[MAX_ID_LEN]; char id[MAX_ID_LEN];
GroundValue value; GroundValue value;
UT_hash_handle hh; UT_hash_handle hh;
bool freed;
} GroundVariable; } GroundVariable;
typedef struct GroundScope { typedef struct GroundScope {