summaryrefslogtreecommitdiff
path: root/luajit.go
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2025-10-30 17:22:26 +0600
committerunwox <me@unwox.com>2025-10-30 17:23:04 +0600
commit8328e4d78455eaa3627455469c7517f6805c6da3 (patch)
tree96991c3f0fce5c0b7ebf6a3a6cfa20fac9780781 /luajit.go
parent06d94fe7f16e8985878a24332978731439599c4e (diff)
add luna.restart functionHEADmaster
Diffstat (limited to 'luajit.go')
-rw-r--r--luajit.go21
1 files changed, 15 insertions, 6 deletions
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.