Skip to content

Add support for a Foreign Function Interface

Yorick Peterse requested to merge ffi into master

Details

This adds support for interfacing with C code using libffi. The standard library std::ffi can be used to defining libraries and functions, executing functions, and reading/writing from/to variables. The current implementation is fairly basic. For example, C callbacks and variadic arguments are not supported at this time. The provided interface is also fairly low-level, and this is deliberate: it's too early to provide all kinds of high level interfaces, as it's not clear yet how often the FFI will be used, what for, etc.

Due to libffi being a bit tricky to compile on Windows using MSVC, we now require the use of MSYS2 or similar Unix like environments for Windows in order to compile IVM. This is annoying, but unfortunately there is no workaround available at this time. Hopefully in the future we can support MSVC again.

Corresponding issue

#113 (closed)

Checklist

  • Added tests
  • Added documentation
    • Update Windows requirements on the website to mention that MSYS2 and similar environments are required
  • Inko source code follows the Inko style guide
  • Add instructions for pinning processes to OS threads. This is necessary to deal with thread-local data, such as errno in libc.
  • Implemented VariableWrite
  • Implement public interface in the form of a std::ffi module
    • Support defining of C structures, and reading from/writing to their members
    • Expose size and alignments of C types. This is necessary to calculate struct member offsets.
  • Support creating Pointer instances using a custom address, such as Pointer.new(0x0).
  • Support reading a char * into a byte array
  • Stop building binaries for musl. When musl is statically linked, dlopen is not supported, and this is required by std::ffi. There is no workaround at this time, and conditionally supporting std:ffi is not something I want to begin with.
    • Update ienv to remove the musl preference code
  • [ ] Varargs support, if worth the effort at this time Postponing this for now, due to the lack of a solid idea on how to properly implement this
    • For example: what should happen if the same varargs CIF is used multiple times?
  • Fix libffi-sys-rs for Windows (https://github.com/tov/libffi-sys-rs/pull/17)

Development resources

Edited by Yorick Peterse

Merge request reports