haskell-polycall
v1.0.0
Published
Haskell binding for the Polycall core (binding ABI v1, polycall >= 1.1.0): direct C FFI (foreign import ccall) for config validation, RPC calls and peer nodes. Source distribution of a Haskell package (modules Polycall, Polycall.Raw); not a JavaScript lib
Maintainers
Readme
haskell-polycall
Haskell binding for the Polycall core,
binding ABI v1 (polycall >= 1.1.0). Modules Polycall (idiomatic layer) and
Polycall.Raw (the 19 foreign import ccall declarations).
Quick start
1. Install the package (a Haskell source package delivered through npm):
npm i haskell-polycall2. Have the Polycall core and GHC >= 9.4 (tested with 9.10.3; only the
boot packages base and bytestring are used, so neither cabal nor stack
nor Hackage is needed). Build the core from
https://github.com/obinexus/polycall and note two directories:
| | link library (POLYCALL_LIB_DIR) | run-time library (POLYCALL_BIN_DIR) |
| --- | --- | --- |
| Windows | polycall\build\windows-cmake\lib (polycall.lib) | polycall\build\windows-cmake\bin (polycall.dll) |
| Linux | <prefix>/lib (libpolycall.so) | the same directory |
# Windows (PowerShell), in a polycall checkout
.\build-windows.ps1# Linux, in a polycall checkout
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_LIBDIR=lib
cmake --build build --parallel && cmake --install build --prefix "$HOME/polycall"3. Build and run the packaged example from your project directory (the
one that now contains node_modules). scripts/ghcbuild.mjs is a thin
wrapper around one ghc --make command that adds the package's src and
links the core:
# Windows (PowerShell)
$env:POLYCALL_LIB_DIR = "C:\path\to\polycall\build\windows-cmake\lib"
$env:POLYCALL_BIN_DIR = "C:\path\to\polycall\build\windows-cmake\bin"
$env:GHC = "C:\path\to\ghc-9.10.3-x86_64-unknown-mingw32\bin\ghc.exe" # or have ghc on PATH
$fx = "node_modules\haskell-polycall\tests\fixtures"
node node_modules\haskell-polycall\scripts\ghcbuild.mjs -o polycall-consumer --run node_modules\haskell-polycall\examples\consumer\Main.hs -- "$fx\good.polycallrc" "$fx\bad.polycallrc"# Linux
export POLYCALL_LIB_DIR="$HOME/polycall/lib"
fx=node_modules/haskell-polycall/tests/fixtures
node node_modules/haskell-polycall/scripts/ghcbuild.mjs -o polycall-consumer --run node_modules/haskell-polycall/examples/consumer/Main.hs -- "$fx/good.polycallrc" "$fx/bad.polycallrc"The two arguments are a configuration file the core accepts and one it
refuses; the package ships one of each. A third argument, the host:port of
a running polycall start, adds one RPC round trip. Expected output (paths
shortened):
core 1.1.0, binding ABI 1
OK good.polycallrc
REFUSED Config
POLYCALL_E_CONFIG: invalid configuration: bad.polycallrc:1: malformed key=value entry
peer listening on 127.0.0.1:53124
all checks passedWithout POLYCALL_LIB_DIR (or --lib-dir) the build stops before ghc runs
with the Polycall core was not located ... Set POLYCALL_LIB_DIR (or pass
--lib-dir <dir>) .... If the program is built but polycall.dll is not
found at run time (Windows reports only exit code 0xC0000135), the wrapper
says could not load the Polycall core ... Set POLYCALL_BIN_DIR (or pass
--bin-dir) ....
Using it in your own Haskell project
With the wrapper (any number of modules; other -flags go to ghc):
node node_modules/haskell-polycall/scripts/ghcbuild.mjs -O2 -o myapp app/Main.hsOr call GHC yourself; this is all the wrapper does:
ghc --make -threaded -inode_modules/haskell-polycall/src app/Main.hs -o myapp -L"$POLYCALL_LIB_DIR" -lpolycallAt run time the loader must find the shared library: polycall.dll on
PATH or next to the executable on Windows, LD_LIBRARY_PATH or an
installed libpolycall.so.1 on Linux. Build with -threaded.
A polycall.cabal is included for cabal users (cabal build
--extra-lib-dirs=<POLYCALL_LIB_DIR>); it is untested -- this package is
built and tested with plain ghc --make only.
{-# LANGUAGE OverloadedStrings #-}
import Polycall
main :: IO ()
main = do
checkAbi -- throws Unsupported unless the library is ABI v1
runConfig "Polycallfile" Run -- throws PolycallError (Config, NotFound ...)
json <- call "127.0.0.1:8084" "inventory" "get" (Just "{\"item_id\":\"widget-a\"}") 5000
withPeer "alpha" defaultPeerOptions { peerBind = Just "127.0.0.1:0" } $ \node -> do
endpoint node >>= putStrLn
send node "127.0.0.1:9000" "\0binary\0" Nothing 5000What it covers
Interop route: direct C FFI. src/Polycall/Raw.hs
declares the 19 functions of polycall.h by hand with foreign import ccall
(CInt, CSize, CString, Ptr, Word32 for uint32_t, Int32 for
polycall_peer_t), so the headers are not needed, and
src/Polycall.hs wraps them. GHC's bundled LLVM linker
links the MSVC import library polycall.lib directly (linking against
polycall.dll itself with -L<bin dir> also works); no MinGW import library
is generated. Nothing is reimplemented in Haskell and no CLI is spawned.
| area | Haskell API (Polycall) | C function |
| --- | --- | --- |
| library | abiVersion, checkAbi, version, strerror, lastError | polycall_ffi_abi_version, polycall_ffi_version, polycall_strerror, polycall_last_error |
| configuration | runConfig path Validate / Run, describe, describeWithCapacity | polycall_ffi_run_config, polycall_ffi_describe |
| RPC | call, callRaw | polycall_call |
| peer node | openPeer, withPeer, closePeer, endpoint, nodeId, register, unregister, list, ping, send, recv, recvWithCapacity, cancel, health | polycall_peer_* |
- Errors: every negative status is thrown as
PolycallError(anException) witherrorStatus(e.g. -13),errorKind(Config,NotFound,Timeout...;UnknownStatus nfor a newer library),errorMessage(the library'sstrerrortext) anderrorDetail(the library's per-thread detail, e.g.bad.polycallrc:1: malformed key=value entry).kindFromStatus/statusFromKindconvert.callRawand the*WithCapacityfunctions report instead of throwing. - Thread-local detail:
polycall_last_erroris per OS thread. The wrapper reads it on the same OS thread as the failing call (in a bound thread; free on the main thread, one short-lived OS thread per call from aforkIOthread). - Blocking:
call,ping,send,recv,openPeerandclosePeeraresafeforeign calls, so with-threadedother Haskell threads keep running. A blockedrecvcannot be interrupted by an async exception: wake it withcancel(Cancelled) orclosePeer(Closed) from another thread. - Ownership: the library never returns memory to free. Outputs are
copied into Haskell values:
String(UTF-8) for ids and endpoints, strictByteStringfor JSON and payloads. Payloads are binary-safe (NUL bytes included). APeerowns one library handle untilclosePeer; a second close or any use afterwards throwsInvalidHandle, never crashes.withPeercloses it withbracketand tolerates an earlier explicit close. - Buffers:
describe,list,healthgrow their buffer as needed;recvuses a 1 MiB buffer (the maximum payload).recvWithCapacity p t nreturnsLeft neededwhennbytes are too few, and the message stays queued.
Tests
Every test runs against the real core; nothing is mocked.
# POLYCALL_LIB_DIR, POLYCALL_CLI (and POLYCALL_BIN_DIR on Windows) point at a built core;
# GHC is the path to ghc (default: ghc on PATH)
npm test # builds and runs tests/Main.hs: 11 integration tests + diagnostics
npm run test:consumer # npm pack -> install into a clean project -> packaged examplenpm test also checks both missing-core diagnostics with every
POLYCALL_* variable unset and the core removed from PATH.
npm run test:consumer installs the packed tarball into a temporary project
outside the checkout, builds the example there from
node_modules/haskell-polycall only and checks a known-good file, a
known-bad file, the peer lifecycle, one RPC round trip against
polycall start, a malformed file passed as known-good, and the build-time
and run-time missing-core diagnostics.
Tested: Windows 11 x64, GHC 9.10.3 (official bindist, bundled clang/lld),
core 66c744d (1.1.0, MSVC build). Linux runs in CI
(.github/workflows/ci.yml) and has not been
run yet.
License
MIT -- see LICENSE.
