import { Deno } from "https://deno.land/x/deno@v2.0.4/cli/tsc/dts/lib.deno.ns.d.ts";
const { dlopen } = Deno;
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.
Examples
Given a C library which exports a foreign function named add()
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(35n, 34n); // 69n
console.log(`Result from external addition of 35 and 34: ${result}`);
Type Parameters
S extends ForeignLibraryInterface