diff --git a/vm/vm.go b/vm/vm.go index 3018619d..ed61d2f9 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -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 + } +}