DynLib

DynLib is a module for loading shared libraries at runtime. It strives to make code loading and calling dynamically easy.

(load "https://github.com/carpentry-org/dynlib@0.3.0")

(defn main []
  (println*
    &(=> (DynLib.open "libt.so")
         (Result.and-then
           (fn [lib] (DynLib.get lib "inc")))
         (Result.map (fn [f] (Int.str (f 1)))))))

close

defn

(Fn [Lib] (Result () String))

                        (close lib)
                    

closes a library lib. Returns a Result with () on success, or the dlerror message on failure.

defbinder

macro

Macro

                        (defbinder name signature)
                    

Defines a typed symbol binder for an owned library. The declared signature is an explicit assertion about the symbol's C ABI. The result owns the library handle and cannot outlive it.

defpinned-binder

macro

Macro

                        (defpinned-binder name signature)
                    

Defines a typed symbol binder for a pinned library. The declared signature is an explicit assertion about the symbol's C ABI. The binder only borrows the process-lifetime handle, allowing many symbols to be bound from one library.

get

defn

(Fn [Lib, (Ref String a)] (Result b String))

                        (get lib fname)
                    

gets a function named fname from a shared library lib. Returns a Result with the function on success, or the dlerror message on failure.

get-from-module

defn

(Fn [Lib, (Ref String a), b] (Result c String))

                        (get-from-module lib md fname)
                    

gets a function named fname from a Carp module md from inside a shared library lib. Returns a Result with the function on success, or the dlerror message on failure.

open

defn

(Fn [(Ref String a)] (Result Lib String))

                        (open lib)
                    

opens a shared library lib. Returns a Result with the library handle on success, or the dlerror message on failure.

open-owned

defn

(Fn [(Ref String a)] (Result DynLibOwned String))

                        (open-owned path)
                    

Opens a library whose handle must remain owned by one bound function. Dropping that function closes the handle.

open-owned-

external

(Fn [(Ref String a)] DynLibOwned)

open-pinned

defn

(Fn [(Ref String a)] (Result DynLibPinned String))

                        (open-pinned path)
                    

Opens a process-lifetime library. It is intentionally not unloaded, so one borrowed handle can safely produce many bindings. Prefer this for Rust cdylibs, callbacks, thread-local state, or foreign code that can retain references into the library.

open-pinned-

external

(Fn [(Ref String a)] DynLibPinned)

rebind

macro

Macro

                        (rebind lib s)
                    

typed-error

external

(Fn [] String)

valid-owned?

external

(Fn [(Ref DynLibOwned a)] Bool)

valid-pinned?

external

(Fn [(Ref DynLibPinned a)] Bool)