Skip to content
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

FFI Gen Feature #591

Open
Paxa opened this issue Jul 11, 2022 · 5 comments
Open

FFI Gen Feature #591

Paxa opened this issue Jul 11, 2022 · 5 comments
Labels
bun:ffi Something related with FFI in Bun enhancement New feature or request

Comments

@Paxa
Copy link

Paxa commented Jul 11, 2022

What is the problem this feature will solve?

Writing ffi interfaces manually

What is the feature you are proposing to solve the problem?

FFI Definition Generator

What alternatives have you considered?

Hi at first I would like to say thank you for such a great work!

Is there any plans to make generator for FFI definitions? Also if you have any perspective on how this should be implemented, I would like to know

I considering try to make it, so far I have 2 ideas

Option 1
For now I'm thinking about modifying existing library (https://github.com/node-ffi-packager/node-ffi-generate/tree/ffi-napi) or make new one that works in a similar way (it uses libclang via FFI to parse header files and generate definitions).

I found other languages have something similar:

https://github.com/rust-lang/rust-bindgen (rust)
https://github.com/ffi/ffi-swig-generator (ruby, outdated)
https://github.com/dart-lang/ffigen (dart)

Option 2
use clang parsers that dump in xml or json format and convert it to js code
https://github.com/rpav/c2ffi
https://github.com/CastXML/CastXML

Either way, I think it will be nice if it can generate js and typescript definitions that can work correctly with editor auto-complete and can automatically split code into modules

(may be it even be possible to make macos apps with native UI using bun and ffi bindings)

@Paxa Paxa added the enhancement New feature or request label Jul 11, 2022
@evanwashere
Copy link
Collaborator

evanwashere commented Jul 11, 2022

we actually have discussed potentially doing this using zig's powerful comptime to inspect all types and build bindings, it would remove the need to parse or produce any headers as source itself would act as comptime header

import { murmur32 } from './hash.zig';

console.log(murmur32(new Uint8Array(1024), 1024));

hash.zig:

pub fn murmur32(ptr: [*]const u8, length: u32) callconv(.C) u32 {
  const slice = ptr[0..length];
  return std.hash.Murmur2_32.hash(slice);
}

@Paxa
Copy link
Author

Paxa commented Jul 11, 2022

Does it mean that we will be able "just load" header files during runtime? but it will require to have zig compiler in a system (if I understand comptime correctly)

@evanwashere
Copy link
Collaborator

Does it mean that we will be able "just load" header files during runtime?

We want to experiment with being able to import source files themselves, so yeah

but it will require to have zig compiler in a system (if I understand comptime correctly)

There are multiple ways to fix this, one of them is for bun to download its own zig compiler

This is still only an idea, so anyone is welcome to try and experiment with other ways of doing ffi bindings generation

@kassane
Copy link

kassane commented Jan 10, 2023

@Jarred-Sumner,

Recently, when looking for some reference about bindgen. I saw that you are developing a bindgen C++ to Zig.

Could you explain how this bindgen works or intends to work?
Because dealing with API C++ using hourglass pattern becomes quite complex depending on the project to be implemented.

I've been experimenting with some bindgen:

I also tried to try SWIG for Go (there is still no C Backend for target) but it doesn't seem to be ideal.

My report when exploring cbindgen-fork (Rust to Zig):
kassane/zFFI#2 (There are no plans to be reviewed, Mozilla devs did not find the proposal relevant to merge PR)

@Electroid Electroid added the bun:ffi Something related with FFI in Bun label Oct 24, 2023
@Morglod
Copy link

Morglod commented Mar 18, 2024

Just published my bindings generator (tested on wgpu)
https://github.com/Morglod/bun-ffi-gen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bun:ffi Something related with FFI in Bun enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants