Stability: 1 - Experimental
The node:ffi module provides a foreign function interface (FFI). It can be
used to make native function calls without the use of native addons. To access
it:
import ffi from 'node:ffi';const ffi = require('node:ffi');This module is only available under the node: scheme. The following will not
work:
import ffi from 'ffi';const ffi = require('ffi');The following types are supported.
charsigned charunsigned charshortshort intsigned shortsigned short intunsigned shortunsigned short intintsignedsigned intunsignedunsigned intlonglong intsigned longsigned long intunsigned longunsigned long intlong longlong long intsigned long longsigned long long intunsigned long longunsigned long long intfloatdoubleuint8_tint8_tuint16_tint16_tuint32_tint32_tuint64_tint64_t
For functions with pointer arguments or return values, use the type pointer.
Any type provided that contains an asterisk (*) will be treated as pointer.
All types map to number in JavaScript, except:
- 64-bit integer types, which are always mapped to
bigint - Pointers, which are mapped to
biginton systems with 64-bit pointers.
Currently, no conversion is done between number and bigint, so be careful
when calling functions returned from getNativeFunction.
Function pointers are not currently supported.
Structs are not supported as arguments or return values, but pointers to them
are. Structs can be built up in or read from Buffers, and getBufferPointer
can be used from there.
buffer{Buffer|TypedArray|ArrayBuffer} A buffer to get the pointer of.- Returns: {bigint} A pointer, represented as a
bigint.
Gets the memory address of the first byte of buffer. If there's an offset,
that's added to the result.
library{string|null} The path to the shared library.func{string} The function name inside the shared library.retType{string} The return value's type.argTypes{Array} The types of the arguments.- Returns: {Function} A JavaScript wrapper for the native function.
Retrieves a native function from a given shared library (i.e. .so, .dylib,
or .dll), and returns a JavaScript wrapper function. The wrapper function does
the appropriate conversion from JavaScript numbers and bigints to the
appropriate types as per the argTypes array, and similarly for return values.
If null is provided as the library, function symbols will be searched for in
the current process.
type{string} The name of the type to get the size of.- Returns: {number} The size of the the type in bytes.
A passthrough to the C sizeof operator. The type must be one of the
supported types, or else the return value will be undefined.