Skip to content

Add ownership-aware typed dynamic bindings - #5

Closed
whilo wants to merge 1 commit into
carpentry-org:masterfrom
replikativ:ownership-aware-bindings
Closed

whilo wants to merge 1 commit into
carpentry-org:masterfrom
replikativ:ownership-aware-bindings

Conversation

@whilo

@whilo whilo commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Summary

This keeps the existing DynLib.open/get API intact and adds an ownership-aware typed binding path:

  • DynLib.open-owned + DynLib.defbinder produce a bound function that owns its loader handle; dropping the function closes the handle.
  • DynLib.open-pinned + DynLib.defpinned-binder intentionally retain a process-lifetime handle and let many functions borrow it. This is the conservative default for callbacks, TLS, Rust cdylibs, and foreign libraries that may retain references to their own code.
  • DynLibBoundFn.call0..call8 and DynLibPinnedFn.call0..call8 invoke the statically declared signature.
  • typed dlsym helpers return Lambda by value, avoiding the allocation leaked by the compatibility lookup.
  • failed opens/lookups return the current dlerror() text.

The binder signature remains an explicit C ABI assertion. dlsym has no portable type reflection, so generated bindings should derive it from an authoritative header or adapter descriptor.

Why two lifetime modes?

An owned binding makes the common safety relationship representable in Carp: the callable cannot outlive the dlopen handle it contains. It uses one handle per bound function; platform loaders normally reference-count repeated opens of the same image.

Pinned loading handles the cases where unloading cannot be proven safe. Its type has no deleter, and binders only borrow it, so one pinned image can provide many functions without manufacturing false unload guarantees.

Compatibility

The original polymorphic API and examples continue to work unchanged. The README now recommends typed binders for new code and documents the remaining ABI assertion.

Validation

  • legacy suite: open, lookup, missing-library/symbol errors, and close behavior
  • typed owned calls: floor
  • typed pinned calls from one handle: floor, ceil, and binary pow
  • typed missing-symbol error
  • exercised with both the reference Carp compiler and Meta-Carp

@hellerve

hellerve commented Aug 26, 2026

Copy link
Copy Markdown
Member

Thank you for your contribution! One more carp-fmt pass and I think this is good to go!

@hellerve

Copy link
Copy Markdown
Member

i took over the reformat in #6. i ported your commit over, so attribution stays the same! thanks!

@hellerve hellerve closed this Sep 16, 2026
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.

2 participants