Added valgrind to the CI, fixed tests

This exposed an issue with my dev environment, which I had to patch.

Fixed #153
This commit is contained in:
2024-11-17 18:49:40 +11:00
parent 2f9489d5fd
commit 7398898a61
12 changed files with 63 additions and 25 deletions

View File

@@ -33,9 +33,7 @@ void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount) {
}
//BUGFIX: the endpoint must be aligned to the word size, otherwise you'll get a bus error from moving pointers
if (amount % 4 != 0) {
amount += 4 - (amount % 4); //ceil
}
amount = (amount + 3) & ~3;
//if you try to allocate too much space
if ((*bucketHandle)->capacity < amount) {

View File

@@ -68,6 +68,8 @@ static void writeBytecodeBody(Toy_Bytecode* bc, Toy_Ast* ast) {
memcpy(bc->ptr + bc->count, module, len);
bc->count += len;
bc->moduleCount++;
free(module);
}
//exposed functions

View File

@@ -379,7 +379,8 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
} while (parser->previous.lexeme[o++] && i < parser->previous.length);
buffer[i] = '\0';
Toy_private_emitAstValue(bucketHandle, rootHandle, TOY_VALUE_FROM_STRING(Toy_createStringLength(bucketHandle, buffer, i - escapeCounter)));
unsigned int len = ((i - escapeCounter) + 3) & ~3;
Toy_private_emitAstValue(bucketHandle, rootHandle, TOY_VALUE_FROM_STRING(Toy_createStringLength(bucketHandle, buffer, len)));
return TOY_AST_FLAG_NONE;
}

View File

@@ -17,6 +17,10 @@ static void incrementRefCount(Toy_Scope* scope) {
static void decrementRefCount(Toy_Scope* scope) {
for (Toy_Scope* iter = scope; iter; iter = iter->next) {
iter->refCount--;
if (iter->refCount == 0 && iter->table != NULL) {
Toy_freeTable(iter->table);
iter->table = NULL;
}
}
}
@@ -64,12 +68,6 @@ Toy_Scope* Toy_popScope(Toy_Scope* scope) {
}
decrementRefCount(scope);
if (scope->refCount == 0) {
Toy_freeTable(scope->table);
scope->table = NULL;
}
return scope->next;
}
@@ -86,7 +84,7 @@ Toy_Scope* Toy_deepCopyScope(Toy_Bucket** bucketHandle, Toy_Scope* scope) {
//forcibly copy the contents
for (int i = 0; i < scope->table->capacity; i++) {
if (!TOY_VALUE_IS_NULL(scope->table->data[i].key)) {
Toy_insertTable(&newScope->table, scope->table->data[i].key, scope->table->data[i].value);
Toy_insertTable(&newScope->table, Toy_copyValue(scope->table->data[i].key), Toy_copyValue(scope->table->data[i].value));
}
}

View File

@@ -229,7 +229,13 @@ char* Toy_getStringRawBuffer(Toy_String* str) {
exit(-1);
}
char* buffer = malloc(str->length + 1);
//BUGFIX: Make sure it's aligned, and there's space for the null
int len = (str->length + 3) & ~3;
if (len == str->length) {
len += 4;
}
char* buffer = malloc(len);
deepCopyUtil(buffer, str);
buffer[str->length] = '\0';

View File

@@ -651,12 +651,17 @@ void Toy_bindVMToModule(Toy_VM* vm, unsigned char* module) {
vm->subsAddr = READ_UNSIGNED_INT(vm);
}
//allocate the stack, scope, and memory
vm->stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
vm->scopeBucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
vm->stack = Toy_allocateStack();
//allocate the stack, scope, and memory (skip if already in use)
if (vm->stringBucket == NULL) {
vm->stringBucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
}
if (vm->scopeBucket == NULL) {
vm->scopeBucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
}
if (vm->stack == NULL) {
vm->stack = Toy_allocateStack();
}
if (vm->scope == NULL) {
//only allocate a new top-level scope when needed, otherwise REPL will break
vm->scope = Toy_pushScope(&vm->scopeBucket, NULL);
}
}