From 7c79616cac7cf52b22cdf5f5f37f417e8309eda5 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Sat, 8 Feb 2025 09:57:59 -0600 Subject: [PATCH] Small update to DoString for stack discipline --- wrapper.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/wrapper.go b/wrapper.go index 453fc70..f4098c4 100644 --- a/wrapper.go +++ b/wrapper.go @@ -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 }) }