feat: {.ffiHandle.} — export complex live objects as opaque handles#81
Merged
Conversation
gmelodie
force-pushed
the
feat/ffi-handle
branch
2 times, most recently
from
June 12, 2026 15:18
deb3be4 to
772aab2
Compare
gmelodie
marked this pull request as ready for review
June 12, 2026 18:04
gmelodie
force-pushed
the
feat/ffi-handle
branch
from
June 12, 2026 18:09
5d5af14 to
9135f24
Compare
gmelodie
force-pushed
the
feat/ffi-handle
branch
from
June 13, 2026 01:44
9135f24 to
a2302c2
Compare
(cherry picked from commit 498416b)
gmelodie
force-pushed
the
feat/ffi-handle
branch
from
June 16, 2026 14:54
a2302c2 to
2947813
Compare
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
gmelodie
force-pushed
the
feat/ffi-handle
branch
from
June 16, 2026 15:40
db371b4 to
af47e4b
Compare
Ivansete-status
approved these changes
Jun 16, 2026
Ivansete-status
left a comment
Collaborator
There was a problem hiding this comment.
LGTM! Thanks for it! 💯
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements #80. Adds
{.ffiHandle.}, letting an.ffi.proc return — or receive — a complex, non-CBOR-serializable live object (aWaku, aKernel, a session) by reference instead of by value.The object never crosses the FFI boundary. Its wire form is a plain
uint64; the livereflives in a per-ctx registry and is reconstituted on the way back in. A given type is either a value type ({.ffi.}, copied) or a handle type ({.ffiHandle.}, referenced) — never both — so there is no per-proc "send the object vs send a handle" choice.Design
ffi/ffi_handles.nim—FFIHandleRegistry, mirroring theFFIEventRegistryidiom: monotonicnextId(0 = null, ids never recycled) + aTable.register/lookup/release/releaseAll. Each entry records its type name, so a stale, forged, or wrong-type id misses cleanly →RET_ERR, never a use-after-free. Single-threaded (FFI-thread) access, so no locking. Handle types inherit an injectedFFIHandleRootbase so heterogeneous refs are storable under one static type.FFIContext— newhandlesfield, init/deinit alongsideeventRegistry. Refs are freed on the FFI thread (ffiThreadBodydefer) because refc heaps are thread-local.ffi/internal/ffi_macro.nim:ffiHandletype macro — validatesref object, injectsof FFIHandleRoot, records the name.storageType— a handle param stores asuint64in the per-proc Req.registered; only the id is CBOR-encoded.lookuped, returningRET_ERRon a miss.FFIContext[LibType]/ pool from the lib type recorded bydeclareLibrary.{.ffiRaw.}with zero special-casing: the wire type is alreadyuint64, so raw bodies callctx.handles.lookup/register/releaseby hand.u64/uint64_t/uint; no struct is emitted for a handle type. The flatkernel_send(ctx, handle, …)form works; the optional OO/RAII wrapper class is left for a follow-up.declareLibrarynow also declares the<LibType>FFIPoolvar (previously onlyffiCtor/ffiDtordid) and records the lib type name.Testing
tests/unit/test_ffi_handle.nim: handle round-trip (out asuint64→ reconstituted live object), handle-as-receiver, and forged / null id →RET_ERR. Passes under orc and refc, and is ASAN/UBSan-clean.nph --checkclean.