Go to file
Nathan Craddock 13a95f3b2a fix: update package -> module
Updates the code to create a Zig module rather than package for the new
API. This is a 1:1 translation, and it will probably change once the
package manager settles down.

What is the best way to expose a module that requires compiling C code?
Not sure yet.
2023-02-15 15:38:34 -07:00
.github/workflows ci: update to Zig master and test on OS matrix 2023-02-15 15:18:47 -07:00
lib hotfix(lua5.1): Apply patch for CVE-2014-5461 2023-02-15 13:58:53 -07:00
src tests: update to latest Zig master 2023-02-15 15:05:53 -07:00
.gitignore initial commit: basic library framework 2022-05-31 19:09:27 -06:00
build.zig fix: update package -> module 2023-02-15 15:38:34 -07:00
changelog.md docs: update readme and add changelog 2022-10-28 21:32:22 -06:00
docs.md docs: update readme and docs for Zig 0.10.0 2022-10-27 21:02:06 -06:00
license rename LICENSE -> license 2022-05-31 21:08:16 -06:00
makefile chore: update Lua 5.1 for Zig 0.10.0 2022-10-28 20:35:42 -06:00
readme.md docs: update readme and add changelog 2022-10-28 21:32:22 -06:00

Ziglua

shield showing current tests status

A Zig library that provides a complete and lightweight wrapper around the Lua C API. Ziglua currently supports the latest releases of Lua 5.1, 5.2, 5.3, and 5.4 and targets Zig 0.10.0.

Ziglua can be used in two ways, either

  • embedded to statically embed the Lua VM in a Zig program,
  • or as a shared module to create Lua libraries that can be loaded at runtime in other Lua-based software.

In both cases, Ziglua will compile Lua from source and link against your Zig code making it easy to create software that integrates with Lua without requiring any system Lua libraries.

Like the Lua C API, the Ziglua API "emphasizes flexibility and simplicity... common tasks may involve several API calls. This may be boring, but it gives us full control over all the details" (Programming In Lua 4th Edition). However, Ziglua takes advantage of Zig's features to make it easier and safer to interact with the Lua API.

Why use Ziglua?

In a nutshell, Ziglua is a simple wrapper around the C API you would get by using Zig's @cImport(). Ziglua aims to mirror the Lua C API as closely as possible, while improving ergonomics using Zig's features. For example:

  • Zig error unions to require failure state handling
  • Null-terminated slices instead of C strings
  • Type-checked enums for parameters and return values
  • Compiler-enforced checking of optional pointers
  • Better types in many cases (e.g. bool instead of int)

While there are some helper functions added to complement the C API, Ziglua aims to remain low-level. This allows full access to the Lua API through a layer of Zig's improvements over C.

If you want something higher-level (but doesn't expose the full API), perhaps try zoltan.

Getting Started

Adding Ziglua to your project takes only a couple of steps. First add this repo as a git submodule, or copy the source into your project (one day the Zig package manager will make this easier). Then add the following to your build.zig file (assuming cloned/copied into a lib/ subdirectory):

// use the path to the Ziglua build.zig file
const ziglua = @import("lib/ziglua/build.zig");

pub fn build(b: *Builder) void {
    ...
    exe.addPackage(ziglua.linkAndPackage(b, exe, .{}));
}

This will compile the Lua C sources and statically link with your project. Then simply import the ziglua package into your code. Here is a simple example that pushes and inspects an integer on the Lua stack:

const std = @import("std");
const ziglua = @import("ziglua");

const Lua = ziglua.Lua;

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer _ = gpa.deinit();

    var lua = try Lua.init(allocator);
    defer lua.deinit();

    lua.pushInteger(42);
    std.debug.print("{}\n", .{try lua.toInteger(1)});
}

See docs.md for documentation and detailed examples of using Ziglua.

Status

Nearly all functions, types, and constants in the C API have been wrapped in Ziglua. Only a few exceptions have been made when the function doesn't make sense in Zig (like functions using va_list).

Nearly all functions have associated Zig tests. Ziglua should be relatively stable and safe to use now, but I am still polishing things and function signatures may change from time to time.

Contributing

Please make suggestions, report bugs, and create pull requests. Anyone is welcome to contribute!

Acknowledgements

Thanks to the following sources:

  • zoltan for insights into compiling Lua with Zig
  • zig-autolua for help on writing an alloc function
  • mach-glfw for inspiration on a clean build.zig

And finally Lua. Thank you to the Lua team for creating and sharing such a great language!