Skip to content

Commit 4f267ac

Browse files
committed
tiny os.* module
1 parent ce80759 commit 4f267ac

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

cmd_scripting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func (m *Miniredis) runLuaScript(c *server.Peer, sha, script string, readOnly bo
9797
l.Push(mod)
9898
return 1
9999
}))
100+
l.RegisterModule("os", mkLuaOS())
100101

101102
_ = doScript(l, protectGlobals)
102103

cmd_scripting_test.go

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -169,40 +169,45 @@ func TestScript(t *testing.T) {
169169
"SCRIPT", "FOO",
170170
proto.Error("ERR unknown subcommand 'FOO'. Try SCRIPT HELP."),
171171
)
172-
}
173172

174-
func TestCJSON(t *testing.T) {
175-
_, c := runWithClient(t)
173+
t.Run("CJSON", func(t *testing.T) {
174+
mustDo(t, c,
175+
"EVAL", `return cjson.decode('{"id":"foo"}')['id']`, "0",
176+
proto.String("foo"),
177+
)
178+
mustDo(t, c,
179+
"EVAL", `return cjson.encode({foo=42})`, "0",
180+
proto.String(`{"foo":42}`),
181+
)
176182

177-
mustDo(t, c,
178-
"EVAL", `return cjson.decode('{"id":"foo"}')['id']`, "0",
179-
proto.String("foo"),
180-
)
181-
mustDo(t, c,
182-
"EVAL", `return cjson.encode({foo=42})`, "0",
183-
proto.String(`{"foo":42}`),
184-
)
183+
mustContain(t, c,
184+
"EVAL", `redis.encode()`, "0",
185+
"Error compiling script",
186+
)
187+
mustContain(t, c,
188+
"EVAL", `redis.encode("1", "2")`, "0",
189+
"Error compiling script",
190+
)
191+
mustContain(t, c,
192+
"EVAL", `redis.decode()`, "0",
193+
"Error compiling script",
194+
)
195+
mustContain(t, c,
196+
"EVAL", `redis.decode("{")`, "0",
197+
"Error compiling script",
198+
)
199+
mustContain(t, c,
200+
"EVAL", `redis.decode("1", "2")`, "0",
201+
"Error compiling script",
202+
)
203+
})
185204

186-
mustContain(t, c,
187-
"EVAL", `redis.encode()`, "0",
188-
"Error compiling script",
189-
)
190-
mustContain(t, c,
191-
"EVAL", `redis.encode("1", "2")`, "0",
192-
"Error compiling script",
193-
)
194-
mustContain(t, c,
195-
"EVAL", `redis.decode()`, "0",
196-
"Error compiling script",
197-
)
198-
mustContain(t, c,
199-
"EVAL", `redis.decode("{")`, "0",
200-
"Error compiling script",
201-
)
202-
mustContain(t, c,
203-
"EVAL", `redis.decode("1", "2")`, "0",
204-
"Error compiling script",
205-
)
205+
t.Run("os.", func(t *testing.T) {
206+
mustDo(t, c,
207+
"EVAL", `return os.clock()`, "0",
208+
proto.Int(42),
209+
)
210+
})
206211
}
207212

208213
func TestLog(t *testing.T) {

integration/script_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ func TestLua(t *testing.T) {
193193
testRaw(t, func(c *client) {
194194
// c.Do("EVAL", "print(1)", "0")
195195
c.Do("EVAL", `return string.format('%q', "pretty string")`, "0")
196-
c.Error("Script attempted to access nonexistent global variable", "EVAL", "os.clock()", "0")
197-
c.Error("Script attempted to access nonexistent global variable", "EVAL", "os.exit(42)", "0")
196+
c.Error("Script attempted to access nonexistent global variable", "EVAL", "foob.clock()", "0")
197+
c.DoLoosely("EVAL", "os.clock()", "0")
198+
c.Error("attempt to call", "EVAL", "os.exit(42)", "0")
198199
c.Do("EVAL", "return table.concat({1,2,3})", "0")
199200
c.Do("EVAL", "return math.abs(-42)", "0")
200201
c.Error("Script attempted to access nonexistent global variable", "EVAL", `return utf8.len("hello world")`, "0")

lua.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,14 @@ func luaStatusReply(msg string) *lua.LTable {
299299
tab.RawSetString("ok", lua.LString(msg))
300300
return tab
301301
}
302+
303+
// Our very minimal "os." lua lib.
304+
func mkLuaOS() map[string]lua.LGFunction {
305+
return map[string]lua.LGFunction{
306+
// > Returns an approximation of the amount in seconds of CPU time used by the program
307+
"clock": func(l *lua.LState) int {
308+
l.Push(lua.LNumber(42))
309+
return 1
310+
},
311+
}
312+
}

0 commit comments

Comments
 (0)