Go to file
Nathan Craddock 942afe836a feat: restructure and add Lua 5.3, 5.2, and 5.1
Restructures the source layout and adds three new versions of Lua.
Documentation is still incomplete at this point, but all four supported
versions are well tested.
2022-10-03 20:24:17 -06:00
lib feat: restructure and add Lua 5.3, 5.2, and 5.1 2022-10-03 20:24:17 -06:00
src feat: restructure and add Lua 5.3, 5.2, and 5.1 2022-10-03 20:24:17 -06:00
.gitignore initial commit: basic library framework 2022-05-31 19:09:27 -06:00
build.zig feat: restructure and add Lua 5.3, 5.2, and 5.1 2022-10-03 20:24:17 -06:00
docs.md feat: restructure and add Lua 5.3, 5.2, and 5.1 2022-10-03 20:24:17 -06:00
license rename LICENSE -> license 2022-05-31 21:08:16 -06:00
readme.md feat: restructure and add Lua 5.3, 5.2, and 5.1 2022-10-03 20:24:17 -06:00

ziglua

A Zig library that provides a lightweight wrapper around the Lua C API to embed the Lua virtual machine into your Zig programs. Currently tracks the latest Lua version (5.4.4).

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
  • More precise types (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 is easy. First add this repo as a git submodule, or copy the source into your repo. 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", .{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).

All functions have been type checked, but only the standard C API has been tested fully. ziglua should be relatively stable and safe to use now, but is still new and changing frequently.

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 providing a great language!