Questions tagged [ffi]

A foreign function interface (FFI) is a mechanism for one language to interact with software written in another language.

1778 questions
13
votes
2 answers

Can a Rust constant/static be exposed to C?

Say I have a Rust API that contains a constant or a static, an i32 for example. I want to use this Rust API from C. From the C side, I want to use that constant as an array size. Am I correct that there is no way to do it? Is the best solution to…
user1096614
13
votes
1 answer

How do I create a Rust callback function to pass to a FFI function?

This is how the C API looks void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int)); rust-bindgen has generated this for me pub fn mosquitto_connect_callback_set( mosq: *mut…
tez
  • 4,990
  • 12
  • 47
  • 67
13
votes
2 answers

Calculate distance between two raw pointers

Some C interfaces return pointer to end of buffer. So then I need to convert the range to a slice. But slice can only be created from pointer and count. So how do I get the count. Writing end - start simply gives me error: binary operation `-`…
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
13
votes
2 answers

unsafePerformIO and FFI library initialization

I'm creating an FFI module to a library in C which wants a 1-time, non-reentrant function to be called before anything else is. This call is idempotent, but stateful, so I could just call it in every Haskell call. But it's slow and due to…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
12
votes
2 answers

Is it possible to create PHP extensions in Haskell?

Is it possible to create PHP extensions with Haskell? Usually PHP extensions are written using C. Is using Haskell FFI to provide functionality for a stub C extension possible (or even a good idea)? What are the caveats to such an approach? Does…
Anupam Jain
  • 7,851
  • 2
  • 39
  • 74
12
votes
1 answer

Passing a list of strings from Python to Rust

I've been learning Rust for about two weeks now and today, I got into its FFI. I used Python to play with Rust, using ctypes and libc. I passed integers, strings and even learned to pass a list of integers (thanks to this wonderful answer). Then, I…
12
votes
1 answer

How to catch a Haskell exception that is thrown in a Haskell callback function called by a C function?

Is there any good way to catch a haskell exception, which is thrown in a haskell callback function called by a c function? For example, let me have a simple c function which just calls a given callback, void callmeback ( void (*callback) () ) { …
tmizmd
  • 123
  • 4
12
votes
2 answers

How to lend a Rust object to C code for an arbitrary lifetime?

I'm writing a library in Rust that has a C interface. C side must be able to create and destroy Rust objects (C side owns them and controls their lifetime). I've managed to "leak" an object to C, but I'm not sure how to properly free it: pub extern…
Kornel
  • 97,764
  • 37
  • 219
  • 309
12
votes
2 answers

Linking to a C++ library that has extern "C" functions

So I'm writing a Rust FFI to a C++ library that has an extern "C" block with C-style function headers within it. And my low-level FFI builds. However, when I use my FFI in another project, it doesn't link properly, and I get undefined reference to…
Ant Manelope
  • 531
  • 1
  • 6
  • 15
12
votes
1 answer

PureScript FFI to mocha

I am trying to write mocha bindings into PureScript and am completely baffled by Control.Monad.Eff describe(function(){ //do stuff }); Describe is a function that takes nothing and returns IO, or Eff or something that means (side-effect…
Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
12
votes
1 answer

What are the possible approaches to Common-Lisp / Java Interoperability?

So... in an attempt to use preexisting wheels, rather than reinvent my own at every turn, I've been trying to get a decent Common Lisp environment working with [a particular Java's library]. My ABCL adventures actually went reasonably well and I was…
Dan Lentz
  • 491
  • 3
  • 13
12
votes
1 answer

Why is GHC distributed with gcc and g++?

On Windows, GHC is distributed with gcc and g++, e.g. under ghc-7.6.3\mingw\bin. From the download page, it is also noted under the windows binary download that the build for Windows "also includes support for compiling C++ files." I could imagine…
Ein
  • 1,553
  • 3
  • 15
  • 22
11
votes
2 answers

Implementation of MVar in C?

Is there any known implementation of Haskell MVar in C? There is an example on how to implement it in C++. But, I will like to implement it in C - let us say only MVar CInt equivalent in C for now. Writing synchronization primitives can be tricky.…
Sal
  • 4,312
  • 1
  • 17
  • 26
11
votes
1 answer

Calling Rust from Swift

On the Rust side I wrote a function that returns a String as a pointer of bytes (laid out in memory as a C struct): #[repr(C)] pub struct RustByteSlice { pub bytes: *const u8, pub len: size_t, } #[no_mangle] pub extern "C" fn…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
11
votes
4 answers

Haskell FFI Support for Functions With Variadic Arguments

Can anyone show me an example of using a C function with variadic arguments (e.g. printf) with Haskell's Foreign Function Interface? I tried searching the HaskellWiki, but found no such examples. Thanks!
user500944