Tweaked stack allocation when inheriting VMs

This commit is contained in:
2026-04-26 10:14:19 +10:00
parent c9a34e2259
commit efc9fe1406
5 changed files with 23 additions and 20 deletions
+1 -1
View File
@@ -379,7 +379,7 @@ int repl(const char* filepath, bool verbose) {
} }
//free the memory, and leave the VM ready for the next loop //free the memory, and leave the VM ready for the next loop
Toy_resetVM(&vm, true); Toy_resetVM(&vm, true, true);
free(bytecode); free(bytecode);
printf("%s> ", prompt); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt
+3 -1
View File
@@ -3,8 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
Toy_Stack* Toy_allocateStack(void) { Toy_Stack* Toy_allocateStack(void) { //TODO: add initial size as parameter
Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); //URGENT: Swap to a bucket (4 instances) Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); //URGENT: Swap to a bucket (4 instances)
if (stack == NULL) { if (stack == NULL) {
@@ -42,6 +43,7 @@ void Toy_resetStack(Toy_Stack** stackHandle) {
//reset to the stack's default state //reset to the stack's default state
if ((*stackHandle)->capacity > TOY_STACK_INITIAL_CAPACITY) { if ((*stackHandle)->capacity > TOY_STACK_INITIAL_CAPACITY) {
(*stackHandle) = realloc((*stackHandle), TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); (*stackHandle) = realloc((*stackHandle), TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack));
memset((*stackHandle), 0, TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack));
(*stackHandle)->capacity = TOY_STACK_INITIAL_CAPACITY; (*stackHandle)->capacity = TOY_STACK_INITIAL_CAPACITY;
} }
+14 -12
View File
@@ -355,7 +355,7 @@ static void processInvoke(Toy_VM* vm) {
case TOY_FUNCTION_CUSTOM: { case TOY_FUNCTION_CUSTOM: {
//spin up a new sub-vm //spin up a new sub-vm
Toy_VM subVM; Toy_VM subVM;
Toy_inheritVM(&subVM, vm); Toy_inheritVM(vm, &subVM);
Toy_bindVM(&subVM, fn->bytecode.code, fn->bytecode.parentScope); Toy_bindVM(&subVM, fn->bytecode.code, fn->bytecode.parentScope);
//check args count //check args count
@@ -1070,7 +1070,7 @@ static unsigned int process(Toy_VM* vm) {
} }
//exposed functions //exposed functions
void Toy_resetVM(Toy_VM* vm, bool preserveScope) { void Toy_resetVM(Toy_VM* vm, bool preserveScope, bool preserveStack) {
vm->code = NULL; vm->code = NULL;
vm->jumpsCount = 0; vm->jumpsCount = 0;
@@ -1086,9 +1086,11 @@ void Toy_resetVM(Toy_VM* vm, bool preserveScope) {
vm->programCounter = 0; vm->programCounter = 0;
Toy_resetStack(&vm->stack); if (!preserveStack) {
Toy_resetStack(&vm->stack); //NOTE: has a realloc()
}
if (preserveScope == false) { if (!preserveScope) {
vm->scope = Toy_popScope(vm->scope); vm->scope = Toy_popScope(vm->scope);
} }
@@ -1103,18 +1105,18 @@ void Toy_initVM(Toy_VM* vm) {
vm->parentBucketHandle = NULL; vm->parentBucketHandle = NULL;
Toy_resetVM(vm, true); Toy_resetVM(vm, true, true);
} }
void Toy_inheritVM(Toy_VM* vm, Toy_VM* parent) { void Toy_inheritVM(Toy_VM* parentVM, Toy_VM* subVM) {
//inherent persistent memory //inherent persistent memory
vm->scope = NULL; subVM->scope = NULL;
vm->stack = Toy_allocateStack(); subVM->stack = Toy_allocateStack();
vm->memoryBucket = parent->memoryBucket; subVM->memoryBucket = parentVM->memoryBucket;
vm->parentBucketHandle = &parent->memoryBucket; //track this to update it later subVM->parentBucketHandle = &parentVM->memoryBucket; //track this to update it later
Toy_resetVM(vm, true); Toy_resetVM(subVM, true, true);
} }
void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, Toy_Scope* parentScope) { void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, Toy_Scope* parentScope) {
@@ -1163,7 +1165,7 @@ unsigned int Toy_runVM(Toy_VM* vm) {
} }
void Toy_freeVM(Toy_VM* vm) { void Toy_freeVM(Toy_VM* vm) {
Toy_resetVM(vm, false); Toy_resetVM(vm, false, true);
//clear the persistent memory //clear the persistent memory
Toy_freeStack(vm->stack); Toy_freeStack(vm->stack);
+2 -3
View File
@@ -42,10 +42,10 @@ typedef struct Toy_VM {
Toy_Bucket** parentBucketHandle; Toy_Bucket** parentBucketHandle;
} Toy_VM; } Toy_VM;
TOY_API void Toy_resetVM(Toy_VM* vm, bool preserveScope); TOY_API void Toy_resetVM(Toy_VM* vm, bool preserveScope, bool preserveStack);
TOY_API void Toy_initVM(Toy_VM* vm); //creates memory TOY_API void Toy_initVM(Toy_VM* vm); //creates memory
TOY_API void Toy_inheritVM(Toy_VM* vm, Toy_VM* parent); //inherits scope bucket TOY_API void Toy_inheritVM(Toy_VM* parentVM, Toy_VM* subVM); //inherits scope bucket
void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, Toy_Scope* parentScope); void Toy_bindVM(Toy_VM* vm, unsigned char* bytecode, Toy_Scope* parentScope);
TOY_API unsigned int Toy_runVM(Toy_VM* vm); TOY_API unsigned int Toy_runVM(Toy_VM* vm);
@@ -53,4 +53,3 @@ TOY_API void Toy_freeVM(Toy_VM* vm);
TOY_API Toy_Array* Toy_extractResultsFromVM(Toy_VM* parentVM, Toy_VM* subVM, unsigned int resultCount); TOY_API Toy_Array* Toy_extractResultsFromVM(Toy_VM* parentVM, Toy_VM* subVM, unsigned int resultCount);
//TODO: inject extra data (hook system for external libraries)
+3 -3
View File
@@ -740,7 +740,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
Toy_bindVM(&vm, bytecode1, NULL); Toy_bindVM(&vm, bytecode1, NULL);
Toy_runVM(&vm); Toy_runVM(&vm);
Toy_resetVM(&vm, true); Toy_resetVM(&vm, true, false);
if (callbackUtilReceived == NULL || strcmp(callbackUtilReceived, "Hello world!") != 0) { if (callbackUtilReceived == NULL || strcmp(callbackUtilReceived, "Hello world!") != 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected value '%s' found in VM reuse run 1\n" TOY_CC_RESET, callbackUtilReceived != NULL ? callbackUtilReceived : "NULL"); fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected value '%s' found in VM reuse run 1\n" TOY_CC_RESET, callbackUtilReceived != NULL ? callbackUtilReceived : "NULL");
@@ -760,7 +760,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
Toy_bindVM(&vm, bytecode2, NULL); //preserve during repeated calls Toy_bindVM(&vm, bytecode2, NULL); //preserve during repeated calls
Toy_runVM(&vm); Toy_runVM(&vm);
Toy_resetVM(&vm, true); Toy_resetVM(&vm, true, false);
if (callbackUtilReceived == NULL || strcmp(callbackUtilReceived, "Hello world!") != 0) { if (callbackUtilReceived == NULL || strcmp(callbackUtilReceived, "Hello world!") != 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected value '%s' found in VM reuse run 2\n" TOY_CC_RESET, callbackUtilReceived != NULL ? callbackUtilReceived : "NULL"); fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected value '%s' found in VM reuse run 2\n" TOY_CC_RESET, callbackUtilReceived != NULL ? callbackUtilReceived : "NULL");
@@ -780,7 +780,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
Toy_bindVM(&vm, bytecode3, NULL); //preserve during repeated calls Toy_bindVM(&vm, bytecode3, NULL); //preserve during repeated calls
Toy_runVM(&vm); Toy_runVM(&vm);
Toy_resetVM(&vm, true); Toy_resetVM(&vm, true, false);
if (callbackUtilReceived == NULL || strcmp(callbackUtilReceived, "Hello world!") != 0) { if (callbackUtilReceived == NULL || strcmp(callbackUtilReceived, "Hello world!") != 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected value '%s' found in VM reuse run 3\n" TOY_CC_RESET, callbackUtilReceived != NULL ? callbackUtilReceived : "NULL"); fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected value '%s' found in VM reuse run 3\n" TOY_CC_RESET, callbackUtilReceived != NULL ? callbackUtilReceived : "NULL");