diff --git a/include/ground.h b/include/ground.h index abbdd6d..24a1166 100644 --- a/include/ground.h +++ b/include/ground.h @@ -529,7 +529,7 @@ struct _Ground { struct GroundExecutionResult (*execute) (GroundBytecodeInstruction* instruction, GroundBytecodeHeap* heap); } Instruction; struct { - void (*set) (GroundBytecodeHeap* heap, GroundSize idx, GroundValue value); + void (*set) (GroundBytecodeHeap* heap, GroundSize idx, GroundBytecodeValue value); GroundValue* (*get) (GroundBytecodeHeap* heap, GroundSize idx); } Heap; } Bytecode; diff --git a/src/Bytecode/Instruction/execute.c b/src/Bytecode/Instruction/execute.c index 9c643be..f7c0113 100644 --- a/src/Bytecode/Instruction/execute.c +++ b/src/Bytecode/Instruction/execute.c @@ -82,18 +82,24 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns PRINT: { for (GroundSize i = 0; i < instruction->args.len; i++) { printValue(HEAP_GET(heap, instruction->args.at[i])); + printf(" "); } return CONTINUE; } PRINTLN: { for (GroundSize i = 0; i < instruction->args.len; i++) { printValue(HEAP_GET(heap, instruction->args.at[i])); + printf(" "); } printf("\n"); return CONTINUE; } SET: { - HEAP_SET(heap, instruction->args.at[0], *HEAP_GET(heap, instruction->args.at[1])); + if (instruction->args.at[0] != instruction->args.at[1]) { + GroundBytecodeValue copy = Ground.Copy.BytecodeValue(HEAP_GET(heap, instruction->args.at[1])); + Ground.Free.BytecodeValue(&heap->heap[instruction->args.at[0]]); + HEAP_SET(heap, instruction->args.at[0], copy); + } return CONTINUE; } GETTYPE: { @@ -179,6 +185,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns } sprintf(buf, "%s%s", left->as.String.cstr, right->as.String.cstr); *final = Ground.New.BytecodeValue(Ground.New.Value.String(Ground.New.String(buf))); + free(buf); break; } default: { @@ -666,7 +673,6 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns return CONTINUE; } TOSTRING: { - GroundBytecodeValue* final = HEAP_GET(heap, instruction->args.at[1]); GroundBytecodeValue* in = HEAP_GET(heap, instruction->args.at[0]); char buf[4096]; @@ -689,7 +695,6 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns break; } case GroundType_String: { - *in = Ground.New.BytecodeValue(Ground.New.Value.String(final->as.String)); return CONTINUE; } @@ -785,8 +790,8 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns // Now we run the thingy GroundBytecodeValue result = Ground.Bytecode.Program.execute(function->program.ground, &newHeap); - // And store the result - HEAP_SET(heap, instruction->args.at[instruction->args.len - 1], result); + // Deep copy the result before freeing the function's heap + GroundBytecodeValue resultCopy = Ground.Copy.BytecodeValue(&result); // Clean up the new heap for (GroundSize i = 0; i < newHeap.len; i++) { @@ -794,6 +799,9 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns } free(newHeap.heap); + // And store the result + HEAP_SET(heap, instruction->args.at[instruction->args.len - 1], resultCopy); + return CONTINUE; } CALLMETHOD: { diff --git a/src/Free/String.c b/src/Free/String.c index c537028..1094fb1 100644 --- a/src/Free/String.c +++ b/src/Free/String.c @@ -4,5 +4,6 @@ void _GroundFreeString(GroundString* in) { free(in->cstr); + in->cstr = NULL; in->len = 0; } diff --git a/src/New/Bytecode.c b/src/New/Bytecode.c index 470e9f3..09e4660 100644 --- a/src/New/Bytecode.c +++ b/src/New/Bytecode.c @@ -238,7 +238,7 @@ GroundBytecode _GroundNewBytecode(GroundProgram* program, GroundState* state) { if (Ground.Flags.error) return bytecode; // Allocate heap - bytecode.heap.heap = malloc(sizeof(GroundValue) * size); + bytecode.heap.heap = malloc(sizeof(GroundBytecodeValue) * size); if (bytecode.heap.heap == NULL) { Ground.Log.Error("malloc failed in Ground.New.Bytecode"); Ground.Flags.error = true; diff --git a/src/libmain.c b/src/libmain.c index f13241d..507045c 100644 --- a/src/libmain.c +++ b/src/libmain.c @@ -109,7 +109,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns void _GroundBytecodeSave(GroundBytecode* bytecode, const char* path); GroundBytecode _GroundBytecodeLoad(const char* path); -void _GroundBytecodeHeapSet(GroundBytecodeHeap* heap, GroundSize idx, GroundValue value); +void _GroundBytecodeHeapSet(GroundBytecodeHeap* heap, GroundSize idx, GroundBytecodeValue value); GroundValue* _GroundBytecodeHeapGet(GroundBytecodeHeap* heap, GroundSize idx);