|
| 1 | +/* SPDX-License-Identifier: MPL-2.0 |
| 2 | + * Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | + * |
| 4 | + * C ABI for libvalence_shell — the stable boundary that any language may call. |
| 5 | + * |
| 6 | + * This header is the C projection of the ABI/FFI spine: |
| 7 | + * |
| 8 | + * src/abi — Idris2 ABI declarations (%foreign) |
| 9 | + * │ |
| 10 | + * ▼ |
| 11 | + * ffi/zig/src/main.zig — Zig FFI implementation (`export fn`) |
| 12 | + * │ compiled to libvalence_shell.{so,a} |
| 13 | + * ▼ |
| 14 | + * THIS HEADER → C, Rust, ReScript, Julia, … |
| 15 | + * |
| 16 | + * NOTE ON PROVENANCE: this file is HAND-MAINTAINED to match the `export fn` |
| 17 | + * signatures in ffi/zig/src/main.zig and the `%foreign` declarations in |
| 18 | + * src/abi/Foreign.idr. Older docs describe an `idris2 --cg c-header` step, but |
| 19 | + * Idris2 ships no such codegen (only chez/node/racket/refc), so the header is |
| 20 | + * authored by hand. When you add or change a Zig `export fn`, update this |
| 21 | + * header in the same commit. See docs/ABI-FFI-BOUNDARY.md. |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef VALENCE_SHELL_H |
| 25 | +#define VALENCE_SHELL_H |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +#ifdef __cplusplus |
| 30 | +extern "C" { |
| 31 | +#endif |
| 32 | + |
| 33 | +/* Opaque library handle. Its layout is private to the Zig implementation; |
| 34 | + * C consumers only ever hold a pointer to it. (Matches `?*Handle`.) */ |
| 35 | +typedef struct ValenceShellHandle ValenceShellHandle; |
| 36 | + |
| 37 | +/* Result codes. Backed by `c_int` to match `enum(c_int)` in main.zig and the |
| 38 | + * Idris2 `Result` type in src/abi/Types.idr. */ |
| 39 | +typedef enum ValenceShellResult { |
| 40 | + VALENCE_SHELL_OK = 0, |
| 41 | + VALENCE_SHELL_ERROR = 1, |
| 42 | + VALENCE_SHELL_INVALID_PARAM = 2, |
| 43 | + VALENCE_SHELL_OUT_OF_MEMORY = 3, |
| 44 | + VALENCE_SHELL_NULL_POINTER = 4 |
| 45 | +} ValenceShellResult; |
| 46 | + |
| 47 | +/* Callback invoked by the library. Matches |
| 48 | + * `*const fn (u64, u32) callconv(.C) u32`. */ |
| 49 | +typedef uint32_t (*ValenceShellCallback)(uint64_t context, uint32_t value); |
| 50 | + |
| 51 | +/* --- Lifecycle -------------------------------------------------------------- */ |
| 52 | + |
| 53 | +/* Initialize the library. Returns a handle, or NULL on failure. */ |
| 54 | +ValenceShellHandle *valence_shell_init(void); |
| 55 | + |
| 56 | +/* Release a handle previously returned by valence_shell_init. */ |
| 57 | +void valence_shell_free(ValenceShellHandle *handle); |
| 58 | + |
| 59 | +/* Non-zero if `handle` refers to an initialized library instance. */ |
| 60 | +uint32_t valence_shell_is_initialized(ValenceShellHandle *handle); |
| 61 | + |
| 62 | +/* --- Operations ------------------------------------------------------------- */ |
| 63 | + |
| 64 | +/* Process a single 32-bit input. */ |
| 65 | +ValenceShellResult valence_shell_process(ValenceShellHandle *handle, |
| 66 | + uint32_t input); |
| 67 | + |
| 68 | +/* Process `len` bytes at `buffer`. */ |
| 69 | +ValenceShellResult valence_shell_process_array(ValenceShellHandle *handle, |
| 70 | + const uint8_t *buffer, |
| 71 | + uint32_t len); |
| 72 | + |
| 73 | +/* Register a C callback. */ |
| 74 | +ValenceShellResult valence_shell_register_callback(ValenceShellHandle *handle, |
| 75 | + ValenceShellCallback callback); |
| 76 | + |
| 77 | +/* --- Strings ---------------------------------------------------------------- */ |
| 78 | + |
| 79 | +/* Borrow the handle's current result string, or NULL. The returned pointer is |
| 80 | + * owned by the library; free it with valence_shell_free_string. */ |
| 81 | +const char *valence_shell_get_string(ValenceShellHandle *handle); |
| 82 | + |
| 83 | +/* Free a string previously returned by the library. */ |
| 84 | +void valence_shell_free_string(const char *str); |
| 85 | + |
| 86 | +/* --- Diagnostics ------------------------------------------------------------ */ |
| 87 | + |
| 88 | +/* Last error message for the calling thread, or NULL if none. Owned by the |
| 89 | + * library; do not free. */ |
| 90 | +const char *valence_shell_last_error(void); |
| 91 | + |
| 92 | +/* Library version string (never NULL). Owned by the library; do not free. */ |
| 93 | +const char *valence_shell_version(void); |
| 94 | + |
| 95 | +/* Build-info string (never NULL). Owned by the library; do not free. */ |
| 96 | +const char *valence_shell_build_info(void); |
| 97 | + |
| 98 | +#ifdef __cplusplus |
| 99 | +} /* extern "C" */ |
| 100 | +#endif |
| 101 | + |
| 102 | +#endif /* VALENCE_SHELL_H */ |
0 commit comments