summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2025-10-13 22:25:27 +0600
committerunwox <me@unwox.com>2025-10-13 22:25:27 +0600
commit06d94fe7f16e8985878a24332978731439599c4e (patch)
treebfecb440546167aa34575a1bed94c24f1bf59231
parentaadee458066c5d623ae2a16b8957f51418139fcd (diff)
fix error handling
-rw-r--r--lua.go4
-rw-r--r--luajit.go4
-rw-r--r--worker.go5
3 files changed, 8 insertions, 5 deletions
diff --git a/lua.go b/lua.go
index b6d7027..11eec3e 100644
--- a/lua.go
+++ b/lua.go
@@ -293,12 +293,14 @@ func (l *Lua) GetGlobal(name string) {
}
func (l *Lua) NewThread(yield func(), resume func() bool) *Lua {
- return &Lua{
+ newl := &Lua{
l: C.lua_newthread(l.l),
running: l.running,
resume: resume,
yield: yield,
}
+ newl.PushGoFunction(TracebackHandler)
+ return newl
}
// TracebackHandler handles stack trace formatting on errors.
diff --git a/luajit.go b/luajit.go
index 2ed3788..0b1fb55 100644
--- a/luajit.go
+++ b/luajit.go
@@ -290,12 +290,14 @@ func (l *Lua) GetGlobal(name string) {
}
func (l *Lua) NewThread(yield func(), resume func() bool) *Lua {
- return &Lua{
+ newl := &Lua{
l: C.lua_newthread(l.l),
running: l.running,
resume: resume,
yield: yield,
}
+ newl.PushGoFunction(TracebackHandler)
+ return newl
}
// TracebackHandler handles stack trace formatting on errors.
diff --git a/worker.go b/worker.go
index d252708..ea4892a 100644
--- a/worker.go
+++ b/worker.go
@@ -110,7 +110,6 @@ func (w *Worker) Start(argv []string, module map[string]any) error {
return err
}
w.lua.SetGlobal("fennel")
-
err = w.lua.LoadAndCall(`
debug.traceback = fennel.traceback
fennel.install()
@@ -342,12 +341,12 @@ func (w *Worker) Eval(code string) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.evalFn != nil {
+ // FIXME: does this branch pollute stack?
w.lua.PushFromRef(*w.evalFn)
w.lua.PushString(code)
- return w.lua.PCall(1, 0, 0)
+ return w.lua.PCall(1, 0, 1)
}
err := w.lua.LoadAndCall(code)
- w.lua.Pop(w.lua.StackLen())
return err
}