Fdlopen
Bun

function

ffi.dlopen

function dlopen<T extends FunctionDefinitions = {}>(
path: null | string,
definitions?: T

Loads a dynamic library and resolves the requested function definitions.

On Windows passing null is not supported.

When definitions is omitted, functions is returned as an empty object until symbols are resolved explicitly.

The returned object also implements the explicit resource management protocol, so it can be used with the using declaration. Disposing the returned object closes the library handle.

import { dlopen } from 'node:ffi';

{
  using handle = dlopen('./mylib.so', {
    add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
  });
  console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
import { dlopen } from 'node:ffi';

const { lib, functions } = dlopen('./mylib.so', {
  add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
  string_length: { arguments: ['pointer'], return: 'u64' },
});

console.log(functions.add_i32(20, 22));
@param path

Path to a dynamic library, or null to resolve symbols from the current process image.

@param definitions

Symbol definitions to resolve immediately.