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
|
2026-07-14 17:07:33 +02:00
|
|
|
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
|
|
|
|
2026-05-16 01:08:42 +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.
|
2025-12-12 14:45:59 +01:00
|
|
|
var registeredRequests*: Table[cstring, FFIRequestProc]
|