Functions are declared using the Library
object's declare()
method. Once declared, functions can be called using standard function syntax.
See ABI.
In this example, we declare the libc clock()
function, which returns the elapsed time since system startup, then fetch and output that value.
const clock = lib.declare("clock", ctypes.default_abi, ctypes.unsigned_long); console.log("Clocks since startup: " + clock());
The clock()
function requires no input parameters; it simply returns an unsigned long.
This example declares the libc asctime()
function, which converts a time structure into a string.
const asctime = lib.declare("asctime", ctypes.default_abi, ctypes.char.ptr, struct_tm.ptr);
For a more complete version of this example (including the implementation of the struct_tm
type), see the structures example.
If the return type can fit into a JavaScript number without loss (that is, it's a number 32 bits or smaller, or is a double or float), then the function just return a JavaScript number. For everything else it returns a ctypes object representing the return value (including 64-bit integers).