Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd_scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,20 @@ func (m *Miniredis) runLuaScript(c *server.Peer, sha, script string, args []stri
l.Push(lua.LString("redis"))
l.Call(1, 0)

// lua can call redis.setresp(...), but it's tmp state.
oldresp := c.Resp3
if err := doScript(l, script); err != nil {
c.WriteError(err.Error())
return false
}

luaToRedis(l, c, l.Get(1))
c.Resp3 = oldresp
c.SwitchResp3 = nil
return true
}

// doScript pre-compiiles the given script into a Lua prototype,
// doScript pre-compiles the given script into a Lua prototype,
// then executes the pre-compiled function against the given lua state.
//
// This is thread-safe.
Expand Down Expand Up @@ -259,7 +263,6 @@ func (m *Miniredis) cmdScript(c *server.Peer, cmd string, args []string) {
c.WriteError(msgScriptFlush)
return
}

default:
setDirty(c)
c.WriteError(fmt.Sprintf(msgFScriptUsageSimple, strings.ToUpper(opts.subcmd)))
Expand All @@ -276,7 +279,6 @@ func (m *Miniredis) cmdScript(c *server.Peer, cmd string, args []string) {
sha := sha1Hex(opts.script)
m.scripts[sha] = opts.script
c.WriteBulk(sha)

case "exists":
c.WriteLen(len(args))
for _, arg := range args {
Expand All @@ -286,11 +288,9 @@ func (m *Miniredis) cmdScript(c *server.Peer, cmd string, args []string) {
c.WriteInt(0)
}
}

case "flush":
m.scripts = map[string]string{}
c.WriteOK()

}
})
}
Expand Down
16 changes: 16 additions & 0 deletions integration/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ func TestScript(t *testing.T) {
c.Error("not allowed with BLOCK option", "EVAL", `redis.call("XREADGROUP", "GROUP", "group", "consumer", "BLOCK", 1000, "STREAMS", "pl", ">")`, "0")
})
})

t.Run("setresp", func(t *testing.T) {
testRaw(t, func(c *client) {
c.Do("EVAL", `redis.setresp(3); redis.call("SET", "foo", 12); return redis.call("GET", "foo")`, "0")
c.Do("SCRIPT", "LOAD", `redis.setresp(3)`)
c.Do("EVALSHA", "d204691e560b5b17f19626b50f84c2dcadff7ed5", "0")
c.Do("EVAL", `return redis.setresp(3)`, "0")
c.Do("EVAL", `return redis.setresp(2)`, "0")
c.Error("RESP version must be 2 or 3", "EVAL", `return redis.setresp(4)`, "0")
})
testRESP3(t, func(c *client) {
c.Do("SCRIPT", "LOAD", `redis.setresp(3)`)
c.Do("EVALSHA", "d204691e560b5b17f19626b50f84c2dcadff7ed5", "0")
c.Do("EVAL", `redis.setresp(3); redis.call("SET", "foo", 12); return redis.call("GET", "foo")`, "0")
})
})
}

func TestLua(t *testing.T) {
Expand Down
20 changes: 18 additions & 2 deletions lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func mkLua(srv *server.Server, c *server.Peer, sha string) (map[string]lua.LGFun
return 1
},
"replicate_commands": func(l *lua.LState) int {
// ignored
// always succeeds since 7.0.0
l.Push(lua.LTrue)
return 1
},
"set_repl": func(l *lua.LState) int {
Expand All @@ -170,6 +171,21 @@ func mkLua(srv *server.Server, c *server.Peer, sha string) (map[string]lua.LGFun
// ignored
return 1
},
"setresp": func(l *lua.LState) int {
level := l.CheckInt(1)
toresp3 := false
switch level {
case 2:
toresp3 = false
case 3:
toresp3 = true
default:
l.Error(lua.LString("RESP version must be 2 or 3"), 1)
return 0
}
c.SwitchResp3 = &toresp3
return 0
},
}, luaRedisConstants
}

Expand Down Expand Up @@ -226,7 +242,7 @@ func luaToRedis(l *lua.LState, c *server.Peer, value lua.LValue) {
luaToRedis(l, c, r)
}
default:
panic("....")
panic(fmt.Sprintf("wat: %T", t))
}
}

Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func (s *Server) Dispatch(c *Peer, args []string) {
s.infoCmds++
s.mu.Unlock()
cb(c, cmdUp, args)
if c.SwitchResp3 != nil {
c.Resp3 = *c.SwitchResp3
c.SwitchResp3 = nil
}
}

// TotalCommands is total (known) commands since this the server started
Expand Down Expand Up @@ -245,6 +249,7 @@ type Peer struct {
w *bufio.Writer
closed bool
Resp3 bool
SwitchResp3 *bool // we'll switch to this version _after_ the command
Ctx interface{} // anything goes, server won't touch this
onDisconnect []func() // list of callbacks
mu sync.Mutex // for Block()
Expand Down
Loading