Functions are declared using the Library
object's declare()
method. Once declared, functions can be called using standard function syntax.
Prerequiste Understanding
See ABI.
Example: No input parameters
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.
Example: Multiple input parameters
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.
Returned values
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).