Foreign Function Interface (FFI)

Part of: Reef Language Reference Last reviewed on version: 0.8.0


Extern Declarations

Declaring C Functions

extern "C" fn strlen(s: string): size_t
extern "C" proc printf(format: string)

No function body - implementation in C library.


Type Mapping

Reef → C:

  • intint32_t
  • int8int8_t
  • int64int64_t
  • uint32uint32_t
  • stringconst char*
  • boolbool
  • floatdouble
  • float32float
  • size_tsize_t
  • pointervoid*

Strings across the FFI boundary (since 0.8.2)

Reef strings carry a heap-block header with an O(1) logical length; raw C strings do not. The compiler bridges the gap automatically in one direction:

  • C returning string to Reef: the result of every user-declared extern "C" fn ...: string call is boundary-copied into a Reef heap string (reef_string_dup_c). Your C function may return a static buffer, a pointer into environ, a string literal, or malloc'd memory — Reef takes a value snapshot at the call site. Two consequences: the returned Reef string does not change if your C code later rewrites its buffer, and each call costs one copy (keep chatty FFI on int/ pointer returns if that matters).
  • C filling a Reef string argument (a getcwd-style out-buffer): the copy cannot help you — the buffer's logical length is stale after the fill. Call str.sync_length(buf) (or str.set_length(buf, n) when the C side reports a length) before reading it in safe context.
  • Freestanding targets (--target *-baremetal): no boundary copy is emitted; your allocator/FFI owns the header contract (see reef_baremetal.h's allocator notes).

Example

extern "C" fn strlen(s: string): size_t

proc main()
    let len: size_t = strlen("Hello")
    println("String length calculated")
end main

Linking: Reef automatically links with the C standard library.


Safety Considerations

  • Type mismatches between Reef and C can cause crashes
  • Ensure extern declarations match actual C signatures
  • String lifetimes managed by Reef GC

Previous: 100_UNSAFE.md Next: 110_INLINE_ASSEMBLY.md Index: 000_INDEX.md