waozi 8033702823 Borrow VM strings through std so wasm32 sees their length
The VM built string views from a hand-written { *u8, u64 } descriptor,
which on wasm32 (size_t lengths) read every string as empty. Views now go
through std's BytesFromPointer, which uses the target's slice layout.

The object records and LuaSyntaxNode are private again, now that the
toolchain (locked to 88b505bf) handles Vecs of module-private records.
`make wasm` builds the VM for the web platform with Emscripten and runs
it in a private headless Chromium as part of `make test`.
2026-11-07 02:48:50 -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). make wasm (part of make test) builds the VM for wasm32 with Emscripten and runs it in a private headless Chromium; set EMCC and CHROMIUM if they are elsewhere (it is skipped without them).

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%