LEVIATHAN vd52e79c4 · d52e79c486

Standard Library · ffi

attribute extern

Declares a foreign (C) function.

since 0.1.0-alpha.1linuxwindowswasm

Overview

Put it on a free function with an empty body (;). The first argument names the library the function comes from: the nickname of a [[native]] table in trident.toml, or "c" / "m" for the C and maths libraries, which are always available. The other fields may be given by position or by name.

Examples

Declaring C functions

uses ffi;
use io::console;

@extern("c", "labs")
int absolute(int n);

@extern("c", "atoi")
int32 parseInt(string s);

console.writeln(absolute(-12));
console.writeln(parseInt("42"));
12
42

Fields

errno

bool errno

Capture the C errno right after each call, for ffi::errno().

lib

string lib

The library nickname; it may not be empty.

slots

int slots

How many callbacks of each callback-parameter signature may be live at once.

symbol

string symbol

The C symbol; empty means the function's own name.

variadic

int variadic

The index of the first variadic parameter, or -1 when the function is not variadic.

See also

  • mayCallback — Asserts that a foreign function may call back into Leviathan through a callback registered earlier.
  • shallow — Asserts that a foreign function uses little stack, so it runs on the task's own stack with no stack switch and no headroom check.
  • reentrant — Asserts that a foreign function tolerates another task entering its library while a callback from that library is waiting.