diff options
| author | unwox <me@unwox.com> | 2025-10-30 17:22:26 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2025-10-30 17:23:04 +0600 |
| commit | 8328e4d78455eaa3627455469c7517f6805c6da3 (patch) | |
| tree | 96991c3f0fce5c0b7ebf6a3a6cfa20fac9780781 /lua.go | |
| parent | 06d94fe7f16e8985878a24332978731439599c4e (diff) | |
Diffstat (limited to 'lua.go')
| -rw-r--r-- | lua.go | 21 |
1 files changed, 15 insertions, 6 deletions
@@ -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 @@ -71,8 +79,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 } @@ -271,11 +284,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. |
