Skip to content

Latest commit

 

History

History
1071 lines (855 loc) · 38.3 KB

embedding.md

File metadata and controls

1071 lines (855 loc) · 38.3 KB

Embedding

Cookbook for hosting luna inside a Rust program. Current as of v3.1.0. Companion docs: architecture.md (crate layout + JIT pipeline), threading.md (async + multi-thread patterns), compatibility.md (per-dialect feature matrix), performance.md (benchmark numbers).

Each section is independently usable — start at "Hello, world" and jump around.


1. Install

luna ships as a Cargo workspace with two crates. Pick based on whether you need the Cranelift JIT:

# Most embedders — full interpreter + Cranelift JIT + capi.
[dependencies]
luna-jit = "3"
# Minimum dep surface — pure interpreter, no Cranelift.
# Embedders running on wasm32, in audited supply chains, or shipping
# small static binaries pick this path.
[dependencies]
luna-core = "3"

luna-core has zero third-party dependencies (cargo tree -p luna-core shows exactly one crate — luna-core itself). The full luna crate adds 6 Cranelift crates and their transitive deps.

The CLI binary lives in luna:

cargo install luna-jit  # installs the `luna` REPL + script runner

The default install keeps the luna binary minimal: lua.c's REPL reading plain lines from stdin, no extra deps beyond Cranelift. For an interactive editor with tab completion against your Vm globals and Lua syntax highlighting, opt into the repl-line-editor feature (pulls rustyline and friends; luna-core stays 0-dep regardless):

cargo install luna-jit --features repl-line-editor

2. Hello, world

use luna_jit::vm::Vm;
use luna_jit::version::LuaVersion;

fn main() {
    let mut vm = Vm::new(LuaVersion::Lua55);  // 5.5 + full stdlib + JIT on
    let result = vm.eval("return 'hello, ' .. 'world'").unwrap();
    let s: String = result[0].try_as_str().unwrap().to_string();
    println!("{s}");
}

Vm::new(version) opens every safe-by-default stdlib library and installs the Cranelift JIT (when using the luna crate; luna-core uses the no-op JIT backend). For finer control see §3.


3. Sandbox setup

