veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

ffi

lib/ffi.vk

lib/ffi.vk Opt-in .ffi foreign shared library boundary and deterministic C-struct packing helpers. - Denied by default at compile time and runtime unless allow-caps = [.ffi] in veko.vk or --allow-ffi - Strictly forbidden on Wasm 3.0 zero-trust targets (--wasm) - Zero-W^X static AOT C-ABI trampolines (Win64, Linux x86_64 dynamic ELF64, macOS arm64 libSystem) - Enforces .secret firewall: .secret values can never cross ffi/open, ffi/sym, or ffi/call

Library struct
signature
Library = struct { handle i64, path str }

Handle to a dynamically loaded foreign shared library.

Symbol struct
signature
Symbol = struct { addr i64, name str }

Handle to a resolved foreign C symbol pointer.

signature
candidate-paths = fn [name str, veko_lib_env str, is_win bool] block[str]

Splits VEKO_LIB on ; (Windows) or : (POSIX) and returns candidate shared-library paths before falling back to name for standard OS dynamic loader resolution.

Executable Example
veko
use [ffi]
cands = ffi/candidate-paths 'custom_ext' '/opt/veko/lib:/usr/local/lib' false
resolve-lib-path fn caps: .env .fs
signature
resolve-lib-path = fn [name str] str

Resolves name against VEKO_LIB directories on disk when present, otherwise returns name.

open-lib fn caps: .env .ffi
signature
open-lib = fn [path str] Library

Opens a foreign shared library (LoadLibraryA on Windows, dlopen on Linux/macOS), searching VEKO_LIB directories first when configured. Requires opt-in .ffi capability.

resolve fn caps: .ffi
signature
resolve = fn [lib Library, name str] Symbol

Resolves a C symbol inside lib (GetProcAddress on Windows, dlsym on Linux/macOS).

call-c fn caps: .ffi
signature
call-c = fn [s Symbol, args block] i64

Invokes a foreign C function s with up to 6 integer/string/bytes/bool arguments using the platform's static AOT C-ABI trampoline and returns its i64 result register.

close-lib fn caps: .ffi
signature
close-lib = fn [lib Library]

Closes a loaded foreign library (FreeLibrary / dlclose).

signature
pack-i32 = fn [mut buf bytes, v i64]

Appends a 32-bit little-endian integer into buf.

signature
pack-u32 = fn [mut buf bytes, v i64]

Appends an unsigned 32-bit little-endian integer into buf.

signature
pack-i64 = fn [mut buf bytes, v i64]

Appends a 64-bit little-endian integer into buf.

signature
pack-cstr = fn [mut buf bytes, s str]

Appends a NUL-terminated UTF-8 C string into buf.

signature
pad-align = fn [mut buf bytes, align i64]

Appends pad zero bytes to align a C struct field to align bytes.

signature
unpack-u32 = fn [buf bytes, off i64] i64

Reads an unsigned 32-bit little-endian integer at byte offset off.

signature
unpack-i32 = fn [buf bytes, off i64] i64

Reads a signed 32-bit little-endian integer at byte offset off.

signature
unpack-i64 = fn [buf bytes, off i64] i64

Reads a 64-bit little-endian integer at byte offset off.

signature
unpack-cstr = fn [buf bytes, off i64] str

Reads a NUL-terminated C string starting at off.