@@ -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
8795function 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}
0 commit comments