v1.9.3Module SystemBSD-2-Clause

Hook-based plugin architecture, dynamic/in-memory/static-archive module loading, and hierarchical region isolation for C applications. Canonical header is <ttypt/xy.h>.

GitHub
git clone https://github.com/tty-pt/libxylem

libxylem logo

libxylem

npm C99 BSD-2-Clause Hook modules Region Isolation

Hook-based plugin architecture, dynamic/in-memory/static-archive module loading, and hierarchical region isolation for C applications. Canonical header is <ttypt/xy.h>.

1. Quickstart: Host & Plugin

Define a typed hook in the host, implement it in a shared module, and dispatch:

Host (host.c):

#include <stdio.h>
#include <ttypt/xy.h>

// Define canonical hook: signature is int on_player_score(int id, int pts)
XY_DEF(int, on_player_score, int, player_id, int, points);

int main(void) {
    xy_init();
    xy_load("mods/scoreboard"); // loads mods/scoreboard.so
    on_player_score(1, 100);    // dispatches to all loaded listeners
    xy_shutdown();
    return 0;
}

Plugin Module (mods/scoreboard.c):

#include <stdio.h>
#include <ttypt/xy-mod.h>

XY_IMPL(int, on_player_score, int, player_id, int, points) {
    printf("Player %d earned %d points!\n", player_id, points);
    return 0;
}

void xy_install(void) {} // Called automatically on first load

Build host and module:

cc -o host host.c -lxylem -pthread
cc -o mods/scoreboard.so mods/scoreboard.c -fPIC -shared -lxylem

2. Architecture & The Two-Header Rule

To prevent symbol pollution and enforce strict encapsulation across shared .so boundaries, libxylem enforces the Two-Header Rule:

  1. module-types.h (Shared Types Header): Contains shared structs, enums, and typedefs. Both caller and implementer include this header.
  2. module.h (Hook Declaration Header): Declares typed hook dispatches via XY_DECL(). Consumers include this header to call the hook. The module implementation includes module-types.h and defines XY_IMPL(), but never includes module.h.

This rule eliminates #ifdef *_IMPL preprocessor switches and ensures compile-time type safety across dynamically loaded modules.


3. Core API Reference

Function / MacroSignatureDescription
XY_DEFXY_DEF(ret, name, ...)Define host-side hook dispatch adapter
XY_DECLXY_DECL(ret, name, ...)Shared header hook declaration
XY_IMPLXY_IMPL(ret, name, ...)Module listener implementation (auto-registered)
xy_loadint xy_load(const char *path)Dynamically load .so (or resolve statically registered XY_STATIC_MOD / in-memory module) into caller's current region
xy_load_mem / xy_register_mem_modint xy_load_mem(name, data, len)Register and load modules directly from memory buffers (for bundled/mobile runtimes without dlopen on disk)
XY_STATIC_MODXY_STATIC_MOD(name, init_fn)Register a statically linked module for zero-dlopen Android/iOS/static builds
xy_denyint xy_deny(what, type)Block specific hooks or modules in child regions
xy_claim_atint xy_claim_at(id, plen, fn, ud)Carve out isolated execution region

๐Ÿ‘‰ For context ABI details, prefix lengths (id, plen), and region traversal, see docs/api.md.


4. Regions, Static/Mobile Modules & In-WASM Dispatch

  • Hierarchical Plugin Sandboxing: Regions constrain hook dispatch. A hook call dispatches strictly to descendant modules in the caller's region.
  • Static & In-Memory Modules: XY_STATIC_MOD, xy_register_mem_mod(), and xy_load_mem() let mobile (iOS/Android) and single-binary targets link and load Xylem modules without dlopen filesystem paths.
  • In-WASM Hook Dispatcher: Includes an embedded hook registry and dispatcher for WebAssembly environments, supporting XY_DECL, XY_IMPL, XY_LISTENER_TAG, xy_call, and xy_last chaining directly inside browser WASM (validated against wasm-allowed-exports.lst).

5. Installation & Build

Prebuilt Packages

Prebuilt packages are distributed from tty.pt for Linux (APT / Alpine / Arch / Fedora-RHEL), macOS (Homebrew), Windows (winget / MSYS2), OpenBSD, and mobile static sysroots for Android (https://tty.pt/android/ โ€” arm64-v8a, armeabi-v7a, x86_64) and iOS (https://tty.pt/ios/ โ€” libxylem.xcframework). Follow the installation instructions and use libxylem as the package name.

Build from Source

Builds use shared libstele rules (include ttypt/include.mk):

make                  # builds lib/libxylem.so
make test             # run validation suite over hooks and regions
sudo make install     # install headers and library to system

6. JavaScript & WebAssembly Bindings (@tty-pt/xylem)

Official Node.js and Browser WebAssembly bindings are colocated in npm/:

npm config set @tty-pt:registry https://tty.pt/npm/
npm install @tty-pt/xylem
  • Universal Engine: Extensible event and hook bus (XylemBus, areg, call) mirroring C dynamic module contracts.
  • C Development Headers: Consume headers from node_modules/@tty-pt/xylem/include.

๐Ÿ‘‰ For complete TypeScript usage, hook guides, and API reference, see npm/README.md.

License

BSD 2-Clause License. Copyright (c) 2025, tty-pt. See LICENSE.

Maintained by:tty-pt