Small update to DoString for stack discipline

This commit is contained in:
Sky Johnson 2025-02-08 09:57:59 -06:00
parent 146b0a51db
commit 7c79616cac

View File

@ -70,9 +70,24 @@ func (s *State) DoString(str string) error {
if s.safeStack {
return stackGuardErr(s, func() error {
return s.safeCall(func() C.int {
return C.do_string(s.L, cstr)
})
// Save the current stack size
initialTop := s.GetTop()
// Execute the string
status := C.do_string(s.L, cstr)
if status != 0 {
// In case of error, get error message from stack
errMsg := s.ToString(-1)
s.SetTop(initialTop) // Restore stack
return &LuaError{
Code: int(status),
Message: errMsg,
}
}
// Return values are now on the stack above initialTop
// We don't pop them as they may be needed by the caller
return nil
})
}