Skip to main content
function Deno.dlopen
allow-ffi
Unstable

Opens an external dynamic library and registers symbols, making foreign functions available to be called.

Requires allow-ffi permission. Loading foreign dynamic libraries can in theory bypass all of the sandbox permissions. While it is a separate permission users should acknowledge in practice that is effectively the same as running with the allow-all permission.

An example, given a C library which exports a foreign function named add():

// Determine library extension based on
// your OS.
let libSuffix = "";
switch (Deno.build.os) {
  case "windows":
    libSuffix = "dll";
    break;
  case "darwin":
    libSuffix = "dylib";
    break;
  default:
    libSuffix = "so";
    break;
}

const libName = `./libadd.${libSuffix}`;
// Open library and define exported symbols
const dylib = Deno.dlopen(
  libName,
  {
    "add": { parameters: ["isize", "isize"], result: "isize" },
  } as const,
);

// Call the symbol `add`
const result = dylib.symbols.add(35, 34); // 69

console.log(`Result from external addition of 35 and 34: ${result}`);

Parameters

filename: string | URL
symbols: S