Lesson 4 of 9 · 21m
Embedding Lua: Designing the Host-Script Boundary
The architectural decisions that matter before writing binding code: state ownership, capabilities, lifetime rules, error handling, and what scripts are allowed to know.
Most embedding failures are design failures. Teams expose the entire engine because it is convenient, then spend years trying to stabilize a scripting API that was never a boundary. Good embedding starts by deciding what scripts are for: configuration, content behavior, automation, or user extensions. Each purpose implies different authority.
Start with the trust model
Ask four questions:
- Who writes scripts — your team, modders, customers, or attackers?
- What host objects may scripts mutate?
- When do scripts run relative to the host loop?
- What must remain stable when the host version changes?
A game scripting API for internal designers can expose rich objects. A customer automation API cannot expose raw pointers, filesystem access, or unchecked process spawning. 'Lua is sandboxable' does not mean your embedding is sandboxed; the default interpreter includes enough standard library surface to ruin a machine.
One state or many?
A lua_State is an interpreter instance plus its stack and global environment. One shared state is simple and memory-efficient, but every script shares globals unless you deliberately isolate environments. Multiple states isolate failures and cleanup but duplicate memory and complicate communication.
Typical choices:
game engine: one state per world or mod environment
editor plugin: one state per plugin lifecycle
network proxy: one state per worker thread, never shared across threads
testing harness: fresh state per test caseNever use the same lua_State concurrently from multiple OS threads. Lua can be embedded in threaded programs, but each state needs a single owner or an external lock.
Capabilities beat broad APIs
Expose narrow nouns and verbs:
world.spawn("goblin", { x = 12, y = 8 })
world.after(1.5, function()
world.emit("gate_opened")
end)Do not expose the engine's mutable entity table wholesale. Every exposed function is an ABI contract, a security decision, and a documentation obligation. Bindings should validate arguments at the boundary and convert them into internal types immediately.
Error policy is part of the API
A script error can mean ignore, disable the script, fail the transaction, or crash the host. Choose deliberately. User-authored extensions usually get quarantined; internal content errors should fail fast in development and degrade safely in production.
Field notes
- Treat script globals as a public API surface. Version and document them like REST endpoints.
- Keep host pointers out of Lua values unless wrapped and lifetime-managed.
- Bindings should be boring: validate, convert, call, translate errors. Business logic belongs on one side of the boundary, not smeared across it.
- If scripts can register callbacks, define when those callbacks are removed. Dangling callbacks are the embedding equivalent of use-after-free.
Was this lesson helpful?