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
- Only numbers cross: parameters and the result are
Int(Cint64_t/long),Float(double) orBool(Cint, nonzero = true); the result may be omitted (Cvoid). At most 6 parameters (any mix of Int, Float and Bool, the same on every platform). Every parameter needs its type. - The symbol is looked up when the program is compiled (libc and libm); a missing one is a compile error, not a crash.
- Authority. C code is outside Trine's checks, so it is split in two: - known-pure math (
sin,exp,pow,hypot,fma, ... the list isffi::PURE) is always allowed and counts as pure (it can even run insidepar_mapthreads); - any other function (getpid,kill,exit, ...) needs the--allow-cgrant at run time, is listed as UNCHECKED bytrine caps, and stops tier-up replay (it may have had effects). - Engines: native code calls the symbol directly under a private name (
__asm__("hypot"), so no clash with the runtime's headers). The interpreter and VM call it throughdlsym: integer and floating-point arguments use separate registers on x86-64 and arm64, so one pointer type with 6 integer + 8 float slots fits every signature.
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 declaration | C sees | notes |
|---|---|---|
Int, Float, Bool | int64_t, double, int | as before |
Str | const 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 Int | int64_t * | C writes it; the call assigns it (pass a var) |
result Str | const char * | copied; NULL is a runtime error |
result Option[Str] | const char * | NULL is None |
extern "z" fn ...: a function of libz (-lzfor native code;libz.so.1/libz.dylib/z.dllfor the interpreter and VM). Without a library: the C library and libm, as before. The symbol must exist when the program is compiled.- A
varargument must be avarvariable: the call reads like an assignment to it. Behind it, the extern function gives back(result, new values)and each call assigns them. - Every buffer handed to C is followed by 256 guard bytes; if the C function wrote over them the call is a runtime error ("wrote past the end of a 3-byte buffer: memory may be corrupted") instead of silent corruption. Lengths are yours to pass correctly: C can't know them.
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).