summaryrefslogtreecommitdiff
path: root/lua.go
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-07-23 23:19:46 +0600
committerunwox <me@unwox.com>2024-07-23 23:24:14 +0600
commitcba8ff3896a59598e9f0aa946fd547002ac23aba (patch)
tree33fa5edd8f9640bb00e28f75e6bb73910e80c529 /lua.go
parent701d73e4a47964ea40100ab2f329ef1dcc0a6f83 (diff)
use luna module instead of returning table for lua->go interaction
Diffstat (limited to 'lua.go')
-rw-r--r--lua.go57
1 files changed, 34 insertions, 23 deletions
diff --git a/lua.go b/lua.go
index 199f13a..e7eee54 100644
--- a/lua.go
+++ b/lua.go
@@ -21,7 +21,7 @@ static inline void luna_push_function(lua_State *l, uintptr_t f) {
import "C"
import (
"errors"
- "log"
+ "fmt"
"runtime/cgo"
"unsafe"
)
@@ -33,7 +33,6 @@ type Lua struct {
//export luna_run_go_func
func luna_run_go_func(f C.uintptr_t) C.int {
- log.Println(f)
fn := cgo.Handle(f).Value().(func() int)
return C.int(fn())
}
@@ -45,36 +44,36 @@ func (l *Lua) Start() {
func (l *Lua) Close() {
C.lua_close(l.l)
- C.free(unsafe.Pointer(l.l))
}
-func (l *Lua) Require(file string) (LuaRef, error) {
+func (l *Lua) Require(file string) error {
if !l.LoadFile(file) {
errMsg := l.ToString(-1)
l.Pop(1)
- return 0, errors.New("could not open the file:\n" + errMsg)
+ return errors.New("could not open the file:\n" + errMsg)
}
- if !l.PCall(0, C.LUA_MULTRET) {
- errMsg := l.ToString(-1)
- l.Pop(1)
- return 0, errors.New("could not execute the file:\n" + errMsg)
- }
- if !l.IsTable(-1) {
- return 0, errors.New("module did not return a table")
+ err := l.PCall(0, C.LUA_MULTRET)
+ if err != nil {
+ return errors.New("could not execute the file:\n" + err.Error())
}
- return l.PopToRef(), nil
+ return nil
}
-func (l *Lua) PCall(nargs int, nresults int) bool {
+func (l *Lua) PCall(nargs int, nresults int) error {
res := C.lua_pcallk(l.l, C.int(nargs), C.int(nresults), 0, 0, nil)
- return res == C.LUA_OK
+ if res != C.LUA_OK {
+ errMsg := l.ToString(-1)
+ l.Pop(1)
+ return errors.New(errMsg)
+ }
+ return nil
}
func (l *Lua) LoadFile(file string) bool {
- cfile := C.CString(file)
- defer C.free(unsafe.Pointer(cfile))
- res := C.luaL_loadfilex(l.l, cfile, nil)
+ cstr := C.CString(file)
+ defer C.free(unsafe.Pointer(cstr))
+ res := C.luaL_loadfilex(l.l, cstr, nil)
return res == C.LUA_OK
}
@@ -102,6 +101,10 @@ func (l *Lua) PushNil() {
C.lua_pushnil(l.l)
}
+func (l *Lua) PushNumber(num int) {
+ C.lua_pushnumber(l.l, C.double(num))
+}
+
func (l *Lua) PushString(str string) {
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
@@ -121,11 +124,13 @@ func (l *Lua) PushGoFunction(f func (l *Lua) int) cgo.Handle {
func (l *Lua) CreateTable(len int) {
C.lua_createtable(l.l, 0, C.int(len))
}
+
func (l *Lua) SetTableItem(key string) {
cstr := C.CString(key)
defer C.free(unsafe.Pointer(cstr))
C.lua_setfield(l.l, -2, C.CString(key))
}
+
func (l *Lua) PushStringTable(table map[string]string) {
l.CreateTable(len(table))
for k, v := range table {
@@ -173,10 +178,9 @@ func (l *Lua) LoadString(code string) error {
l.Pop(1)
return errors.New(errMsg)
}
- if !l.PCall(0, 0) {
- errMsg := l.ToString(-1)
- l.Pop(1)
- return errors.New(errMsg)
+ err := l.PCall(0, 0)
+ if err != nil {
+ return err
}
l.Pop(l.StackLen())
return nil
@@ -190,11 +194,12 @@ func (l *Lua) RestoreStackFunc() func () {
if diff == 0 {
return
} else if diff < 0 {
- log.Fatalf(
+ msg := fmt.Sprintf(
"too many stack pops: len before: %d, after: %d\n",
before,
after,
)
+ panic(msg)
}
C.lua_settop(l.l, C.int(before))
}
@@ -205,3 +210,9 @@ func (l *Lua) SetGlobal(name string) {
defer C.free(unsafe.Pointer(cstr))
C.lua_setglobal(l.l, cstr)
}
+
+func (l *Lua) GetGlobal(name string) {
+ cstr := C.CString(name)
+ defer C.free(unsafe.Pointer(cstr))
+ C.lua_getglobal(l.l, cstr)
+}