summaryrefslogtreecommitdiff
path: root/lua.go
diff options
context:
space:
mode:
Diffstat (limited to 'lua.go')
-rw-r--r--lua.go21
1 files changed, 15 insertions, 6 deletions
diff --git a/lua.go b/lua.go
index 11eec3e..79b5ba3 100644
--- a/lua.go
+++ b/lua.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
@@ -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.