Skip to content

feat(codegen/cpp): a typed client for the poll mode - #276

Draft
Ivansete-status wants to merge 6 commits into
feat/poll-host-cppfrom
feat/poll-cpp-client
Draft

Ivansete-status wants to merge 6 commits into
feat/poll-host-cppfrom
feat/poll-cpp-client

Conversation

@Ivansete-status

@Ivansete-status Ivansete-status commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #271 (base is feat/poll-host-cpp; retarget to master once #271 merges). Second of the four PRs (#271, this, #278, #279). Part of #266.

Under -d:ffiPollMode the C++ generator now emits:

  • the poll-shaped extern "C" declarations,
  • a <Lib>Ctx class over nim_ffi::Host: create(...), one typed method per proc (encode, Host::call, decode; static procs through a host attached to the library's static context), a typed addOn<Event>Listener matched by name id, and runLoop(), poll(), stop(), fd() and host() forwarded from the host,
  • nim_ffi_host.hpp and nim_ffi.h beside the header.

Methods return errors that name the call ("echo: ..."), and an event payload that cannot be decoded is reported on stderr. The fixed C++ lives in three templates (poll_exports, poll_context, poll_items); the emitter only fills in what depends on the library's procs.

Not here: typed reverse-call handlers (#278) and an example (#279).

Callback mode is unchanged: the shared top of the header and the callback client moved into procs verbatim, and the generated output is byte-identical for a sample library (checked by diff).

Test: tests/unit/test_cpp_poll_codegen.nim generates the header and builds it (ASan/UBSan where available) against a scripted fake library: create, a typed call, a static call that fails before the static context exists and then succeeds, an event ahead of the reply, listener removal, poll() and runLoop()/stop(). #279 runs it against a real library.

🤖 Generated with Claude Code

Ivansete-status and others added 4 commits November 20, 2026 14:46
A -d:ffiPollMode library hands out messages through <lib>_poll and never
calls the host back, so each host needs its own pump. Ship the C++ one as
headers: it owns no thread, which leaves the host free to choose its own
event loop, and a call pumps on the caller until its reply.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A timed-out call's late reply would otherwise leak; ctx is read across
threads. Adds a scripted fake-library test and says "poll mode".

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A closed context left submit callbacks waiting forever and a poll that
reported CLOSED left drain() spinning on a context already gone, so both
now mark the context closed and tell every pending callback. A library
reply carrying RET_TIMEOUT was mistaken for the host's own deadline and
left a stale entry behind; the host now records its deadline explicitly.
drain() no longer blocks an event loop behind another thread's call().
The header's message block is now the library's own cMsgDecl output,
checked byte for byte, since only names and values were compared before
and the docs had drifted. The usage example moves into the header
comment: the repo takes no new .md files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
call() and submit() take raw CBOR and the library's export pointers, so a
reader should know a typed client is meant to wrap them rather than users
calling them directly.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ivansete-status and others added 2 commits November 21, 2026 02:22
A C++ developer should not write the OS-specific wait to serve the queue.
runLoop() blocks the calling thread until stop(), for an app with no loop;
poll() serves what is waiting and returns, for a loop that calls it each
iteration or when fd() is readable. Neither starts a thread, so the
developer decides which thread serves; ~Host stops and waits for a running
runLoop(). attach() serves a context the library owns (its static
context) without destroying it. Handlers run on the serving thread, so Host
times each one and reports one that keeps the queue past a limit. The
header comment is one pass through what a reader cannot guess.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Under -d:ffiPollMode the generated C++ header declares the poll-shaped
exports and a context class over nim_ffi::Host, so a host calls typed
methods instead of passing raw CBOR and export pointers to Host::call, and
gets typed event listeners matched by name id. Static procs go through a
host attached to the library's static context, and the class forwards
runLoop(), poll() and stop(). The fixed C++ lives in three templates and the
emitter fills in what depends on the library's procs. The callback model's
output is byte-identical to before.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant