denoland / denoland/std

feat: abstraction of ffi api libraries loading

Open
#1,968 7 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**

The [FFI API](https://deno.land/manual@main/runtime/ffi_api#usage) starting boilerplate feels like it could be abstracted in deno std lib to be more friendly.

I feel like the `switch` should be put under the carpet and users should just pass the lib without the extension and the underlying function automatically append the correct one. I know that extensions are conventions, but users who don't wish to use the most commons one could still fallback on the current solution.

Currently you need the following:
```ts
// Determine library extension based on
// your OS.
let libSuffix = "";
switch (Deno.build.os) {
case "windows":
libSuffix = "dll";
break;
case "darwin":
libSuffix = "dylib";
break;
case "linux":
libSuffix = "so";
break;
}

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

Granted, it's a small incovenience of ~10 lines of code (which could be even less) but it'd be nice anyway.

**Describe the solution you'd like**

Something along:
```ts
import { load } from "https://deno.land/std/ffi/load.ts"
const dylib = load("./libadd", {
"add": { parameters: ["isize", "isize"], result: "isize" },
});
```

**Describe alternatives you've considered**

Currently using the first solution

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.