New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support generation of Zig bindings #732
base: master
Are you sure you want to change the base?
Conversation
I ask for a little patience on this adjustment as I am still learning about the internal parsing mechanism of this api. @emilio, there any limit or deadline to still have PR open/draft? |
Definitely not! I'll try to get to it and provide some feedback, but I don't close PRs unless they're superseded by other work, is definitely-unwanted functionality, or have gone completely stale for other reasons. So no rush :) |
That said, a high-level question from taking a look at Zig and so on: Is there anything cbindgen would be able to do that |
First of all, thanks for the feedback. The goal in this feature would not only be the issue of compatibility with But I think it would be interesting to evaluate a possible direct compatibility between rust and zig without relying on C code to make the transition for better security, even though for the rust side any foreign language would still be |
Pointers & Arrayshttps://github.com/eqrion/cbindgen/blob/b94318a8b700d0d94e0da0efe9f2c1bcc27c855f/tests/rust/ptrs_as_arrays.rs#L1-L19 const std = @import("std")
pub fn ptr_as_array( n: u32, arg: [3]u32, _v: ?*u64) anyopaque;
pub fn ptr_as_array1( n: u32, arg: [3]u32, v: [4]u64) anyopaque;
pub fn ptr_as_array2( n: u32, arg: []u32, v: []u64) anyopaque;
pub fn ptr_as_array_wrong_syntax( _arg: ?*u32, _v: ?*u32, _: ?*u32) anyopaque;
pub fn ptr_as_array_unnamed(_: ?*u32, _: ?*u32) anyopaque; |
@eqrion @emilio Since yesterday I focused on fixing the ones that are already marked. Now, I will need to look for other compatibilities between the languages. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good except for the empty test stubs and commented portions.
Looks great so far, awesome job! I made some quick observations, but please take them with a grain of salt. The It would be interesting to see if there is a way to use custom global allocators once that RFC goes through. |
No problem, thanks for the review! Also, I would like to share an important detail that discovered. ExampleRust lib: // #[no_mangle]
pub fn sum_add(x: i32, y: i32) -> i32 { x + y } Cargo config (toml) => lib: Run $> objdump -x rust_mangle.so | grep sum_add
... F.text .... _ZN11rust_mangle7sum_add17hf6faed35db54da66E
$> echo "_ZN11rust_mangle7sum_add17hf6faed35db54da66E" | c++filt
rust_mangle::sum_add::hf6faed35db54da66 Zig app: const std = @import("std");
extern "C++" fn @"_ZN11rust_mangle7sum_add17hf6faed35db54da66E" (x: i32, y: i32) i32;
//Why not rust? Missing librust
pub fn main() void {
const x: i32 = 43;
const y: i32 = 32;
const add = @"_ZN11rust_mangle7sum_add17hf6faed35db54da66E";
std.log.info("Value add {} + {} = {}\n",.{x, y, add(x, y)});
} |
1dabc3a
to
5fcb6f4
Compare
New ProgressSourceCExpected// zig - translate-c generator
pub const Fns = extern struct {
noArgs: ?fn () callconv(.C) void,
anonymousArg: ?fn (i32) callconv(.C) void,
returnsNumber: ?fn () callconv(.C) i32,
namedArgs: ?fn (i32, i16) callconv(.C) i8,
namedArgsWildcards: ?fn (i32, i16, i64) callconv(.C) i8,
};
pub extern fn root(_fns: Fns) void;
pub extern fn no_return() void; Generated (cbindgen)const std = @import("std");
pub const Fns = extern struct {
_noArgs: ?fn() anyopaque,
_anonymousArg: ?fn() anyopaque,
_returnsNumber: ?fn() i32,
_namedArgs: ?fn(first: i32, snd: i16) i8,
_namedArgsWildcards: ?fn(_: i32, named: i16, _1: i64) i8,
};
extern fn root(_fns: Fns) anyopaque;
extern fn no_return() anyopaque; |
f6f4270
to
044a0a9
Compare
If it is possible to review it, thank you. @emilio |
- Opaque return type 'anyopaque' not allowed replaced to `callconv(.C) void`
- extern functions - cstring to const cstring (z-stage2/3)
- new test - refactor zig build-tests flags
Latest expected: pub const MyCallback = ?fn(a: usize, b: usize) anyopaque; // zig stage1
pub const MyCallback = ?* const fn(a: usize, b: usize) anyopaque; // zig stage2/3 (self-hosting) Fix - removing References: |
Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Status: [ WIP ]
c_void
renamed toanyopaque
- see) supportstruct
,enum
,union
with C compatibility- [ ] trycomptime
supportTest Project
Clone and build: https://github.com/kassane/zFFI