#C Lua bad translated NULL

1 messages · Page 1 of 1 (latest)

peak bear
#

Hello! am using Lua C library directly by translating and building from source.
But the translated file contains NULL defined as follows:

pub const NULL = @import("std").zig.c_translation.cast(?*anyopaque, @as(c_int, 0));

but that results in the error shown in the image.
but changing its value to

pub const NULL = null;

works just fine!
Any thoughts?

glass plinth
#

C functions which get translated by translate-c (either explicitly on the command line or automatically with @cImport()) that have pointer parameters always get the ?[*c] pointer type, as described in https://ziglang.org/documentation/master/#C-Pointers . If you want more info on that, I'm happy to explain, but it's a way to deal with C's more limited type system.

The NULL const that was generated was translated from stddef.h, I believe, which is included from Lua's headers. Now, you don't actually want to use that value when passing a null pointer to a function in Zig, you would want to pass Zig's null as you did, which satisfies the ?[*c] parameters, or any optional parameter.

#

That std.zig.c_translation.cast function is casting to ?*anyopaque, as the error message shows, which won't be accepted by a function expecting a [*c]const u8. The NULL definition there isn't actually useful, it just needs to be there in case it is referenced.

#

Basically:

  • translate-c created a declaration for that C NULL because it has to, but it is given the most vague definition possible so that you would be forced to cast it to something before using it — don't use it!
  • C functions taking pointers are forced to accept a ?[*c]T for pointer params, even if they are expected to be non-null, because of C type system being less precise than Zig's
  • Use Zig's null to pass a null pointer to a translated C function

Hope that helps!

peak bear
glass plinth
#

Oh snap, is it a Lua macro? In my experience I had to basically translate by hand any Lua macro I wanted to use

glass plinth
#

Which one if I may ask? And which Lua version?

peak bear
#

master

#

am building from source

#
luaL_dofile
#

I guess am just going to do it without macros by checking the macros source code

glass plinth
#

That's the best way

peak bear
#

so in this case am going to use luaL_loadfilex

#

which is a legit func

glass plinth
peak bear
#

I'd like to roll my own, so when something happens, I know what's wrong

glass plinth
#

(Soon will integrate with the zig package manager)

peak bear
#

also am using git submodules

glass plinth
#

As you wish. I did the same, it's such a small API it doesn't matter

peak bear
#

and is it automated?

glass plinth
#

It's the whole API for Lua versions 5.1 through 5.4 (no LuaJIT). As far as I know it's hand-written. The author is on this discord and has a thread in #1024381264213594242 if you need to get in touch

#

It's quite nicer than using the translate-c output directly, too

#

(Actually I do know it's hand-written, there are lots of cleanups and manual macro translations applied)

peak bear
#

yea I see. currently I try to stay away from the zig ecosystem as it looks a bit immature

glass plinth
#

ziglua is an example of a well-done Zig library, in my personal opinion. Also actively maintained

#

but I understand your hesitation, it's still the wild west

peak bear
#

btw I'm also using a custom zig script that translates the headers so I have autocomplete

#

lol

#

so I guess I might as well search for the line that starts with pub const NULL = and replace it with null

#

before even savinig the generated file

#

but am not sure if that's enough to make all macros work

glass plinth
#

It kind of sounds like you are doing what has already been done in ziglua. I don't mean to say it's a bad thing, but there was a lot of good quality work done on that side that you might find useful. Unless your goal is learning to DIY of course, then it doesn't necessarily apply :)

peak bear
#

well you said it's hand edited

#

not automated 100%

glass plinth
#

Yep, and there are good reasons for that. C macros being one of them. But there are others, such as adding a wrapper type that can convert between Zig's Allocator interface and Lua's allocator interface, which can't be automated with a script

#

(Then again, I'm generally not a user or fan of auto-binding systems...aside from translate-c, so please do take my thoughts as having that bias)

peak bear
#

well thanks for the info tho, I may try those bindings on the side and see if they work for me

glass plinth
#

For sure, either way I'm happy there is another Lua user in the chat!