Skip to content

Commit

Permalink
Added support of closing iterators if the implement io.Closer
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Mar 25, 2024
1 parent 39fa266 commit d0a5aac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}

if isForInLoop {
// pop the iterator
v.emitPop()
v.emitPopAndClose()
}

return nil
Expand Down Expand Up @@ -918,6 +918,10 @@ func (v *visitor) emitPop() {
v.emit(runtime.OpPop)
}

func (v *visitor) emitPopAndClose() {
v.emit(runtime.OpPopClose)
}

func (v *visitor) emit(op runtime.Opcode, args ...int) {
v.bytecode = append(v.bytecode, op)

Expand Down
3 changes: 2 additions & 1 deletion pkg/runtime/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ const (
OpCall3
OpCall4
OpCallN
OpPop
OpPush
OpPop
OpPopClose
OpJumpIfFalse
OpJumpIfTrue
OpJump
Expand Down
8 changes: 8 additions & 0 deletions pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/operators"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"io"
)

type VM struct {
Expand Down Expand Up @@ -58,6 +59,13 @@ loop:
case OpPop:
stack.Pop()

case OpPopClose:
closable, ok := stack.Pop().(io.Closer)

if ok {
closable.Close()
}

case OpStoreGlobal:
vm.globals[program.Constants[arg].String()] = stack.Pop()

Expand Down

0 comments on commit d0a5aac

Please sign in to comment.