mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-05-06 08:50:10 +10:00
Tweaked stack allocation when inheriting VMs
This commit is contained in:
+3
-1
@@ -3,8 +3,9 @@
|
||||
|
||||
#include <stdio.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)
|
||||
|
||||
if (stack == NULL) {
|
||||
@@ -42,6 +43,7 @@ void Toy_resetStack(Toy_Stack** stackHandle) {
|
||||
//reset to the stack's default state
|
||||
if ((*stackHandle)->capacity > TOY_STACK_INITIAL_CAPACITY) {
|
||||
(*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;
|
||||
}
|
||||
|
||||
+14
-12
@@ -355,7 +355,7 @@ static void processInvoke(Toy_VM* vm) {
|
||||
case TOY_FUNCTION_CUSTOM: {
|
||||
//spin up a new sub-vm
|
||||
Toy_VM subVM;
|
||||
Toy_inheritVM(&subVM, vm);
|
||||
Toy_inheritVM(vm, &subVM);
|
||||
Toy_bindVM(&subVM, fn->bytecode.code, fn->bytecode.parentScope);
|
||||
|
||||
//check args count
|
||||
@@ -1070,7 +1070,7 @@ static unsigned int process(Toy_VM* vm) {
|
||||
}
|
||||
|
||||
//exposed functions
|
||||
void Toy_resetVM(Toy_VM* vm, bool preserveScope) {
|
||||
void Toy_resetVM(Toy_VM* vm, bool preserveScope, bool preserveStack) {
|
||||
vm->code = NULL;
|
||||
|
||||
vm->jumpsCount = 0;
|
||||
@@ -1086,9 +1086,11 @@ void Toy_resetVM(Toy_VM* vm, bool preserveScope) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1103,18 +1105,18 @@ void Toy_initVM(Toy_VM* vm) {
|
||||
|
||||
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
|
||||
vm->scope = NULL;
|
||||
vm->stack = Toy_allocateStack();
|
||||
vm->memoryBucket = parent->memoryBucket;
|
||||
subVM->scope = NULL;
|
||||
subVM->stack = Toy_allocateStack();
|
||||
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) {
|
||||
@@ -1163,7 +1165,7 @@ unsigned int Toy_runVM(Toy_VM* vm) {
|
||||
}
|
||||
|
||||
void Toy_freeVM(Toy_VM* vm) {
|
||||
Toy_resetVM(vm, false);
|
||||
Toy_resetVM(vm, false, true);
|
||||
|
||||
//clear the persistent memory
|
||||
Toy_freeStack(vm->stack);
|
||||
|
||||
+2
-3
@@ -42,10 +42,10 @@ typedef struct Toy_VM {
|
||||
Toy_Bucket** parentBucketHandle;
|
||||
} 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_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);
|
||||
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);
|
||||
|
||||
//TODO: inject extra data (hook system for external libraries)
|
||||
|
||||
Reference in New Issue
Block a user