Skip to content

Commit

Permalink
Fix SetTop bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen committed Dec 14, 2024
1 parent ab4c769 commit c2476dc
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions internal/mylua/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,24 @@ func (l *State) Top() int {
// then the new elements are filled with nil.
// If idx is 0, then all stack elements are removed.
func (l *State) SetTop(idx int) {
l.init()
if idx == 0 {
l.setTop(l.frame().registerStart())
return
if isPseudo(idx) {
panic("invalid new top")
}
i, err := l.stackIndex(idx)
if err != nil {
panic(err)
l.init()
base := l.frame().registerStart()
var newTop int
if idx >= 0 {
newTop = base + idx
if newTop > cap(l.stack) {
panic("new top too large")
}
} else {
newTop = len(l.stack) + idx + 1
if newTop < base {
panic("invalid new top")
}
}
l.setTop(i + 1)
l.setTop(newTop)
}

func (l *State) setTop(i int) {
Expand Down

0 comments on commit c2476dc

Please sign in to comment.