20. extern fn
In an allow unsafe file,
extern fn toupper(c: i32) i32
extern fn write(fd: i32, buf: *u8, n: usize) i64 uses io
declares a C function, called by its own name. It has no body. Its
parameters and result are C types: integers, floats, bool, pointers to C
types, function types taking and giving C types, and extern structs (the
result may also be void), so the C types are exact; anything else, an
inout parameter, a C keyword, main (the generated entry point), a
name the C headers the generated code includes already declare
(functions such as puts and malloc, macros such as NULL and
INT8_MAX, types such as size_t and int8_t), a name C reserves
(_X, __x), or a name starting with a prefix the generated code uses
(egg_, fn_, …) is E132. So is declaring the same C function twice
with different types. Declaring it twice with the same types is fine;
each declaration’s uses is checked against its own file.
C can do anything, so the declaration says which capabilities the
function uses after uses, and calling it uses them (E130 if its file
does not declare them). Whoever writes the declaration is responsible for
it being true.
// expect: 65
uses io
allow unsafe
extern fn toupper(c: i32) i32
fn main(io: Io) void {
io.print(toupper(97))
}
C strings end in a 0 byte; a str has a length instead and no 0. Two
Allocator methods convert, copying:
a.cstr(s) *u8 is s followed by a 0 (C sees s up to its first 0 byte), in
memory that is never freed,
and a.str_from_c(p) Buf(u8) is the bytes at p up to the first 0. Making a
pointer is safe, but a.str_from_c reads through one, so it needs
allow unsafe (E131); on null it panics null pointer dereference.
Where a C function takes a pointer and a length, @strPtr(t) and
@len(t) pass text without copying: a str, a []u8 or a Buf(u8). For
a C string that is freed, end the text with a 0 yourself,
a.concat(s, "\0"), and pass @strPtr of that Buf(u8). Where C fills
memory, give it @bufPtr(b) of a buffer made long enough first, as
a.alloc(n, 0). Where C hands back a pointer and a length,
@sliceFromPtr(p, n) (n an i64) lets the rest of the function read them as a slice,
without copying: as an argument (a.append_str(&out, @sliceFromPtr(p, n))
copies them into a buffer) or as a local slice const s =
@sliceFromPtr(p, n). Nothing checks how long C keeps the memory valid. @strPtr and @bufPtr
borrow a buffer as @len does: it must be a variable or part of one, not a
temporary (E133), and since C may write through @bufPtr(b), no local slice
of b may be used after it (E149). The pointer itself is not tracked: freeing
or resizing the buffer while C still holds it is the unsafe file’s
responsibility.
// expect: HELLO
uses io, alloc
allow unsafe
extern fn toupper(c: i32) i32
fn main(io: Io, a: Allocator) void {
const p = a.cstr("hello")
var i = 0
while (@ptrAdd(p, i).* != 0) {
const c = @ptrAdd(p, i)
c.* = @as(u8, toupper(@as(i32, c.*)))
i = i + 1
}
const up = a.str_from_c(p)
defer a.free(up)
io.print(up)
}
A C function outside the C library is linked with link, also only in
an allow unsafe file:
allow unsafe
link "sqlite3"
extern fn sqlite3_libversion_number() i32
eggc build and eggc run pass -lsqlite3 to the C compiler for every
link in every file the program loads, so a program that imports a
module wrapping a library links it without saying so. The name is what
follows -l: letters, digits and _ . + -, not starting with ., +
or - (E138). eggc’s own -l LIB links one more library, and -L DIR
tells the C compiler where to look.
A struct C knows is declared extern struct:
allow unsafe
const Timespec = extern struct { sec: i64, nsec: i64 }
extern fn clock_gettime(clock: i32, ts: *Timespec) i32
Its fields are C types (integers, floats, bool, pointers to C types,
function types, arrays of C types and other extern structs; anything else
is E154), in the order written, so the C compiler lays it out as it lays
out the C library’s struct with the same fields. An extern fn takes and
returns one by value, as C does, or by pointer. A plain struct does not
cross into C, by value or behind a pointer (E132): it may hold str,
slices or buffers, which C cannot read, and Egg keeps its layout to
itself. In Egg code an extern struct is a struct like any other, with
methods; only an argument larger than 16 bytes differs, going by value
where a plain struct would go by pointer, so a function C calls through a
function value receives it as C passes it.
// expect: 3
uses io
const Point = extern struct {
x: i32,
y: i32,
fn sum(self: Point) i32 {
return self.x + self.y
}
}
fn main(io: Io) void {
io.print(Point{ .x = 1, .y = 2 }.sum())
}