dynlib

Checked native dynamic library loading for MoonBit.

moonbit
native
dynamic-library
ffi
moon add Nanaloveyuki/dynlib@0.1.0
Download zip
Version
0.1.0
License
Apache-2.0
Last updated
19 days ago
Downloads
192
README

#dynlib

Nanaloveyuki/dynlib is a native-only MoonBit package for loading dynamic libraries on Windows, Linux, and macOS.

#Install

Add the package to your moon.mod:

import {
"Nanaloveyuki/dynlib@0.1.0",
}

Build the consuming package for the native target.

#Load, Resolve, and Read an Address

Load a platform library name or an absolute library path, resolve a symbol, then obtain its address:

let library = match @dynlib.load("example-library") {
Ok(value) => value
Err(error) => abort("load failed: \{error}")
}

let symbol = match library.resolve("example_symbol") {
Ok(value) => value
Err(error) => abort("symbol missing: \{error}")
}

let address = match symbol.address() {
Ok(value) => value
Err(error) => abort("library was closed: \{error}")
}

// Use address only with an explicit, ABI-correct native binding.
ignore(address)

dynlib does not invoke resolved symbols. The consuming package owns the FFI signature and calling convention.

#Close

Close each loaded library at a deterministic shutdown point:

ignore(library.close())

Library::close is idempotent. After it succeeds, Library::resolve and Symbol::address return Closed; do not retain or use a previously returned address.

#
DynlibError

pub(all) enum DynlibError {
InvalidLibraryPath
InvalidSymbolName
LoadFailed
SymbolNotFound(String)
CloseFailed
Closed
} derive(Eq,
Debug
)

A failure produced while loading, resolving, or closing a native library.

#
Library

pub struct Library {
// private fields
}

An explicitly owned native dynamic-library handle.

Call close when the library is no longer needed. Closing is idempotent.

#
Library::close

fn Library::close(self : Library) -> Result[Unit, DynlibError]

Unloads the library. Calling close again returns success without touching the operating system handle.

#
Library::is_open

fn Library::is_open(self : Library) -> Bool

Returns whether this library remains open.

#
Library::resolve

fn Library::resolve(self : Library, name : String) -> Result[Symbol, DynlibError]

Resolves a native symbol without invoking it.

Dynamic function calls are deliberately outside this package's API because calling conventions and signatures must be modeled by the consuming FFI.

#
Symbol

pub struct Symbol {
// private fields
}

A resolved symbol tied to the library that produced it.

The address cannot be obtained after the source library is closed.

#
Symbol::address

fn Symbol::address(self : Symbol) -> Result[UInt64, DynlibError]

Returns a raw symbol address while its source library remains open.

The caller must not retain or invoke the address after Library::close.

#
load

fn load(path : String) -> Result[Library, DynlibError]

Loads a dynamic library by absolute path or platform loader name.

Paths are encoded as UTF-8. On Windows they are converted to UTF-16 before calling the operating system loader.

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io