diff options
Diffstat (limited to 'lua.go')
| -rw-r--r-- | lua.go | 49 |
1 files changed, 27 insertions, 22 deletions
@@ -11,6 +11,7 @@ import "C" import ( "errors" "unsafe" + "log" ) type LuaRef C.longlong @@ -18,22 +19,26 @@ type Lua struct { l *C.lua_State } -func (l *Lua) Start () { +func (l *Lua) Start() { l.l = C.luaL_newstate() C.luaL_openlibs(l.l) } -func (l *Lua) Close () { +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) (LuaRef, error) { if !l.LoadFile(file) { - return 0, errors.New("could not open the file") + errMsg := l.ToString(-1) + l.Pop(1) + return 0, errors.New("could not open the file:\n" + errMsg) } if !l.PCall(0, C.LUA_MULTRET) { - return 0, errors.New("could not execute the file") + 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") @@ -42,49 +47,49 @@ func (l *Lua) Require (file string) (LuaRef, error) { } -func (l *Lua) PCall (nargs int, nresults int) bool { +func (l *Lua) PCall(nargs int, nresults int) bool { res := C.lua_pcallk(l.l, C.int(nargs), C.int(nresults), 0, 0, nil) return res == C.LUA_OK } -func (l *Lua) LoadFile (file string) bool { +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) return res == C.LUA_OK } -func (l *Lua) StackLen () int { +func (l *Lua) StackLen() int { return int(C.lua_gettop(l.l)) } -func (l *Lua) ToString (index int) string { +func (l *Lua) ToString(index int) string { return C.GoString(C.lua_tolstring(l.l, C.int(index), nil)) } -func (l *Lua) ToInt (index int) int { +func (l *Lua) ToInt(index int) int { return int(C.lua_tonumberx(l.l, C.int(index), nil)) } -func (l *Lua) PopToRef () LuaRef { +func (l *Lua) PopToRef() LuaRef { return LuaRef(C.luaL_ref(l.l, C.LUA_REGISTRYINDEX)) } -func (l *Lua) Pop (n int) { +func (l *Lua) Pop(n int) { C.lua_settop(l.l, C.int(-n - 1)) } -func (l *Lua) PushNil () { +func (l *Lua) PushNil() { C.lua_pushnil(l.l) } -func (l *Lua) PushString (str string) { +func (l *Lua) PushString(str string) { cstr := C.CString(str) defer C.free(unsafe.Pointer(cstr)) C.lua_pushstring(l.l, cstr) } -func (l *Lua) PushTable (table map[string]string) { +func (l *Lua) PushTable(table map[string]string) { C.lua_createtable(l.l, 0, C.int(len(table))) for k, v := range table { l.PushString(v) @@ -92,31 +97,31 @@ func (l *Lua) PushTable (table map[string]string) { } } -func (l *Lua) PushFromRef (ref LuaRef) { +func (l *Lua) PushFromRef(ref LuaRef) { C.lua_rawgeti(l.l, C.LUA_REGISTRYINDEX, C.longlong(ref)); } -func (l *Lua) IsString (index int) bool { +func (l *Lua) IsString(index int) bool { return C.lua_isstring(l.l, C.int(index)) == 1 } -func (l *Lua) IsNumber (index int) bool { +func (l *Lua) IsNumber(index int) bool { return C.lua_isnumber(l.l, C.int(index)) == 1 } -func (l *Lua) IsFunction (index int) bool { +func (l *Lua) IsFunction(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TFUNCTION } -func (l *Lua) IsTable (index int) bool { +func (l *Lua) IsTable(index int) bool { return C.lua_type(l.l, C.int(index)) == C.LUA_TTABLE } -func (l *Lua) Next () bool { +func (l *Lua) Next() bool { return C.lua_next(l.l, -2) != 0 } -func (l *Lua) PushTableItem (key string) { +func (l *Lua) PushTableItem(key string) { ckey := C.CString(key) defer C.free(unsafe.Pointer(ckey)) C.lua_pushstring(l.l, ckey); |
