Deno Foreign Function Interface
deno-ffi is a Deno plugin for loading and calling dynamic libraries using pure TypeScript. It can be used to create bindings to native libraries without writing any C++ code.
It also simplifies the augmentation of Deno with C code as it takes care of handling the translation of types across TypeScript and C, which can add reams of boilerplate code to your otherwise simple C. See the ./tests
for an example of this use case.
import { ApiDefine, DataType, loadLibrary } from "https://deno.land/x/ffi@v0.1.0/mod.ts";
const libPath = "test.dylib";
interface LibApi {
rust_fun_print_something(): void;
rust_fun_add_one(num: number): number;
}
const apiDefine: ApiDefine[] = [
{
name: "rust_fun_print_something",
type: "function",
},
{
name: "rust_fun_add_one",
type: "function",
params: [DataType.i32],
returnType: DataType.i32,
},
];
const lib = await loadLibrary<LibApi>(libPath, apiDefine);
lib.rust_fun_print_something();
const value = lib.rust_fun_add_one(1);
console.log(value);