tromey / emacs-ffi

FFI for Emacs
99 stars 17 forks source link

How do you expose a constant? #14

Closed jkitchin closed 7 years ago

jkitchin commented 7 years ago

I would like to do something like:

(ffi-define-constant ZMQ-REQ "ZMQ_REQ" "some documentation)

(ffi-define-constant sym c-constantname doc-string) and have it do the equivalent of a defconst.

Is something like this already possible?

tromey commented 7 years ago

There's nothing like this. I am missing a piece, though -- why not just use defconst directly?

jkitchin commented 7 years ago

Because I don't know the value of the ZMQ_REQ constant in advance (I could look it up in some header). I figured it is known to the library, so it shouldn't be necessary to look it up to find out it is 3. That way if it is every updated in the c-lib, it will be updated automatically here.

I wanted to do something like I did here: http://kitchingroup.cheme.cmu.edu/blog/2017/07/10/Adding-GSL-constants-to-Emacs-in-a-dynamic-module/

tromey commented 7 years ago

Ok, I see. The issue here is that this FFI doesn't know anything about C -- it's operating solely at the ABI level. So, in particular, constants in header files are invisible to it.

For my experimental (which is a nice way of saying "written but never really tried") Emacs bindings to gcc-jit (https://github.com/tromey/emacs-gcc-jit), I solved this problem by writing a GCC plugin to instrument a compilation and translate the things I needed. Maybe some extra work would be needed to make this sort of approach work for #define.

jkitchin commented 7 years ago

Thanks. I realized that while on a plane today! and just hacked a script to try parsing #define out of the header file.

I will post the work I have done with zeromq bindings with this ffi in the next day or two on my blog. It is pretty nice. Thanks for making this!