
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0005: FFI and native dependencies
Status: Accepted — 2026-10-08, reviewed and accepted by the project owner (issue #2).
Context
Programs must be able to use existing C and Rust libraries, e.g. argon2, HTTP parsers or large prebuilt libraries, without rewriting them.
The compiler emits C (ADR 0003), so the C ABI is the natural boundary.
Decision
Declaring an extern
An @extern attribute goes on a function declared without a body:
@extern c "argon2id_hash_encoded" blocking
fn argon2_hash ::
pw: String
salt: Bytes
t: Int
m: Int
p: Int
-> Result<String, Int> / <ffi>
Effects: the declared row says what the call performs, and <ffi> is the conventional effect for foreign calls. An omitted row means total, as for every function (ADR 0008). Leaving it out is the binding author's explicit trust decision, used for pure built-ins such as integer arithmetic.
Types allowed at the boundary:
| Louise type | C side |
|---|---|
Int, Float, Bool | unboxed |
String, Bytes | borrowed ptr + len for the duration of the call |
| owned results | built by the C side with tl_string_from / tl_bytes_alloc |
Resource<t> | an opaque handle with a finalizer, marked thread_safe or not (ADR 0004) |
Results: a Result-shaped return is built by a thin generated wrapper.
Blocking calls: blocking sends the call to the blocking thread pool, so the scheduler is never stalled. This is the right choice for argon2 and similar CPU-heavy or IO-heavy calls.
Timing:
- Extern calls arrive in M5, because
stditself callslouise_rtthis way. - Native dependencies (below) arrive in M7.
Native dependencies
They are declared in louise.toml:
[native.argon2]
kind = "c" # c | static | pkg-config | cargo
sources = ["vendor/argon2/src/*.c"]
include = ["vendor/argon2/include"]
kind | How it is built |
|---|---|
c | Compiled by the driver using the same C compiler as the generated code. |
static | Links a prebuilt .a (the "big libs as binaries" case). |
pkg-config | Uses system libraries. |
cargo | Builds a staticlib crate exporting extern "C" functions. |
Rust libraries use the cargo kind. The louise-ffi helper crate provides the C-ABI string, bytes and resource types and the allocation entry points, so Rust bindings never hand-write layouts.
Consequences
- Binding a C library takes one
@externper function plus a manifest entry; no glue code is needed. - Callbacks from C back into Louise are out of scope for v0. They need re-entry into the scheduler (v1: ADR 0015).
- Header-driven binding generation (bindgen-style) is deferred (v1: ADR 0015).
Alternatives considered
- A Rust-ABI or
cxx-style bridge. It would tie the language to Rust's unstable ABI. The C ABI works for both C and Rust. - Dynamic loading (
dlopen) only. Simpler, but it gives up static single-binary deployment.
Revision 1 — 2026-10-09: what M7 implemented (proposed)
Status: Accepted by the project owner on 2026-10-12; needs engineering review (#44). These are details decided while implementing M7.
- Extern convention.
The rule: a function bound with
@extern(c, "sym")has the convention oflouise_rt.h:tl_value sym(tl_ctx *ctx, tl_value a, ...), with borrowed arguments and an owned result.Why not generated marshalling: the table above (
Stringasptr + len,Resultbuilt by a generated wrapper) is replaced by a few lines of C per bound library (the binding's shim).louise_rt.hgives the shim what it needs:tl_string_ptrandtl_string_len;tl_string_cstrandtl_int;tl_ok,tl_error,tl_someandTL_NONE;tl_resource_newandtl_resource_get.
One convention covers every type, including
Result-shaped results, without per-type wrappers in the compiler. A generator for simple signatures can come later.
blocking:- Where the call runs: on a pool of four threads while the actor is parked (two pools since Revision 2).
- Kills: a kill of that actor waits until the call returns, because the C code still reads the actor's values.
- Restrictions: a blocking extern must not panic, perform operations or drop values.
Resource<t>lives instd/ffi. Its block holds the pointer and the finalizer; the finalizer runs when the last reference is dropped. A resource that is notthread_safeis pinned.- Packages:
[dependencies] name = { path = "..." }loads a package's modules asname(itssrc/lib.lse) andname/.... A package's ownlib.lsehas the same name.- The manifest's
[native.*]entries of the whole dependency tree are built and linked.
kind = "cargo":- the driver runs
cargo build --release; - the Rust standard library inside the resulting
staticlibduplicates the copy inliblouise_rt.a, so the link uses--allow-multiple-definition; runtime/louise-ffiprovides the C API to Rust bindings.
- the driver runs
louise test --nativebuilds one executable for all tests and runs each test in its own process. Packages with native dependencies need it, because the interpreter cannot call C.- First binding:
packages/argon2vendors the reference implementation (release 20190702) and is tested against a known answer computed independently with argon2-cffi.
Revision 2 — 2026-10-23: blocking pools (issue #305)
Status: Proposed; needs engineering review (#44).
Revision 1's pool had four threads, fixed, one queue, invisible to programs. toki2's stress test (22 cores, argon2id at 19 MiB) logged in at ~44 per second with a 1.13 s median, against 65 ms for one login: four hashes at a time, whatever the cores, behind a queue that grew without bound. The cap also did good: hashing kept to four threads, so browsing beside it ran at 14,000 requests per second, and memory stayed at 4 × 19 MiB. So the pool should not simply have a thread per core; it should be sized on purpose, per kind of call, and visible.
- Two pools, as Erlang's dirty CPU and dirty I/O schedulers:
@extern(c, "sym", blocking(cpu)), for calls that keep a core busy (hashing, compression). Its threads compete with the scheduler's for the same cores, and each running call holds the memory its C function uses (argon2'sm_cost): small by default, half the cores, at least 1, at most 4 (Revision 1's size), so the scheduler keeps most of the machine.LOUISE_BLOCKING_CPU_THREADSsets it.@extern(c, "sym", blocking(io)), for calls that mostly wait (file reads, DNS, a C library that sleeps): 16 threads by default, since a waiting thread costs no CPU.LOUISE_BLOCKING_IO_THREADSsets it.- Plain
blockingkeeps its meaning and iscpu;LOUISE_BLOCKING_THREADS, the shorthand, sets thecpupool (the specific variable wins). - The runtime's own:
std/file,net.resolveandtls.configareio; PBKDF2 iscpu.packages/argon2saysblocking(cpu).
- Configuration is checked at start: 0, a negative number or anything but an integer stops the program before
main, withlouise: LOUISE_BLOCKING_IO_THREADS must be a positive integer (a number of threads), got "x"and status 2, rather than silently falling back to the default (LOUISE_THREADSdoes fall back; a pool size is a capacity decision worth refusing loudly). Each pool starts its threads on its first call, as before. There is no programmatic setting: the environment serves embedders and tests alike. - Statistics:
std/runtime.blocking_stats()gives each pool's size, its running calls and its queued ones. A program can then refuse work early (toki2 answers 429 when too many password checks wait) instead of queueing users for seconds. There is no bounded variant (try_blocking, a queue limit per extern): the statistics give programs the same decision with their own policy, and a bounded queue would need an error path in every blocking extern's type. - Syntax: an attribute argument may be an identifier applied to one identifier (
blocking(io); the grammar, the tree-sitter grammar and the formatter accept it). An unknown pool is E0114. - ABI:
tl_call_blocking(ctx, pool, thunk, argc, args), withTL_BLOCKING_CPUandTL_BLOCKING_IO;TL_ABI_VERSION9. A pool belongs to a symbol: one symbol declared on two pools runs on one of them. - The interpreter accepts the new syntax and runs blocking calls inline, as before; its statistics show the configured sizes with nothing running or queued.
crates/louise/tests/blocking.rs checks, natively: LOUISE_BLOCKING_THREADS=16 runs sixteen 100 ms calls in about 100 ms (400 ms by default); a full pool does not delay a call on the other; the statistics show the queue; an invalid size is refused.
Note — 2026-10-12: Rust natives and the fast runtime (issue #133)
Status: Proposed (epic #135); needs engineering review (#44).
Release builds link liblouise_rt-fast.a (ADR 0031), whose Rust standard library is built for panic = "abort". A kind = "cargo" dependency still brings its own, built for unwinding, and the link keeps the first definition of each duplicate (--allow-multiple-definition; the natives come before the runtime). The native fixture gives the same output with both runtimes, and native_dependencies_of_every_kind checks it. A panic in a Rust native was already undefined at the C boundary (extern "C" functions abort on unwinding since Rust 1.81), so panic = "abort" doesn't change what a binding may do. Building a cargo native and the runtime as one Rust program, with one std, waits for a package that needs it.