Zig β‘ language bindings for the LuaJIT C API. Use zig-luajit
to run Lua scripts within a Zig application.
The goal of the zig-luajit
project is to provide the most idiomatic Zig language bindings for the LuaJIT C API and C
API Auxilary Library. Additionally the zig-luajit
project emphasizes safety by making liberal use of runtime safety
checks in Debug
and ReleaseSafe
builds and provides full test coverage of the API.
The main
branch targets recent builds of Zig's master
branch (last tested with Zig 0.15.0-dev.565+8e72a2528
).
It is recommended that you install zig-luajit
using zig fetch
. This will add a luajit
dependency to your build.zig.zon
file.
zig fetch --save=luajit git+https://github.com/sackosoft/zig-luajit
Next, in order for your code to import zig-luajit
, you'll need to update your build.zig
to do the following:
- get a reference the
zig-luajit
dependency. - get a reference to the
luajit
module, which contains the core Zig language bindings for LuaJIT. - add the module as an import to your executable or library.
// (1) Get a reference to the `zig fetch`'ed dependency
const luajit_dep = b.dependency("luajit", .{
.target = target,
.optimize = optimize,
});
// (2) Get a reference to the language bindings module.
const luajit = luajit_dep.module("luajit");
// Set up your library or executable
const lib = // ...
const exe = // ...
// (3) Add the module as an import to your executable or library.
my_exe.root_module.addImport("luajit", luajit);
my_lib.root_module.addImport("luajit", luajit);
Now the code in your library or exectable can import and use the LuaJIT Zig API!
const luajit = @import("luajit");
const Lua = luajit.Lua;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const lua = Lua.init(gpa.allocator());
defer lua.deinit();
lua.openBaseLib();
lua.doString(
\\ print("Hello, world!")
);
Some examples are provided in examples/ to aid users in learning to use zig-luajit
. These
small self-contained applications should always be working, please create an issue if they do not work for
you.
API | Support |
---|---|
Lua C API (lua_* ) |
π 100% coverageβ (92/92) |
Auxilary Library (luaL_* ) |
π€© 100% coverage (48/48) |
Debug API (lua_Debug ) |
π₯³ 100% coverage (12/12) |
LuaJIT Extensions | No plans to implement. |
β : Coroutine yield/resume is not yet part of the public zig-luajit
Zig API, see #6.
This section describes the current status of Zig language bindings ("the Zig API").
- βοΈ Fully Supported: Test coverage and runtime safety checks enabled in
Debug
orReleaseSafe
builds. - β Internal: Used internally and intentionally hidden from the Zig API.
- π Superseded: Has no direct Zig equivalent, but the functionality is provided by a different part of the Zig API.
- π’ Renamed: Renamed in a non-obvious way from the C API. Renaming is avoided but done in cases deemed required:
- to conform to Zig idioms or patterns, such as the
init()
/deinit()
pattern. - to avoid conflicts with Zig language keywords, such as the Zig
error
keyword. - to show that the Zig API has slightly different behavior than the C API, such as using
lua.setAllocator()
instead oflua.setAllocF()
; since the Zig API usesstd.mem.Allocator
instead of allocation functions. - to improve the clarity, discoverability or consistency of the symbol in the overall API surface.
- to conform to Zig idioms or patterns, such as the
C Type Definition | Available in zig-luajit |
---|---|
lua_State |
βοΈ Lua |
lua_Alloc |
β Hidden, please use lua.setAllocator() and lua.getAllocator() |
lua_CFunction |
βοΈ lua.CFunction |
lua_Integer |
βοΈ Lua.Integer |
lua_Number |
βοΈ Lua.Number |
lua_Reader |
βοΈ std.io.AnyReader |
lua_Writer |
βοΈ std.io.AnyWriter |
C API Symbols | Available in zig-luajit |
---|---|
lua_atpanic |
βοΈ lua.atPanic() |
lua_call |
βοΈ lua.call() |
lua_pcall |
βοΈπ’ lua.callProtected() |
lua_cpcall |
βοΈπ’ lua.callProtectedC() |
lua_checkstack |
βοΈ lua.checkStack() |
lua_close |
βοΈπ’ lua.deinit() |
lua_concat |
βοΈ lua.concat() |
lua_createtable |
βοΈ lua.createTable() |
lua_dump |
βοΈ lua.dump() |
lua_equal |
βοΈ lua.equal() |
lua_error |
βοΈπ’ lua.raiseError() |
lua_gc |
βοΈ lua.gc() + lua.gcIsRunning() |
lua_getallocf |
βοΈπ’ lua.getAllocator() |
lua_getfenv |
βοΈπ’ lua.getEnvironment() |
lua_getfield |
βοΈ lua.getField() |
lua_getglobal |
βοΈ lua.getGlobal() |
lua_getmetatable |
βοΈ lua.getMetatable() |
lua_gettable |
βοΈ lua.getTable() |
lua_gettop |
βοΈ lua.getTop() |
lua_insert |
βοΈ lua.insert() |
lua_isboolean |
βοΈ lua.isBoolean() |
lua_iscfunction |
βοΈ lua.isCFunction() |
lua_isfunction |
βοΈ lua.isFunction() |
lua_islightuserdata |
βοΈ lua.isLightUserdata() |
lua_isnil |
βοΈ lua.isNil() |
lua_isnone |
βοΈ lua.isNone() |
lua_isnoneornil |
βοΈ lua.isNilOrNone() |
lua_isnumber |
βοΈ lua.isNumber() |
lua_isstring |
βοΈ lua.isString() |
lua_istable |
βοΈ lua.isTable() |
lua_isthread |
βοΈ lua.isThread() |
lua_isuserdata |
βοΈ lua.isUserdata() |
lua_lessthan |
βοΈ lua.lessThan() |
lua_load |
βοΈ lua.load() |
lua_newstate |
βοΈπ’ Lua.init() |
lua_newtable |
βοΈ lua.newTable() |
lua_newthread |
βοΈ lua.newThread() |
lua_newuserdata |
βοΈ lua.newUserdata() |
lua_next |
βοΈ lua.next() |
lua_objlen |
βοΈπ’ lua.getLength() |
lua_pop |
βοΈ lua.pop() |
lua_pushboolean |
βοΈ lua.pushBoolean() |
lua_pushcclosure |
βοΈ lua.pushCClosure() |
lua_pushcfunction |
βοΈ lua.pushCFunction() |
lua_pushfstring |
βοΈ lua.pushFString() |
lua_pushinteger |
βοΈ lua.pushInteger() |
lua_pushlightuserdata |
βοΈ lua.pushLightUserdata() |
lua_pushliteral |
π please use lua.pushLString() |
lua_pushlstring |
βοΈ lua.pushLString() |
lua_pushnil |
βοΈ lua.pushNil() |
lua_pushnumber |
βοΈ lua.pushNumber() |
lua_pushstring |
βοΈ lua.pushString() |
lua_pushthread |
βοΈ lua.pushThread() |
lua_pushvalue |
βοΈ lua.pushValue() |
lua_pushvfstring |
π please use lua.pushFString() |
lua_rawequal |
βοΈπ’ lua.equalRaw() |
lua_rawgeti |
βοΈπ’ lua.getTableIndexRaw() |
lua_rawget |
βοΈπ’ lua.getTableRaw() |
lua_rawseti |
βοΈπ’ lua.setTableIndexRaw() |
lua_rawset |
βοΈπ’ lua.setTableRaw() |
lua_register |
βοΈπ’ lua.registerFunction() |
lua_remove |
βοΈ lua.remove() |
lua_replace |
βοΈ lua.replace() |
lua_resume |
β Hidden, see Issue #6 |
lua_setallocf |
βοΈπ’ lua.setAllocator() |
lua_setfenv |
βοΈπ’ lua.setEnvironment() |
lua_setfield |
βοΈ lua.setField() |
lua_setglobal |
βοΈ lua.setGlobal() |
lua_setmetatable |
βοΈ lua.setMetatable() |
lua_settable |
βοΈ lua.setTable() |
lua_settop |
βοΈ lua.setTop() |
lua_status |
βοΈ lua.status() |
lua_toboolean |
βοΈ lua.toBoolean() |
lua_tocfunction |
βοΈ lua.toCFunction() |
lua_tointeger |
βοΈ lua.toInteger() |
lua_tolstring |
βοΈ lua.toLString() |
lua_tonumber |
βοΈ lua.toNumber() |
lua_topointer |
βοΈ lua.toPointer() |
lua_tostring |
βοΈ lua.toString() |
lua_tothread |
βοΈ lua.toThread() |
lua_touserdata |
βοΈ lua.toUserdata() |
lua_type |
βοΈπ’ lua.getType() |
lua_typename |
βοΈ lua.getTypeName() |
lua_xmove |
βοΈ lua.xmove() |
lua_yield |
β Hidden, see Issue #6 |
C Type Definition | Available in zig-luajit |
---|---|
luaL_Buffer |
βοΈ Lua.Buffer |
luaL_Reg |
βοΈ Lua.Reg and Lua.RegEnd |
C API Symbol | Available in zig-luajit |
---|---|
luaL_addchar |
βοΈ buffer.addChar() |
luaL_addsize |
βοΈ buffer.addSize() |
luaL_addlstring |
βοΈ buffer.addLString() |
luaL_addstring |
βοΈ buffer.addString() |
luaL_addvalue |
βοΈ buffer.addValue() |
luaL_argcheck |
βοΈπ’ lua.checkArgument() |
luaL_argerror |
βοΈπ’ lua.raiseErrorArgument() |
luaL_buffinit |
βοΈπ’ lua.initBuffer() |
luaL_callmeta |
βοΈ lua.callMeta() |
luaL_checkany |
βοΈ lua.checkAny() |
luaL_checkinteger |
βοΈ lua.checkInteger() |
luaL_checkint |
π please use lua.checkInteger() |
luaL_checklong |
π please use lua.checkInteger() |
luaL_checklstring |
βοΈ lua.checkLString() |
luaL_checknumber |
βοΈ lua.checkNumber() |
luaL_checkoption |
βοΈ lua.checkOption() |
luaL_checkstack |
βοΈπ’ lua.checkStackOrError() |
luaL_checkstring |
βοΈ lua.checkString() |
luaL_checktype |
βοΈ lua.checkType() |
luaL_checkudata |
βοΈ lua.checkUserdata() |
luaL_dofile |
βοΈ lua.doFile() |
luaL_dostring |
βοΈ lua.doString() |
luaL_error |
βοΈπ’ lua.raiseErrorFormat() |
luaL_getmetafield |
βοΈ lua.getMetaField() |
luaL_getmetatable |
βοΈπ’ lua.getMetatableRegistry() |
luaL_gsub |
βοΈ lua.gsub() |
luaL_loadbuffer |
βοΈ lua.loadBuffer() |
luaL_loadfile |
βοΈ lua.loadFile() |
luaL_loadstring |
βοΈ lua.loadString() |
luaL_newmetatable |
βοΈ lua.newMetatable() |
luaL_newstate |
π please use Lua.init() |
luaL_openlibs |
βοΈ lua.openLibs() |
luaL_optinteger |
βοΈπ’ lua.checkIntegerOptional() |
luaL_optint |
π please use lua.checkIntegerOptional() |
luaL_optlong |
π please use lua.checkIntegerOptional() |
luaL_optlstring |
βοΈπ’ lua.checkLStringOptional() |
luaL_optnumber |
βοΈπ’ lua.checkNumberOptional() |
luaL_optstring |
βοΈπ’ lua.checkStringOptional() |
luaL_prepbuffer |
βοΈ buffer.prepBuffer() |
luaL_pushresult |
βοΈ buffer.pushResult() |
luaL_ref |
βοΈ lua.ref() |
luaL_unref |
βοΈ lua.unref() |
luaL_register |
βοΈπ’ lua.registerLibrary() |
luaL_typename |
βοΈ lua.getTypeNameAt() |
luaL_typerror |
βοΈπ’ lua.raiseErrorType() |
luaL_where |
βοΈ lua.where() |
C Type Definition | Available in zig-luajit |
---|---|
lua_Debug |
βοΈ Lua.DebugInfo |
lua_Hook |
βοΈπ’ Lua.HookFunction |
C API Symbol | Available in zig-luajit |
---|---|
lua_getinfo |
βοΈ lua.getInfo() |
lua_getstack |
βοΈ lua.getStack() |
lua_gethookcount |
βοΈ lua.getHookCount() |
lua_gethookmask |
βοΈ lua.getHookMask() |
lua_gethook |
βοΈ lua.getHook() |
lua_sethook |
βοΈ lua.setHook() |
lua_getlocal |
βοΈ lua.getLocal() |
lua_setlocal |
βοΈ lua.setLocal() |
lua_getupvalue |
βοΈ lua.getUpvalue() |
lua_setupvalue |
βοΈ lua.setUpvalue() |
The following functions are added in zig-luajit
and do not necessarily have a corresponding
function or macro in the C API.
zig-luajit Extension Function |
Description |
---|---|
lua.getInfoFunction() |
A simplified version of lua.getInfo() for inspecting functions, that has a more idiomatic Zig result type. |
lua.toNumberStrict() |
Gets the value of a number on the stack, without doing type coersion (e.g. from string values). |
lua.toIntegerStrict() |
Gets the value of an integer on the stack, without doing type coersion (e.g. from string values). |
lua.toBooleanStrict() |
Gets the value of a boolean on the stack, without doing type coersion based on "truthyness" of the value. |
lua.openBaseLib() |
Opens the Base Lua standard library. |
lua.openMathLib() |
Opens the Math Lua standard library. |
lua.openStringLib() |
Opens the String Lua standard library. |
lua.openTableLib() |
Opens the Table Lua standard library. |
lua.openIOLib() |
Opens the IO Lua standard library. |
lua.openOSLib() |
Opens the OS Lua standard library. |
lua.openPackageLib() |
Opens the Package Lua standard library. |
lua.openDebugLib() |
Opens the Debug Lua standard library. |
lua.openBitLib() |
Opens the Bit LuaJIT standard library. |
lua.openJITLib() |
Opens the JIT LuaJIT standard library. |
lua.openFFILib() |
Opens the FFI LuaJIT standard library. |
lua.openStringBufferLib() |
Opens the StringBuffer LuaJIT standard library. |
The zig-luajit
Zig languge bindings are distributed under the terms of the AGPL-3.0 License. The terms of this
license can be found in the LICENSE file.
This project depends on source code and other artifacts from third parties. Information about their respective licenses can be found in the COPYRIGHT file.
This project was inspired by natecraddock/ziglua which provides great functionality if you're looking to use Lua runtimes other than LuaJIT!