#2088 Is there any portable way to call native c code from fantom?

alex Fri 25 Jan 2013

I need to call native c code from my fantom app. I can do it by using Java Native Interface or library like Java Native Access. But I want to know: is there any portable (Java or C# independent) way to do it?

KevinKelley Fri 25 Jan 2013

Interop will be easier if you can wrap it in a Process.exec. Otherwise you're going to need to know some interesting history about ABIs and calling conventions, and object lifetimes.

Fantom doesn't have an FFI, except with the runtime system on which it's running ( using [java] com.sun.Whatever as Whatever ). You can tunnel thru that way to a native interop layer (below)... Feels like a lot of layers, though.

From Java:

  • JNI is painful, requires a compiled interface layer, which adds a painful, error-prone step to the process.
  • JNA is widely used, small, and about as painless as is possible, but of course it's still easy to find edge cases and things that are hard to do.
  • Olivier Chafik's jnaerator and bridj are interesting, and work extremely well, but is young, and you may find yourself in some seriously undocumented weeds. Here JNAerator is an attempt to parse the C headers and infer proper interop types; and bridj handles the actual runtime marshalling. When it works it's really cool.

Login or Signup to reply.