Skip to content

Commit

Permalink
err code
Browse files Browse the repository at this point in the history
  • Loading branch information
xan105 committed Aug 26, 2023
1 parent 6afe239 commit 50f7ecf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Calling directly from a library
```js
import { load, types } from "@xan105/ffi/[ napi | koffi ]";

const call = load("user32.dll", { abi: "stdcall" });
const MessageBoxA = call("MessageBoxA", "int", [
const lib = load("user32.dll", { abi: "stdcall" });
const MessageBoxA = lib("MessageBoxA", "int", [
"void *",
types.win32.LPCSTR,
types.win32.LPCSTR,
Expand All @@ -64,7 +64,7 @@ Callback with Deno like syntax
```js
import { dlopen, Callback} from "@xan105/ffi/koffi";

const library = dlopen(
const lib = dlopen(
"./callback.so",
{
set_status_callback: {
Expand All @@ -86,8 +86,9 @@ const callback = new Callback(
(success) => {}
);

library.set_status_callback(callback.pointer);
library.start_long_operation();
lib.set_status_callback(callback.pointer);
lib.start_long_operation();
callback.close();
```

Install
Expand Down Expand Up @@ -124,12 +125,12 @@ Load the given library path and return an handle function to call library's symb
- `ignoreLoadingFail?: boolean` (false)

Silent fail if the given library couldn't be loaded.<br />
💡 Called symbol will be `undefined` in that case.
💡 Handle will return `undefined` in that case.

- `ignoreMissingSymbol?: boolean` (false)

Silent fail if the given library doesn't have the called symbol.<br />
💡 Called symbol will be `undefined` in that case.
💡 Handle will return `undefined` in that case.

- `abi?: string` ("func" for koffi and "default_abi" for ffi-napi)

Expand All @@ -151,9 +152,8 @@ See the corresponding FFI library for more information on what to pass for `resu

```js
import { load } from "@xan105/ffi/[ napi | koffi ]";
const call = load("libm");

const ceil = call("ceil", "double", ["double"])
const lib = load("libm");
const ceil = lib("ceil", "double", ["double"])
ceil(1.5); //2
```

Expand Down
39 changes: 23 additions & 16 deletions lib/ffi-napi/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,54 @@ function load(path, option = {}){

if(err && !options.ignoreLoadingFail){
const errCode = RegExp(/\d+$/).exec(err)?.[0];
if(errCode == null) //If couldn't extract error code
if(errCode == null){ //If couldn't extract error code
err.code = "ERR_FFI";
throw err; //Throw the default ffi error
else {
const [ message, code ] = errorLookup(+errCode); //lookup error code
} else {
const [ message ] = errorLookup(+errCode); //lookup error code
throw new Failure(message, {
code,
code: "ERR_FFI",
cause: err,
info: path
info: { lib: path }
});
}
}

return function(symbol, result, parameters){
const handle = function(symbol, result, parameters){
try{
if (!dylib) return undefined;
const fnPtr = dylib.get(symbol);
return ffi.ForeignFunction(fnPtr, result, parameters);
}catch(err){
const errCode = RegExp(/\d+$/).exec(err)?.[0];
if(errCode == null) //If couldn't extract error code
if(errCode == null){ //If couldn't extract error code
err.code = "ERR_FFI";
throw err; //Throw the default ffi error
else {
} else {
const [ message, code ] = errorLookup(+errCode); //lookup error code
if(
options.ignoreMissingSymbol &&
code === "ERROR_PROC_NOT_FOUND"
(code === "ERROR_PROC_NOT_FOUND" || err.message.includes("undefined symbol"))
) return undefined;
throw new Failure(message, { code, cause: err, info: symbol });
throw new Failure(message, {
code: "ERR_FFI",
cause: err,
info: { symbol }
});
}
}
};

return handle;
}

function dlopen(path, symbols, option){

shouldObjWithinObj(symbols);

const lib = Object.create(null);
const call = load(path, option);
const handle = load(path, option);

for (const [name, definition] of Object.entries(symbols)){

if (name === "__proto__") continue; //not allowed
Expand All @@ -99,11 +108,9 @@ function dlopen(path, symbols, option){
const nonblocking = asBoolean(definition.nonblocking) ?? false;
const symbol = definition.symbol || name;

const fn = call(symbol, result, parameters);
if(typeof fn === "function"){
const fn = handle(symbol, result, parameters);
if(typeof fn === "function")
lib[name] = nonblocking ? promisify(fn.async) : fn;
}

}
return lib;
}
Expand Down
29 changes: 17 additions & 12 deletions lib/koffi/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ function load(path, option = {}){

if(err && !options.ignoreLoadingFail){
throw new Failure(err.message, {
code: 0,
code: "ERR_FFI",
cause: err,
info: path
info: { lib: path }
});
}
return function(symbol, result, parameters){

const handle = function(symbol, result, parameters){
try{
if (!dylib) return undefined;
return dylib[options.abi](symbol, result, parameters);
Expand All @@ -60,17 +60,24 @@ function load(path, option = {}){
err.message.startsWith("Cannot find function") &&
err.message.endsWith("in shared library")
) return undefined;
throw new Failure(err.message, {code: 0, cause: err, info: symbol});
throw new Failure(err.message, {
code: "ERR_FFI",
cause: err,
info: { symbol }
});
}
}
};

return handle;
}

function dlopen(path, symbols, option){

shouldObjWithinObj(symbols);

const lib = Object.create(null);
const call = load(path, option);
const handle = load(path, option);

for (const [name, definition] of Object.entries(symbols)){

if (name === "__proto__") continue; //not allowed
Expand All @@ -80,12 +87,10 @@ function dlopen(path, symbols, option){
const nonblocking = asBoolean(definition.nonblocking) ?? false;
const symbol = definition.symbol || name;

const fn = call(symbol, result, parameters);
if(typeof fn === "function"){
const fn = handle(symbol, result, parameters);
if(typeof fn === "function")
lib[name] = nonblocking ? promisify(fn.async) : fn;
}

}

return lib;
}

Expand Down

0 comments on commit 50f7ecf

Please sign in to comment.