text

source of std::ffi

1// Values C holds for Alloy (LANGUAGE_SPEC.md section 6.3): a C library2// that calls back through an `extern (...) -> R` function often passes3// along a `void *` its caller gave it. `c_pointer` is that pointer: made4// from a reference, read back as one by the callback, which names the5// type it was made from.67import std::process;8import std::number;9import std::option;1011exp type c_pointer = if (wasm32()) u32 else u64; // an address's width1213// C's integer types whose width follows the target (section 6.3), for14// externs to write as the C header does: `size_t` and `uintptr_t` are 3215// bits on wasm32 and 64 elsewhere, `ssize_t` and `ptrdiff_t` likewise16// signed, and `long` is 32 bits on Windows and wasm32, 64 on Linux and17// macOS (`unsigned long` alike).18exp type c_size : Number = if (wasm32()) u32 else u64;19exp type c_ssize : Number = if (wasm32()) i32 else i64;20exp type c_long : Number = if (wasm32() || target() == "windows-x86_64") i32 else i64;21exp type c_ulong : Number = if (wasm32() || target() == "windows-x86_64") u32 else u64;2223fn wasm32() -> bool {24    return target() == "wasi-wasm32" || target() == "web-wasm32";25}2627type Shared<T> = struct {28    target: &T,29};3031type Unique<T> = struct {32    target: &var T,33};3435// The address of the value, for C to pass back.36exp fn c_pointer::from<T>(target: &T) -> c_pointer {37    const held = Shared<T> { .target = &target };38    return held as c_pointer;39}4041// The value the pointer was made from, which must be a `T` still alive.42exp fn as_ref<T>(self p: &c_pointer) -> &T {43    const held = p as Shared<T>;44    return &held.target;45}4647// The value the pointer was made from, to change; it must be a `T` still48// alive, made from a `&var` reference.49exp fn as_var<T>(self p: &c_pointer) -> &var T {50    const held = p as Unique<T>;51    return &var held.target;52}5354type Kept<T> = struct {55    held: *var T,56};5758// Hands the heap value to C, which holds it until `take` gives it back:59// its address, for callbacks to reach it through `as_var` and `as_ref`.60// Nothing frees it meanwhile, so state a browser calls back after `main`61// returns stays alive (std::web).62exp fn c_pointer::keep<T>(value: *var T) -> c_pointer {63    return Kept<T> { .held = move value } as c_pointer;64}6566// The heap value `c_pointer::keep` handed to C, owned again.67exp fn take<T>(self p: &c_pointer) -> *var T {68    var raw = p;69    var kept = move raw as Kept<T>;70    return move kept.held;71}7273type Elements<T> = struct {74    target: &[T],75};7677type VarElements<T> = struct {78    target: &var [T],79};8081type Span = struct {82    address: c_pointer,83    length: u64,84};8586// The `length` values of type `T` from the address, memory C gave and87// keeps alive while they are used: an array a callback receives, a88// mapped file.89exp fn view<T>(self p: &c_pointer, length: u64) -> &[T] {90    const held = Span { .address = p, .length = length } as Elements<T>;91    return &held.target;92}9394// The `length` values of type `T` from the address, to change: a buffer C95// gave to fill.96exp fn view_var<T>(self p: &c_pointer, length: u64) -> &var [T] {97    const held = Span { .address = p, .length = length } as VarElements<T>;98    return &var held.target;99}100101// The C string at the address, up to its NUL.102exp fn c_text(self p: &c_pointer) -> &[u8] {103    var length: u64 = 0;104    while ((p + length to c_pointer).view<u8>(1)[0] != 0) {105        length += 1;106    }107    return &p.view<u8>(length);108}109110// The address of the elements' first, for C to read or fill them; 0 when111// there are none.112exp fn c_pointer::of<T>(elements: &[T]) -> c_pointer {113    const held = Elements<T> { .target = &elements } as Span;114    return if (elements.length() == 0) 0 else held.address;115}116117// What every C function type, `extern (T) -> R`, and every type named118// over one satisfy with no marker, and nothing else (a lang item): the119// types a library's symbols are read as.120exp interface CFunction {}121122extern al_sys_library_open(path: c_string) -> c_pointer;123extern al_sys_library_symbol(library: c_pointer, name: c_string) -> c_pointer;124125// A shared library loaded while the program runs (section 6.3): a `.so`126// on Linux and Android, a `.dylib` or framework on Apple systems, a DLL127// on Windows. It stays loaded until the program ends. On Linux a program128// that loads one links against glibc dynamically, since a static program129// cannot load libraries; WebAssembly has none to load.130exp type Library = struct {131    handle: c_pointer132};133134// The library at the path, or found by the system's search for a bare135// name (`libvulkan.so.1`, `vulkan-1.dll`); None when it cannot be loaded.136exp fn Library::open(path: &[u8]) -> Option<Library> {137    const handle = al_sys_library_open(&terminated(&path));138    if (handle == 0) {139        return ::None;140    }141    return ::Some(Library { .handle = handle });142}143144// The library's function of that name, called through the C function145// type `F`, which must be its C prototype; None when the library has no146// such symbol. One symbol may be read as several types, as Objective-C's147// `objc_msgSend` is, a prototype per message.148exp fn symbol<F: CFunction>(self library: &Library, name: &[u8]) -> Option<F> {149    const address = al_sys_library_symbol(library.handle, &terminated(&name));150    if (address == 0) {151        return ::None;152    }153    return ::Some(copy address as F);154}155156// The text with a NUL after it, as C reads it.157fn terminated(text: &[u8]) -> *[u8] {158    var out: *var [u8] = new [0 : text.length() + 1];159    out[..text.length()] = text;160    return move out;161}