33 Commits

Author SHA1 Message Date
Ivan FB
2e30f06757
fix: reject {.ffi.} calls on an unconstructed ref library
When no {.ffiCtor.} has stored a library, the FFI thread points `myLib` at a
default-valued fallback so handlers always have something to bind, and
`myLibOwned` is what distinguishes that fallback from a real one. For an
`object` library the fallback is a usable zero value and callers legitimately
depend on it, but for a `ref` library it is `nil`: the user body received a nil
ref and faulted on its first field access. A failing ctor is the common way to
get there, since the C entry point hands back a live context before the ctor
body has run on the FFI thread.

Guard on `myLibOwned` for `ref` library types only. The check sits in the
generated handler rather than the C entry point, so it runs behind any queued
constructor — a host that fires a call without awaiting the create callback
still succeeds, as before.

Backport of the same fix on master, where the state had to be added rather than
reused.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ULz7Md52AF6PmqZeCmh8b7
2026-07-28 02:44:09 +02:00
Ivan FB
40cd6d2d26
feat(ffi): add {.ffiExport.} for simple synchronous C exports
{.ffi.}/{.ffiCtor.} cover the async, context-handle, CBOR-marshaled path.
{.ffiExport.} covers the complementary case: a no-argument lifecycle/health
entry point a host loads with dlopen+dlsym and calls synchronously — no
context, no callback, no CBOR, the return value crossing the ABI directly.

Native Nim return types are bridged to the C ABI (int/bool -> int,
uint64 -> unsigned long long, string -> const char* kept alive in shared
memory; no-return -> void), and initializeLibrary() is injected so the Nim
runtime is up on first call — the host never invokes NimMain itself.

Fills the no-argument scalar case of the declared-but-unimplemented abi=c path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0138fdSv6Xhv2KjhiyYR4SAu
2026-07-12 11:55:10 +02:00
Ivan FB
8f15afce5c
feat: multi-parameter {.ffiEvent.} via synthesised envelope
`{.ffiEvent.}` previously accepted exactly one parameter, forcing every
multi-field event to declare a hand-written {.ffi.} payload type. The macro
now bundles two or more parameters into a synthesised, registered envelope
object named `<WireNamePascalCase>Payload`, whose fields are the parameters,
and dispatches an instance of it. A single parameter still rides the wire
directly (scalar or existing {.ffi.} object), so this is backwards
compatible. Because the envelope is registered like any {.ffi.} type, the
foreign bindings gain it as a first-class struct plus a typed handler.

The timer example gains an `on_job_scheduled(jobId, willRunCount)` event to
exercise the path; the C++ and Rust bindings are regenerated accordingly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 03:05:37 +02:00
Ivan FB
a66c53a34b
feat: recycle pooled FFI contexts; compile-time request ids
Two foreign-host concurrency fixes for refc, both needed so a Go host can
hammer the FFI under load without corrupting Nim's per-thread GC.

1. Compile-time request ids. ffiNewReq / the method + ctor wrappers built
   the request id with `$T` at runtime, allocating a Nim GC string on the
   foreign caller's (often transient) thread. Emit a `cstring` literal of
   the type name instead — no allocation on the caller thread.

2. Recycle pooled contexts instead of destroy/recreate. Restores the
   release/v0.1 model that v0.2 dropped: a pool slot's worker + event
   threads and signal fds are built once and reused. The ffiDtor now
   requests a synchronous recycle (drain in-flight handlers, free the lib,
   clear listeners, release the slot) on the FFI thread, keeping the
   threads alive; createFFIContext reuses an initialised slot. Without this
   every create/destroy churned ~6 signal fds, so fd numbers climbed past
   FD_SETSIZE (1024) and ThreadSignalPtr.waitSync's select() failed with
   EINVAL under create/destroy load.

   Adds CtxLifecycle (Active/RecyclePending/Recycling), ctx-level
   inUse/tryClaim/release/markAsActive, requestRecycle (waits on a new
   recycleDoneSignal), freeLib (refc GC_unref / orc =destroy of ctor-owned
   libs), recycleContext, FFIEventRegistry.clearListeners, and roots the
   ctor-stored ref lib under refc (GC_ref, balanced in freeLib).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 20:07:33 +02:00
Ivan FB
94293349ff
fix: set up foreign-thread GC in event-listener entry points
sds_add_event_listener / sds_remove_event_listener run on the foreign
caller thread and allocate Nim memory ($eventName, the listener registry
Table+seq), but unlike the ctor/dtor they never called initializeLibrary.
A foreign host (Go) migrates goroutines across OS threads, so the thread
running add_event_listener may not be the one that ran a prior entry
point; without setupForeignThreadGc its per-thread allocator region is
uninitialised and the first Nim allocation faults in the allocator.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 19:01:13 +02:00
Gabriel Cruz
f6881274ca
feat(ffi): cwire codec composite fields (#88) 2026-06-23 12:55:07 -03:00
Gabriel Cruz
6c4657ad7e
feat(ffi): cwire codec core (flat scalars + strings) (#87) 2026-06-23 11:49:19 -03:00
Gabriel Cruz
a5ccadd62d
feat(ffi): per-interaction ABI-format annotations + c codec (ffiRaw) (#85) 2026-06-23 11:18:03 -03:00
Gabriel Cruz
021f469041
feat: {.ffiHandle.} — export complex live objects as opaque handles (#81) 2026-06-16 14:11:31 -03:00
Gabriel Cruz
16dc1b3573
chore(ci): add nph linting (#77) 2026-06-10 16:30:30 -03:00
Gabriel Cruz
54c41a3e62
refactor(ffi): event thread scaffolding + FFIContext lifecycle split (#71) 2026-06-08 11:46:16 -03:00
Ivan FB
f96a5b158a
Remove wildcard event listener; keep per-event dispatch (#70) 2026-06-02 22:22:35 +02:00
Ivan FB
3f19411684
Event listener abi (#50) 2026-05-28 16:00:28 +02:00
Ivan FB
496a341466
Event registry wiring (#49) 2026-05-27 22:26:39 +02:00
Gabriel Cruz
436c0d760b
test(cpp-e2e): add multi-context, cross-library, pipeline, stress tests (#30) (#42) 2026-05-26 09:18:12 -03:00
Ivan FB
6a7e4616fd
Adjust events to cbor (#39) 2026-05-25 15:51:56 +02:00
Ivan FB
e12745e85c
Add cddl generator (#24) 2026-05-18 20:00:57 +02:00
Ivan FB
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
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
Pablo Lopez
bb8a3e7e22
fix: add install_name for mac (#8)
not to hardcode the paths
2026-02-20 17:37:13 +02: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
5964e287a9
rm useless comments 2025-12-11 17:21:47 +01:00
Ivan Folgueira Bande
e0cf0c8842
fixes for proper proc exposure to c 2025-12-11 17:11:59 +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
b974eab39c
comment echo repr lines 2025-09-17 14:38:01 +02: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