Skip to main content

Crate kevy_lua

Crate kevy_lua 

Source
Expand description

kevy-lua — Redis EVAL / EVALSHA / SCRIPT surface backed by luna-core.

kevy’s script-host layer. Thin “cement” crate (per the stone-cement-stone model) — it carries no algorithmic content, only the bridge between kevy-rt’s command dispatch path, kevy-resp’s wire codec, and luna-core’s sandboxed Vm.

Design lock-in:

  • Default Lua 5.1 — preserves the Redis Lua ecosystem (BullMQ, Redlock, rate limiters, anything copied from Redis docs).
  • Per-script dialect opt-in via #!lua version=N — scripts opt into 5.2 / 5.3 / 5.4 / 5.5 with a single shebang line. SHA1 cache key is the raw script bytes, so EVALSHA is version-aware for free.
  • VM per-shard, per-dialect, lazily spawned — first EVAL hitting a dialect on a shard constructs the VM; reused afterwards. Idle RSS scales with dialects actually used.
  • Atomic execution — entering EVAL pauses other dispatch on that shard until the script returns. Matches Redis semantics.

Bridge holds a per-dialect Vm pool (lazy-spawned); eval() runs the script under the sandbox and marshals the first returned Value into a RESP reply. Shebang parsing, SHA1 cache, EVALSHA, SCRIPT LOAD/EXISTS/FLUSH, and the redis.call host plumbing all live here.

let mut bridge = kevy_lua::Bridge::with_no_dispatch();
// KEYS and ARGV are bound per call; the reply is RESP bytes.
let reply = bridge.eval(b"return #KEYS + tonumber(ARGV[1])", &[b"k"], &[b"41"]);
assert_eq!(reply, b":42\r\n");
// EVAL caches the script, so EVALSHA finds it by digest.
let sha = kevy_lua::sha1::sha1(b"return 'hi'");
bridge.eval(b"return 'hi'", &[], &[]);
assert_eq!(bridge.script_exists(&[sha]), vec![true]);

Modules§

sha1
SHA-1 digest helpers. Exposed because the operator-side wire layer (kevy-rt’s SCRIPT LOAD / EVALSHA codec) needs to convert between the 20-byte digest used as a cache key and the 40-char ASCII hex Redis uses on the wire. Hand-rolled SHA-1 (RFC 3174 / FIPS 180-1).

Structs§

Bridge
kevy-lua per-shard bridge. One Bridge lives in each shard’s runtime; it owns the per-dialect VM pool, the SHA1 cache, and the kevy-side dispatch callback that redis.call invokes.

Enums§

FlushMode
SCRIPT FLUSH mode (Redis 6.2+ semantics). The default is FlushMode::Sync, what SCRIPT FLUSH without an argument means.
LuaVersion
Re-export so callers can name the dialect without depending on luna-core directly. Lua dialect the VM emulates. Drives lexer, parser, and runtime feature gating. Lua55 is the primary; the others are compat modes; MacroLua (v1.3 Phase ML) is an additive dialect built on top of the 5.4 base.

Type Aliases§

Reply
A wire-level reply: just the encoded RESP bytes.
ScriptSha1
A SHA1 hash of a script’s source bytes. Used as the EVALSHA cache key. Includes any #!lua version=N shebang in the input, so a 5.1 script and the same script with a 5.3 shebang have distinct SHA1s and never collide in the cache.