Calling C: extern fn (36a-c)

extern fn hypot(x: Float, y: Float) -> Float
extern fn ldexp(x: Float, e: Int) -> Float

fn main():
  print(hypot(3.0, 4.0))    # 5.0

Strings, buffers and other libraries (36b)

extern "z" fn compress(dest: var List[Int], dest_len: var Int, source: List[Int], source_len: Int) -> Int
extern "z" fn zlibVersion() -> Str

fn deflate(data: List[Int]) -> Result[List[Int], Int]:
  var out = [0] * compressBound(len(data))
  var n = len(out)
  var rc = 0
  unsafe:
    rc = compress(out, n, data, len(data))   # out and n are updated
  if rc != 0:
    return Err(rc)
  Ok(out[:n])

examples/ffi/zlib.tr compresses, uncompresses and checksums with zlib (--allow-c); a test runs it on the interpreter, the VM and native code.

in the declarationC seesnotes
Int, Float, Boolint64_t, double, intas before
Strconst char *a NUL-terminated copy; a string containing NUL is a runtime error
List[Int]const uint8_t *the list as bytes (each element 0 to 255, or a runtime error)
var List[Int]uint8_t *C writes the bytes; the call assigns them to the variable passed
var Intint64_t *C writes it; the call assigns it (pass a var)
result Strconst char *copied; NULL is a runtime error
result Option[Str]const char *NULL is None

unsafe: blocks (36c)

A C function that takes a Str, a byte list or a var, or returns a string, gets a pointer into Trine's memory (or hands one back), and C can write anywhere through it. Such calls are allowed only inside an unsafe: block:

outside.tr:4:15: type error: `strlen` hands C pointers into Trine's memory (strings, byte lists or
`var` parameters), and C can write anywhere through them: call it inside an `unsafe:` block

So the places where Trine's guarantees rest on C code are marked and few. An unsafe: block is a statement; inside it everything else is ordinary Trine. Such a function also can't be used as a value. trine caps marks every function that reaches C (UNCHECKED C code (calls compress, in an unsafe: block here)), and every C call except the pure math still needs --allow-c.

Not yet: C structs and callbacks, other element types for buffers (use bytes), Windows long (32 bits there: declare the C function with Int only where C takes 64-bit integers or where the value fits).