mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
fix VM not clearing memory references when reused (#813)
This commit is contained in:
committed by
GitHub
parent
125b43e22f
commit
a23efbf4d0
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user