[PR #1906] [CLOSED] Mods in Lua: horizon-mod-lua, a Lua 5.4 interpreter built into the mod #3855

Closed
opened 2026-11-03 04:12:48 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/1906
Author: @ludovic111
Created: 10/25/2026
Status: ❌ Closed

Base: main ← Head: feat/mods-lua


📝 Commits (10+)

  • 27de895 Mods in Lua: horizon-mod-lua, a Lua 5.4 interpreter built into the mod
  • 34f4d3b Merge remote-tracking branch 'origin/main' into feat/mods-lua
  • ad3ddeb Merge remote-tracking branch 'origin/main' into feat/mods-lua
  • ce1db1b Merge main into feat/mods-lua
  • 32f6079 Merge main into feat/mods-lua
  • 1f6e498 Merge main into feat/mods-lua
  • 7c5efff Merge main into feat/mods-lua
  • 6b7617c Merge remote-tracking branch 'origin/main' into feat/mods-lua
  • 9a7f955 Merge remote-tracking branch 'origin/main' into feat/mods-lua
  • 2e7283d Merge remote-tracking branch 'origin/main' into feat/mods-lua

📊 Changes

18 files changed (+1373 additions, -17 deletions)

View changed files

📝 Cargo.lock (+115 -2)
📝 Cargo.toml (+2 -0)
📝 crates/cargo-horizon-mod/src/main.rs (+54 -8)
➕ crates/cargo-horizon-mod/template/lua/Cargo.toml.in (+17 -0)
➕ crates/cargo-horizon-mod/template/lua/horizon-mod.toml (+12 -0)
➕ crates/cargo-horizon-mod/template/lua/main.lua (+12 -0)
➕ crates/cargo-horizon-mod/template/lua/src/lib.rs (+2 -0)
➕ crates/horizon-mod-lua/Cargo.toml (+29 -0)
➕ crates/horizon-mod-lua/src/lib.rs (+574 -0)
➕ crates/horizon-mod-lua/src/stdlib.rs (+337 -0)
➕ crates/horizon-mod-sdk/examples/coords-hud-lua/Cargo.toml (+16 -0)
➕ crates/horizon-mod-sdk/examples/coords-hud-lua/horizon-mod.toml (+8 -0)
➕ crates/horizon-mod-sdk/examples/coords-hud-lua/main.lua (+67 -0)
➕ crates/horizon-mod-sdk/examples/coords-hud-lua/src/lib.rs (+2 -0)
📝 crates/horizon-mods/tests/sandbox.rs (+61 -5)
📝 docs/api/mods.md (+2 -0)
📝 docs/api/stability.md (+1 -1)
📝 docs/mods.md (+62 -1)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

What

A mod's code can be Lua 5.4. The new crate horizon-mod-lua builds a Lua interpreter into the mod together with the script, so a Lua mod is an .hmod like any other. It runs in the same wasm sandbox and API (0.1), with the same capabilities, fuel, limits and publishing checks. The game doesn't change: it has no Lua in it and doesn't know a mod is written in Lua.

horizon.command("where")

function on_command(name, args)
  local p = horizon.game.player()
  if not p then error("Not in a world") end
  return string.format("%d, %d, %d in %s", math.floor(p.x), math.floor(p.y), math.floor(p.z), p.dimension)
end
  • Interpreter: piccolo 0.3.3, pure Rust, MIT, used as a dependency. It runs on wasm32-unknown-unknown with no WASI. The Rust side of a mod is one line, horizon_mod_lua::lua_mod!("main.lua");.
  • Shape: the script runs once at load and registers commands with horizon.command. Hooks are global functions (on_tick, on_frame, on_command, on_chat). error("text") answers in red. Capabilities are tables of horizon (game, chat, hud, storage, log), each behind a cargo feature, so a mod imports only what it lists and the manifest check still holds.
  • Standard library: piccolo has no string.format, string.rep, table.insert, table.remove or table.concat, so this adds them (format takes C's flags, width and precision). io and os aren't there, as in any mod.
  • No entropy in the sandbox: getrandom 0.2 and 0.3, which piccolo's hashing pulls in, get a custom backend. cargo horizon-mod build sets --cfg getrandom_backend="custom" for the wasm target.
  • Example coords-hud-lua: the Coordinates HUD example rewritten line for line in Lua. The sandbox test runs the same checks on both (panel, /where, /coords, "Not in a world" on the title screen).
  • cargo horizon-mod new --template lua: main.lua, manifest, a Cargo.toml with the capability features, the one-line src/lib.rs.
  • Docs: website page "Write it in Lua" (in the "Make a mod" group), docs/mods.md (the runtime table, "Lua mods"), the changelog, and docs/api/stability.md: script mods are experimental.

Numbers

Rust coords-hud Lua coords-hud-lua
.hmod size 25 KB 240 KB
one tick (new panel every tick), 2000 ticks in the sandbox, busy Mac mini 22–63 µs 54–68 µs

Both are far under a tick (50 ms) and the mod budget. A Lua mod that does heavy work every tick costs more than the same mod in Rust, and the page says so.

How tested

  • cargo test -p horizon-mod-lua: the registration, answers and errors, string.format against C's output, table.*.
  • cargo test -p horizon-mods --features runtime,pack --test sandbox: 13 pass. coords_hud_draws_and_answers runs over both mods, and coords_hud_tick_cost_in_rust_and_lua asserts < 1 ms a tick.
  • cargo test -p cargo-horizon-mod: the_lua_template_is_a_valid_mod makes a mod from the template, builds it and checks it.
  • Clippy -D warnings on the host with no features and with all features, and on wasm32-unknown-unknown for the example.
  • In game (fresh HORIZON_HOME / XDG folders, HORIZON_NO_AUDIO=1), the built coords_hud_lua.hmod as --dev-mod, commands typed in the chat box. In-frame and on the server thread, the panel draws (screenshots), /where answers with the position and /coords hides the panel: 8/8.
  • The website builds.

Self-check:

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at 406fbd779, 19 min 17 s)

Check Result
Scope ⚠️ 20 files, +1429 −19; 1236 lines of code: over 500, split it if it can ship in parts; mixes game, website
Cargo.lock ✅ changes with a Cargo.toml
Public tree ✅ (1 s)
Python tests ➖ no Python change
rustfmt ✅ (13 s)
clippy ✅ (1 min 54 s)
tests: workspace ✅ 2087 passed (8 min 51 s)
doc tests ✅ 1 passed (1 min 34 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ✅ (1 s)
launcher clippy ✅ (2 min 26 s)
launcher tests ✅ 126 passed (4 min 16 s)
Parity ➖ 331 affected, to run in CI (--quick)

(Docs only after that run: the stability row, the "experimental" line and the size, 240 KB.) Parity lists everything because Cargo.toml and Cargo.lock changed. No game crate changes: the new crates are guests and the SDK.

The size is over 500 lines: about 600 of them are the interpreter glue and the standard library, the rest are the example, the template, tests and docs. They don't work apart.

Label skip-release: SDK and docs. Nothing changes in the game.

Leftovers

  • Lua patterns (string.find, gsub, match) and string.byte / char aren't there yet.
  • math.random gets the same sequence every run, since the sandbox has no entropy. The game could give mods a seed.
  • Server mods (API 0.3, #1894) in Lua: the same macro for the horizon-server-mod world.
  • horizon-ctl's chat-log shows no lines on a server-thread window, client mods' lines included; messages shows them. That's older than this PR (a test-tool gap).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/1906 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/25/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/mods-lua` --- ### 📝 Commits (10+) - [`27de895`](https://github.com/horizon-rust/horizon/commit/27de895fdb7b61ef20ab884ded03c37ccda8af19) Mods in Lua: horizon-mod-lua, a Lua 5.4 interpreter built into the mod - [`34f4d3b`](https://github.com/horizon-rust/horizon/commit/34f4d3b451db05d87f20d596f3ea8b888403c37a) Merge remote-tracking branch 'origin/main' into feat/mods-lua - [`ad3ddeb`](https://github.com/horizon-rust/horizon/commit/ad3ddeb43f58dc06668e7ae09d2415809e6d76df) Merge remote-tracking branch 'origin/main' into feat/mods-lua - [`ce1db1b`](https://github.com/horizon-rust/horizon/commit/ce1db1b0c3c9221babb6233ae06f613ff999e458) Merge main into feat/mods-lua - [`32f6079`](https://github.com/horizon-rust/horizon/commit/32f6079134d8d14ed68348ccbe999aa5faf89db8) Merge main into feat/mods-lua - [`1f6e498`](https://github.com/horizon-rust/horizon/commit/1f6e498238ec4b9cdae6b54872cec03a421e02c5) Merge main into feat/mods-lua - [`7c5efff`](https://github.com/horizon-rust/horizon/commit/7c5efff30707fde7ebc9a1d40a274d1fa43c2743) Merge main into feat/mods-lua - [`6b7617c`](https://github.com/horizon-rust/horizon/commit/6b7617c2775e340c79d9a90797e37746ba547105) Merge remote-tracking branch 'origin/main' into feat/mods-lua - [`9a7f955`](https://github.com/horizon-rust/horizon/commit/9a7f955a77886564fa49cbb4a5d97e21b2f8a383) Merge remote-tracking branch 'origin/main' into feat/mods-lua - [`2e7283d`](https://github.com/horizon-rust/horizon/commit/2e7283d01c6f89ebe66cd6c9a5594fc19e400ee6) Merge remote-tracking branch 'origin/main' into feat/mods-lua ### 📊 Changes **18 files changed** (+1373 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `Cargo.lock` (+115 -2) 📝 `Cargo.toml` (+2 -0) 📝 `crates/cargo-horizon-mod/src/main.rs` (+54 -8) ➕ `crates/cargo-horizon-mod/template/lua/Cargo.toml.in` (+17 -0) ➕ `crates/cargo-horizon-mod/template/lua/horizon-mod.toml` (+12 -0) ➕ `crates/cargo-horizon-mod/template/lua/main.lua` (+12 -0) ➕ `crates/cargo-horizon-mod/template/lua/src/lib.rs` (+2 -0) ➕ `crates/horizon-mod-lua/Cargo.toml` (+29 -0) ➕ `crates/horizon-mod-lua/src/lib.rs` (+574 -0) ➕ `crates/horizon-mod-lua/src/stdlib.rs` (+337 -0) ➕ `crates/horizon-mod-sdk/examples/coords-hud-lua/Cargo.toml` (+16 -0) ➕ `crates/horizon-mod-sdk/examples/coords-hud-lua/horizon-mod.toml` (+8 -0) ➕ `crates/horizon-mod-sdk/examples/coords-hud-lua/main.lua` (+67 -0) ➕ `crates/horizon-mod-sdk/examples/coords-hud-lua/src/lib.rs` (+2 -0) 📝 `crates/horizon-mods/tests/sandbox.rs` (+61 -5) 📝 `docs/api/mods.md` (+2 -0) 📝 `docs/api/stability.md` (+1 -1) 📝 `docs/mods.md` (+62 -1) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What A mod's code can be **Lua 5.4**. The new crate `horizon-mod-lua` builds a Lua interpreter into the mod together with the script, so a Lua mod is an `.hmod` like any other. It runs in the same wasm sandbox and API (0.1), with the same capabilities, fuel, limits and publishing checks. The game doesn't change: it has no Lua in it and doesn't know a mod is written in Lua. ```lua horizon.command("where") function on_command(name, args) local p = horizon.game.player() if not p then error("Not in a world") end return string.format("%d, %d, %d in %s", math.floor(p.x), math.floor(p.y), math.floor(p.z), p.dimension) end ``` - **Interpreter:** [piccolo](https://github.com/kyren/piccolo) 0.3.3, pure Rust, MIT, used as a dependency. It runs on `wasm32-unknown-unknown` with no WASI. The Rust side of a mod is one line, `horizon_mod_lua::lua_mod!("main.lua");`. - **Shape:** the script runs once at load and registers commands with `horizon.command`. Hooks are global functions (`on_tick`, `on_frame`, `on_command`, `on_chat`). `error("text")` answers in red. Capabilities are tables of `horizon` (`game`, `chat`, `hud`, `storage`, `log`), each behind a cargo feature, so a mod imports only what it lists and the manifest check still holds. - **Standard library:** piccolo has no `string.format`, `string.rep`, `table.insert`, `table.remove` or `table.concat`, so this adds them (`format` takes C's flags, width and precision). `io` and `os` aren't there, as in any mod. - **No entropy in the sandbox:** getrandom 0.2 and 0.3, which piccolo's hashing pulls in, get a custom backend. `cargo horizon-mod build` sets `--cfg getrandom_backend="custom"` for the wasm target. - **Example `coords-hud-lua`:** the Coordinates HUD example rewritten line for line in Lua. The sandbox test runs the same checks on both (panel, `/where`, `/coords`, "Not in a world" on the title screen). - **`cargo horizon-mod new --template lua`:** `main.lua`, manifest, a `Cargo.toml` with the capability features, the one-line `src/lib.rs`. - **Docs:** website page "Write it in Lua" (in the "Make a mod" group), docs/mods.md (the runtime table, "Lua mods"), the changelog, and `docs/api/stability.md`: script mods are **experimental**. ## Numbers | | Rust `coords-hud` | Lua `coords-hud-lua` | | --- | --- | --- | | `.hmod` size | 25 KB | 240 KB | | one tick (new panel every tick), 2000 ticks in the sandbox, busy Mac mini | 22–63 µs | 54–68 µs | Both are far under a tick (50 ms) and the mod budget. A Lua mod that does heavy work every tick costs more than the same mod in Rust, and the page says so. ## How tested - `cargo test -p horizon-mod-lua`: the registration, answers and errors, `string.format` against C's output, `table.*`. - `cargo test -p horizon-mods --features runtime,pack --test sandbox`: 13 pass. `coords_hud_draws_and_answers` runs over both mods, and `coords_hud_tick_cost_in_rust_and_lua` asserts < 1 ms a tick. - `cargo test -p cargo-horizon-mod`: `the_lua_template_is_a_valid_mod` makes a mod from the template, builds it and checks it. - Clippy `-D warnings` on the host with no features and with all features, and on `wasm32-unknown-unknown` for the example. - In game (fresh `HORIZON_HOME` / XDG folders, `HORIZON_NO_AUDIO=1`), the built `coords_hud_lua.hmod` as `--dev-mod`, commands typed in the chat box. In-frame and on the server thread, the panel draws (screenshots), `/where` answers with the position and `/coords` hides the panel: 8/8. - The website builds. **Self-check:** **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 406fbd779, 19 min 17 s) | Check | Result | | --- | --- | | Scope | ⚠️ 20 files, +1429 −19; 1236 lines of code: over 500, split it if it can ship in parts; mixes game, website | | Cargo.lock | ✅ changes with a Cargo.toml | | Public tree | ✅ (1 s) | | Python tests | ➖ no Python change | | rustfmt | ✅ (13 s) | | clippy | ✅ (1 min 54 s) | | tests: workspace | ✅ 2087 passed (8 min 51 s) | | doc tests | ✅ 1 passed (1 min 34 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ✅ (1 s) | | launcher clippy | ✅ (2 min 26 s) | | launcher tests | ✅ 126 passed (4 min 16 s) | | Parity | ➖ 331 affected, to run in CI (--quick) | (Docs only after that run: the stability row, the "experimental" line and the size, 240 KB.) Parity lists everything because `Cargo.toml` and `Cargo.lock` changed. No game crate changes: the new crates are guests and the SDK. The size is over 500 lines: about 600 of them are the interpreter glue and the standard library, the rest are the example, the template, tests and docs. They don't work apart. Label `skip-release`: SDK and docs. Nothing changes in the game. ## Leftovers - Lua patterns (`string.find`, `gsub`, `match`) and `string.byte` / `char` aren't there yet. - `math.random` gets the same sequence every run, since the sandbox has no entropy. The game could give mods a seed. - Server mods (API 0.3, #1894) in Lua: the same macro for the `horizon-server-mod` world. - horizon-ctl's `chat-log` shows no lines on a server-thread window, client mods' lines included; `messages` shows them. That's older than this PR (a test-tool gap). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 04:12:48 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#3855
No description provided.