Coder Social home page Coder Social logo

ziglua's Introduction

Ziglua

shield showing current tests status Discord

A Zig package 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, 5.4, and Luau and targets Zig master. Tagged versions of Ziglua are made for stable Zig releases.

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.

Documentation

Docs are a work in progress and are automatically generated for each push to main. Most functions and public declarations are documented:

See docs.md for more general information on Ziglua and how it differs from the C API.

Example code is included in the examples directory.

  • Run an example with zig build run-example-<name>
  • Install an example with zig build install-example-<name>

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)
  • Comptime convenience functions to make binding creation easier

Nearly every function in the C API is exposed in Ziglua. Additional convenience functions like toAny and pushAny use comptime reflection to make the API easier to use.

Integrating Ziglua in your project

Find the archive url of the Ziglua version you want to integrate with your project. For example, the url for the commit 41a110981cf016465f72208c3f1732fd4c92a694 is https://github.com/natecraddock/ziglua/archive/41a110981cf016465f72208c3f1732fd4c92a694.tar.gz.

Then run zig fetch --save <url>. This will add the dependency to your build.zig.zon file.

Then in your build.zig file you can use the dependency.

pub fn build(b: *std.Build) void {
    // ... snip ...

    const ziglua = b.dependency("ziglua", .{
        .target = target,
        .optimize = optimize,
    });

    // ... snip ...

    // add the ziglua module and lua artifact
    exe.root_module.addImport("ziglua", ziglua.module("ziglua"));

}

This will compile the Lua C sources and link with your project.

There are currently two additional options that can be passed to b.dependency():

  • .lang: Set the Lua language to build and embed. Defaults to .lua54. Possible values are .lua51, .lua52, .lua53, .lua54, and luau.
  • .shared: Defaults to false for embedding in a Zig program. Set to true to dynamically link the Lua source code (useful for creating shared modules).

For example, here is a b.dependency() call that and links against a shared Lua 5.2 library:

const ziglua = b.dependency("ziglua", .{
    .target = target,
    .optimize = optimize,
    .lang = .lua52,
    .shared = true,
});

The ziglua module will now be available in 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 {
    // Create an allocator
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer _ = gpa.deinit();

    // Initialize the Lua vm
    var lua = try Lua.init(allocator);
    defer lua.deinit();

    // Add an integer to the Lua stack and retrieve it
    lua.pushInteger(42);
    std.debug.print("{}\n", .{try lua.toInteger(1)});
}

Contributing

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

I only use a subset of the Lua API through Ziglua, so if there are parts that aren't easy to use or understand, please fix it yourself or let me know!

Thank you to the Lua team for creating such a great language!

ziglua's People

Contributors

natecraddock avatar nurpax avatar efjimm avatar visendev avatar ryleelyman avatar hryx avatar davidgranstrom avatar deanoc avatar ntbbloodbath avatar t1nk3r1 avatar theoparis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.