added destructors to extlibs

also removed all generic stuff from extlibs cause it will probs never work
This commit is contained in:
2026-07-05 18:51:35 +10:00
parent a5e6e877ee
commit baada169d4
4 changed files with 75 additions and 77 deletions

View File

@@ -31,8 +31,6 @@ UseList(CometSerializedFunc);
UseList(cometFuncPtr);
UseList(StructField);
API_EXPORT CometStruct* cometGetExceptionStruct(CometEnvironment* env);
CometType cometCreateArrayType(CometType elem, uint8_t dimensions, bool isFixedSize[], uint64_t fixedSize[]);
API_EXPORT CometSerializedFunc cometSerializeFunction(
@@ -55,8 +53,6 @@ API_EXPORT char* cometArgString(int64_t argVal);
API_EXPORT uintptr_t cometArgPointer(int64_t argVal);
API_EXPORT int64_t cometSerializeString(char* cString);
API_EXPORT CometType cometGenericType(char* name);
API_EXPORT CometFunction* cometDefineFunc(
CometEnvironment* env,
char* name,
@@ -80,7 +76,6 @@ API_EXPORT StructField cometCreateField(char* name, CometType type, FieldAttribu
API_EXPORT void cometSetStructFieldsAndMethods(CometStruct* cometStruct, List(StructField) fields, List(cometFuncPtr) methods);
API_EXPORT CometStruct* cometDefineStruct(CometEnvironment* env, char* name, CometStruct* parent);
API_EXPORT CometStruct* cometDefineGenericStruct(CometEnvironment* env, char* name, CometStruct* parent, List(charptr) genericTypeNames);
API_EXPORT void cometDefineConstructor(
CometEnvironment* env,
@@ -89,6 +84,11 @@ API_EXPORT void cometDefineConstructor(
bool isVarArgs,
...
);
API_EXPORT void cometDefineDestructor(
CometEnvironment* env,
CometStruct* structType
);
API_EXPORT void cometSetField(CometObject* object, uint32_t index, int64_t value);
API_EXPORT CometSerializedStruct* cometVMGetStruct(CometVM* vm, char* structName);

View File

@@ -48,38 +48,6 @@ int64_t cometSerializeString(char* cString) {
return cometSerializeValue(cometArray);
}
int64_t Exception_INIT(int64_t* args, CometVM* vm) {
CometObject* exception = (CometObject*)args[0];
cometSetField(exception, 0, args[1]);
cometSetField(exception, 1, args[2]);
return (int64_t)exception;
}
CometStruct* cometGetExceptionStruct(CometEnvironment* env) {
if (exceptStruct != NULL)
return exceptStruct;
CometStruct* exceptStruct = cometDefineStruct(env, "Exception", NULL);
List(StructField) fields = newList(StructField);
StructField typeField = { .name = "type", .type = cometTypeString };
StructField msgField = { .name = "message", .type = cometTypeString };
List(cometFuncPtr) methods = newList(cometFuncPtr);
cometSetStructFieldsAndMethods(exceptStruct, fields, methods);
cometDefineConstructor(env, exceptStruct, 2, false, cometTypeString, cometTypeString);
return exceptStruct;
}
CometType cometGenericType(char* name) {
return (CometType){
.typeKind = COMET_GENERIC,
.genericParamName = strdup(name)
};
}
void cometSetStructFieldsAndMethods(CometStruct* cometStruct, List(StructField) fields, List(cometFuncPtr) methods) {
size_t fieldCount = cometStruct->parent == NULL ? fields.count : fields.count + cometStruct->parent->fieldCount;
size_t methodCount = cometStruct->parent == NULL ? methods.count : methods.count + cometStruct->parent->numMethods;
@@ -123,19 +91,6 @@ void cometSetStructFieldsAndMethods(CometStruct* cometStruct, List(StructField)
cometStruct->vtable = (CometMethod**)methodsArr;
}
CometStruct* cometDefineGenericStruct(CometEnvironment* env, char* name, CometStruct* parent, List(charptr) genericTypeNames) {
CometStruct* baseStruct = cometDefineStruct(env, name, parent);
for (size_t i = 0; i < genericTypeNames.count; i++) {
genericTypeNames.pointer[i] = strdup(genericTypeNames.pointer[i]);
}
baseStruct->genericTypeNames = genericTypeNames.pointer;
baseStruct->numGenericTypes = genericTypeNames.count;
return baseStruct;
}
CometStruct* cometDefineStruct(CometEnvironment* env, char* name, CometStruct* parent) {
CometStruct* newStruct = malloc(sizeof(CometStruct));
@@ -339,6 +294,49 @@ void cometDefineConstructor(
defineVar(env, constructorName, RECORD_LOCAL, funcVal, type, false);
}
void cometDefineDestructor(
CometEnvironment* env,
CometStruct* cometStruct
) {
CometType structType = {
.typeKind = COMET_STRUCT,
.structType = cometStruct
};
char* destructorName = malloc(32);
snprintf(destructorName, 32, "%s_DESTROY", cometStruct->name);
CometFunction* func = malloc(sizeof(CometFunction));
memcpy(func->name, destructorName, 32);
func->argCount = 1;
func->isMethod = false;
func->returnType = structType;
func->blockIdx = 0;
func->isExternal = true;
func->isVarArgs = false;
func->funcDef = NULL;
CometType type = {
.typeKind = COMET_FUNCTION,
.functionType = func
};
CometOperand funcVal = {
.type = CO_IMMEDIATE,
.imm = {
.typeKind = COMET_FUNCTION,
.bigVal = (int64_t)func
}
};
CometType* argTypes = malloc(sizeof(CometType));
*argTypes = structType;
func->argTypes = argTypes;
if (env)
defineVar(env, destructorName, RECORD_LOCAL, funcVal, type, false);
}
CometOperand cometValue(CometValueTypeKind valueType, ...) {
va_list args;
va_start(args, valueType);

View File

@@ -1,30 +1,8 @@
import io
import testlib
struct Foo {
int[*] numbers
func main() -> void {
testlib.Test x = new testlib.Test()
drop x
init() {
self.numbers = new int[100]
}
destroy {
drop self.numbers
}
}
struct Bar : Foo {
init() {
super(self)
}
destroy {
super(self)
}
}
func main() -> int {
Bar myStruct = new Bar()
drop myStruct
return 0
return
}

22
testlib.c Normal file
View File

@@ -0,0 +1,22 @@
#include <comet/cometlib.h>
#include <stdio.h>
ResultType(int64_t, objectPtr) impl_Test_INIT(int64_t* args, CometVM* vm) {
printf("constructor\n");
return Success(int64_t, objectPtr, args[0]);
}
ResultType(int64_t, objectPtr) impl_Test_DESTROY(int64_t* args, CometVM* vm) {
printf("destructor\n");
return Success(int64_t, objectPtr, args[0]);
}
on_import {
CometStruct* testStruct = cometDefineStruct(env, "Test", NULL);
List(StructField) fields = newList(StructField);
List(cometFuncPtr) methods = newList(cometFuncPtr);
cometDefineConstructor(env, testStruct, 0, false);
cometDefineDestructor(env, testStruct);
}