Fixed stack overflow caused by assignments, read more

Variable declaration was also causing this issue.

It was caused by a value being left on the stack after these statements.
It wasn't a quick fix, as chained assignments depended on it. Now, the
assignment opcode has a configuration option, indicating if the last
value should be left on the stack or not.

This also means the benchmark in 'scripts/benchpress.toy' will no longer
cause a stack overflow.

Fixed #171
This commit is contained in:
2025-01-11 15:07:29 +11:00
parent ce03a342c9
commit 9141102f2e
2 changed files with 102 additions and 47 deletions

View File

@@ -227,7 +227,10 @@ static void processAssign(Toy_VM* vm) {
Toy_assignScope(vm->scope, TOY_VALUE_AS_STRING(name), value); //scope now owns the value, doesn't need to be freed
//in case of chaining, leave a copy on the stack
Toy_pushStack(&vm->stack, Toy_copyValue(value));
bool chainedAssignment = READ_BYTE(vm);
if (chainedAssignment) {
Toy_pushStack(&vm->stack, Toy_copyValue(value));
}
//cleanup
Toy_freeValue(name);
@@ -275,7 +278,10 @@ static void processAssignCompound(Toy_VM* vm) {
array->data[index] = Toy_copyValue(Toy_unwrapValue(value));
//in case of chaining, leave a copy on the stack
Toy_pushStack(&vm->stack, Toy_copyValue(value));
bool chainedAssignment = READ_BYTE(vm);
if (chainedAssignment) {
Toy_pushStack(&vm->stack, Toy_copyValue(value));
}
//cleanup
Toy_freeValue(value);
@@ -288,7 +294,10 @@ static void processAssignCompound(Toy_VM* vm) {
Toy_insertTable(&table, Toy_copyValue(Toy_unwrapValue(key)), Toy_copyValue(Toy_unwrapValue(value)));
//in case of chaining, leave a copy on the stack
Toy_pushStack(&vm->stack, Toy_copyValue(value));
bool chainedAssignment = READ_BYTE(vm);
if (chainedAssignment) {
Toy_pushStack(&vm->stack, Toy_copyValue(value));
}
//cleanup
Toy_freeValue(value);