From 8328e4d78455eaa3627455469c7517f6805c6da3 Mon Sep 17 00:00:00 2001 From: unwox Date: Thu, 30 Oct 2025 17:22:26 +0600 Subject: add luna.restart function --- luajit.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'luajit.go') diff --git a/luajit.go b/luajit.go index 0b1fb55..4c88ccf 100644 --- a/luajit.go +++ b/luajit.go @@ -36,6 +36,7 @@ type Lua struct { running **Lua yield func() resume func() bool + deferred func() } //export luna_run_go_func @@ -46,19 +47,26 @@ func luna_run_go_func(f C.uintptr_t) C.int { } // Start opens the Lua context with all built-in libraries. -func (l *Lua) Start() { +func (l *Lua) Start() error { + if l.l != nil { + return errors.New("already started") + } l.l = C.luaL_newstate() l.running = &l + l.yield = nil + l.resume = nil C.luaL_openlibs(l.l) // Put traceback function at the start of the stack so it's always // possible to refer to it via stack index 1. l.PushGoFunction(TracebackHandler) + return nil } // Close closes the Lua context. func (l *Lua) Close() { C.lua_close(l.l) + l.l = nil } // PCall calls a function with the stack index (-nargs-1) expecting nresults @@ -69,8 +77,13 @@ func (l *Lua) PCall(nargs int, nresults int, errfunc int) error { if res != C.LUA_OK { errMsg := l.ToString(-1) l.Pop(1) + l.deferred = nil return errors.New(errMsg) } + if l.deferred != nil { + l.deferred() + l.deferred = nil + } return nil } @@ -268,11 +281,7 @@ func (l *Lua) LoadAndCall(code string) error { l.Pop(1) return errors.New(errMsg) } - err := l.PCall(0, C.LUA_MULTRET, 1) - if err != nil { - return err - } - return nil + return l.PCall(0, C.LUA_MULTRET, 1) } // SetGlobal sets a global value at the -1 stack index with the name. -- cgit v1.2.3