Converting a C API to Zig with the help of comptime

gut reaction:

You’re hammering metadata attributes into name mangling. Another name for it is data-smuggling. I think a better approach is to create an actual purpose-built attribute/metadata system that doesn’t contaminate your symbols with a lot of junk that really gets in the way when you’re debugging.

2 Likes

I guess it’s pretty pointless to stick that info into the name when it’s not going to be exported. We could easily just store it in a struct:

pub extern fn foo(bytes: ?[*]const u8, bytes_len: usize) callconv(.c) void;

pub const @"meta(translate-c, foo)" = .{
    .return_type = "void",
    .params = .{
        .{ .name = "bytes", .type = "const char*" },
        .{ .name = "bytes_len" .type = "size_t" },
    },
};
1 Like

Okay, I’ve added callback function that let you let pick out individual values and group them into a Zig enum. That should take care of the issue with unnamed enum and enum defined using #define. There’re also new callbacks for changing params and fields declared as int to enum.

Handling of invalid value has been rolled into the BasicErrorScheme. You can use the invalid_value_fn callback to specific which value represents an error. By default, return values that are pointers are handled (since null pointers are always invalid in Zig).

And void and int can now be used as the root type.

Here’s the result with the wildmidi library:

3 Likes

Thanks again.