Go · lesson 14 of 15

Talking to C

Qsort.raz
module Qsort;
import Out, Fmt, SYSTEM;

link "libm.so.6";

extern proc qsort(base: ^U8; n, size: UINT;
    less: proc(a, b: ^U8): I32);
extern proc cbrt(x: F64): F64;

(* the order of two INTs, as C's qsort wants it *)
proc order(a, b: ^U8): I32;
var x, y: ^INT;
begin
    x := ^INT(a);
    y := ^INT(b);
    if x^ < y^ then return -1; elsif x^ > y^ then return 1; end;
    return 0;
end;

proc main*;
var a: [5]INT; i: INT;
begin
    a := [30, 10, 50, 20, 40];
    qsort(^U8(SYSTEM.ADR(a)), LEN(a), SIZE(INT), order);
    for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end;
    Out.ln;
    Fmt.print("{}\n", [Fmt.real(cbrt(27.0), 3)]);
end;

end Qsort.
10 20 30 40 50 
3.000

An extern procedure is found by its name in the C library, or in a library a link line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as order is. A record goes to C by pointer, never by value.

What using C costs

A program that uses C is linked against it dynamically and starts through it, so that C's own set-up and clean-up run; one that doesn't is a static executable with nothing in it but its own code. A program uses C if any module it uses has an extern or a link of a library.

Varargs

... after an extern's parameters takes any number of further arguments, as printf wants. A string literal goes as a C string, an integer constant as an INT, a real constant as an F64. An F32 is an error there, since C promotes it; write F64(x).

Say.raz
module Say;
import Out;
import SYSTEM;

extern proc printf(format: ^U8; ...): I32;
extern proc fflush(f: ^U8): I32;
extern proc puts(s: ^U8): I32;

proc main*;
var n: INT; x: F32;
begin
    n := 42;
    x := 2.5;
    Out.str("raz first\n");
    Out.flush;                         (* Out has a buffer; so does C *)
    _ := printf("C says %ld and %.2f and %s\n", n, F64(x), "hello");
    _ := puts("and puts");
    _ := fflush(nil);                  (* nil: every C stream *)
    Out.str("raz again\n");
end;

end Say.
raz first
C says 42 and 2.50 and hello
and puts
raz again

Out and C's printf keep two buffers, so a program that writes with both flushes one before it writes with the other, or the text comes out of order.

Libraries, and C's variables

link "libm.so.6"; names a shared library to load at run time. An extern var is C's variable itself: reading it reads C's, assigning it writes C's.

Env.raz
module Env;
import Out;
import SYSTEM;

extern var environ: ^^U8;
extern proc strlen(s: ^U8): UINT;
extern proc getenv(name: ^U8): ^U8;

proc main*;
var p: ^U8; n: INT;
begin
    n := 0;
    while environ[n] # nil do INC(n); end;
    if n > 0 then Out.str("the environment is not empty\n"); end;
    p := getenv("RAZ_NO_SUCH_VARIABLE");
    if p = nil then Out.str("not set\n"); end;
    Out.int(INT(strlen("six"))); Out.ln;
end;

end Env.
the environment is not empty
not set
3

Callbacks

Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as qsort is given order above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.

Roots.raz
module Roots;
import Fmt;
import SYSTEM;

link "libm.so.6";

extern proc cbrt(x: F64): F64;
extern proc hypot(x, y: F64): F64;

proc main*;
begin
    Fmt.print("{} {}\n", [Fmt.real(cbrt(27.0), 3), Fmt.real(hypot(3.0, 4.0), 3)]);
end;

end Roots.
3.000 5.000

Save a program under its module’s name and run it with raz run Name.raz.