Skip to content

sackosoft/zig-luajit-build

Repository files navigation

zig-luajit-build

Used to compile and link the native LuaJIT C API into Zig ⚡ applications.

Ubuntu Build Status Badge Windows Build Status Badge GitHub License

LuaJIT is a fork from the Lua project -- "Lua is a powerful, efficient, lightweight, embeddable scripting language."

Are you looking for a Zig interface to the LuaJIT C API?

This package does not contain Zig language bindings to the C API. This package only handles building and linking the LuaJIT library into a Zig application.

If you're looking to run Lua on LuaJIT in your Zig application, you're probably looking for one of these projects:

  1. sackosoft/zig-luajit - Zig language bindings LuaJIT.
    • Preferred solution when only one Lua runtime (LuaJIT) is required; built on top of zig-luajit-build.
  2. natecraddock/ziglua - Zig language bindings for Lua 5.x and Luau and LuaJIT.
    • More mature project, maintained by Nathan Craddock. Has some quirks as a result of supporting all Lua runtimes with the same Zig API.

Zig Version

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

Installation & Usage

Install using zig fetch. This will add a luajit_build dependency to your build.zig.zon file.

zig fetch --save=luajit_build git+https://github.com/sackosoft/zig-luajit-build

Next, in order for your code to import the LuaJIT C API, you'll need to update your build.zig to:

  1. get a reference to the luajit-build dependency which was added by zig fetch.
  2. get a reference to the luajit-build module, containing the native LuaJIT C API.
  3. attach that module as an import to your library or executable, so that your code can reference the C API.
// (1) Reference the dependency
const luajit_build_dep = b.dependency("luajit_build", .{
    .target = target, 
    .optimize = optimize,
    .link_as = .static  // Or .dynamic to link as a shared library
});

// (2) Reference the module containing the LuaJIT C API.
const luajit_build = luajit_build_dep.module("luajit-build");

// Set up your library or executable
const lib = // ...
const exe = // ...

// (3) Add the module as an import, available via `@import("c")`, or any other name you choose here.
lib.root_module.addImport("c", luajit_build);
// Or
exe.root_module.addImport("c", luajit_build);

Now the code in your library or executable can import and access the LuaJIT C API!

const c = @import("c");  // Access LuaJIT functions via 'c'

pub fn main() !void {
    const state: ?*c.lua_State = c.luaL_newstate();
    if (state) |L| {
        c.luaL_openlibs(L);
        c.luaL_dostring(
            L, 
            \\ print("Hello, world!")
        );
    }
}

Configuration

This package supports one configuration option, shown in the example above.

  • link_as: Controls how LuaJIT is linked
    • .static: Build and link LuaJIT as a static library (default).
    • .dynamic: Build and link LuaJIT as a shared library.

License

Some files in this repository were copied or adapted from the natecraddock/ziglua project. Any files copied or adapted from that project have a comment describing the attribution at the top. Such files are shared by Nathan Craddock under the MIT License in ziglua/license.

All other files are released under the MIT License in zig-luajit-build/LICENSE.

About

A package to compile LuaJIT using the Zig toolchain. Install to access the LuaJIT C API. For Zig API see https://github.com/sackosoft/zig-luajit

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •