| 1 | title: Talking to C |
| 2 | part: Go |
| 3 | summary: Call a C library, be called back, share variables, and mind the two buffers. |
| 4 | --- |
| 5 | ```raz Qsort run |
| 6 | ``` |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 | ## What using C costs |
| 11 | |
| 12 | 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. |
| 13 | |
| 14 | ## Varargs |
| 15 | |
| 16 | `...` 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)`. |
| 17 | |
| 18 | ```raz Say run |
| 19 | module Say; |
| 20 | import Out; |
| 21 | import SYSTEM; |
| 22 | |
| 23 | extern proc printf(format: ^U8; ...): I32; |
| 24 | extern proc fflush(f: ^U8): I32; |
| 25 | extern proc puts(s: ^U8): I32; |
| 26 | |
| 27 | proc main*; |
| 28 | var n: INT; x: F32; |
| 29 | begin |
| 30 | n := 42; |
| 31 | x := 2.5; |
| 32 | Out.str("raz first\n"); |
| 33 | Out.flush; (* Out has a buffer; so does C *) |
| 34 | _ := printf("C says %ld and %.2f and %s\n", n, F64(x), "hello"); |
| 35 | _ := puts("and puts"); |
| 36 | _ := fflush(nil); (* nil: every C stream *) |
| 37 | Out.str("raz again\n"); |
| 38 | end; |
| 39 | |
| 40 | end Say. |
| 41 | ``` |
| 42 | |
| 43 | `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. |
| 44 | |
| 45 | ## Libraries, and C's variables |
| 46 | |
| 47 | `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. |
| 48 | |
| 49 | ```raz Env run |
| 50 | module Env; |
| 51 | import Out; |
| 52 | import SYSTEM; |
| 53 | |
| 54 | extern var environ: ^^U8; |
| 55 | extern proc strlen(s: ^U8): UINT; |
| 56 | extern proc getenv(name: ^U8): ^U8; |
| 57 | |
| 58 | proc main*; |
| 59 | var p: ^U8; n: INT; |
| 60 | begin |
| 61 | n := 0; |
| 62 | while environ[n] # nil do INC(n); end; |
| 63 | if n > 0 then Out.str("the environment is not empty\n"); end; |
| 64 | p := getenv("RAZ_NO_SUCH_VARIABLE"); |
| 65 | if p = nil then Out.str("not set\n"); end; |
| 66 | Out.int(INT(strlen("six"))); Out.ln; |
| 67 | end; |
| 68 | |
| 69 | end Env. |
| 70 | ``` |
| 71 | |
| 72 | ## Callbacks |
| 73 | |
| 74 | 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. |
| 75 | |
| 76 | ```raz Roots run |
| 77 | module Roots; |
| 78 | import Fmt; |
| 79 | import SYSTEM; |
| 80 | |
| 81 | link "libm.so.6"; |
| 82 | |
| 83 | extern proc cbrt(x: F64): F64; |
| 84 | extern proc hypot(x, y: F64): F64; |
| 85 | |
| 86 | proc main*; |
| 87 | begin |
| 88 | Fmt.print("{} {}\n", [Fmt.real(cbrt(27.0), 3), Fmt.real(hypot(3.0, 4.0), 3)]); |
| 89 | end; |
| 90 | |
| 91 | end Roots. |
| 92 | ``` |