nim-ffi/ffi/ffi_types.nim

33 lines
1.1 KiB
Nim
Raw Normal View History

2025-12-09 18:51:50 +01:00
import std/tables
2025-09-02 23:47:08 +02:00
import chronos
2025-08-09 22:56:44 +02:00
type FFICallBack* = proc(
callerRet: cint, msg: ptr cchar, len: csize_t, userData: pointer
) {.cdecl, gcsafe, raises: [].}
2026-07-15 11:46:05 -03:00
## Result-delivery callback. `RET_OK`/`RET_ERR` fire once and end the request;
## `RET_STALE_WARN` may fire repeatedly before them.
2025-08-09 22:56:44 +02:00
const RET_OK*: cint = 0
const RET_ERR*: cint = 1
const RET_MISSING_CALLBACK*: cint = 2
const RET_STALE_WARN*: cint = 3
2026-07-15 11:46:05 -03:00
## Non-terminal: request still in flight, fires every `StaleWarnInterval` with
## `msg` = elapsed ms as decimal ASCII, always followed by a terminal code.
2025-08-09 22:56:44 +02:00
type FFIRequestProc* = proc(
request: pointer, reqHandler: pointer
): Future[Result[seq[byte], string]] {.async.}
2026-07-15 11:46:05 -03:00
## OK payload is a CBOR-encoded response body; errors are plain UTF-8.
2025-09-02 23:47:08 +02:00
2025-08-09 22:56:44 +02:00
template foreignThreadGc*(body: untyped) =
when declared(setupForeignThreadGc):
setupForeignThreadGc()
body
when declared(tearDownForeignThreadGc):
tearDownForeignThreadGc()
2026-07-15 11:46:05 -03:00
## Compile-time-populated table: request type name (cstring) -> async handler.
var registeredRequests*: Table[cstring, FFIRequestProc]