NAME
std/clib - Dynamic C library loading and scalar FFI calls.
SYNOPSIS
from std/clib import CLib;
let lib := CLib.open("t/fixtures/example_clib/libgreet.so");
let greet := lib.func(
"greet",
[],
{
type: "binary",
terminated_by: "nul",
free: "greet_free"
}
);
say to_string(greet.call());
IMPLEMENTATION SUPPORT
This module is supported by zuzu.pl, zuzu-rust, and zuzu-js on Node and Electron. It is not supported by zuzu-js in the browser.
DESCRIPTION
This runtime-supported module loads C dynamic libraries and calls exported C functions through explicitly declared signatures.
The first implementation supports null, booleans, 64-bit integers, 64-bit floats, and BinaryString byte buffers. Other data types are out of scope.
The runtime copies returned binary data before returning it to ZuzuScript. If a return descriptor includes free, that symbol is called after the copy so libraries can return owned memory safely.
On Unix-like systems, dynamic libraries usually use .so on Linux and .dylib on macOS. Windows libraries usually use .dll. Exact availability depends on the host runtime. Browser runtimes reject this module as unsupported.
EXPORTS
Classes
CLibStatic methods:
CLib.open(String path)Parameters:
pathis a dynamic library path. Returns:CLibrary. Loads a dynamic library.
CLibraryMethods:
func(String name, Array params, return_type, options?)Parameters:
nameis a symbol name,paramsdescribes parameter types,return_typedescribes the return type, andoptionsis optional. Returns:CFunction. Looks up a symbol and returns a callable C function wrapper.has_symbol(String name)Parameters:
nameis a symbol name. Returns:Boolean. Returns whether the loaded library exports the named symbol.close()Parameters: none. Returns:
null. Invalidates the library and functions created from it.
CFunctionMethods:
call(...)Parameters: arguments must match the declared C signature. Returns: value. Calls the C function using the declared signature.
COPYRIGHT AND LICENCE
std/clib is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.