LuaJIT-to-Go/stack.go
2025-02-26 07:00:01 -06:00

48 lines
1010 B
Go

package luajit
/*
#include <lua.h>
#include <lauxlib.h>
*/
import "C"
import "fmt"
// LuaError represents an error from the Lua state
type LuaError struct {
Code int
Message string
}
func (e *LuaError) Error() string {
return fmt.Sprintf("lua error (code=%d): %s", e.Code, e.Message)
}
// Stack management constants from lua.h
const (
LUA_MINSTACK = 20 // Minimum Lua stack size
LUA_MAXSTACK = 1000000 // Maximum Lua stack size
LUA_REGISTRYINDEX = -10000 // Pseudo-index for the Lua registry
LUA_GLOBALSINDEX = -10002 // Pseudo-index for globals table
)
// GetStackTrace returns the current Lua stack trace
func (s *State) GetStackTrace() string {
s.GetGlobal("debug")
if !s.IsTable(-1) {
s.Pop(1)
return "debug table not available"
}
s.GetField(-1, "traceback")
if !s.IsFunction(-1) {
s.Pop(2) // Remove debug table and non-function
return "debug.traceback not available"
}
s.Call(0, 1)
trace := s.ToString(-1)
s.Pop(1) // Remove the trace
return trace
}