func luaPushError(l *lua.State, msg string) { l.NewTable() l.PushString("err") err := l.NewError(msg) l.PushString(err.Error()) l.SetTable(-3) }
func luaSetGlobalArray(l *lua.State, name string, ay [][]byte) { l.NewTable() for i := 0; i < len(ay); i++ { l.PushString(ledis.String(ay[i])) l.RawSeti(-2, i+1) } l.SetGlobal(name) }
func luaReturnSingleFieldTable(l *lua.State, filed string) int { if l.GetTop() != 1 || l.Type(-1) != lua.LUA_TSTRING { luaPushError(l, "wrong number or type of arguments") return 1 } l.NewTable() l.PushString(filed) l.PushValue(-3) l.SetTable(-3) return 1 }
func luaSha1Hex(l *lua.State) int { argc := l.GetTop() if argc != 1 { luaPushError(l, "wrong number of arguments") return 1 } s := l.ToString(1) s = hex.EncodeToString(ledis.Slice(s)) l.PushString(s) return 1 }
func luaReplyToLedisReply(l *lua.State) interface{} { base := l.GetTop() defer func() { l.SetTop(base - 1) }() switch l.Type(-1) { case lua.LUA_TSTRING: return ledis.Slice(l.ToString(-1)) case lua.LUA_TBOOLEAN: if l.ToBoolean(-1) { return int64(1) } else { return nil } case lua.LUA_TNUMBER: return int64(l.ToInteger(-1)) case lua.LUA_TTABLE: l.PushString("err") l.GetTable(-2) if l.Type(-1) == lua.LUA_TSTRING { return fmt.Errorf("%s", l.ToString(-1)) } l.Pop(1) l.PushString("ok") l.GetTable(-2) if l.Type(-1) == lua.LUA_TSTRING { return l.ToString(-1) } else { l.Pop(1) ay := make([]interface{}, 0) for i := 1; ; i++ { l.PushInteger(int64(i)) l.GetTable(-2) if l.Type(-1) == lua.LUA_TNIL { l.Pop(1) break } ay = append(ay, luaReplyToLedisReply(l)) } return ay } default: return nil } }