Embedders running untrusted scripts use the sandbox builder. It opens only the libraries you whitelist, sets the budget and memory cap, and rejects precompiled bytecode (which bypasses the parser's safety gates):

use luna_jit::Lua;  // or `luna_jit::vm::Vm` for the low-level handle
use luna_jit::version::LuaVersion;

let mut lua = Lua::sandbox(LuaVersion::Lua54)
    .open_base()
    .open_math()
    .open_string()
    .open_table()
    .with_instr_budget(1_000_000)   // ~10 ms wall-clock budget
    .with_memory_cap(8 * 1024 * 1024)  // 8 MiB cap
    .build();

// Now run anything you want — the script can't `require`, touch the
// filesystem, or compile bytecode chunks.
let r: i64 = lua.eval("return 1 + 2").unwrap();

The builder omits io, os, debug, package intentionally. Trusted hosts that need them call vm.open_os_io() (io.* + os.* together) / vm.open_debug() / vm.open_package() on the built Vm. See compatibility.md for the per-library feature matrix and security.md for the threat model covering each opt-in.

Bytecode loading is off by default in the builder. Enable it with .allow_bytecode_loading() only for fully trusted input.

Limiting and watching a Vm's memory

Every allocation a Vm makes goes through its allocation context (luna_core::runtime::mem). Give it a MemoryPolicy to see each allocation and refuse it: a refused allocation is a Lua memory error ("not enough memory", which pcall catches) instead of the end of the process, and a collection is tried first, as PUC does.

use luna_core::runtime::mem::{MemOwner, MemoryLimit};
use luna_core::version::LuaVersion;
use luna_core::vm::Vm;

let mut vm = Vm::new_with_mem(LuaVersion::Lua54, MemOwner::policy(Box::new(MemoryLimit(8 << 20))));
let r = vm.eval("local t = {} for i = 1, 1e9 do t[i] = i end");
assert!(r.is_err());                  // not enough memory
println!("{:?}", vm.memory_in_use());  // bytes the Vm holds now

A host that supplies the memory itself passes a lua_Alloc-style function to the unsafe MemOwner::raw. Without either, a Vm uses the system allocator, counts nothing, and pays nothing for the context. The constructors that do not start with try_ (Vm::new, Heap::new_table, Vm::create_userdata, ...) end the process when memory runs out, as the standard library's containers do.

Exhausted limits stay exhausted

When the instruction budget runs out or the memory cap is exceeded the call fails with "instruction budget exceeded" / "memory cap exceeded" (vm.error_kind() is InstrBudget / MemoryCap), and the limit stays at zero: until the host calls set_instr_budget / set_memory_cap again, every instruction raises the same error — the script after a pcall that caught it, an xpcall handler, a __close or __gc handler, a metamethod, a library callback, a coroutine — so the script cannot catch the error and keep running, and the finalizers run when the Vm is dropped cannot either. This is the Redis SCRIPT KILL model: a request that ran out of budget is over, and the host decides what runs next.

vm.set_instr_budget(Some(100_000));
let err = vm.eval("pcall(function() while true do end end) while true do end")
    .unwrap_err();                       // the loop after pcall never ran
assert_eq!(vm.error_kind(), LuaErrorKind::InstrBudget);
assert_eq!(vm.instr_budget_remaining(), Some(0));

vm.set_instr_budget(Some(100_000));     // the next request starts afresh
let r: Vec<Value> = vm.eval("return 1 + 2").unwrap();

While a budget or a cap is armed no compiled code runs: loops the JIT would compile stay in the interpreter, where they are counted, so the JIT does not need to be disabled for sandboxed scripts.

Freed blocks the Vm keeps

A Vm on the system allocator (no lua_Alloc from lua_newstate, no MemoryPolicy) keeps some of the blocks of 1 KiB to 256 KiB it frees, to hand them back to its next allocations of the same size without going through the allocator: with a second thread in the process (the LLVM backend's compile thread, or the host's own) glibc's allocator takes a lock for each of those. It keeps at most what it allocated of those sizes during the last collection cycle, at most the larger of 1 MiB and an eighth of its live heap, and never more than 8 MiB; the collector trims the pool at the end of every cycle, and the Vm gives it all back when it is dropped. The kept blocks count as freed: collectgarbage("count") and the collector's pace do not see them. A Vm with a host allocation function or a memory policy keeps nothing, so the host sees every allocation and free.

4. Setting globals

Use vm.set_global(name, value) for any [IntoValue] type — integers, floats, booleans, strings, table/function/userdata handles, Option<T>, or Value itself:

vm.set_global("answer", 42_i64)?;
vm.set_global("pi", 3.14_f64)?;
vm.set_global("name", "luna")?;
vm.set_global("ready", true)?;
vm.set_global("missing", Option::<i64>::None)?;  // sets to nil

IntoValue is implemented for the common Rust primitive types plus the GC handle types (Gc<Table>, Gc<LuaClosure>, Gc<NativeClosure>). Embedders adding their own types implement IntoValue directly:

use luna_jit::vm::{IntoValue, Vm};
use luna_jit::runtime::Value;

struct UserId(u64);

impl IntoValue for UserId {
    fn into_value(self, _vm: &mut Vm) -> Value {
        Value::Int(self.0 as i64)
    }
}

// Now:
vm.set_global("uid", UserId(1234))?;

5. Tables

The dogfood §4.1 friction point was building tables. v1.1 ships two ergonomic paths:

use luna_jit::runtime::Value;

// One-shot, fixed-size:
let t = vm.table_of([
    ("answer", 42_i64),
    ("year", 2026_i64),
    ("name", "luna"),  // mixed value types — IntoValue covers them
]);
vm.set_global("config", Value::Table(t))?;

// Multi-step builder for variable shapes:
let t = vm.new_table()
    .with("name", "luna")
    .with("major", 1_i64)
    .with("minor", 1_i64)
    .with(1_i64, "first array entry")  // integer keys + mixed values OK
    .with(2_i64, "second array entry")
    .build();
vm.set_global("info", Value::Table(t))?;

The builder consumes itself on each .with(...) call so chains are ownership-clean. .try_with(k, v) is the fallible variant for embedders who want Result propagation on table overflow (extremely unlikely in practice — MAX_ASIZE = 1<<27).

5.1 Read-only tables

A host that reuses one Vm for many scripts (Redis's EVAL model) can stop a script from changing the environment the next script sees:

use luna_core::runtime::Value;

let g = vm.globals();
vm.set_readonly(g, true);
for lib in ["string", "table", "math"] {
    if let Value::Table(t) = vm.eval(&format!("return {lib}"))?[0] {
        vm.set_readonly(t, true);
    }
}

Vm::set_readonly(t, on) marks a table read-only or writable again, as Redis's lua_enablereadonlytable does; Table::is_readonly reads the mark. While a table is read-only, every write to it raises Attempt to modify a readonly table, in every dialect and with or without the JIT:

  • assignments: t.k = v, t[k] = v, a global assignment when the table is the globals table, and a __newindex chain that reaches the table (its own __newindex is not called). The error carries the position of the assignment: user_script:1: Attempt to modify a readonly table
  • rawset, setmetatable, debug.setmetatable, the stores of table.insert, table.remove, table.sort and table.move (its destination), and 5.1's package.seeall. These raise the message with no position, as PUC does for an error raised inside a C function. table.sort raises only when it would store, so an already sorted array of up to three elements is left alone, as in Redis
  • the host side: Vm::set_global, Table::set and Table::set_int return the error (TableError::ReadOnly from the Table methods; turn it into the interpreter's error with Vm::table_error), and LuaTable::set on the facade does too

Reads, __index lookups and iteration are not affected and cost nothing extra; a write checks one bit in the table's header (the bit the write barrier checks), and compiled code checks a table it keeps writing to once per run. Only the table itself is protected: its metatable, and tables stored in it, stay writable unless they are marked too. To change a read-only table from the host, unmark it, write, and mark it again; the facade has LuaTable::set_readonly for the same purpose.


6. Native functions

Expose Rust functions to Lua via vm.native_typed — the framework decodes arguments and encodes returns automatically:

// Pure function:
let add = vm.native_typed(|a: i64, b: i64| -> i64 { a + b });
vm.set_global("add", add)?;

// Multi-return:
let split = vm.native_typed(|x: i64| -> (i64, i64) { (x / 10, x % 10) });
vm.set_global("split", split)?;

// Fallible (use Result<T, LuaError> in the return):
use luna_jit::vm::LuaError;
use luna_jit::runtime::Value;

let safe_div = vm.native_typed(|a: i64, b: i64| -> Result<i64, LuaError> {
    if b == 0 {
        Err(LuaError::new(Value::Nil))  // or build a string error
    } else {
        Ok(a / b)
    }
});
vm.set_global("safe_div", safe_div)?;

// Lua sees:
vm.eval("return add(40, 2)")?;  // -> 42
vm.eval("return split(127)")?;  // -> 12, 7
vm.eval("return safe_div(10, 0)")?;  // -> error

Argument types can be any FromLuaValue impl: i64, f64, bool, String, Vec<u8>, Value, Option<T>. Return types implement IntoLuaReturn: any single IntoValue type, () (zero returns), tuples up to arity 6, or Result<T, LuaError>.

Arity is 0-6. Beyond 6, embedders tuple their inputs (fn(t: (a, b, c, d, e, f, g))) or use vm.native_with(...) with manual argument decoding.

Captures: vm.native_typed accepts non-capturing closures and function pointers (both ZST or pointer-sized). Capturing closures fall back to vm.native_with(...) with explicit upvals.


7. Userdata — exposing host types

Stash arbitrary T: 'static Rust values inside Lua userdata. v1.2 exposes Lua-callable methods + metamethods through the LuaUserdata trait; for plain T: 'static types without Lua-side methods, an empty impl is the one-line bridge:

use luna_core::vm::LuaUserdata;

#[derive(Debug)]
struct DbConn {
    url: String,
    pool_size: u32,
}

impl LuaUserdata for DbConn {}
let conn = DbConn {
    url: "postgres://localhost/app".to_string(),
    pool_size: 8,
};
vm.set_userdata("db", conn)?;

// Host-side read/write:
let c: &DbConn = vm.userdata_borrow("db").unwrap();
println!("pool size: {}", c.pool_size);
let c: &mut DbConn = vm.userdata_borrow_mut("db").unwrap();
c.pool_size = 16;

Vm::create_userdata / Vm::set_userdata require T: LuaUserdata; a type that needs no methods satisfies it with an empty impl LuaUserdata for T {}. The trait's metatable is auto-installed on the userdata at creation time, cached per-Vm by TypeId::of::<T>(). (Tightened from T: 'static in 1.2.)

7.1 LuaUserdata trait — methods + metamethods

use luna_core::vm::{LuaUserdata, MetaMethod, UserdataMethods};

struct Counter {
    value: i64,
}

impl LuaUserdata for Counter {
    fn type_name() -> &'static str { "Counter" }

    fn add_methods<M: UserdataMethods<Self>>(m: &mut M) {
        // Regular method — `obj:get()`.
        m.add_method("get", |_vm, this, ()| Ok::<_, _>(this.value));

        // Mutating method — `obj:incr(by)`.
        m.add_method_mut("incr", |_vm, this, (by,): (i64,)| {
            this.value += by;
            Ok::<_, _>(())
        });

        // Metamethod — `tostring(obj)`.
        m.add_meta_method(MetaMethod::ToString, |_vm, this, ()| {
            Ok::<_, _>(format!("Counter({})", this.value))
        });
    }
}

vm.set_userdata("c", Counter { value: 100 })?;
vm.eval("c:incr(50); print(tostring(c))")?;   // → Counter(150)

The closure shape is Fn(&mut Vm, &T, A) -> Result<R, LuaError> for add_method and Fn(&mut Vm, &mut T, A) -> Result<R, LuaError> for add_method_mut. A is any FromLuaArgs tuple (0-6 fixed args, (T0, T1, …)) or Vec<Value> for variadic dispatch. R is any IntoLuaReturn (primitives, tuples, Value, …). Closures must be non-capturing (Copy + 'static + ZST) — capture state by making it part of T itself.

7.2 Static constructors via add_function

add_function registers an entry directly on the metatable (not under __index), so it is callable as a static fn when the metatable is exposed as a global:

impl LuaUserdata for Vec3 {
    fn add_methods<M: UserdataMethods<Self>>(m: &mut M) {
        m.add_function("new", |vm, (x, y, z): (f64, f64, f64)| {
            Ok::<_, _>(vm.create_userdata(Vec3 { x, y, z }))
        });
        m.add_meta_method(MetaMethod::Add, /* ... */);
    }
}

// Expose the metatable as the `Vec3` global so scripts can call
// `Vec3.new(1, 2, 3)`:
let mt = vm.register_userdata::<Vec3>()?;
vm.set_global("Vec3", luna_core::runtime::Value::Table(mt))?;

See crates/luna-jit/examples/userdata_vec3.rs for the runnable version with __add / __sub arithmetic metamethods.

7.3 Variadic dispatch (Vec<Value>)

For a Redis-style obj:call(cmd, ...) dispatcher, take Vec<Value> to collect all positional args:

m.add_method_mut("call", |vm, this, args: Vec<Value>| {
    // args[0] = cmd; args[1..] = command-specific.
    this.dispatch(vm, args)
});

See crates/luna-jit/examples/userdata_redis_stub.rs for the full pattern.

7.4 Field-style sugar (v1.3 UD1+UD2)

add_field_method_get(name, fn) and the new add_field_method_set(name, fn) register accessors for true field-style syntax:

impl LuaUserdata for Box2 {
    fn add_methods<M: UserdataMethods<Self>>(m: &mut M) {
        m.add_field_method_get("width", |_vm, this| Ok::<_, _>(this.width));
        m.add_field_method_set("width", |_vm, this, (w,): (i64,)| {
            this.width = w;
            Ok(())
        });
    }
}
print(b.width)   -- 16
b.width = 100
print(b.width)   -- 100
b.unknown = 1    -- error: attempt to write unknown field 'unknown' on Box2

Implementation: when any getter is registered, the metatable's __index slot becomes a native trampoline that dispatches methods → field- getters → nil; when any setter is registered, the metatable's __newindex slot becomes a trampoline that dispatches to the setter or raises attempt to write unknown field …. There is no silent fallback: writing an unknown field is a mistake worth surfacing, not something to absorb.

A field getter is not a method. add_field_method_get dispatches through the function-__index trampoline, so obj.width returns the value directly and obj:width() does not work for it — the call-syntax form tries to call the returned Int(16) and errors. If you need both shapes, register an explicit add_method("width", ...) alongside the field getter. (Call syntax did work in 1.2, when the getter generated a method-table entry; 1.3 changed the dispatch.) Methods win over field-getters on name collision (matches mlua; precedence is documented in crates/luna-core/tests/it/userdata_trait.rs::methods_win_on_collision).

7.4a #[derive(LuaUserdata)] proc-macro (v1.3 UD3)

For ≥5-method userdata types the builder calls become repetitive; the new luna-jit-derive crate ships a derive that emits the trait impl for you:

use luna_jit::{LuaUserdata, lua_userdata_methods};
use luna_core::vm::{LuaError, Vm};

#[derive(LuaUserdata)]
#[lua_type_name = "Counter"]
struct Counter { value: i64 }

#[lua_userdata_methods]
impl Counter {
    #[lua_method("get")]
    fn get(&self, _vm: &mut Vm, _: ()) -> Result<i64, LuaError> {
        Ok(self.value)
    }

    #[lua_method_mut("incr")]
    fn incr(&mut self, _vm: &mut Vm, (by,): (i64,)) -> Result<(), LuaError> {
        self.value += by;
        Ok(())
    }
}

Helper attrs (placed on fn items inside the impl block):

Attribute Lowers to
#[lua_method("name")] add_method
#[lua_method_mut("name")] add_method_mut
#[lua_function("name")] add_function (no receiver)
#[lua_meta_method(Add)] add_meta_method(MetaMethod::Add, …)
#[lua_meta_method_mut(Concat)] add_meta_method_mut
#[lua_field_get("name")] add_field_method_get
#[lua_field_set("name")] add_field_method_set
#[lua_skip] keep the fn as a pure-Rust helper

luna-jit-derive lives downstream of luna-core so the 0-dep contract is preserved — luna-core embedders who want the derive can either upgrade to luna-jit, or add luna-jit-derive = "3" as a direct dep alongside luna-core (the derive emits fully-qualified ::luna_core::vm::* paths and has no other runtime deps). Hand-impl remains the supported escape hatch for generic types, conditional method sets, and embedders who would rather not pull syn into their tree at all.

Add the dep:

luna-jit = "3"
# or
luna-core = "3"
luna-jit-derive = "3"

7.5 GC + __gc finalizers

A trait-installed MetaMethod::Gc metamethod fires before the Rust Drop on the boxed T. PUC's contract is that __gc is registered for finalization at metatable-set time, not at later mutations of the metatable; v1.2's auto-install honors this by calling check_finalizer_userdata at create_userdata time.

7.6 Trait contract reminders

  • T must be 'static.
  • Method closures must be ZST (non-capturing). Capture state in T.
  • During an add_method_mut body, do not concurrently borrow the same userdata payload through another API (e.g. a host-side userdata_borrow_mut("name") on the same global). The trampoline's &mut T is exclusive within the call; aliasing is undefined.
  • If T carries a Gc<...> field, override [LuaUserdata::trace] to mark it — see §7.7. The default trace is a no-op, suitable for pure host types (no Gc-managed inner state).

For the low-level path, get the Gc<Userdata> handle out of Value::Userdata(g) and use g.as_ptr() (or the heap-safe accessors) to inspect/mutate.

7.7 Trace-bearing host payloads (v1.3+)

When T stashes a Gc<Table> / Gc<LuaStr> / Gc<NativeClosure> / Gc<Coro> / Gc<Userdata> inside its fields, the collector cannot discover those handles by walking the Box<dyn Any> payload alone — the Any vtable has no "trace" entry. The embedder declares the reachable Gc set by overriding LuaUserdata::trace:

use luna_core::runtime::{Gc, Table};
use luna_core::vm::{LuaUserdata, UserdataMarker};

struct Cache {
    entries: Gc<Table>,
}

impl LuaUserdata for Cache {
    fn type_name() -> &'static str { "Cache" }

    fn trace(&self, m: &mut UserdataMarker) {
        m.mark(self.entries);
    }
}

UserdataMarker exposes two methods:

  • mark<T>(&mut self, g: Gc<T>) -> bool — mark a typed Gc handle.
  • mark_value(&mut self, v: Value) -> bool — mark every Gc-managed object behind a Value (no-op for primitives like Int / Bool).

For container fields, walk them and call mark per element:

struct Pool { tables: Vec<Gc<Table>> }
impl LuaUserdata for Pool {
    fn trace(&self, m: &mut UserdataMarker) {
        for &t in &self.tables { m.mark(t); }
    }
}

Contract inside trace:

  • The call runs synchronously inside the collector's mark phase.
  • The embedder may only read &self and call mark / mark_value.
  • The embedder must not allocate new GC objects, reenter the Vm, acquire locks, or perform I/O.
  • The default trace is a no-op, so existing v1.1 / v1.2 types with empty impl LuaUserdata for T {} keep compiling and running unchanged.

Forgetting to override trace when T carries a Gc<...> field whose lifetime isn't otherwise rooted (via Vm::pin_host or a Lua-side table) risks dangling references after the next GC cycle. The contract is on the embedder; the runtime does not detect it.


8. Coroutines from Rust

Drive Lua coroutines without going through coroutine.create / :resume on the Lua side:

use luna_jit::runtime::Value;

// Compile a Lua coroutine body:
let body = vm.eval(r#"
    return function()
        coroutine.yield(1)
        coroutine.yield(2)
        return 3  -- terminal return
    end
"#)?[0];

// Create + drive:
let co = vm.create_coroutine(body);

let r1 = vm.resume_coroutine(co, vec![])?;
// r1[0] == Int(1)  — first yield

let r2 = vm.resume_coroutine(co, vec![])?;
// r2[0] == Int(2)  — second yield

let r3 = vm.resume_coroutine(co, vec![])?;
// r3[0] == Int(3)  — terminal return; further resumes error

resume_coroutine returns Err(LuaError) if the body raises or if co isn't a Value::Coro.


9. Debug hooks (Rust-side)

Install a Rust callback that fires on script events without going through Lua-side debug.sethook:

use luna_jit::vm::Vm;
use luna_jit::vm::exec::{
    RustHookEvent,
    HOOK_MASK_CALL, HOOK_MASK_RETURN, HOOK_MASK_LINE, HOOK_MASK_COUNT,
};

fn observe(_vm: &mut Vm, event: RustHookEvent) {
    match event {
        RustHookEvent::Call => println!("→ function entry"),
        RustHookEvent::Return => println!("← function return"),
        RustHookEvent::Line(n) => println!("• line {n}"),
        RustHookEvent::Count => println!("count event"),
        RustHookEvent::TailCall => println!("→ tail call"),
    }
}

vm.set_rust_debug_hook(
    Some(observe),
    HOOK_MASK_CALL | HOOK_MASK_RETURN,  // pick the events you want
    0,  // count interval (only used with HOOK_MASK_COUNT)
);

// Clear later:
vm.clear_rust_debug_hook();

The Rust hook fires synchronously inside the dispatcher; reentrancy is suppressed (the in_hook flag prevents the hook from triggering itself). Lua-side debug.sethook continues to work independently; both can coexist.


10. Error handling

Lua errors propagate as Result<T, LuaError>. The error value is the Lua-side error(...) argument (usually a string); rich context lives on the Vm:

use luna_jit::vm::{LuaError, LuaErrorKind};

match vm.eval("error('something failed')") {
    Ok(v) => println!("ok: {:?}", v),
    Err(e) => {
        // Quick: format the LuaError directly:
        println!("error: {}", e);

        // Or get details from the Vm:
        let kind = vm.error_kind();  // -> LuaErrorKind::Runtime
        let source = vm.error_source();  // -> Option<(&str, u32)>
        let traceback = vm.take_error_traceback();  // -> Option<String>, see below
        eprintln!("kind={kind} source={source:?}");
    }
}

The traceback snapshot

When an error reaches the host (nothing in Lua catches it), the Vm keeps the traceback of the stack as it stood where the error was raised, before it unwound. vm.take_error_traceback() returns it once; the next call into the Vm clears it. Its text is exactly what PUC gives a C host that calls the function with lua_pcall under a message handler returning luaL_traceback(L, L, NULL, 1) (5.1, which has no luaL_traceback: debug.traceback("", 2), without the leading newline):

stack traceback:
	[C]: in function 'error'
	[C]: in function 'string.gsub'
	user_script:3: in local 'f'
	user_script:5: in main chunk

That is, stack traceback: followed by one line per stack level, innermost first, each line starting with a newline and a tab. There is no message line: the error value is the Err itself.

  • The first level is what raised the error: the C function for error, assert or a library function's argument error, the Lua function for an error raised by an operation (attempt to index a nil value).
  • C functions are levels of their own, written [C]: in <name> (5.1: [C]: in function '<name>', or [C]: ? without a name). luna's library functions and natives registered by the host appear as C functions. The host's own call adds no level, as lua_pcall adds none.
  • A Lua level is <short_src>:<line>: in <name>, where <short_src> is the chunk name as PUC shortens it (@user_script reads user_script, a string chunk [string "..."]). Without line information (a stripped binary chunk) it is <short_src>: in <name>.

<name> follows the dialect:

dialect name
5.1 function '<name>' for any name the calling instruction gives (global, local, method, field, upvalue); main chunk; function <src:line> for an unnamed Lua function; a C function without a name prints ? in place of in <name>. A tail call left a level (tail call): ?
5.2 function '<name>' for a name from the calling instruction; main chunk; an unnamed C function by the global it is reachable from (function 'string.gsub', else ?); function <src:line>
5.3, 5.4 first the name the function is reachable by from package.loaded, two tables deep, without _G. (function 'error', function 'string.gsub', function 'mymod.f'); else <kind> '<name>' with kind global, local, method, field, upvalue, metamethod, for iterator or hook; else main chunk, function <src:line>, or ?
5.5 first <kind> '<name>' from the calling instruction (global 'error', field 'gsub', method 'rep'); else main chunk; else the package.loaded name; else function <src:line> or ?

From 5.2 on, a level whose function was entered by a tail call is followed by the line (...tail calls...).

A deep stack is shortened as luaL_traceback shortens it. 5.1 leaves out the middle of a stack of 22 or more levels: the first 10 levels, a line ..., the last 10. 5.2 and 5.3 do it from 23 levels: the first 10, ..., the last 11. 5.4 and 5.5 do the same with the line ... (skipping <n> levels), where <n>, as PUC computes it, is one less than the number of levels left out.

The same text in the same format is what debug.traceback(co) returns for a coroutine an error killed, from level 0, and what Coro::error_traceback holds.

A program that only needs the innermost Lua level (Redis-style @user_script:<line>) finds it as the first line whose <short_src> is not [C]; one that, like Redis, wants the level that raised, takes the first level when it is a Lua level and otherwise the next one.

To handle an error where it is raised, before the stack unwinds — as lua.c does to print a traceback — call through a message handler: vm.call_value_with_handler(f, &args, msgh) is PUC's lua_pcall with a handler, and inside the handler vm.traceback(Some(msg), 1) is luaL_traceback. Like lua_pcall, the call is not a level of the stack the handler sees: the traceback ends with the function the host called. vm.load_file(name, mode) and vm.load_buffer(src, chunkname, mode) are luaL_loadfilex / luaL_loadbufferx, returning the error message those leave. The luna CLI (crates/luna-jit/src/bin/luna.rs) is built on these.

LuaErrorKind classifies common cases:

variant when it fires
Runtime default — error(...), type errors, missing globals
Syntax parser/lexer rejected the source
InstrBudget set_instr_budget exhausted
MemoryCap set_memory_cap exceeded
Native a native callback returned Err(LuaError)
OutOfMemory allocation failed (see below for loading)
Type type mismatch at an arithmetic boundary

A load (Vm::load, load, lua_load) that runs out of memory fails with the memory error "not enough memory" (LUA_ERRMEM in the C API) and leaves the Vm usable. The parser and compiler reach that error by unwinding to the load, so it depends on the crate being built with panic = "unwind", Rust's default on native targets. With panic = "abort", which wasm32 targets use by default, a load that runs out of memory ends the process (a trap on wasm), exactly as an allocation failure in a standard library Vec does. Out-of-memory at run time is reported as an error under both settings.

LuaError implements std::fmt::Display and std::error::Error, so it composes with ? and the anyhow / thiserror ecosystem.


11. The Lua newtype facade

luna_jit::Lua is an mlua-shape front door:

use luna_jit::Lua;
use luna_jit::version::LuaVersion;

let mut lua = Lua::new();  // JIT on, Lua 5.5
lua.open_base();
lua.open_math();

// All the IntoValue / FromLuaValue / native_typed plumbing reuses
// the lower-level Vm code.
let add = lua.create_function(|a: i64, b: i64| -> i64 { a + b });
lua.set_global("add", add)?;

let r: i64 = lua.eval("return add(40, 2)")?;
assert_eq!(r, 42);

// Tables as first-class handles:
let t = lua.create_table();
t.set(&mut lua, "name", "luna")?;
t.set(&mut lua, "year", 2026_i64)?;
let name: String = t.get(&mut lua, "name")?;

// Globals via the same handle interface:
let g = lua.globals();
let answer: i64 = g.get(&mut lua, "answer")?;

Handles (LuaFunction, LuaTable, LuaRoot) are Copy + Clone and survive across calls. They wrap an opaque luna_core::vm::HostRootTicket (8 bytes) that the underlying slot-recycling pool issues at pin_host time.

Slot recycling (since v1.3): long-running embedders (request-per-script loops, edge workers) release individual handles via lua.unpin(handle); the slot is recycled for the next pin so the pool size stays bounded. The whole batch can still be released in one shot via lua.unpin_all(). Both operations bump the slot's generation; using a stale LuaFunction / LuaTable / LuaRoot after release panics with "<HandleType> used after unpin / unpin_all".

// Request-per-script loop — pool stays at ≤ N slots regardless of
// loop count.
loop {
    let t = lua.create_table();
    t.set(&mut lua, "name", "request")?;
    // ... use t ...
    lua.unpin(t)?;  // single-handle release; slot recycled
}

Embedders authoring their own facade (parallel to LuaFunction) work with the raw Vm API: vm.pin_host(v) -> HostRootTicket, vm.read_host(t) -> Option<Value>, vm.write_host(t, v) -> Result<(), HostRootStale>, vm.unpin(t) -> Result<(), HostRootStale>. The HostRootStale error type carries the ABA-detection semantics — stale tickets (held across an unpin/re-pin on the same slot) read as None rather than silently returning the new slot's unrelated value.

Migration from v1.1 / v1.2: callers stashing the bare usize from Vm::pin_host move to HostRootTicket; host_root_at(idx) and host_root_set(idx, v) (panic on OOB) are removed in favor of read_host(t) (returns Option<Value>) and write_host(t, v) (returns Result<(), HostRootStale>).

For escape-hatch access to the underlying Vm:

let vm: &mut Vm = lua.vm();

11.1 Sharing compiled code between VMs

An embedder that runs the same scripts in many VMs (a fresh VM per request, or one VM per worker thread) builds them through one luna_jit::Engine. A trace or function the JIT compiles in one of them is kept in the engine, and the others install it instead of compiling the same code again:

use luna_jit::{Engine, Lua};
use luna_jit::version::LuaVersion;

let engine = Engine::new(); // Clone + Send + Sync: share it between threads
for request in requests {
    let mut vm = engine.new_vm(LuaVersion::Lua54); // or engine.new_minimal_vm
    vm.eval(&request.script)?;
}
let lua = Lua::with_engine(&engine, LuaVersion::Lua55);
  • Code is shared only between VMs of the same dialect and the same trace settings (set_trace_tier, set_trace_tier_up_at, set_field_ic_enabled, set_self_link_enabled), and only for functions whose bytecode, constants and upvalue descriptions are the same; line numbers and chunk names do not matter.
  • Each VM copies the code it installs into its own code memory, so dropping a VM frees its code whether or not it compiled it, and VMs on different threads never run the same copy. The engine keeps about 64 MiB of shared code and data at most (set_capacity_bytes), dropping the oldest first; trace_count, function_count and bytes report what it holds.
  • The VMs of an engine hash strings with the engine's seed (random per engine) rather than one of their own.
  • vm.trace_adopted_count() counts the traces a VM installed from its engine, luna_jit::jit::chunk_adopted_count(&vm) the functions.

12. Threading model

Vm (and Lua) is !Send + !Sync. One Vm lives on the OS thread that created it. Embedders wanting concurrency spawn one Vm per worker; async embedders pin Vm access to a single-thread Tokio runtime or a LocalSet. See threading.md for the canonical patterns (single-thread Tokio, LocalSet, per-OS-thread Vm + channels) and for feature = "send", which surfaces SendVm and has shipped since 2.1.

The constraint is type-system-enforced; a compile_fail doctest on Vm catches accidental loss of the !Send invariant.


13. Stable API contract

Starting at v2.7.0 (2026-07-01) luna's public API is partitioned into a stable surface (SemVer-major to break) and an unstable / internal surface (may break in minor releases). The public contract:

Stable — SemVer-major to break

  • luna_jit's front-door types: Lua, LuaFunction, LuaTable, LuaRoot, LuaSandboxBuilder, IntoLuaArgs
  • Constructor entry points: new_with_jit, new_minimal_with_jit, install_default_jit
  • Derive + attr macros: LuaUserdata, lua_userdata_methods
  • VmExt trait for advanced embedders
  • luna_core transitively re-exported via luna_jit::*: Vm, LuaVersion, Value, LuaError, Vm::new, Vm::eval, Vm::set_memory_cap, Vm::set_print_handler, Vm::host_roots accessors
  • luna_aot::{BYTECODE_START_SYMBOL, BYTECODE_END_SYMBOL, BYTECODE_SECTION_NAME} (AOT ABI constants); cli, embed modules
  • luna_runtime_helpers::{run_bytecode, run_bytecode_as, dialect_code, dialect_from_code, force_link_*}; aot_*_resolver modules (AOT metadata ABI)

Unstable / internal — may break in minor

  • Deep-nested pub modules: luna_core::{compiler, frontend, jit, pattern} (crate-internal reasons)
  • luna_jit::{capi, jit_backend, inspect, jit} (backend internals + experimental C ABI)
  • luna_aot::runtime_stub (test scaffold)

If your embedder currently reaches into an unstable surface, please file an issue — that's the signal we need to promote the symbol into the stable set for future compatibility.


14. Known limitations

Interpreter speed on aarch64 depends on code alignment

On aarch64 the interpreter's dispatch loop runs measurably faster or slower depending on where the linker happens to place it relative to the core's 32-byte fetch blocks. The builds of this repository avoid that with a compiler flag in its .cargo/config.toml, which a crate that depends on luna does not inherit. Add it to your own .cargo/config.toml when you build for aarch64 Linux:

[target.aarch64-unknown-linux-gnu]
rustflags = ["-C", "llvm-args=-align-all-nofallthru-blocks=5"]

It aligns every block entered only by a branch (each bytecode handler is one) to 32 bytes. Without it, measured on a Cortex-A76 with LTO off or thin, the same interpreter built twice with an unrelated change elsewhere in the binary ran exactly the same instructions, yet a float for loop took 18% more cycles with the same cache and branch misses, and a table-heavy loop 10% more, because the loop started at a different offset within its cache line. Rust has no stable attribute yet to align one function, so luna cannot set this for you.

Resolved in v2.13 — Windows gc.lua / gengc.lua / tracegc.lua weak-table sweep STATUS_ACCESS_VIOLATION: root- caused to two platform-independent GC bugs (a stale stack-root cursor on collectgarbage() calls, and weak-table tombstone keys escaping the clear-key sweep), both fixed and validated with ASAN + a new gc-verify invariant-checking build plus repeated Windows stress runs. The official-suite Windows gate is removed; all three files run unconditionally on every platform.


Where to go next

  • architecture.md — crate layout, JIT pipeline, source classification
  • threading.md — async + multi-thread embedding
  • compatibility.md — per-dialect feature matrix (Lua 5.1 / 5.2 / 5.3 / 5.4 / 5.5)
  • performance.md — cross-dialect + Redis-Lua bench numbers
  • cargo doc --open — full API reference for every public type