Skip to content

Commit 50f7ecf

Browse files
committed
err code
1 parent 6afe239 commit 50f7ecf

3 files changed

Lines changed: 50 additions & 38 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Calling directly from a library
4747
```js
4848
import { load, types } from "@xan105/ffi/[ napi | koffi ]";
4949

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

67-
const library = dlopen(
67+
const lib = dlopen(
6868
"./callback.so",
6969
{
7070
set_status_callback: {
@@ -86,8 +86,9 @@ const callback = new Callback(
8686
(success) => {}
8787
);
8888

89-
library.set_status_callback(callback.pointer);
90-
library.start_long_operation();
89+
lib.set_status_callback(callback.pointer);
90+
lib.start_long_operation();
91+
callback.close();
9192
```
9293

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

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

129130
- `ignoreMissingSymbol?: boolean` (false)
130131

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

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

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

152153
```js
153154
import { load } from "@xan105/ffi/[ napi | koffi ]";
154-
const call = load("libm");
155-
156-
const ceil = call("ceil", "double", ["double"])
155+
const lib = load("libm");
156+
const ceil = lib("ceil", "double", ["double"])
157157
ceil(1.5); //2
158158
```
159159

lib/ffi-napi/open.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,45 +51,54 @@ function load(path, option = {}){
5151

5252
if(err && !options.ignoreLoadingFail){
5353
const errCode = RegExp(/\d+$/).exec(err)?.[0];
54-
if(errCode == null) //If couldn't extract error code
54+
if(errCode == null){ //If couldn't extract error code
55+
err.code = "ERR_FFI";
5556
throw err; //Throw the default ffi error
56-
else {
57-
const [ message, code ] = errorLookup(+errCode); //lookup error code
57+
} else {
58+
const [ message ] = errorLookup(+errCode); //lookup error code
5859
throw new Failure(message, {
59-
code,
60+
code: "ERR_FFI",
6061
cause: err,
61-
info: path
62+
info: { lib: path }
6263
});
6364
}
6465
}
6566

66-
return function(symbol, result, parameters){
67+
const handle = function(symbol, result, parameters){
6768
try{
6869
if (!dylib) return undefined;
6970
const fnPtr = dylib.get(symbol);
7071
return ffi.ForeignFunction(fnPtr, result, parameters);
7172
}catch(err){
7273
const errCode = RegExp(/\d+$/).exec(err)?.[0];
73-
if(errCode == null) //If couldn't extract error code
74+
if(errCode == null){ //If couldn't extract error code
75+
err.code = "ERR_FFI";
7476
throw err; //Throw the default ffi error
75-
else {
77+
} else {
7678
const [ message, code ] = errorLookup(+errCode); //lookup error code
7779
if(
7880
options.ignoreMissingSymbol &&
79-
code === "ERROR_PROC_NOT_FOUND"
81+
(code === "ERROR_PROC_NOT_FOUND" || err.message.includes("undefined symbol"))
8082
) return undefined;
81-
throw new Failure(message, { code, cause: err, info: symbol });
83+
throw new Failure(message, {
84+
code: "ERR_FFI",
85+
cause: err,
86+
info: { symbol }
87+
});
8288
}
8389
}
8490
};
91+
92+
return handle;
8593
}
8694

8795
function dlopen(path, symbols, option){
8896

8997
shouldObjWithinObj(symbols);
90-
98+
9199
const lib = Object.create(null);
92-
const call = load(path, option);
100+
const handle = load(path, option);
101+
93102
for (const [name, definition] of Object.entries(symbols)){
94103

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

102-
const fn = call(symbol, result, parameters);
103-
if(typeof fn === "function"){
111+
const fn = handle(symbol, result, parameters);
112+
if(typeof fn === "function")
104113
lib[name] = nonblocking ? promisify(fn.async) : fn;
105-
}
106-
107114
}
108115
return lib;
109116
}

lib/koffi/open.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ function load(path, option = {}){
4444

4545
if(err && !options.ignoreLoadingFail){
4646
throw new Failure(err.message, {
47-
code: 0,
47+
code: "ERR_FFI",
4848
cause: err,
49-
info: path
49+
info: { lib: path }
5050
});
5151
}
52-
53-
return function(symbol, result, parameters){
52+
53+
const handle = function(symbol, result, parameters){
5454
try{
5555
if (!dylib) return undefined;
5656
return dylib[options.abi](symbol, result, parameters);
@@ -60,17 +60,24 @@ function load(path, option = {}){
6060
err.message.startsWith("Cannot find function") &&
6161
err.message.endsWith("in shared library")
6262
) return undefined;
63-
throw new Failure(err.message, {code: 0, cause: err, info: symbol});
63+
throw new Failure(err.message, {
64+
code: "ERR_FFI",
65+
cause: err,
66+
info: { symbol }
67+
});
6468
}
65-
}
69+
};
70+
71+
return handle;
6672
}
6773

6874
function dlopen(path, symbols, option){
6975

7076
shouldObjWithinObj(symbols);
7177

7278
const lib = Object.create(null);
73-
const call = load(path, option);
79+
const handle = load(path, option);
80+
7481
for (const [name, definition] of Object.entries(symbols)){
7582

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

83-
const fn = call(symbol, result, parameters);
84-
if(typeof fn === "function"){
90+
const fn = handle(symbol, result, parameters);
91+
if(typeof fn === "function")
8592
lib[name] = nonblocking ? promisify(fn.async) : fn;
86-
}
87-
88-
}
93+
8994
return lib;
9095
}
9196

0 commit comments

Comments
 (0)