Commit Graph
25 Commits
Author SHA1 Message Date
Ivan FBandClaude Opus 4.8 eb62813af5 refactor(host): rename the host-call token to callId
"token" was overloaded (auth tokens, cgo handles, lexer tokens) and didn't say
what it is — a per-call correlation id linking an outgoing {.ffiHost.} call to
the answer that arrives later (possibly from another thread). Renamed across the
runtime (ffi_host / ffi_context), the macro, the exported C ABI (FFIHostFn,
<lib>_host_complete), the Go trampoline, and the tests; regenerated bindings.

The unrelated request-path cgo.Handle result-slot (also informally called a
"token" in go.nim comments) is left as-is — different mechanism.

16 host unit tests + the examples/host_demo Go round-trip stay green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 00:40:29 +02:00
Ivan FBandClaude Opus 4.8 556599787c feat(host): {.ffiHost.} macro (raw string round-trip)
Increment 3: the {.ffiHost.} pragma. A bodyless
  proc fetchToken(key: string): Future[Result[string, string]] {.ffiHost.}
expands into an async proc that resolves the thread-local host registry +
pending table, looks the fn up by snake_case wire name, allocates a token,
invokes the host with the raw request bytes, and awaits the answer.

This is the inverse of {.ffi.} and the first end-to-end use of the registry
(increment 1) + completion bridge (increment 2). First slice is deliberately
narrow — raw ABI, one string param, Future[Result[string, string]] — to prove
the round-trip with zero serialization; struct params/returns and the
{.ffiHost: cbor.} format arg are follow-ups.

The body reads two new threadvars (ffiCurrentHostRegistry / ffiCurrentPendingTable)
set by ffiThreadBody alongside ffiCurrentEventRegistry, so the user's signature
stays ctx-free. The host fn is invoked synchronously before the await, while the
string arg is still alive (honouring the "req valid only for the call" contract).

5 macro tests pass under orc+refc; host + ffi_context suites stay green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 23:08:18 +02:00
Ivan FBandClaude Opus 4.8 c90200de2b feat(host): FFIContext completion bridge for {.ffiHost.}
Increment 2: wires the host-call machinery into the running FFI thread so a
host answer (delivered from any thread) resolves the chronos Future an awaiting
handler is blocked on.

- FFICompletionQueue (ffi_host.nim): a GC-free intrusive queue. host_complete
  pushes c_malloc'd nodes from any thread; the FFI thread drains, copies the
  payload into GC memory, completes the future by token, and frees the node.
- FFIContext gains hostRegistry / pendingTable / completionQueue, init'd and
  deinit'd alongside the event registry.
- completeHostCall parks the answer and fires the EXISTING reqSignal — no second
  ThreadSignalPtr needed; the loop drains completions every iteration, on the
  loop thread (chronos single-thread invariant).
- On shutdown the loop failAllPendings first, so a handler awaiting a host
  answer that never arrives can't hang the allFutures(pending) drain.

4 new queue unit tests (10 total) pass under orc+refc; the 19 ffi_context
integration tests stay green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 22:28:58 +02:00
Ivan FB 496a341466 Event registry wiring (#49) 2026-05-27 22:26:39 +02:00
Ivan FBandGabriel Cruz e99220a3e4 fix use-after-free concern (#47)
Co-authored-by: Gabriel Cruz <8129788+gmelodie@users.noreply.github.com>
2026-05-26 23:46:27 +02:00
Ivan FB 6a7e4616fd Adjust events to cbor (#39) 2026-05-25 15:51:56 +02:00
Gabriel Cruz ee472f05ad chore(ci): fsanitize tests (#34) 2026-05-20 14:14:42 -03:00
ac303a707e Start using CBOR (#23)
Co-authored-by: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com>
Co-authored-by: Gabriel Cruz <8129788+gmelodie@users.noreply.github.com>
2026-05-16 01:08:42 +02:00
Ivan FB 6d31fa30bd use fixed array of ctx to avoid consuming all fds (#14) 2026-05-13 00:02:23 +02:00
Gabriel Cruz 81c62c263e fix: context buffer overflow (#21) 2026-05-11 19:21:40 -03:00
Ivan FB a52c4facd9 Simplified FFI authoring with auto-generated C++ and Rust bindings (#15) 2026-05-11 23:28:17 +02:00
Gabriel Cruz 37d19c0687 chore: remove CatchableError (#19) 2026-05-08 14:14:16 -03:00
Ivan FB df2277e726 Fix memleaks (#11)
* protect against mem leak in case of failures sending requests to ffi thread
* better cleanup if failures in createFFIContext
* avoid dangling cstring in handleRes under ARC/ORC
* better resource cleanup in destroyFFIContext
* invoke onNotResponding if failure in destroyFFIContext
* correct seq copy in alloc
* make sure the lock is init before cleanUpResources
* better possible exception handling in processReq
* guard allocSharedSeq if given seq is empty
* enhance error handling in ffi_context
* add new tests and some corrections
2026-04-27 21:22:45 +02:00
Ivan Folgueira Bande bb8ed28ab0 add require chronicles taskpools and import std tables 2026-01-23 15:01:08 +01:00
Ivan FB 9fff004b19 add explicit timeout when sending watchdog requests (#7) 2026-01-16 15:48:35 +01:00
Ivan Folgueira Bande d7a5492121 avoid use gc ed types in FFIContext and better macro documentation 2025-12-13 23:53:59 +01:00
Ivan Folgueira Bande 803744dd29 rm some Waku leftover references 2025-12-11 23:50:00 +01:00
Ivan Folgueira Bande e0cf0c8842 fixes for proper proc exposure to c 2025-12-11 17:11:59 +01:00
Ivan Folgueira Bande 142a57f543 set logger in main thread body 2025-12-11 17:10:24 +01:00
Ivan Folgueira Bande da3251aa1a evolving more 2025-12-10 17:43:26 +01:00
Ivan Folgueira Bande 86dc58e7c2 add ffi macro 2025-12-09 18:51:50 +01:00
Ivan Folgueira Bande a1a6536b3c general ffi increments 2025-09-17 14:37:45 +02:00
Ivan Folgueira Bande 356f0ccc1b working simplification 2025-09-17 14:37:36 +02:00
Ivan Folgueira Bande 46e51e45a6 more positive progress. Getting closer 2025-09-17 14:37:28 +02:00
Ivan Folgueira Bande bbddf6925b First commit 2025-09-17 14:37:11 +02:00