fix VM not clearing memory references when reused (#813)

This commit is contained in:
Diego Augusto Molina
2025-07-05 13:46:48 +02:00
committed by GitHub
parent 125b43e22f
commit a23efbf4d0
+9
View File
@@ -67,9 +67,11 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
if vm.Stack == nil {
vm.Stack = make([]any, 0, 2)
} else {
clearSlice(vm.Stack)
vm.Stack = vm.Stack[0:0]
}
if vm.Scopes != nil {
clearSlice(vm.Scopes)
vm.Scopes = vm.Scopes[0:0]
}
if len(vm.Variables) < program.variables {
@@ -614,3 +616,10 @@ func (vm *VM) Step() {
func (vm *VM) Position() chan int {
return vm.curr
}
func clearSlice[S ~[]E, E any](s S) {
var zero E
for i := range s {
s[i] = zero // clear mem, optimized by the compiler, in Go 1.21 the "clear" builtin can be used
}
}