Skip to content

feat(ffi): native event delivery + dual-ABI event symbols (C + Go)#62

Draft
Ivansete-status wants to merge 3 commits into
ffi-native-typed-returnsfrom
native-events
Draft

feat(ffi): native event delivery + dual-ABI event symbols (C + Go)#62
Ivansete-status wants to merge 3 commits into
ffi-native-typed-returnsfrom
native-events

Conversation

@Ivansete-status

Copy link
Copy Markdown
Collaborator

What

Foundation for emitting both the native (zero-serialization C-POD) and CBOR ABIs side-by-side for {.ffiEvent.} events, completing the request-side dual ABI from the earlier PRs in this stack.

  • Dual event dispatch — one {.ffiEvent.} declaration now fires both a native typed-POD listener and a CBOR listener from a single dispatch. Native listeners get the payload built once via nimToPod/freePod; CBOR is serialized once for the rest.
  • Dual-ABI symbol naming mirrors requests: bare <lib>_add_event_listener is native, <lib>_add_event_listener_cbor is CBOR.
  • -d:ffiMode=native|cbor|both strdefine (default both) gates which ABI each generator emits, with genbindings_*_native / _cbor nimble tasks.
  • C generator documents native event payloads ("on_echo_fired" -> const EchoEvent *) and routes the CBOR header to _add_event_listener_cbor.
  • Go generator emits per-event typed native handlers On<Event>(func(<Payload>)) with exported callbacks + fromC.

Native is the same-process path; CBOR is IPC-only.

Test

test_native_events, test_native_pod_types, and the C native+CBOR concurrency stress harness pass (ASAN/TSAN clean).

🤖 Generated with Claude Code

Ivansete-status and others added 3 commits May 31, 2026 18:37
Events now mirror the native/CBOR split already in place for requests, with the
same symbol-naming convention:

- `<lib>_add_event_listener`      -> NATIVE listener (typed `<T>Pod` by pointer)
- `<lib>_add_event_listener_cbor` -> CBOR listener (EventEnvelope bytes)

Framework: `FFIEventListener` gains a `native` flag; `addEventListener` a
`native` param; a new `dispatchFFIEventDual` template builds the `<T>Pod` once
for native listeners (`nimToPod`/`freePod`) and the CBOR envelope once for the
rest, fanning each out — so a single `{.ffiEvent.}` dispatch serves both kinds.
`declareLibrary` exports both registration entry points.

Generators: the bare `<lib>_add_event_listener` is the native symbol; every
CBOR consumer (C/C++/Go/Rust) now targets `<lib>_add_event_listener_cbor`. The
rename and the generator updates ship together so the bare name is never briefly
broken. Bindings regenerated.

Validated: native-event unit test (typed POD to native + CBOR to cbor listener,
orc/refc/ASAN); full unit suite; C++ e2e 19/19; Go example; existing event
tests unchanged. The per-event *typed* native callback + wildcard router (the
ergonomic consumer surface) is a follow-up.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Adds the ergonomic native event surface for Go: `node.On<Event>(func(<Payload>))`
registers a native listener for that event and the library delivers the typed
`<Payload>` POD, which an exported Go callback reads into a Go struct (reusing
the generated `fromC`) and hands to the user's handler — no CBOR parsing. Sits
beside the existing CBOR `SetEventHandler` (wildcard / inter-process).

The example registers `OnEchoFired` and receives a typed `EchoEvent` when Echo
fires it. Verified end-to-end with `go run -race`.

C/C++/Rust get the same per-event typed handler + router next.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…) + tasks

- Add the `-d:ffiMode=native|cbor|both` strdefine (default both) with
  `ffiEmitNative`/`ffiEmitCbor` helpers; the C generator now emits only the
  selected header(s) (`<lib>.h` and/or `<lib>_cbor.h`).
- Native C events: the native header documents each event's payload type
  (`"on_echo_fired" -> const EchoEvent *`) so consumers cast the callback's msg
  to the typed struct — the bare native listener already delivers it.
- nimble tasks: `genbindings_c` (both), `genbindings_c_native`,
  `genbindings_c_cbor`.

Verified: native mode emits only my_timer.h, cbor only my_timer_cbor.h, both
emits both.

Co-Authored-By: Claude Opus 4.8 <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