#How to bind Lua (the c library) to Zig?

1 messages · Page 1 of 1 (latest)

sharp ruin
#

I'm new to Zig and all of the docs regarding C library interop are either convoluted or outdated. Therefore, I'm asking for help here

pure grail
#

pretty sure someone has already done it

#

or do you want to do it yourself for learning?

sharp ruin
pure grail
#

well, summary would be to compile the language using addCSourceFiles

#

then, you could use its headers (to invoke the functions) by either @cImport in a zig file, or addTranslateC + addImport in the build script (later is recommended)

sharp ruin
#

I see, I'll try that out and let you know how it went

brazen loom
#

Feel free to ask questions about the build system if you feel the need. It is notoriously under-documented and you won't find up-to-date answers googling online.

south marsh
#

since you probably dont want to look at ziglua linked above for examples since it defeat the point of doing it your self
checkout https://github.com/allyourcodebase for other c projects ported to the zig build system

GitHub

...are belong to Ziguanas, but we'd also be delighted to give them back! - All Your Codebase

sharp ruin
#

I managed to bind the Lua 5.2 C library to a Zig project. However, whenever I build it and translate it via translateC, it seems to assign @import("std").zig.c_translation.cast(?*anyopaque, @as(c_int, 0)); to NULL, instead of just null, causing errors. How do I resolve this without having to manually modify Lua's include.zig? Is there a better solution?

#

Here's the build.zig:

brazen loom
#

@sharp ruin - Currently you'd need to declare whatever doesn't properly translate manually.

mint grove
mint grove
#

I set up a repro for what you mean. And I'm assuming you're using some kind of function like: lua_tostring.
this is what it generates:

pub inline fn lua_tostring(L: anytype, i: anytype) @TypeOf(lua_tolstring(L, i, NULL)) {
    _ = &L;
    _ = &i;
    return lua_tolstring(L, i, NULL);
}

the clear/simplest solution is to just use lua_tolstring directly instead.

mint grove
mint grove
mint grove
#

youre not missing any

#

just use the correct functions

#

NULL is correctly interpreted as an ?*anyopaque (it's the most general thing you can do with a ((void *)0)) but it can't be passed to the lua functions, which expect something more typed

sharp ruin
mint grove
#

No, that's what zig generates. which is what is giving you issues. You need to un-wrap them instead

sharp ruin
#

Does this mean I have to unwrap every function I'm using?

mint grove
#

yes, fortunately they're not that big of a deal to unwrap:
like this https://github.com/brianferri/luazig/blob/5d77a8458be4143d09f4ae3d2101861a0264d453/src/main.zig#L4-L6

From the translation:

pub inline fn lua_pcall(L: anytype, n: anytype, r: anytype, f: anytype) @TypeOf(lua_pcallk(L, n, r, f, @as(c_int, 0), NULL)) {
    _ = &L;
    _ = &n;
    _ = &r;
    _ = &f;
    return lua_pcallk(L, n, r, f, @as(c_int, 0), NULL);
}

in your code:

fn lua_pcall(L: ?*liblua.lua_State, n: c_int, r: c_int, f: c_int) callconv(.c) c_int {
    return liblua.lua_pcallk(L, n, r, f, @as(c_int, 0), null);
}
#

you can just use them directly