Skip to content

sackosoft/zig-luajit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

zig-luajit

Zig ⚑ language bindings for the LuaJIT C API. Use zig-luajit to run Lua scripts within a Zig application.

Ubuntu Regression Tests Badge Windows Regression Tests Badge GitHub License

About

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.

Zig Version

The main branch targets recent builds of Zig's master branch (last tested with Zig 0.15.0-dev.565+8e72a2528).

Installation & Usage

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:

  1. get a reference the zig-luajit dependency.
  2. get a reference to the luajit module, which contains the core Zig language bindings for LuaJIT.
  3. 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!")
);

Examples

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.

Language Binding Coverage Progress

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.

Coverage and Compatibility

This section describes the current status of Zig language bindings ("the Zig API").

  • β˜‘οΈ Fully Supported: Test coverage and runtime safety checks enabled in Debug or ReleaseSafe 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:
    1. to conform to Zig idioms or patterns, such as the init() / deinit() pattern.
    2. to avoid conflicts with Zig language keywords, such as the Zig error keyword.
    3. to show that the Zig API has slightly different behavior than the C API, such as using lua.setAllocator() instead of lua.setAllocF(); since the Zig API uses std.mem.Allocator instead of allocation functions.
    4. to improve the clarity, discoverability or consistency of the symbol in the overall API surface.

Core C API Coverage (lua_)

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

Auxilary Library Coverage (luaL_)

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()

Debug API Coverage (lua_Debug)

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()

Additions to the API in zig-luajit

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.

Licensing

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.

Credits

This project was inspired by natecraddock/ziglua which provides great functionality if you're looking to use Lua runtimes other than LuaJIT!

About

Run Lua code in Zig apps! A package providing Zig language bindings to LuaJIT.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages