Skip to content

Commit

Permalink
Add *State.Copy and *State.Replace methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen committed Dec 21, 2024
1 parent e3a74ad commit b898824
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion internal/mylua/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var errMissingArguments = errors.New("not enough elements in stack")
//
// If the msgHandler argument to a [State] method is 0,
// then errors are returned as a Go error value.
// (This is in contrast to the C Lua implementation which pushes an error object onto the stack.)
// (This is in contrast to the C Lua API which pushes an error object onto the stack.)
// Otherwise, msgHandler is the stack index of a message handler.
// (This index cannot be a pseudo-index.)
// In case of runtime errors, this handler will be called with the error object
Expand Down Expand Up @@ -326,6 +326,41 @@ func (l *State) Remove(idx int) {
l.Pop(1)
}

// Copy copies the element at index fromIdx into the valid index toIdx,
// replacing the value at that position.
// Values at other positions are not affected.
func (l *State) Copy(fromIdx, toIdx int) {
l.init()
v, _, err := l.valueByIndex(fromIdx)
if err != nil {
panic(err)
}

if i, isUpvalue := upvalueFromIndex(toIdx); isUpvalue {
fv := l.stack[l.frame().functionIndex]
f, ok := fv.(function)
if !ok {
panic(fmt.Errorf("internal error: call frame missing function (found %T)", fv))
}
*l.resolveUpvalue(f.upvaluesSlice()[i-1]) = v
return
}

i, err := l.stackIndex(toIdx)
if err != nil {
panic(err)
}
l.stack[i] = v
}

// Replace moves the top element into the given valid index without shifting any element
// (therefore replacing the value at that given index),
// and then pops the top element.
func (l *State) Replace(idx int) {
l.Copy(-1, idx)
l.Pop(1)
}

// CheckStack ensures that the stack has space for at least n extra elements,
// that is, that you can safely push up to n values into it.
// It returns false if it cannot fulfill the request,
Expand Down

0 comments on commit b898824

Please sign in to comment.