#How to bind Lua (the c library) to Zig?
1 messages · Page 1 of 1 (latest)
pretty sure someone has already done it
or do you want to do it yourself for learning?
I wanna do it myself
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)
I see, I'll try that out and let you know how it went
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.
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
Awesome, thanks
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:
@sharp ruin - Currently you'd need to declare whatever doesn't properly translate manually.
How can I do that?
You're probably missing some flags. NULL is in the stdlib. What errors are you getting exactly?
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.
Find an example here: https://github.com/brianferri/luazig/blob/main/src/main.zig @sharp ruin
Here's the log:
You're not missing anything in the build system. You just need to use the correct functions (see linked message).
What flags do I add?
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
Does this mean I have to wrap every function I need like this?
No, that's what zig generates. which is what is giving you issues. You need to un-wrap them instead
Does this mean I have to unwrap every function I'm using?
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