waozi 4b1ab58056 Document the embedding API and test it from the host
Unit tests cover values, tables, natives, userdata finalizers, errors,
refs, environments, the print sink, hooks, the memory limit and native
yields. LuaReplace now resolves its index before popping, the collector
never lets the heap pass max_memory unchecked, and strings accept
LuaJIT's \u{XXX} escapes.
2026-11-07 01:46:57 -03:00

Lua

Lua is a Ziran package that runs Lua 5.1 code in the LuaJIT dialect. Everything is written in Ziran, with no C code and no native libraries: the lexer and parser, a compiler to register bytecode, the interpreter, a mark-and-sweep collector with weak tables and __gc, and the standard libraries. It was written so that Katalis can run Luanti mods from ContentDB.

The embedding API follows the Lua C API: a stack, natives as plain Ziran procedures, userdata with metatables, registry references, coroutines a native can yield from, and limits on memory, call depth and instruction count for running untrusted mods. File and environment access is off until the host grants it.

Use it

ziran add https://github.com/ziranlang/lua.git
#import "lua/vm"

state: LuaState;

Add :: (L: *LuaState) -> s32 {
    a, ok1 := LuaCheckNumber(L, 1)
    if !ok1 { return LUA_NATIVE_ERROR }
    b, ok2 := LuaCheckNumber(L, 2)
    if !ok2 { return LUA_NATIVE_ERROR }
    LuaPushNumber(L, a + b)
    return 1
}

Run :: () -> bool {
    L := *state
    LuaOpen(L)
    LuaRegister(L, "add", Add)
    status := LuaDoString(L, "print(add(1, 2))", "=example")
    if status != LUA_OK {
        message, ok := LuaToString(L, -1)
        print("%\n", message)
    }
    LuaClose(L)
    return status == LUA_OK
}

docs/API.md describes the whole API and what each library supports.

Check it

make test

This runs the API unit tests, the Lua scripts in tests/lua, and loads every .lua file of the Katalis mods (set KATALIS_MODS to another folder; it is skipped when missing).

S
Description
Virtual machine for the LuaJIT dialect of Lua 5.1.
Readme MIT
155 KiB
0 Stars 1 Watchers 0 Forks
Languages
Ziran 91.4%
Lua 7.8%
Makefile 0.6%
HTML 0.2%